Sample of @RestCall used in Anypoint Custom Connector

Seems that a simple code sample for using the Mule @RestCall in an Anypoint custom connector is hard to find. This code snippet is built from the Anypoint DevKit starter code. The sample simply uses the @RestCall and @RestUriParam annotations in a GET call. Nothing fancy, but it shows where the annotations can be placed and demonstrates the simplicity of building a REST client in a custom connector.

You can cut and paste the Java code below into the Anypoint (or Mule) studio, but be aware that the associated documentation files and MUnit test scripts will need to be changed. Also note the change to abstract class and abstract process methods.

Code

package org.mule.modules.objectexamplesrest;

import java.io.IOException;
import java.util.List;

import org.mule.api.ConnectionException;
import org.mule.api.annotations.Configurable;
import org.mule.api.annotations.Connect;
import org.mule.api.annotations.ConnectionIdentifier;
import org.mule.api.annotations.Connector;
import org.mule.api.annotations.Disconnect;
import org.mule.api.annotations.MetaDataKeyRetriever;
import org.mule.api.annotations.MetaDataRetriever;
import org.mule.api.annotations.Processor;
import org.mule.api.annotations.ValidateConnection;
import org.mule.api.annotations.display.Password;
import org.mule.api.annotations.param.ConnectionKey;
import org.mule.api.annotations.rest.HttpMethod;
import org.mule.api.annotations.rest.RestCall;
import org.mule.api.annotations.rest.RestUriParam;
import org.mule.common.metadata.MetaData;
import org.mule.common.metadata.MetaDataKey;

/**
 * Anypoint Connector
 * 
 * @author MuleSoft, Inc.
 */
@Connector(name = "objectexamplesrest", schemaVersion = "1.0", friendlyName = "ObjectExamplesRest", minMuleVersion = "3.5")
public abstract class ObjectExamplesRestConnector {
	/**
	 * Configurable
	 */
	@Configurable
	private String myProperty;

	@MetaDataKeyRetriever
	public List getMetaDataKeys() throws Exception {
		return null;
	}

	@MetaDataRetriever
	public MetaData getMetaData(MetaDataKey key) throws Exception {
		return null;
	}

	/**
	 * Set property
	 * 
	 * @param myProperty
	 *            My property
	 */
	public void setMyProperty(String myProperty) {
		this.myProperty = myProperty;
	}

	/**
	 * Get property
	 */
	public String getMyProperty() {
		return this.myProperty;
	}

	/**
	 * Connect
	 * 
	 * @param username
	 *            A username
	 * @param password
	 *            A password
	 * @throws ConnectionException
	 */
	@Connect
	public void connect(@ConnectionKey String username,
			@Password String password) throws ConnectionException {
		/*
		 * CODE FOR ESTABLISHING A CONNECTION GOES IN HERE
		 */
	}

	/**
	 * Disconnect
	 */
	@Disconnect
	public void disconnect() {
		/*
		 * CODE FOR CLOSING A CONNECTION GOES IN HERE
		 */
	}

	/**
	 * Are we connected
	 */
	@ValidateConnection
	public boolean isConnected() {
		return true;
	}

	/**
	 * Are we connected
	 */
	@ConnectionIdentifier
	public String connectionId() {
		return "001";
	}

	/**
	 * Custom processor
	 * 
	 * {@sample.xml ../../../doc/objectexamplesrest-connector.xml.sample
	 * objectexamplesrest:example}
	 * 
	 * @param objectName
	 *            object name to serialize
	 * @return serialized object
	 * @throws IOException
	 *             on error
	 */
	@Processor
	@RestCall(uri = "http://objectexamples.cloudhub.io/example/{objectName}", method = HttpMethod.GET)
	public abstract String example(@RestUriParam("objectName") String objectName)
			throws IOException;
}

Basic Authentication

To add basic authentication to the call, you need to build your own Authorization header. Here is the code to do that:


  @RestHeaderParam("Authorization")
  private String authorization = generateBasicAuthorization("username", "password");

and this is the code to build the header:


private static String generateBasicAuthorization(String userName, String password) {
  byte[] encodedPassword = (userName + ":" + password).getBytes();
  BASE64Encoder encoder = new BASE64Encoder();
  return "Basic " + encoder.encode(encodedPassword);
}

Using HttpMethod.POST

Help on POSTing data with @RestCall can be found here: http://forum.mulesoft.org/mulesoft/topics/can_restpostparam_be_used_to_post_json. Basically you can build the POSTed data in the Anypoint flow that uses the connector and pass that value as a String parameter to the connector method specifying the @Payload annotation in front of the parameter in the method signature.  Or you can use the multipart form data format and include @RestPostParam annotations for each field of the method call which is to be included in the POSTed data.  The latter method will create a POSTed data stream of ampersand separated name value pairs (such as name=Bill&phone=(999)555-1212).

Advertisement
Leave a comment

Comments

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: