Creating a forum post from restlet

Hi,

I have a forum topic and I try to add a post to it.

I have the following restlet:

@Name(value = "discussionService")
@Scope(ScopeType.EVENT)
public class DiscussionService extends BaseStatelessNuxeoRestlet {...

And declaration:

    <restletPlugin
        name="discussionService"
        class="com.sirma.itt.rs.rest.DiscussionService"
        enabled="true"
        useSeam="true"
        useConversation="true">
        <urlPatterns>
            <urlPattern>/discussionService</urlPattern>
        </urlPatterns>
    </restletPlugin>

Also, navigationContext is injected:

@In(create = true)
private transient NavigationContext navigationContext;

Then I get a json request, that contains Nuxeo UID of an existing forum topic and I try to add a post to it:

    documentManager = SessionFactory.getSession(getHttpRequest(req));

    DocumentModel replyToDoc = documentManager.getDocument(new IdRef(replyTo));

    log.debug("replyToDoc "+replyTo); // this is the topic; it exists

    if (replyToDoc != null) {
        navigationContext.setCurrentDocument(replyToDoc);
        navigationContext.setChangeableDocument(replyToDoc);
    }

    DocumentModel dm = documentManager.createDocumentModel("CustomPost");
    dm.setProperty(....);
    dm.setProperty(....);
    ....

    dm = commentManagerActions.addComment(dm);
    ....

I am getting the following exception:

java.lang.NullPointerException
    at org.nuxeo.ecm.webapp.context.NavigationContextBean.setCurrentDocument(NavigationContextBean.java:192)

Unfortunately, I could not get the 5.5 source code so I have no idea what's on row 192 in NavigationContextBean.

Is this the proper way of doing this task? Is there a similar example somewhere? And what is the reason for the NullPointerException?

Thanks!

0 votes

1 answers

956 views

ANSWER

  • If you want the source code download it fom here and go to the 5.5 branch.
  • Or if you want to see online.
03/12/2012



I changed my code to:

    navigationContext.setCurrentServerLocation(new RepositoryLocation("default"));
    documentManager = navigationContext.getOrCreateDocumentManager();

and saving the post seems to work.

0 votes



I strongly recommend you to look example of Nuxeo into our source code. You have many examples in the nuxeo-platform-ui-web project.

To download the source code, go there. Hope this will help you. And you can especially look org.nuxeo.ecm.platform.ui.web.restAPI.UploadRestlet class.

We do this:

    String repo = (String) req.getAttributes().get(&quot;repo&quot;);
    ....

    try {
        navigationContext.setCurrentServerLocation(new RepositoryLocation(
                repo));
        documentManager = navigationContext.getOrCreateDocumentManager();
        ...
    } catch (Exception e) {
        handleError(res, e);
        return;
    }

If one day you implement a multi-repository in your instance, this will be managed :D

03/12/2012