Snippets

Pontus Ullgren Mule APIKit return Location header

You are viewing an old version of this snippet. View the current version.
Revised by Pontus Ullgren 00bb431
1
2
3
4
5
.classpath
.project
.settings/
.studio/
target/

Mule APIKit return Location header

This is a simple demo on howto return a response with a location header after a create (POST) operation when using Mule ESB and APIkit.

1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mule-project xmlns="http://www.mulesoft.com/tooling/project" runtimeId="org.mule.tooling.server.3.6.1" schemaVersion="3.5.0.0">
    <name>return.location-header-apikit</name>
    <description></description>
    <muleExtension name="APIkit" qualifier="org.mule.tooling.apikit.3.6.0"/>
</mule-project>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.ullgren.pontus.demo</groupId>
	<artifactId>return.location-header-apikit</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>mule</packaging>
	<name>Mule return.location-header-apikit Application</name>

    <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

		<mule.version>3.6.1</mule.version>
    	<mule.tools.version>1.0</mule.tools.version>
	</properties>

	<build>
		<plugins>
			<plugin>
				<groupId>org.mule.tools.maven</groupId>
				<artifactId>mule-app-maven-plugin</artifactId>
				<version>${mule.tools.version}</version>
				<extensions>true</extensions>
				<configuration>
                    <copyToAppsDirectory>true</copyToAppsDirectory>
				<inclusions>
                        <inclusion>
                            <groupId>org.mule.modules</groupId>
                            <artifactId>mule-module-apikit</artifactId>
                        </inclusion>
                    </inclusions>
                </configuration>
			</plugin>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.2.1</version>
				<configuration>
					<descriptorRefs>
						<descriptorRef>project</descriptorRef>
					</descriptorRefs>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>build-helper-maven-plugin</artifactId>
				<version>1.7</version>
				<executions>
					<execution>
						<id>add-resource</id>
						<phase>generate-resources</phase>
						<goals>
							<goal>add-resource</goal>
						</goals>
						<configuration>
							<resources>
								<resource>
									<directory>src/main/app/</directory>
								</resource>
								<resource>
									<directory>mappings/</directory>
								</resource>
							<resource>
                                    <directory>src/main/api/</directory>
                                </resource>
                            </resources>
						</configuration>
					</execution>
				</executions>
			</plugin>
		<plugin>
                <groupId>org.mule.munit.tools</groupId>
                <artifactId>munit-maven-plugin</artifactId>
                <version>3.6.0-BETA</version>
                <executions>
                    <execution>
                        <id>test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
	<testResources>
            <testResource>
                <directory>src/test/munit</directory>
            </testResource>
        </testResources>
    </build>

	<!-- Mule Dependencies -->
	<dependencies>
		<!-- Xml configuration -->
		<dependency>
			<groupId>org.mule.modules</groupId>
			<artifactId>mule-module-spring-config</artifactId>
			<version>${mule.version}</version>
			<scope>provided</scope>
		</dependency>
		<!-- Mule Transports -->
		<dependency>
			<groupId>org.mule.transports</groupId>
			<artifactId>mule-transport-file</artifactId>
			<version>${mule.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.mule.transports</groupId>
			<artifactId>mule-transport-http</artifactId>
			<version>${mule.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.mule.transports</groupId>
			<artifactId>mule-transport-jdbc</artifactId>
			<version>${mule.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.mule.transports</groupId>
			<artifactId>mule-transport-jms</artifactId>
			<version>${mule.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.mule.transports</groupId>
			<artifactId>mule-transport-vm</artifactId>
			<version>${mule.version}</version>
			<scope>provided</scope>
		</dependency>
		<!-- Mule Modules -->
		<dependency>
			<groupId>org.mule.modules</groupId>
			<artifactId>mule-module-scripting</artifactId>
			<version>${mule.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.mule.modules</groupId>
			<artifactId>mule-module-xml</artifactId>
			<version>${mule.version}</version>
			<scope>provided</scope>
		</dependency>
		<!-- for testing -->
		<dependency>
			<groupId>org.mule.tests</groupId>
			<artifactId>mule-tests-functional</artifactId>
			<version>${mule.version}</version>
			<scope>test</scope>
		</dependency>
	<dependency>
            <groupId>org.mule.munit</groupId>
            <artifactId>munit-assert</artifactId>
            <version>3.6.0-BETA</version>
            <scope>test</scope>
        </dependency>
    <dependency>
            <groupId>org.mule.munit</groupId>
            <artifactId>munit-mock</artifactId>
            <version>3.6.0-BETA</version>
            <scope>test</scope>
        </dependency>
    <dependency>
            <groupId>org.mule.munit</groupId>
            <artifactId>munit-common</artifactId>
            <version>3.6.0-BETA</version>
            <scope>test</scope>
        </dependency>
    <dependency>
            <groupId>org.mule.munit</groupId>
            <artifactId>munit-runner</artifactId>
            <version>3.6.0-BETA</version>
            <scope>test</scope>
        </dependency>
    <dependency>
            <groupId>org.mule.modules</groupId>
            <artifactId>mule-interceptor-module</artifactId>
            <version>3.6.0-BETA</version>
            <scope>test</scope>
        </dependency>
    <dependency>
            <groupId>org.mule.modules</groupId>
            <artifactId>mule-module-apikit</artifactId>
            <version>1.6.1</version>
        </dependency>
    </dependencies>

	<repositories>
          <repository>
            <id>Central</id>
            <name>Central</name>
            <url>http://repo1.maven.org/maven2/</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>mulesoft-releases</id>
            <name>MuleSoft Releases Repository</name>
            <url>http://repository.mulesoft.org/releases/</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>mulesoft-snapshots</id>
            <name>MuleSoft Snapshots Repository</name>
            <url>http://repository.mulesoft.org/snapshots/</url>
            <layout>default</layout>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>mulesoft-release</id>
            <name>mulesoft release repository</name>
            <layout>default</layout>
            <url>http://repository.mulesoft.org/releases/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>
#%RAML 0.8
---
title: Widget repository

/widget:
  post:
    description: Creates a new widget
    body:
      application/json:
        example: |
          {
            "name": "Widget 1",
            "pno" : "W001",
            "price" : 10.00
          }
    responses:
      201:
        headers:
          location: 
            description: URI pointing to the location of the newly created widget
            example: http://api.widgets.example.org/api/1/widget/abc123
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:apikit="http://www.mulesoft.org/schema/mule/apikit" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/apikit http://www.mulesoft.org/schema/mule/apikit/current/mule-apikit.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" version="CE-3.6.1">
    <http:listener-config name="api-httpListenerConfig" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
    <apikit:config name="api-config" raml="api.raml" consoleEnabled="true" consolePath="console" doc:name="Router"/>
    <apikit:mapping-exception-strategy name="api-apiKitGlobalExceptionMapping">
        <apikit:mapping statusCode="404">
            <apikit:exception value="org.mule.module.apikit.exception.NotFoundException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
            <set-payload value="{ &quot;message&quot;: &quot;Resource not found&quot; }" doc:name="Set Payload"/>
        </apikit:mapping>
        <apikit:mapping statusCode="405">
            <apikit:exception value="org.mule.module.apikit.exception.MethodNotAllowedException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
            <set-payload value="{ &quot;message&quot;: &quot;Method not allowed&quot; }" doc:name="Set Payload"/>
        </apikit:mapping>
        <apikit:mapping statusCode="415">
            <apikit:exception value="org.mule.module.apikit.exception.UnsupportedMediaTypeException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
            <set-payload value="{ &quot;message&quot;: &quot;Unsupported media type&quot; }" doc:name="Set Payload"/>
        </apikit:mapping>
        <apikit:mapping statusCode="406">
            <apikit:exception value="org.mule.module.apikit.exception.NotAcceptableException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
            <set-payload value="{ &quot;message&quot;: &quot;Not acceptable&quot; }" doc:name="Set Payload"/>
        </apikit:mapping>
        <apikit:mapping statusCode="400">
            <apikit:exception value="org.mule.module.apikit.exception.BadRequestException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
            <set-payload value="{ &quot;message&quot;: &quot;Bad request&quot; }" doc:name="Set Payload"/>
        </apikit:mapping>
    </apikit:mapping-exception-strategy>
    <flow name="api-main">
        <http:listener config-ref="api-httpListenerConfig" path="/api/*" doc:name="HTTP"/>
        <apikit:router config-ref="api-config" doc:name="APIkit Router"/>
        <exception-strategy ref="api-apiKitGlobalExceptionMapping" doc:name="Reference Exception Strategy"/>
    </flow>
    <flow name="post:/widget:api-config">
    	<set-property value="http://api.widgets.example.org/api/1/widget/#[message.id]" propertyName="location" doc:name="Set Location header" />
        <set-payload value="#[NullPayload.getInstance()]" doc:name="Set Payload"/>
    </flow>
</mule>
1
2
3
4
5
#Fri May 01 23:24:59 CEST 2015
encoding=UTF-8
domain=default
redeployment.enabled=true
config.resources=api.xml,return.location-header-apikit.xml
1
2
3
4
5
6
7
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.6.1"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd"></mule>
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.