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

jpotts
World-Class Innovator
World-Class Innovator
That NoClassDefFound indicates that the app server can't find a class it is expecting. The javax.xml.rpc.ServiceException class is in jaxrpc.jar so make sure that's in WEB-INF/lib of your web app.

Jeff

keerthi
Champ in-the-making
Champ in-the-making
Hi,

Thanks for your reply..

I have included the jar file as you said, but now i m getting new error as follows

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: org/bouncycastle/jce/provider/BouncyCastleProvider
   org.apache.ws.axis.security.WSDoAllSender.<clinit>(WSDoAllSender.java:69)
   java.lang.Class.forName0(Native Method)
   java.lang.Class.forName(Unknown Source)
   org.apache.axis.utils.ClassUtils$2.run(ClassUtils.java:177)
   java.security.AccessController.doPrivileged(Native Method)
   org.apache.axis.utils.ClassUtils.loadClass(ClassUtils.java:160)
   org.apache.axis.utils.ClassUtils.forName(ClassUtils.java:100)
   org.apache.axis.deployment.wsdd.WSDDDeployableItem.getJavaClass(WSDDDeployableItem.java:353)
   org.apache.axis.deployment.wsdd.WSDDDeployableItem.makeNewInstance(WSDDDeployableItem.java:295)
   org.apache.axis.deployment.wsdd.WSDDDeployableItem.getNewInstance(WSDDDeployableItem.java:274)
   org.apache.axis.deployment.wsdd.WSDDDeployableItem.getInstance(WSDDDeployableItem.java:260)
   org.apache.axis.deployment.wsdd.WSDDChain.makeNewInstance(WSDDChain.java:125)
   org.apache.axis.deployment.wsdd.WSDDDeployableItem.getNewInstance(WSDDDeployableItem.java:274)
   org.apache.axis.deployment.wsdd.WSDDDeployableItem.getInstance(WSDDDeployableItem.java:260)
   org.apache.axis.deployment.wsdd.WSDDDeployment.getGlobalRequest(WSDDDeployment.java:473)
   org.apache.axis.configuration.FileProvider.getGlobalRequest(FileProvider.java:269)
   org.apache.axis.AxisEngine.getGlobalRequest(AxisEngine.java:365)
   org.apache.axis.client.AxisClient.invoke(AxisClient.java:126)
   org.apache.axis.client.Call.invokeEngine(Call.java:2784)
   org.apache.axis.client.Call.invoke(Call.java:2767)
   org.apache.axis.client.Call.invoke(Call.java:2443)
   org.apache.axis.client.Call.invoke(Call.java:2366)
   org.apache.axis.client.Call.invoke(Call.java:1812)
   org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub.update(RepositoryServiceSoapBindingStub.java:986)
   sampleTest.doPost(sampleTest.java:120)
   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.


I m using jdk1.6.0_13
And also i have included the jar files in the following location, even then its showing the same error

in <ext> I put jce1_2_2.jar, local_policy.jar, sunjce_provider.jar, and US-export_policy.jar
in <security> I put (a copy of) local_policy.jar, and US_export_policy.jar


Smiley Sad  Help me resolve it

keerthi
Champ in-the-making
Champ in-the-making
Hi,

After adding those jar files i m getting the error as follows


SEVERE: Servlet.service() for servlet sampleTest threw exception
java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
   at org.apache.ws.axis.security.WSDoAllSender.<clinit>(WSDoAllSender.java:69)
   at java.lang.Class.forName0(Native Method)
   at java.lang.Class.forName(Unknown Source)
   at org.apache.axis.utils.ClassUtils$2.run(ClassUtils.java:177)
   at java.security.AccessController.doPrivileged(Native Method)
   at org.apache.axis.utils.ClassUtils.loadClass(ClassUtils.java:160)
   at org.apache.axis.utils.ClassUtils.forName(ClassUtils.java:100)
   at org.apache.axis.deployment.wsdd.WSDDDeployableItem.getJavaClass(WSDDDeployableItem.java:353)
   at org.apache.axis.deployment.wsdd.WSDDDeployableItem.makeNewInstance(WSDDDeployableItem.java:295)
   at org.apache.axis.deployment.wsdd.WSDDDeployableItem.getNewInstance(WSDDDeployableItem.java:274)
   at org.apache.axis.deployment.wsdd.WSDDDeployableItem.getInstance(WSDDDeployableItem.java:260)
   at org.apache.axis.deployment.wsdd.WSDDChain.makeNewInstance(WSDDChain.java:125)
   at org.apache.axis.deployment.wsdd.WSDDDeployableItem.getNewInstance(WSDDDeployableItem.java:274)
   at org.apache.axis.deployment.wsdd.WSDDDeployableItem.getInstance(WSDDDeployableItem.java:260)
   at org.apache.axis.deployment.wsdd.WSDDDeployment.getGlobalRequest(WSDDDeployment.java:473)
   at org.apache.axis.configuration.FileProvider.getGlobalRequest(FileProvider.java:269)
   at org.apache.axis.AxisEngine.getGlobalRequest(AxisEngine.java:365)
   at org.apache.axis.client.AxisClient.invoke(AxisClient.java:126)
   at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
   at org.apache.axis.client.Call.invoke(Call.java:2767)
   at org.apache.axis.client.Call.invoke(Call.java:2443)
   at org.apache.axis.client.Call.invoke(Call.java:2366)
   at org.apache.axis.client.Call.invoke(Call.java:1812)
   at org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub.update(RepositoryServiceSoapBindingStub.java:986)
   at sampleTest.doPost(sampleTest.java:120)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
   at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:857)
   at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:565)
   at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1509)
   at java.lang.Thread.run(Unknown Source)


Could you plz help me to resolve it…


Thanks

jpotts
World-Class Innovator
World-Class Innovator
You're still missing a JAR. If you go to http://www.findjar.com you can enter a class and it will tell you what JARs it is typically packaged in. When I searched for that BouncyCastle class I found: bcprov-jdk15-137.jar which is also in $TOMCAT_HOME/webapps/alfresco/WEB-INF/lib. So make sure that's in your classpath. Continue this process until you've resolved all of your "NoClassDefFound" exceptions.

Jeff

keerthi
Champ in-the-making
Champ in-the-making
Hi Jeff,

Thanks for reply

i have inculded all the jars which you have givnen, Still i will not be able to upload a file.
its giving the error message as follows

Error uploading file :


Do i need to change anything in my coding


Thanks,
Keerthi

mdutoo
Champ on-the-rise
Champ on-the-rise
Hi Keerthi

In your exception handling, don't only print the exception, but also its stack trace :
e.printStackTrace();
This way you'll know more about your error.
Then I'd advise you to enable remote debugging in tomcat, and debug using Eclipse remote debugging, by first putting a breakpoint before the last place met in the trace.

Regards,
Marc

keerthi
Champ in-the-making
Champ in-the-making
Hi,

After giving e.getPrintStackTrace() , am getting error message as

Error uploading file : org.alfresco.webservice.util.WebServiceException: Error starting session.
[Ljava.lang.StackTraceElement;@d75c47
org.alfresco.webservice.util.WebServiceException: Error starting session.


Could you plz help me resolve it..

Thanks,
Keerthi

mdutoo
Champ on-the-rise
Champ on-the-rise
Well, I'd say "error starting session" means there's been an error while starting the session, for instance are you sure your "admin" user still has the "admin" password ?

Regards,
Marc

keerthi
Champ in-the-making
Champ in-the-making
Hi,

i got the following error message when i try to upload file into alfresco server

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)
[Ljava.lang.StackTraceElement;@1041876
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)


Plz anyone help me to resolve it


Thanks
Keerthi