AnsweredAssumed Answered

Upload a file into the repository

Question asked by puntino on Jul 9, 2009
Latest reply on Jul 10, 2009 by jpotts
Hallo,
I have developed a simple class with sa method "CreateNewDocument()" that uplodads a file from the filesystem (e.g.C:\\alfrescxo.txt) onto the alfresco server.
If I run the following code on my pc where Alfresco is installed, it works fine.

    public class ConnectorAlf {
      
       private String username;
       private String password;
       private Store storeRef;
      
       public static void main(String[] args){
          final String wsurl="/app:company_home/cm:Someco/cm:Marketing/cm:Whitepapers";
          String filePath="C:\\";
          String titolo = "ikhjvof" ;
          String descrizione  = "descizione di prova";
          String original_filename = "alfresco.txt";
          ConnectorAlf conn = new ConnectorAlf("admin","admin");
          conn.createNewDocument(wsurl, filePath, titolo, descrizione, original_filename);
       }
      
       public ConnectorAlf(String username, String Password){
          this.setPassword(Password);
          this.setUsername(username);
          //WebServiceFactory.setEndpointAddress(Constants.URLSERVER);
          authentication();
          }
      
       /**
        * @author
        * @param name
        * @param value
        * @return namedValue
        */
       private NamedValue setProperty(String name, String value){
          return Utils.createNamedValue(name, value);     
       }
       private Store getStoreRef() {
             if (storeRef == null) {
                this.storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
             }

             return this.storeRef;
          }
       private CMLAddAspect setAspect(String value){
          return new CMLAddAspect(value, null, null, "ref1");
       }
       private String authentication(){
          String ticket = null;
          try {
             AuthenticationUtils.startSession(this.getUsername(),this.getPassword());
             ticket = AuthenticationUtils.getTicket();
             //TODO ask to Pietro
             if(ConstantsGFIDoc.DEBUG){
                System.out.println("Logged" + "ticket" +ticket);
             }
            
             return ticket;
          } catch (AuthenticationFault af) {
             af.printStackTrace();
             AuthenticationUtils.endSession();
             return ticket;
      
          }
       }
       public String createNewDocument(String wsurl, String filePath,
              String titolo, String descrizione, String original_filename) {
              if(null==wsurl || wsurl.equals("")){
                 return null;
              }
              try {
                  // Create a reference to the parent where we want to create content
                   Store storeRef = getStoreRef();
                   System.out.println(storeRef);
                   ParentReference docParent = new ParentReference(storeRef, null, wsurl, Constants.ASSOC_CONTAINS, Constants.createQNameString(ConstantsGFIDoc.NAMESPACE_GFI_CONTENT_MODEL,titolo));
                                
                   // Create an array of NamedValue objects with the props we want to set
                   //TODO we might to write some set and get methods
                   NamedValue nameValue = setProperty(Constants.PROP_NAME,titolo);
                   NamedValue activeValue = setProperty(Constants.createQNameString(ConstantsGFIDoc.NAMESPACE_GFI_CONTENT_MODEL, ConstantsGFIDoc.PROP_IS_ACTIVE_STRING), "true");
                   NamedValue description = setProperty(Constants.PROP_DESCRIPTION, descrizione);
                   NamedValue[] contentProps = new NamedValue[] {nameValue, activeValue, description};

                   // Construct CML statement to create test doc content node          
                   CMLCreate createDoc = new CMLCreate("ref1", docParent, null, null, null, Constants.createQNameString(ConstantsGFIDoc.NAMESPACE_GFI_CONTENT_MODEL, "whitepaper"), contentProps);
                  // Construct CML statement to add webable and client-related aspects
                   //The grouping of properties that need to be tracked for content that is client-related is called an aspect.
                   // The "ref1" string is a reference that tells Alfresco we're adding aspects to the doc created in the CMLCreate step
                   //TODO To review these code lines
                   CMLAddAspect addWebableAspectToDoc = setAspect(Constants.createQNameString(ConstantsGFIDoc.NAMESPACE_GFI_CONTENT_MODEL, ConstantsGFIDoc.ASPECT_GFI_WEBABLE_STRING));
                  CMLAddAspect addClientRelatedAspectToDoc = setAspect(Constants.createQNameString(ConstantsGFIDoc.NAMESPACE_GFI_CONTENT_MODEL, ConstantsGFIDoc.ASPECT_GFI_CLIENT_RELATED_STRING));
                  // Construct CML Block
                   CML cml = new CML();
                   cml.setCreate(new CMLCreate[] {createDoc});
                   cml.setAddAspect(new CMLAddAspect[] {addWebableAspectToDoc, addClientRelatedAspectToDoc});
             
                   // Execute CML Block
                   UpdateResult[] results;
                results = WebServiceFactory.getRepositoryService().update(cml);
                Reference docRef = results[0].getDestination();
                   String uuid = results[ConstantsGFIDoc.UUIDINDEX].getSource().getUuid();
                      if(null ==uuid || uuid.equals("")) return "";
                   // Nodes are created, now write some content
                      if(ConstantsGFIDoc.DEBUG){
                         System.out.println("Uuid "+uuid);
                      }
                   boolean success =writeDocument(wsurl,uuid,filePath,original_filename);
                      if(success){
                         return uuid;
                      }else{
                         return null;
                      }
                    } catch (RepositoryFault e) {
                   e.printStackTrace();
                   return "";
                } catch (RemoteException e) {
                   e.printStackTrace();
                   return "";
             }                  
        }
      
        /**
         * @author
        * @param wsurl
        * @param uuid, unique identifier
        * @param filePath, path from which to copy the document
        * @param original_fileName, filename with extension e.g. pippo.txt
        */
       public boolean writeDocument(String wsurl,String uuid,String filePath,String original_fileName){
          try {
                Store storeRef = getStoreRef();
                Reference docRef = new Reference(storeRef,uuid,wsurl);
                //Reference docRef = new Reference();
                //docRef.setUuid(uuid);
                //if docRef, maybe it is not possible to write into the file
                if(ConstantsGFIDoc.DEBUG){
                   System.out.println(docRef);
                }
                ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
                if(ConstantsGFIDoc.DEBUG){
                   System.out.println(contentService);
                }
                ContentFormat contentFormat = new ContentFormat(ConstantsGFIDoc.DEFAULTFORMAT,ConstantsGFIDoc.ENCODING);
                //fill the file referenced by doc ref with the content of the file "filler"
                   Content docContentRef = contentService.write(docRef,Constants.PROP_CONTENT, Utilities.fillBuffer(filePath+original_fileName), contentFormat);
                   if(docContentRef.getLength()>0){
                      return true;
                   }else{
                      return false;
                   }
                  
                } catch (ContentFault e) {
                   e.printStackTrace();
                   return false;
                } catch (RemoteException e) {
                   e.printStackTrace();
                   return false;
                }
               
            
          }
         
       private String getUsername() {
          return username;
       }
       private void setUsername(String username) {
          this.username = username;
       }
       /**
        * @return the password
        */
       private String getPassword() {
          return password;
       }
       /**
        * @param password the password to set
        */
       private void setPassword(String password) {
          this.password = password;
       }
    }


If I point to another Alfresco server that is not on my pc by changing the repository.location line in the webserviceclient.properties,
I get the error that comes out at line:
results = WebServiceFactory.getRepositoryService().update(cml);
Any help is appreciated
thank you in advance

    AxisFault
    faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.generalException
    faultSubcode:
    faultString:
    faultActor:
    faultNode:
    faultDetail:
       {http://www.alfresco.org/ws/service/repository/1.0}RepositoryFault:<ns1:errorCode>0</ns1:errorCode><ns1:message>org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update</ns1:message>
       {http://xml.apache.org/axis/}exceptionName:org.alfresco.repo.webservice.repository.RepositoryFault
       {http://xml.apache.org/axis/}stackTrace:
       at org.alfresco.repo.webservice.repository.RepositoryWebService.update(RepositoryWebService.java:341)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397)
       at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186)
       at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
       at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
       at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
       at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
       at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)
       at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
       at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
       at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)
       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.Http11Processor.process(Http11Processor.java:845)
       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
       at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
       at java.lang.Thread.run(Thread.java:619)

       {http://xml.apache.org/axis/}hostname:rm-dileo-n01


       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
       at java.lang.Class.newInstance0(Class.java:355)
       at java.lang.Class.newInstance(Class.java:308)
       at org.apache.axis.encoding.ser.BeanDeserializer.<init>(BeanDeserializer.java:104)
       at org.apache.axis.encoding.ser.BeanDeserializer.<init>(BeanDeserializer.java:90)
       at org.alfresco.webservice.repository.RepositoryFault.getDeserializer(RepositoryFault.java:146)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at org.apache.axis.encoding.ser.BaseDeserializerFactory.getSpecialized(BaseDeserializerFactory.java:154)
       at org.apache.axis.encoding.ser.BaseDeserializerFactory.getDeserializerAs(BaseDeserializerFactory.java:84)
       at org.apache.axis.encoding.DeserializationContext.getDeserializer(DeserializationContext.java:464)
       at org.apache.axis.encoding.DeserializationContext.getDeserializerForType(DeserializationContext.java:547)
       at org.apache.axis.message.SOAPFaultDetailsBuilder.onStartChild(SOAPFaultDetailsBuilder.java:157)
       at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
       at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
       at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
       at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
       at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
       at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
       at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
       at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
       at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
       at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
       at org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source)
       at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
       at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
       at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
       at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
       at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
       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 com.someco.examples.ConnectorAlf.createNewDocument(ConnectorAlf.java:124)
       at com.someco.examples.ConnectorAlf.main(ConnectorAlf.java:44)

Outcomes