Search code examples
apache-cameljbossfuseservicenow

ServiceNow attachments in Camel


How to download or upload attachments to servicenow from camel connector. The project is setup with camel-servicenow (v2.21.0.fuse-000077-redhat-1) in maven. Creating, retrieve and updating of tickets is working fine, however, not able to download any attachments using Attachment resource.


Solution

  • Download :

    url = "https4://" 
            +  instance
            + ".service-now.com/api/now/v1/attachment?sysparm_query="
            + "table_name=" 
            + table 
            + "%5Etable_sys_id=" 
            + sysId
            + "&authenticationPreemptive=true&authUsername=" 
            + username 
            + "&authPassword="
            + password
            + "&authMethod=Basic";
    

    In route definition :

    from("direct:servicenowAttachmentDownload").setHeader(Exchange.HTTP_METHOD, constant("GET")).recipientList().simple("${header.url}")
    

    Upload :

    url = "https4://"
            + instance
            + ".service-now.com/api/now/attachment/file?table_name="
            + table
            + "&table_sys_id="
            + sysId
            + "&file_name="
            + attachmentName
            + "&authenticationPreemptive=true&authUsername="
            + username
            + "&authPassword="
            + password
            + "&authMethod=Basic";
    

    In route definition :

    from("direct:servicenowAttachmentUpload").process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
                multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                multipartEntityBuilder.setContentType(ContentType.MULTIPART_FORM_DATA);
    
                String filename = (String) exchange.getIn().getHeader(Exchange.FILE_NAME);
    
                String filePath = (String) exchange.getIn().getHeader("filePath");
                String attachmentName = (String) exchange.getIn().getHeader("attachmentName");
    
                File file = new File(filePath);
                multipartEntityBuilder.addPart("upload",
                        new FileBody(file, ContentType.MULTIPART_FORM_DATA, attachmentName));
                exchange.getIn().setBody(multipartEntityBuilder.build());
            }
        }).removeHeaders("CamelHttp*").setHeader(Exchange.HTTP_METHOD, constant("POST")).recipientList()
                .simple("${header.url}")