Sunday 7 February 2016

How to move files from dropbox to Alfresco using mulesoft

The code will work on mule 3.7.

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:cmis="http://www.mulesoft.org/schema/mule/cmis" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:dropbox="http://www.mulesoft.org/schema/mule/dropbox" 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" 
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/cmis http://www.mulesoft.org/schema/mule/cmis/current/mule-cmis.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/dropbox http://www.mulesoft.org/schema/mule/dropbox/current/mule-dropbox.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
    <dropbox:config name="Dropbox" appKey="${dropboxAppkey}" appSecret="${dropboxAppsecret}" doc:name="Dropbox">
        <dropbox:oauth-callback-config domain="localhost" localPort="8082" remotePort="8082" path="auth"/>
    </dropbox:config>
    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
    <spring:beans>
     <spring:bean name="getParentPath" class="dropboxalfresco.GetParentPath" id="Bean"/>
    </spring:beans>
    <cmis:config name="ALFRESCO-CMIS" baseUrl="http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom" username="${alfrescoUserName}" password="${alfrescoPassword}" useAlfrescoExtension="true" useCookies="true" doc:name="CMIS: Configuration"/>
    <flow name="dropboxalfrescoFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <expression-filter expression="#[message.inboundProperties.'http.request.uri' != '/favicon.ico']" doc:name="Expression"/>
        <dropbox:authorize config-ref="Dropbox" doc:name="Authourize Dropbox Connection"/>
        <choice doc:name="IsOAuthAccessTokenIdAvailable">
            <when expression="flowVars.OAuthAccessTokenId != null">
                <logger level="INFO" doc:name="LogOAuth Successful"/>
                <set-variable variableName="#['counter']" value="#[0]" doc:name="Variable"/>
                <flow-ref name="Getting-Files-and-Directories" doc:name="Getting-Files-and-Directories"/>
            </when>
            <otherwise>
                <logger level="INFO" doc:name="LogOAuth Failed"/>
                <set-payload value="Authorization to dropbox Failed" doc:name="SetAuthFailedPayload"/>
            </otherwise>
        </choice>
    </flow>
    <flow name="Getting-Files-and-Directories">
        <set-variable variableName="counter" value="#[flowVars['counter']+1]" doc:name="Variable"/>
        <choice doc:name="Root ">
            <when expression="#[flowVars['counter'] == 1]">
                <dropbox:list config-ref="Dropbox" path="/" accessTokenId="#[flowVars.OAuthAccessTokenId]" doc:name="List files &amp; folders"/>
                <mulexml:object-to-xml-transformer doc:name="Object to XML"/>
                <set-variable variableName="isDir" value=" #[xpath3('//contents//isDir')] " doc:name="isDir"/>
                <set-variable variableName="path" value="#[xpath3('//contents//path')] " doc:name="Path"/>
                <logger message="#[&quot;Is Dir  ? &quot; + flowVars.isDir]" level="INFO" doc:name="Logger"/>
                <choice doc:name="File or Directory">
                    <when expression="#[xpath3('//isDir')==false] ">
                        <dropbox:download-file config-ref="Dropbox" path="#[flowVars.path]" accessTokenId="#[flowVars.OAuthAccessTokenId]" doc:name="Download file"/>
                        <file:outbound-endpoint path="C:\Users\Enkindle3\Desktop" outputPattern="fear.jpg" responseTimeout="10000" doc:name="File"/>
                    </when>
                    <otherwise>
                        <logger message="It's a directory" level="INFO" doc:name="Logger"/>
                        <flow-ref name="Getting-Files-and-Directories" doc:name="Getting-Files-and-Directories"/>
                    </otherwise>
                </choice>
            </when>
            <otherwise>
                <logger message="#[flowVars.path]" level="INFO" doc:name="Logger"/>
                <invoke object-ref="getParentPath" method="isZPath" methodArguments="#[flowVars.path]" doc:name="Invoke"/>
                <set-variable variableName="isZPath" value="#[payload]" doc:name="isZPath"/>
                <dropbox:list config-ref="Dropbox" path="#[flowVars.path]" accessTokenId="#[flowVars.OAuthAccessTokenId]" doc:name="List Files &amp; Folders"/>
                <mulexml:object-to-xml-transformer doc:name="Object to XML"/>
                <set-variable variableName="isFolder" value=" #[xpath3('//isDir')] " doc:name="Dir ?"/>
                <invoke object-ref="getParentPath" method="hasContent" methodArguments="#[payload]" doc:name="Invoke"/>
                <set-variable variableName="length" value="#[payload]" doc:name="Contents length"/>
                <choice doc:name="Choice">
                    <when expression="#[flowVars.length &gt; 2 || flowVars.isFolder == &quot; false &quot;]">
                        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
                        <remove-variable variableName="isFolder" doc:name="Remove Dir ?"/>
                        <dropbox:list config-ref="Dropbox" path="#[flowVars.path]" doc:name="Dropbox" accessTokenId="#[flowVars.OAuthAccessTokenId]"/>
                        <mulexml:object-to-xml-transformer doc:name="Object to XML"/>
                        <set-variable variableName="icon" value="#[xpath3('//icon')]" encoding="UTF-8" doc:name="icon"/>
                        <set-variable variableName="path" value="#[xpath3('//path')]" doc:name="Path"/>
                        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
                        <choice tracking:enable-default-events="true" doc:name="Folder or File">
                            <when expression="#[flowVars['icon'] == &quot;folder&quot;]">
                                <set-variable variableName="isDir" value=" #[xpath3('//isDir')] " doc:name="isDir"/>
                                <set-variable variableName="path" value="#[xpath3('//contents//path')] " doc:name="Path"/>
                                <choice doc:name="File or Directory">
                                    <when expression="#[xpath3('//contents//isDir')==false] ">
                                        <dropbox:download-file config-ref="Dropbox" path="#[flowVars.path]" accessTokenId="#[flowVars.OAuthAccessTokenId]" doc:name="Download File"/>
                                        <file:outbound-endpoint path="C:\Users\Enkindle3\Desktop" outputPattern="fear.jpg" responseTimeout="10000" doc:name="File"/>
                                    </when>
                                    <otherwise>
                                        <logger message="It's a sub directory" level="INFO" doc:name="Logger"/>
                                        <flow-ref name="Getting-Files-and-Directories" doc:name="Getting-Files-and-Directories"/>
                                    </otherwise>
                                </choice>
                            </when>
                            <otherwise>
                                <flow-ref name="UploadInToAlfresco" doc:name="UploadInToAlfresco"/>
                            </otherwise>
                        </choice>
                    </when>
                    <otherwise>
                        <logger message="#[&quot;Is Z path &quot; + flowVars.isZPath]" level="INFO" doc:name="Logger"/>
                        <choice doc:name="Choice">
                            <when expression="#[flowVars.isZPath == false]">
                                <dropbox:delete config-ref="Dropbox" path="#[flowVars.path]" accessTokenId="#[flowVars.OAuthAccessTokenId]" doc:name="Delete folder"/>
                                <invoke object-ref="getParentPath" method="getParent" methodArguments="#[flowVars.path]" doc:name="Invoke"/>
                                <set-variable variableName="path" value="#[payload]" doc:name="Path"/>
                                <flow-ref name="Getting-Files-and-Directories" doc:name="Getting-Files-and-Directories"/>
                            </when>
                            <otherwise>
                                <logger message="Everything Moved to Alfresco " level="INFO" doc:name="Logger"/>
                            </otherwise>
                        </choice>
                    </otherwise>
                </choice>
            </otherwise>
        </choice>
    </flow>
    <flow name="UploadInToAlfresco">
        <logger message="#[xpath3('//path')] " level="INFO" doc:name="Logger"/>
        <set-variable variableName="mimeType" value="#[xpath3('//mimeType')]" doc:name="mimeType"/>
        <invoke object-ref="getParentPath" method="getFileName" methodArguments="#[flowVars.path]" doc:name="fileName"/>
        <set-variable variableName="fileName" value="#[payload]" doc:name="File Name"/>
        <dropbox:download-file config-ref="Dropbox" path="#[flowVars.path] " accessTokenId="#[flowVars.OAuthAccessTokenId]" doc:name="Download"/>
        <cmis:create-document-by-path config-ref="ALFRESCO-CMIS" filename="#[flowVars.fileName]" folderPath="/Shared" mimeType="#[flowVars.mimeType]" objectType="cmis:document" versioningState="MAJOR" doc:name="CMIS"/>
        <remove-variable variableName="fileName" doc:name="Remove File Name"/>
        <logger message="#[flowVars.path]" level="INFO" doc:name="Logger"/>
        <dropbox:move config-ref="Dropbox" from="#[flowVars.path] " to="#[&quot;z&quot; + flowVars.path] " doc:name="Move"/>
        <invoke object-ref="getParentPath" method="getParent" methodArguments="#[flowVars.path]" doc:name="Invoke"/>
        <set-variable variableName="path" value="#[payload]" doc:name="Path"/>
        <flow-ref name="Getting-Files-and-Directories" doc:name="Getting-Files-and-Directories"/>
    </flow>
</mule>