cancel
Showing results for 
Search instead for 
Did you mean: 

to upload file into alfresco server (with java web service)

keerthi
Champ in-the-making
Champ in-the-making
Hi,
Am new to alfresco server and web service calls,i want to upload a file into alfresco server with the help of java web service calls , for that i created a jsp page and servlet class.. those are given below

my jsp file as follows
————————-
<html>
<body>    
    This is my JSP page. <br>
     
   <!–<form action="CreateFolder.jsp"  method="post"  >–>
   
    <form method="post" action="sampleTest" >
   <table>
   <tR>
   <td>Title
   </td>
   <td><input type="text" name="title"  />
   </td>
   </tR>
      <tR>
   <td>Description
   </td>
      <td><input type="text" name="description"  />
   </td>
   </tR>
      <tR>
   <td>select File
   </td><td><input type="file" name="file" id="file" />
      
   </td>
   </tR>
   <tr>
   
   <td colspan="2">
   <input type="submit"  value="upload" />
   </td>
   </tr>
   </table>
   </form>
   
   
   </body>
</html>

and my servlet class as follows

import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.webservice.content.ContentServiceSoapBindingStub;
import org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub;
import org.alfresco.webservice.repository.UpdateResult;
import org.alfresco.webservice.types.CML;
import org.alfresco.webservice.types.CMLAddAspect;
import org.alfresco.webservice.types.CMLCreate;
import org.alfresco.webservice.types.ContentFormat;
import org.alfresco.webservice.types.NamedValue;
import org.alfresco.webservice.types.ParentReference;
import org.alfresco.webservice.types.Reference;
import org.alfresco.webservice.types.Store;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.ContentUtils;
import org.alfresco.webservice.util.Utils;
import org.alfresco.webservice.util.WebServiceFactory;

public class sampleTest extends HttpServlet {
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
   String msg="";
   try
   {
   System.out.println("inside try");
   System.out.println("For Testing Valuess….");
   
   System.out.println("title::Value::::"+(String)request.getParameter("title"));   
   //System.out.println("title::::::"+request.getParameter("title").trim());
   String title=(String) request.getParameter("title").trim();
   System.out.println("title::::::"+title);
   
   String description=request.getParameter("description").trim();
   System.out.println("description ::::"+description);
   String path = request.getParameter("file");
   System.out.println("file name:::: " +path);

   path = path.replaceAll("\\\\", "/");
   String fileName = path.substring(path.lastIndexOf('/')+1,path.length());
   System.out.println("file name:::: " +fileName);

   String ext= fileName.substring(fileName.lastIndexOf('.')+1,fileName.length());
   System.out.println("ext name:::: " +ext);
   String contType="";

   if(ext.equals("txt"))
   contType = "text/plain";
   else if (ext.equals("xls"))
   contType = "application/vnd.ms-excel";
   else if (ext.equals("doc"))
   contType = "application/msword";
   else if (ext.equals("html") || ext.equals("htm"))
   contType = "text/html";
   else if (ext.equals("jpg") || ext.equals("jpeg"))
   contType = "image/jpeg";
   else if (ext.equals("bmp"))
   contType = "image/bmp";
   else if (ext.equals("pdf"))
   contType = "application/pdf";
   else if (ext.equals("ppt"))
   contType = "application/vnd.ms-powerpoint";
   else if(ext.equals("xml"))
   contType = "text/xml";
   else if (ext.equals("zip"))
   contType = "application/vnd.ms-zip";

   if(contType.equals(""))
   {
      System.out.println(":::::::insdide file not found");
   msg="Unknown file format. File not uploded.";
   }
   else
   {
   System.out.println("inside alfresco part:::::::::");   
   WebServiceFactory.setEndpointAddress("http://203.193.158.170:8080/alfresco/");
   AuthenticationUtils.startSession("admin", "admin");
   
   
   System.out.println("after login");
   
   // Create a reference to the parent where we want to create content
   Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
   
   ParentReference companyHomeParent = new ParentReference(storeRef, null, "/app:company_home/cm:bneps", Constants.ASSOC_CONTAINS, null);

   RepositoryServiceSoapBindingStub repositoryService =WebServiceFactory.getRepositoryService();
   ContentServiceSoapBindingStub contentService =WebServiceFactory.getContentService();
   NamedValue[] contentProps = new NamedValue[1];
   NamedValue[] titledProps = new NamedValue[2];

   // Assign name
   companyHomeParent.setChildName("{http://www.alfresco.org/model/content/1.0}" + fileName);

   contentProps[0] = Utils.createNamedValue(Constants.PROP_NAME, fileName);
   System.out.println("contentProps :::::"+contentProps[0]);
   //Construct CML statement to add titled aspect
   titledProps[0] = Utils.createNamedValue(Constants.PROP_TITLE, title);
   System.out.println("titledProps :::::"+titledProps[0]);
   titledProps[1] = Utils.createNamedValue(Constants.PROP_DESCRIPTION, description);
   System.out.println("titledProps :::::"+titledProps[1]);
   CMLAddAspect addAspect = new CMLAddAspect(Constants.ASPECT_TITLED, titledProps, null, "1");

   //create content
   // Construct CML statement to create content node
   // Note: Assign "1" as a local id, so we can refer to it in subsequent
   // CML statements within the same CML block
   CMLCreate create = new CMLCreate("1", companyHomeParent, companyHomeParent.getUuid(), Constants.ASSOC_CONTAINS, null, Constants.PROP_CONTENT, contentProps);
   // Construct CML Block
   CML cml = new CML();
   cml.setCreate(new CMLCreate[] {create});
   cml.setAddAspect(new CMLAddAspect[] {addAspect});
   // Issue CML statement via Repository Web Service and retrieve result
   // Note: Batching of multiple statements into a single web call
   UpdateResult[] result = repositoryService.update(cml);
   Reference content = result[0].getDestination();
   // Write some content
   FileInputStream is = new FileInputStream(path);
   byte[] bytes = ContentUtils.convertToByteArray(is);

   ContentFormat format = new ContentFormat(contType, "UTF-8");
   // Write the content
   contentService.write(content, Constants.PROP_CONTENT, bytes, format);
   msg="File Uploaded Successfully";
   System.out.println("msg:::::::::::::::::;"+msg);
   }
   }
   catch(Exception e)
   {
   msg="Error uploading file";
   System.out.println("Error uploading file : "+e);
   System.out.println(e.toString());
   }
   finally
   {
   // End the session
   AuthenticationUtils.endSession();
   }
   request.setAttribute("msg",msg);
   RequestDispatcher rd=request.getRequestDispatcher("StatusMessage.jsp");
   rd.forward(request,response);
   }
}

But when i run this it shows the following error, Plz help me to resolve it…. could u plz tel me where i made mistakes..



type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception


root cause

java.lang.NoClassDefFoundError: javax/xml/rpc/ServiceException
   sampleTest.doPost(sampleTest.java:81)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.
12 REPLIES 12

shamabbas
Champ in-the-making
Champ in-the-making
Hi keerti and all.,
I'm having same problem.. keerti if u have resolved it already please help me out as soon as possible. il b very very thankful to you.
Ill paste below what exception i got…

13:09:44,883 INFO  [STDOUT] inside try
13:09:44,883 INFO  [STDOUT] For Testing Values….
13:09:44,883 INFO  [STDOUT] title::Valueas
13:09:44,883 INFO  [STDOUT] title::::::as
13:09:44,883 INFO  [STDOUT] Description:::::asasas
13:09:44,883 INFO  [STDOUT] file name:::2.json
13:09:44,883 INFO  [STDOUT] file name :::2.json
13:09:44,883 INFO  [STDOUT] ext name :::json
13:09:44,883 INFO  [STDOUT] inside alfresco part::::::::
13:09:45,898 INFO  [STDOUT] Error uploading file:::org.alfresco.webservice.util.WebServiceException: Error starting session.
13:09:45,898 ERROR [STDERR] org.alfresco.webservice.util.WebServiceException: Error starting session.
13:09:45,898 ERROR [STDERR]     at org.alfresco.webservice.util.AuthenticationUtils.startSession(AuthenticationUtils.java:94)
13:09:45,898 ERROR [STDERR]     at upload3.doPost(upload3.java:176)
13:09:45,898 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
13:09:45,898 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
13:09:45,898 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
13:09:45,898 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
13:09:45,898 ERROR [STDERR]     at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
13:09:45,898 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
13:09:45,898 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
13:09:45,898 ERROR [STDERR]     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
13:09:45,898 ERROR [STDERR]     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
13:09:45,898 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
13:09:45,898 ERROR [STDERR]     at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
13:09:45,898 ERROR [STDERR]     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
13:09:45,898 ERROR [STDERR]     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
13:09:45,898 ERROR [STDERR]     at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
13:09:45,898 ERROR [STDERR]     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
13:09:45,898 ERROR [STDERR]     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
13:09:45,898 ERROR [STDERR]     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
13:09:45,898 ERROR [STDERR]     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
13:09:45,898 ERROR [STDERR]     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
13:09:45,898 ERROR [STDERR]     at java.lang.Thread.run(Thread.java:619)
13:09:45,898 ERROR [STDERR] Caused by: java.net.ConnectException: Connection refused: connect
13:09:45,898 ERROR [STDERR]     at org.apache.axis.AxisFault.makeFault(AxisFault.java:101)
13:09:45,898 ERROR [STDERR]     at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:154)
13:09:45,898 ERROR [STDERR]     at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
13:09:45,898 ERROR [STDERR]     at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
13:09:45,898 ERROR [STDERR]     at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
13:09:45,898 ERROR [STDERR]     at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165)
13:09:45,898 ERROR [STDERR]     at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
13:09:45,898 ERROR [STDERR]     at org.apache.axis.client.Call.invoke(Call.java:2767)
13:09:45,898 ERROR [STDERR]     at org.apache.axis.client.Call.invoke(Call.java:2443)
13:09:45,898 ERROR [STDERR]     at org.apache.axis.client.Call.invoke(Call.java:2366)
13:09:45,898 ERROR [STDERR]     at org.apache.axis.client.Call.invoke(Call.java:1812)
13:09:45,898 ERROR [STDERR]     at org.alfresco.webservice.authentication.AuthenticationServiceSoapBindingStub.startSession(AuthenticationServiceSoapBindingS
13:09:45,898 ERROR [STDERR]     at org.alfresco.webservice.util.AuthenticationUtils.startSession(AuthenticationUtils.java:79)
13:09:45,898 ERROR [STDERR]     … 21 more
13:09:45,898 ERROR [STDERR] Caused by: java.net.ConnectException: Connection refused: connect
13:09:45,898 ERROR [STDERR]     at java.net.PlainSocketImpl.socketConnect(Native Method)
13:09:45,898 ERROR [STDERR]     at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
13:09:45,898 ERROR [STDERR]     at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
13:09:45,898 ERROR [STDERR]     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
13:09:45,898 ERROR [STDERR]     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
13:09:45,898 ERROR [STDERR]     at java.net.Socket.connect(Socket.java:519)
13:09:45,898 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
13:09:45,898 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
13:09:45,898 ERROR [STDERR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
13:09:45,898 ERROR [STDERR]     at java.lang.reflect.Method.invoke(Method.java:597)
13:09:45,898 ERROR [STDERR]     at org.apache.axis.components.net.DefaultSocketFactory.create(DefaultSocketFactory.java:153)
13:09:45,898 ERROR [STDERR]     at org.apache.axis.components.net.DefaultSocketFactory.create(DefaultSocketFactory.java:120)
13:09:45,898 ERROR [STDERR]     at org.apache.axis.transport.http.HTTPSender.getSocket(HTTPSender.java:191)
13:09:45,914 ERROR [STDERR]     at org.apache.axis.transport.http.HTTPSender.writeToSocket(HTTPSender.java:404)
13:09:45,914 ERROR [STDERR]     at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:138)
13:09:45,914 ERROR [STDERR]     … 32 more
13:09:45,914 INFO  [STDOUT] [Ljava.lang.StackTraceElement;@a25267
13:09:45,914 INFO  [STDOUT] org.alfresco.webservice.util.WebServiceException: Error starting session.
13:09:45,914 INFO  [STDOUT] session ending





and also that when i copy my application in JBOSS\..\deploy folder than alfresco explorer http://localhost:8080/alfresco/ doesnt work.. only does when i delete my application… pleaseeeeeeeeeeeee HELPP IF ANYONE CAN!!!!!??!??

pedrito77
Champ in-the-making
Champ in-the-making
to shamabbas:
"13:09:45,898 ERROR [STDERR] Caused by: java.net.ConnectException: Connection refused: connect"
means there is something wrong with connection to Alfresco server. It can be stopped or maybe you defined wrong connection URL ? It should be something like:
WebServiceFactory.setEndpointAddress("http://localhost:8080/alfresco/api");  (this is when you have deployed your own Alfresco server on your machine)

maybe it will help

nareshthota
Champ in-the-making
Champ in-the-making
Hi keerthi…

do u solved the above exception…
Error uploading file : WSDoAllReceiver: security processing failed; nested exception is:
   org.apache.ws.security.WSSecurityException: The message has expired (WSSecurityEngine: Invalid timestamp The security semantics of message have expired)…

if yes please help me to solve it…

thanks
Naresh