Hi Sriashalya,

You can use a class mediator to get the attachment. This code is used to
get the attachment and convert base64 encoding then assign to a property.
The property can be used in the template. Before that you need identify the
API support encoding.

Sample code for the attachment

public class Attachment extends AbstractMediator {

    private static final Log log = LogFactory.getLog(Attachment.class);

    public Attachment() {
    }

    public boolean mediate(MessageContext synCtx) {
        SynapseLog synLog = getLog(synCtx);

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Start : Sample attachment mediator");

            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Message : " + synCtx.getEnvelope());
            }
        }
        try {
             String contentId = getCotentIds(synCtx)[1]; InputStream
             inputStream = getInputStream(synCtx, contentId); byte[] bytes
= IOUtils.toByteArray(inputStream);
             String encodedString = Base64.encodeBytes(bytes);
             synCtx.setProperty("attachmentBody", encodedString);
             inputStream.close();


        } catch (NumberFormatException nfe) {
            synLog.auditWarn("sample adaptor - invalid value returned : "
                    + nfe);
        } catch (Exception e) {
            synLog.error("sample adaptor - error generating the attachment
payload : "
                    + e);
        }

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("End : Sample Attachment mediator");

            String contentId = getCotentIds(synCtx)[1];
            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Message : " + synCtx.getEnvelope());
            }
        }
        return true;
    }
    //get the content Ids
    public String[] getCotentIds(MessageContext synCtx) {
        Axis2MessageContext axisMsglengthContext = (Axis2MessageContext)
synCtx;
        org.apache.axis2.context.MessageContext msgContext =
axisMsglengthContext
                .getAxis2MessageContext();
        Attachments attachments = msgContext.getAttachmentMap();
        return attachments.getAllContentIDs();

    }

    // get the input stream attachment
    public InputStream getInputStream(MessageContext synCtx, String
contentID) {
        Axis2MessageContext axisMsgContext = (Axis2MessageContext) synCtx;
        org.apache.axis2.context.MessageContext msgContext = axisMsgContext
                .getAxis2MessageContext();
        Attachments attachments = msgContext.getAttachmentMap();
        DataHandler dataHandler = attachments.getDataHandler(contentID);
        InputStream inputStream = null;
        try {
            inputStream = dataHandler.getInputStream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return inputStream;
    }

}

<class name="org.wso2.carbon.connector.sample.Attachment" />
<property name="attachmentBody"
expression="get-property('attachmentBody')"/>


Thanks,
Kathees


On Mon, Mar 2, 2015 at 9:39 AM, Malaka Silva <[email protected]> wrote:

> Then in that case we may need to use a class mediator do that part. Also
> we need to handle MTOM and Soap attachments separately i guess.
>
> On Mon, Mar 2, 2015 at 9:35 AM, Vanjikumaran Sivajothy <[email protected]>
> wrote:
>
>> Hi Sriashalaya,
>>
>> Isn't it possible to reuse the Gmail connector already there for this
>> encoding concept ?
>>
>> On Mon, Mar 2, 2015 at 9:32 AM, Sriashalya Srivathsan <[email protected]>
>> wrote:
>>
>>> Hi Malaka,
>>> Gmail uploading is slightly different.we need to encode the entire
>>> message and pass it through the row parameter.But for message it is
>>> working, for attachment it fails.there's no correct format in the API.I
>>> found the concept in Stack overflow.I followed that way.But it didn't work.
>>>
>>>
>>>
>>> Rgds
>>>
>>> On Sat, Feb 28, 2015 at 10:47 PM, Malaka Silva <[email protected]> wrote:
>>>
>>>> Hi
>>>>
>>>> Can we do similar approach to dropbox [1]?
>>>>
>>>> Here we are just passing the request MTOM attachment to api.
>>>>
>>>> [1]
>>>> https://github.com/wso2/esb-connectors/blob/master/dropbox/dropbox-connector/dropbox-connector-1.0.0/org.wso2.carbon.connector/src/main/resources/operation/uploadFile.xml
>>>>
>>>> On Sat, Feb 28, 2015 at 1:06 PM, Sriashalya Srivathsan <
>>>> [email protected]> wrote:
>>>>
>>>>> Hi All,
>>>>> I'm working with Gmail API Connector. I'm struggling on send mail with
>>>>> attachment through REST.Can any one please help me to create a POST 
>>>>> request
>>>>> with JSON payload for send attachment through REST client.
>>>>>
>>>>> Regards
>>>>> --
>>>>> S.Sriashalya,
>>>>> Associate Software Engineer,
>>>>> WSO2.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>>
>>>> Best Regards,
>>>>
>>>> Malaka Silva
>>>> Senior Tech Lead
>>>> M: +94 777 219 791
>>>> Tel : 94 11 214 5345
>>>> Fax :94 11 2145300
>>>> Skype : malaka.sampath.silva
>>>> LinkedIn : http://www.linkedin.com/pub/malaka-silva/6/33/77
>>>> Blog : http://mrmalakasilva.blogspot.com/
>>>>
>>>> WSO2, Inc.
>>>> lean . enterprise . middleware
>>>> http://www.wso2.com/
>>>> http://www.wso2.com/about/team/malaka-silva/
>>>> <http://wso2.com/about/team/malaka-silva/>
>>>>
>>>> Save a tree -Conserve nature & Save the world for your future. Print
>>>> this email only if it is absolutely necessary.
>>>>
>>>
>>>
>>>
>>> --
>>> S.Sriashalya,
>>> Associate Software Engineer,
>>> WSO2.
>>>
>>
>>
>>
>> --
>> Sivajothy Vanjikumaran
>> *Senior Software Engineer*
>> *Integration Technologies Team*
>> *WSO2 Inc. http://wso2.com <http://wso2.com/>*
>> *Mobile:(+94)777219209*
>> [image: Facebook] <https://www.facebook.com/vanjikumaran> [image:
>> Twitter] <https://twitter.com/vanjikumaran> [image: LinkedIn]
>> <http://www.linkedin.com/pub/vanjikumaran-sivajothy/25/b31/293> [image:
>> Blogger] <http://vanjikumaran.blogspot.com/> [image: SlideShare]
>> <http://www.slideshare.net/vanjikumaran>
>>
>> This communication may contain privileged or other
>> confidential information and is intended exclusively for the addressee/s.
>> If you are not the intended recipient/s, or believe that you may
>> have received this communication in error, please reply to the
>> sender indicating that fact and delete the copy you received and in
>> addition, you should not print, copy, re-transmit, disseminate, or
>> otherwise use the information contained in this communication.
>> Internet communications cannot be guaranteed to be timely, secure, error
>> or virus-free. The sender does not accept liability for any errors
>> or omissions
>>
>
>
>
> --
>
> Best Regards,
>
> Malaka Silva
> Senior Tech Lead
> M: +94 777 219 791
> Tel : 94 11 214 5345
> Fax :94 11 2145300
> Skype : malaka.sampath.silva
> LinkedIn : http://www.linkedin.com/pub/malaka-silva/6/33/77
> Blog : http://mrmalakasilva.blogspot.com/
>
> WSO2, Inc.
> lean . enterprise . middleware
> http://www.wso2.com/
> http://www.wso2.com/about/team/malaka-silva/
> <http://wso2.com/about/team/malaka-silva/>
>
> Save a tree -Conserve nature & Save the world for your future. Print this
> email only if it is absolutely necessary.
>
> _______________________________________________
> Dev mailing list
> [email protected]
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>


-- 
Kathees
Software Engineer,
email: [email protected]
mobile: +94772596173
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to