cancel
Showing results for 
Search instead for 
Did you mean: 

REST file upload with Jersey client

driekhof
Champ in-the-making
Champ in-the-making
I'm able to upload a file with curl from command prompt like this:

$ curl –form "filedata=@test.txt" –form siteid=mysite –form containerid=documentLibrary –form uploaddirectory=java http://localhost:8080/alfresco/service/api/upload?alf_ticket=MYTICKET

I'm also able to use my ticket to successfully do GETs in Jersey Java client.

But I can't get the file upload to work in my jersey client.  The response is showing a 500 status.  Here's my code:


private static void upload() {
   System.out.println("ticket=" + ticket);

   // i've tried putting ticket here and as request param
   String payload = "{" +
//            "\"alf_ticket\":"+ ticket + "," +
            "\"filedata\":\"@test2.txt\"," +
            "\"siteid\":\"mysite\"," +
            "\"containerid\":\"documentLibrary\"," +
            "\"uploaddirectory\":\"java\"" +
            "}";
System.out.println("payload=" + payload);
      
      
//      WebTarget target = client.target(getBaseURI(uriUpload));
      WebTarget target = client.target(getBaseURI(uriUpload)).queryParam("alf_ticket", ticket);

/// I've tried a few things here, nothing works      
//      Response response = target.request().accept(MediaType.TEXT_PLAIN).post(
//            Entity.entity(payload, MediaType.APPLICATION_JSON), Response.class);
      Response response = target.request().accept(MediaType.APPLICATION_JSON).post(
//      Response response = target.request().accept(MediaType.MULTIPART_FORM_DATA).post(
            Entity.entity(payload, MediaType.APPLICATION_JSON), Response.class);

System.out.println("response=" + response);
      

      String plainAnswer = target.request().accept(MediaType.TEXT_PLAIN).get(String.class);

      System.out.println("response=" + response.toString());
      System.out.println("plain=" + plainAnswer);
   }
}


and here's my log:


———- upload() ———-
ticket=TICKET_b7b318252c4bfd3a27d60c176e96c5df070798ce
payload={"filedata":"@test2.txt","siteid":"mysite","containerid":"documentLibrary","uploaddirectory":"java"}
response=InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://127.0.0.1:8080/alfresco/service/api/upload?alf_ticket=TICKET_b7b318252c4bfd3a27d60c176e96c5df..., status=500, reason=Internal Server Error}}
response.status=500
response.entity=org.glassfish.jersey.client.internal.HttpUrlConnector$2@667a738
response.mediaType=application/json;charset=UTF-8
key: Transfer-Encoding, val=chunked
key: Cache-Control, val=no-cache
key: Server, val=Apache-Coyote/1.1
key: Connection, val=close
key: Pragma, val=no-cache
key: Expires, val=Thu, 01 Jan 1970 00:00:00 GMT
key: Date, val=Tue, 19 Jan 2016 18:28:39 GMT
key: Content-Type, val=application/json;charset=UTF-8
Exception in thread "main" javax.ws.rs.NotAllowedException: HTTP 405 Method Not Allowed
   at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1011)
   at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:816)
   at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
   at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)
   at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
   at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
   at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
   at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
   at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
   at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:420)
   at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:316)
   at ourpkg.AlfrescoClient.upload(AlfrescoClient.java:217)
   at ourpkg.AlfrescoClient.main(AlfrescoClient.java:51)


That's my client log,  here's my alfresco.log.


2016-01-19 16:27:16,262 ERROR [org.springframework.extensions.webscripts.AbstractRuntime] [http-apr-8080-exec-7] Exception from executeScript - redirecting to status template error: 00190000 Unexpected error occurred during upload of new content.
org.springframework.extensions.webscripts.WebScriptException: 00190000 Unexpected error occurred during upload of new content.
   at org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:1112)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:171)
   at org.alfresco.repo.web.scripts.RepositoryContainer$3.execute(RepositoryContainer.java:489)
   at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:457)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:551)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:619)
   at org.alfresco.repo.web.scripts.RepositoryContainer.executeScriptInternal(RepositoryContainer.java:399)
   at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:280)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:378)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:209)
   at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:132)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
   at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
   at org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:61)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
   at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
   at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
   at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
   at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
   at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
   at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
   at java.lang.Thread.run(Thread.java:745)
Caused by: org.mozilla.javascript.JavaScriptException: ReferenceError: Unexpected error occurred during upload of new content. (classpath*:alfresco/templates/webscripts/org/alfresco/repository/upload/upload.post.js#412)
   at org.mozilla.javascript.gen.classpath__alfresco_templates_webscripts_org_alfresco_repository_upload_upload_post_js_1._c_main_3(classpath*:alfresco/templates/webscripts/org/alfresco/repository/upload/upload.post.js:412)
   at org.mozilla.javascript.gen.classpath__alfresco_templates_webscripts_org_alfresco_repository_upload_upload_post_js_1.call(classpath*:alfresco/templates/webscripts/org/alfresco/repository/upload/upload.post.js)
   at org.mozilla.javascript.optimizer.OptRuntime.callName0(OptRuntime.java:74)
   at org.mozilla.javascript.gen.classpath__alfresco_templates_webscripts_org_alfresco_repository_upload_upload_post_js_1._c_script_0(classpath*:alfresco/templates/webscripts/org/alfresco/repository/upload/upload.post.js:416)
   at org.mozilla.javascript.gen.classpath__alfresco_templates_webscripts_org_alfresco_repository_upload_upload_post_js_1.call(classpath*:alfresco/templates/webscripts/org/alfresco/repository/upload/upload.post.js)
   at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
   at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
   at org.mozilla.javascript.gen.classpath__alfresco_templates_webscripts_org_alfresco_repository_upload_upload_post_js_1.call(classpath*:alfresco/templates/webscripts/org/alfresco/repository/upload/upload.post.js)
   at org.mozilla.javascript.gen.classpath__alfresco_templates_webscripts_org_alfresco_repository_upload_upload_post_js_1.exec(classpath*:alfresco/templates/webscripts/org/alfresco/repository/upload/upload.post.js)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:502)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:200)
   at org.alfresco.repo.processor.ScriptServiceImpl.execute(ScriptServiceImpl.java:212)
   at org.alfresco.repo.processor.ScriptServiceImpl.executeScript(ScriptServiceImpl.java:174)
   at org.alfresco.repo.web.scripts.RepositoryScriptProcessor.executeScript(RepositoryScriptProcessor.java:102)
   at org.springframework.extensions.webscripts.AbstractWebScript.executeScript(AbstractWebScript.java:1364)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:86)
   … 34 more


Any suggestions on what's wrong or how to debug the problem?
2 REPLIES 2

driekhof
Champ in-the-making
Champ in-the-making
Finally found a solution, used multipart form instead of raw json:


   public static void upload2()
   {
      System.out.println("ticket=" + ticket);
      
      
       Client clientUpload = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
       FileDataBodyPart filePart = new FileDataBodyPart("filedata", new File("test2.txt"));
       FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
      
       FormDataMultiPart multipart =
             (FormDataMultiPart)formDataMultiPart.field("siteid", "mysite")
                                                      .field("containerid", "documentLibrary")
                                                      .field("uploaddirectory", "java")
                                                      .bodyPart(filePart);

       WebTarget target = clientUpload.target(getBaseURI(uriUpload)).queryParam("alf_ticket", ticket);
       Response response = target.request().post(Entity.entity(multipart, multipart.getMediaType()));
       
       System.out.println("response=" + response);
       try {
      formDataMultiPart.close();
           multipart.close();
       } catch (IOException e) {
      e.printStackTrace();
       }
   }   

driekhof
Champ in-the-making
Champ in-the-making
I'm trying to set the title and description fields for the uploaded file.  I thought it would be as easy as this, but it's not working:


FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart
            .field("siteid", site).field("containerid", "documentLibrary")
            .field("title", "THIS DOESNT WORK").field("description", "NEITHER DOES THIS")
            .field("uploaddirectory", uploadFolder).bodyPart(filePart);


Any ideas?