Apache Abdera Multipart Nullpointer Exception Request (IBM Connectivity API)

I am using Apache abdera to send a multiple request to the IBM 4.0 connectivity API. I get a nullpointer exception from the Abdera API. Please let me know what the reason is.

private void createEntryWithAttachment(){
    try {
        String activityId = "urn:lsid:ibm.com:oa:662d0dc7-0308-48ee-8291-d730c733d2d1";         
        String activityIdLocal = activityId.substring(activityId.lastIndexOf(":")+1, activityId.length());
        String createEntryLocal = createEntry+activityIdLocal;      

        Abdera abdera = new Abdera();
        AbderaClient client = new AbderaClient(abdera);         
        AbderaClient.registerTrustManager();
        System.out.println("pd --->"+pd);
        client.addCookie("poktam2cl.iespc.ibm.com", "PD-S-SESSION-ID", pd, "/", null, true);

        RequestOptions requestOptions = client.getDefaultRequestOptions();
        requestOptions.setUseChunked(true);
        requestOptions.setHeader("Connection", "close");
        requestOptions.setHeader("Content-Type", "multipart/related;type=\"application/atom+xml\"");                        
        requestOptions.setContentType("multipart/related;type=\"application/atom+xml\"");
        requestOptions.setSlug("Sample.txt");


        Credentials credentials = new UsernamePasswordCredentials(username, password);
        client.addCredentials(createEntryLocal, AuthScope.ANY_REALM,AuthScope.ANY_SCHEME, credentials);

        Entry entry = abdera.getFactory().newEntry();
        entry.setTitle("create entry with attachment title ");
        entry.setContent("create entry with attachment content");

        javax.xml.namespace.QName field = new QName("http://www.ibm.com/xmlns/prod/sn", "field", "snx");
        org.apache.abdera.model.Element fieldElement = entry.addExtension(field);
        fieldElement.setAttributeValue("type", "file");
        fieldElement.setAttributeValue("name", "sampletextfile1");
        fieldElement.setAttributeValue("position", "3000");

        FileInputStream fis = new FileInputStream(filepath);
        requestOptions.setHeader("Content-Length", "35");

        entry.addCategory("http://www.ibm.com/xmlns/prod/sn/type","entry", "Entry");

        ClientResponse response = client.post(createEntryLocal, entry, fis, "multipart/related;type=\"application/atom+xml\"", requestOptions );

        System.out.println("Entry Created with attachment resp: " + response.getStatus());

        if(response.getStatus() == 201){
            System.out.println("Entry Created with attachment successfully .....");
            printIBMConnectionErrorMessage(response);
        }else{
            System.out.println("Entry with attachment creation failed");
            printIBMConnectionErrorMessage(response);
            //System.exit(0);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
} 

Output

java.lang.NullPointerException
at org.apache.abdera.protocol.client.util.MultipartRelatedRequestEntity.writeInput(MultipartRelatedRequestEntity.java:74)
at org.apache.abdera.protocol.client.util.MultipartRelatedRequestEntity.writeRequest(MultipartRelatedRequestEntity.java:59)
at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:499)
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2114)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at org.apache.abdera.protocol.client.AbderaClient.execute(AbderaClient.java:688)
at org.apache.abdera.protocol.client.AbderaClient.post(AbderaClient.java:306)
at JavaAgentEntryWithAttachment.createEntryWithAttachment(JavaAgentEntryWithAttachment.java:157)
at JavaAgentEntryWithAttachment.main(JavaAgentEntryWithAttachment.java:66)

This exception comes from the abdera API, class MultipartRelatedRequestEntity.java, line number 74. I placed the line without code 74 below. Therefore, it is clear that contentSrc is null and the Abdera API does not allow me to set this value. Please let me know what I'm missing here.

String contentId = entry.getContentSrc().toString();
0
source share
2 answers

I did this in two steps:

  • Send file
  • Call to update data

mime. XML mime. .

0

. , , . .

, HttpClient Part, StringPart FilePart

  final Entry entry = // ... Create your Entry
  final RequestOptions options = this.client.getDefaultRequestOptions();
  options.setHeader("Content-Type", "multipart/related;type=\"application/atom+xml\"");

  StringPart entryPart = new StringPart("entry", entry.toString());
  entryPart.setContentType("application/atom+xml");

  FilePart filePart = new FilePart("file", new File(resource.getFile()));           

  RequestEntity request = new MultipartRequestEntity(new Part[] { entryPart, filePart}, this.client.getHttpClientParams());
  ClientResponse response = client.post(this.url + this.activityId, request, options);

, , .

0

Source: https://habr.com/ru/post/1599508/


All Articles