Thursday, October 17, 2013

Mule Upload Example with custom connector

Following code shows how the upload can be done via mule custom connector.
  • Code for the custom upload connector :


import java.io.BufferedInputStream;
import java.io.IOException;
import org.mule.api.ConnectionException;
import org.mule.api.annotations.Configurable;
import org.mule.api.annotations.ConnectionIdentifier;
import org.mule.api.annotations.Connector;
import org.mule.api.annotations.Disconnect;
import org.mule.api.annotations.Processor;
import org.mule.api.annotations.ValidateConnection;
import org.mule.api.annotations.lifecycle.Start;
import org.mule.api.annotations.param.Payload;

/**
 * Upload Connector
 *
 * @author MuleSoft, Inc.
 */
@Connector(name="uploadconnector", schemaVersion="1.0", 
friendlyName="UploadConnector", description="Upload Functioanlity")
public class UploadConnector {
 
private String connectionIdentifier  = null;
private boolean isConnected = false;
 
/**
 * Specifies to which server to connect.
 */
 @Configurable
 private String host = null;
 
/**
  * @return the connectionIdentifier
  */
  @ConnectionIdentifier
 public String getConnectionIdentifier() {
    return connectionIdentifier;
 }
 
/**
 * @param connectionIdentifier the connectionIdentifier to set
 */
 public void setConnectionIdentifier(String connectionIdentifier) {
    this.connectionIdentifier = connectionIdentifier;
 }
 
 
/**
 * @return the host
 */
 public String getHost() {
    return host;
 }


/**
 * @param host the host to set
 */
 public void setHost(String host) {
    this.host = host;
 }

/**
 * Connects to server.
 * 
 * @throws ConnectionException, IOException
 */
 @Start
 public synchronized void connect()
    throws ConnectionException, IOException {
 }

/**
 * Disconnect
 */
 @Disconnect
 public synchronized void disconnect() {
    this.isConnected = false;
 }

/**
 * Are we connected
 */
 @ValidateConnection
 public boolean isConnected() {
    return this.isConnected;
 }
/**
 * Uploads the document
 * 
 * {@sample.xml ../../
 * ../doc/UploadConnector.xml.sample upload:publishdocument}
 *
 * @param payload File dat in the fromat of BufferedInputStream.
 * @param fileName File name of the file to upload.
 * 
 * @throws IOException If there is any exception while connecting 
 *         to the server.
 */
 @Processor
 public void publishdocument(@Payload BufferedInputStream payload, 
     String fileName) throws IOException {

     fileName = getFileName(fileName);
     //TODO Proceed with your client library.
 }
    
    
/**
 * Parse the content-dispposition parameter and get the file name 
 * out of it.
 * 
 * @param fileName content disposition parameter.
 */
 private String getFileName(String fileName) {
     String fileNameFinal = null;
     if(fileName != null){
         String filenameSplit[] = fileName.split(";");
         for (String fn : filenameSplit) {
             //filename="YT.jpg"
             if(fn.contains("filename=")){
                 fileNameFinal = fn.substring("filename=".length() + 2, 
                                 fn.length()-1);
             }
         }
     }
     return fileNameFinal;
 }
}


  • Code in the UploadConnector.xml.sample file
  1. Name of the method 'publishdocument' must be same as the name in the documentation file. i.e. UploadConnector.xml.sample
  2. Parameter name fileName must be same as specified in the function in the connector.


<!-- BEGIN_INCLUDE(uploadconnector:publishdocument) -->
    <uploadconnector:publishdocument 
        fileName="#[message.inboundProperties['content-disposition']]" 
        config-ref="uploadconnector"/>
<!-- END_INCLUDE(uploadconnector:publishdocument) -->


  • Entry in the UploadConnector.mflow file.
  1. Need to specify the host and port property in the properties file. Otherwise one can hard code it.
  2. Path attribute 'publishdocument' must be same as specified in the HTML form bellow. i.e. http://localhost:8081/publishdocument



<http:connector name="httpConnector" doc:name="HTTP\HTTPS">
  <service-overrides 
   messageFactory="org.mule.transport.http.HttpMultipartMuleMessageFactory" />
</http:connector>

<flow name="publishdocument" doc:name="publishdocument">

    <http:inbound-endpoint  connector-ref="httpConnector" 
        exchange-pattern="request-response"  host="${http.host}" 
        port="${http.port}" path="publishdocument" doc:name="HTTP"/>

    <fusionlive:publishdocument config-ref="uploadconnector" 
       fileName="#[message.inboundProperties['content-disposition']]" 
       doc:name="UploadCon"/>
</flow>



HTML Upload Form
  • Name of the file type element must be payload.
  • Method must be post and encrypt attribute must be 'multipart/form-data'


<html>
  <head>
    <title>UploadConnector</title>
  </head>
  <body>
    </br>
      Upload Form
      <form  action="http://localhost:8081/publishdocument" method="post" 
        enctype="multipart/form-data">
 <input type="file" name ="payload"/>
        <input type="submit" value="Upload" />
      </form>
  </body>
</html>

1 comment:

  1. Thanks to admin for this Blog because I have read your whole site Content and shared to people your site is informative and people can take information from your site.

    BT Mail Login

    ReplyDelete