AnsweredAssumed Answered

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

Question asked by keerthi on Jun 18, 2009
Latest reply on Apr 12, 2012 by nareshthota
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.

Outcomes