0

I am using Apache abdera to post multipart request to IBM connection 4.0 API. I am getting nullpointer exception from Abdera API. Please let me know what's the root cause.

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's 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 is coming from abdera API, class called MultipartRelatedRequestEntity.java, Line no 74. I have placed line no 74 source code below. So its clear that contentSrc is null & Abdera API not allowing me to set this value. Please let me know what I am missing here.

String contentId = entry.getContentSrc().toString();
raghav132
  • 23
  • 1
  • 1
  • 6
  • you probably are getting a resource back, check the response status, and then log out your repsonse information, it sounds like it's invalid – Paul Bastide Apr 03 '14 at 21:08
  • Some other property I need to set, That's why I am getting this exception. But This exception is happening before posting the request. its happening inside the abdera API. – raghav132 Apr 04 '14 at 03:58
  • yeah - I suggest you create an entry without an attachment, and prove that code works, once you know that works, you can mess with the attachment - reference http://www-10.lotus.com/ldd/lcwiki.nsf/revisions/D16B019BB7AE7CE3852574B200325170?OpenDocument ... it seems like you'd want to break out the various parts and test individually – Paul Bastide Apr 04 '14 at 11:35
  • @raghav132 this probably doesn't apply to you anymore, but I had the same issue and was able to solve it. Feel free to accept it as answer so people will find it easier in the future. – Driss Amri Aug 31 '15 at 13:57

2 Answers2

0

I did in two steps:

  1. Send the file
  2. Call to update the data

Each with the good mime type. You can not send the file with XML mime type. And put the length of the file.

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Tony
  • 1
  • Welcome, Tony. Your post is difficult to understand. Maybe make it better with some (pseudo-)code. How do your steps relate to the NullPointerException? – Felix Sep 18 '14 at 06:56
  • I don't think this is possible in two steps, since the [API](http://www-10.lotus.com/ldd/lcwiki.nsf/xpAPIViewer.xsp?lookupName=IBM+Connections+5.0+API+Documentation#action=openDocument&res_title=Updating_activity_nodes_ic50&content=apicontent) mentions: `All existing activity entry information will be replaced with the new data. To avoid deleting existing data, retrieve any data that you want to retain first, and send it back with this request. See Retrieving activity nodes for more information ` – Driss Amri Jul 23 '15 at 09:44
0

It is possible to avoid the nullpointer and do it in one request. I had the same issue and created another issue and managed to find a solution. You can find it here.

It comes down to the following code example where you create a HttpClient Part which can contain a StringPart and a 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);

Hope this will help people in the future if they are using Abdera.

Community
  • 1
  • 1
Driss Amri
  • 1,805
  • 2
  • 20
  • 28