cancel
Showing results for 
Search instead for 
Did you mean: 

Java backed - add a comment

konda
Champ in-the-making
Champ in-the-making
Hi all,

I'm trying to write a new Web Script to add comment to a node but using the CommentService (Alfresco Community 4.0.d) I can't see any useful method. I was looking for (http://dev.alfresco.com/resource/docs/java/repository/org/alfresco/repo/forum/CommentService.html#cr... but I can't find it.

Is it missing in my version of Alfresco?

Alternatively how can I achieve this? I tried using the POST /alfresco/service/api/node/{store_type}/{store_id}/{id}/comments but I didn't found a complete example of JSON Entity to use.

Thanks


PS I tried with

{
   "title" : "title",
   "content" : "content"
}

entity but I received an Internal Server Error

{
    "status" :
  {
    "code" : 500,
    "name" : "Internal Error",
    "description" : "An error inside the HTTP server which prevented it from fulfilling the request."
  }, 
 
  "message" : "07270010 Wrapped Exception (with status template): 07270348 Errore durante l'elaborazione del modello 'Error executing macro: commentJSON\nrequired parameter: item is not specified.'. Contattare l'amministratore di sistema.", 
  "exception" : "org.springframework.extensions.webscripts.WebScriptException - 07270010 Wrapped Exception (with status template): 07270348 Errore durante l'elaborazione del modello 'Error executing macro: commentJSON\nrequired parameter: item is not specified.'. Contattare l'amministratore di sistema.",
 
  "callstack" :
  [
        ""      ,"freemarker.template.TemplateException: Error executing macro: commentJSON\nrequired parameter: item is not specified."
      ,"freemarker.core.Macro$Context.sanityCheck(Macro.java:211)"
      ,"freemarker.core.Macro$Context.runMacro(Macro.java:169)"
      ,"freemarker.core.Environment.visit(Environment.java:614)"
      ,"freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)"
      ,"freemarker.core.Environment.visit(Environment.java:221)"
      ,"freemarker.core.MixedContent.accept(MixedContent.java:92)"
      ,"freemarker.core.Environment.visit(Environment.java:221)"
      ,"freemarker.core.Environment.process(Environment.java:199)"
      ,"org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:202)"
      ,"org.springframework.extensions.webscripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:800)"
      ,"org.springframework.extensions.webscripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:267)"
      ,"org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:147)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:400)"
      ,"org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:388)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:462)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:500)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:316)"
      ,"org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:372)"
      ,"org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:209)"
      ,"org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:118)"
      ,"javax.servlet.http.HttpServlet.service(HttpServlet.java:717)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)"
      ,"org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:58)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)"
      ,"org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)"
      ,"org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)"
      ,"org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:465)"
      ,"org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)"
      ,"org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)"
      ,"org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)"
      ,"org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)"
      ,"org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)"
      ,"org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)"
      ,"org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)"
      ,"java.lang.Thread.run(Thread.java:662)"
      ,"org.alfresco.service.cmr.repository.TemplateException: 07270348 Errore durante l'elaborazione del modello 'Error executing macro: commentJSON\nrequired parameter: item is not specified.'. Contattare l'amministratore di sistema."
      ,"org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:206)"
      ,"org.springframework.extensions.webscripts.WebScriptException: 07270010 Wrapped Exception (with status template): 07270348 Errore durante l'elaborazione del modello 'Error executing macro: commentJSON\nrequired parameter: item is not specified.'. Contattare l'amministratore di sistema."
      ,"org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:970)"

  ],
 
  "server" : "Community v4.0.0 (4003) schema 5.025",
  "time" : "27-ago-2013 15.21.02"
}
3 REPLIES 3

kaynezhang
World-Class Innovator
World-Class Innovator
Make sure your content type is application/json.


   HttpClient client = new HttpClient();
      client.getState().setCredentials(
            new AuthScope("localhost", 8080, "Alfresco"),
            new UsernamePasswordCredentials("admin", "admin"));
      String uid = "51920ee3-14c0-4477-a637-ab82a0d8df70";
      String apiurl = "http://localhost:8080/alfresco/service/api/node/workspace/SpacesStore/'+uid+'/comments";
      PostMethod post = new PostMethod(apiurl);
      try {
         JSONObject site = new JSONObject();
         site.put("title", "this is a test title");
         site.put("content", "this is a test content");

         System.out.println(site.toString());
         post.setDoAuthentication(true);
         post.setRequestHeader("Content-Type", "application/json");
         post.setRequestEntity(new StringRequestEntity(site.toString(),
               "application/json", "UTF-8"));

         int status = client.executeMethod(post);
         if (status != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + post.getStatusLine());
         }
         String resultString = post.getResponseBodyAsString();
         System.out.println(resultString);
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         post.releaseConnection();
      }

konda
Champ in-the-making
Champ in-the-making
Thanks for this useful example!

Just one last question: is it possible to invoke the POST using a ticket (alf_ticket or ticket, gained invoking http://localhost:8080/alfresco/service/api/login?u=admin&pw=admin) instead of basic authentication?

Thanks again


PS Solved:       

String apiurl = "http://localhost:8080/alfresco/service/api/node/workspace/SpacesStore/'+uid+'/comments?alf_ticket=TI...";

kaynezhang
World-Class Innovator
World-Class Innovator

   public String login() {
      String ticket = null;
      HttpClient client = new HttpClient();

      String apiurl = "http://localhost:8080/alfresco/service/api/login";
      PostMethod post = new PostMethod(apiurl);
      try {
         JSONObject login = new JSONObject();
         login.put("username", "admin");
         login.put("password", "admin");

         //System.out.println(login.toString());
         post.setDoAuthentication(true);
         post.setRequestHeader("Content-Type", "application/json");
         post.setRequestEntity(new StringRequestEntity(login.toString(),
               "application/json", "UTF-8"));

         int status = client.executeMethod(post);
         if (status != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + post.getStatusLine());
         }
         String responseData = post.getResponseBodyAsString();
         
         System.out.println(responseData);
         JSONObject response = new JSONObject(responseData);
         ticket = response.getJSONObject("data").getString("ticket");
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         post.releaseConnection();
      }
      return ticket;
   }
   public void addCommment(String ticket){
      HttpClient client = new HttpClient();
/*      client.getState().setCredentials(
            new AuthScope("localhost", 8080, "Alfresco"),
            new UsernamePasswordCredentials("admin", "admin"));*/
      String uid = "17372462-1062-40ea-a470-6c27ac395d3e";
      String apiurl = "http://localhost:8080/alfresco/service/api/node/workspace/SpacesStore/"
            + uid + "/comments"+"?alf_ticket="+ticket;
      PostMethod post = new PostMethod(apiurl);
      try {
         JSONObject comment = new JSONObject();
         comment.put("title", "this is a test title");
         comment.put("content", "this is a test content");
         System.out.println(comment.toString());
         post.setDoAuthentication(true);
         post.setRequestHeader("Content-Type", "application/json");
         post.setRequestEntity(new StringRequestEntity(comment.toString(),
               "application/json", "UTF-8"));

         int status = client.executeMethod(post);
         if (status != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + post.getStatusLine());
         }
         String resultString = post.getResponseBodyAsString();
         System.out.println(resultString);
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         post.releaseConnection();
      }
   }
   public static void main(String[] args) {
      LoginAddCommentTest t = new LoginAddCommentTest();
      String ticket = t.login();
      t.addCommment(ticket);
   }