cancel
Showing results for 
Search instead for 
Did you mean: 

Create folder and document using OpenCmis 0.2.0

achillies
Champ in-the-making
Champ in-the-making
Hi,
I am using the below code to create a document and to upload a file.
This code used to work fine with OpenCmis version 0.1.0 without the Alfresco OpenCmis Extension. When I upgraded the version of OpenCmis I started getting Internal Server Error. I have been tried using the Alfresco OpenCmisExtension but I cannot seem to get it to work. Any help on how to get it to work is highly appreciated. I have also tried to use the folder.createFolder method.
I am using the alfresco repository version 3.4.0(b)
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.ObjectId;
import org.apache.chemistry.opencmis.client.api.Policy;
import org.apache.chemistry.opencmis.client.api.Repository;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.PropertyIds;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.Ace;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.enums.VersioningState;
import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;

public class test {

   public void createFolder() {
      String serverURL = "******";
      String serverUserName = "******";
      String serverPassword = "****";
      String repositoryID = "*******";
      String folderName = "test";

      try {
         Session session = null;

         // create session parameters
         Map<String, String> sessionParameters = new HashMap<String, String>();
         sessionParameters.put(SessionParameter.ATOMPUB_URL, serverURL);
         sessionParameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
         sessionParameters.put(SessionParameter.USER, serverUserName);
         sessionParameters.put(SessionParameter.PASSWORD, serverPassword);
         sessionParameters.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
         sessionParameters.put(SessionParameter.REPOSITORY_ID, repositoryID);

         // create session using the given repositoryId
         SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
         List<Repository> repositories = sessionFactory.getRepositories(sessionParameters);
         sessionParameters.put(SessionParameter.REPOSITORY_ID, repositories.get(0).getId());
         session = sessionFactory.createSession(sessionParameters);

         Folder root = session.getRootFolder();
         ObjectId parentId = session.createObjectId(root.getId());

         // try to create the folder "test" which fails.
         createMainFolder(session, parentId, folderName);

         // Assuming we were able to create the folder (Done by using an existing folder and skipping the above creation)

         CmisObject folderObj = null;
         for (CmisObject childrens : root.getChildren()) {
            if (childrens.getName().equals(folderName)) {
               folderObj = childrens;
               break;
            }
         }
         Folder folder = (Folder) folderObj;

         File file = new File("C:\\testfiles\\a.pdf");

         uploadDocument(session, folder, file);

      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   private ObjectId createMainFolder(Session session, ObjectId parentId, String rootFolder) {

      ObjectId mainFolderID = null;

      Map<String, Object> properties = new HashMap<String, Object>();
      properties.put(PropertyIds.OBJECT_TYPE_ID, BaseTypeId.CMIS_FOLDER.value());

      properties.put(PropertyIds.NAME, rootFolder);

      List<Ace> addAces = new LinkedList<Ace>();
      List<Ace> removeAces = new LinkedList<Ace>();
      List<Policy> policies = new LinkedList<Policy>();

      try {

         mainFolderID = session.createFolder(properties, parentId, policies, addAces, removeAces);

      } catch (Exception e) {
         e.printStackTrace();
         /*
          * Exception is thrown here. org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException: Internal Server Error
          */
      }

      return mainFolderID;
   }

   private void uploadDocument(Session session, Folder folder, File file) throws IOException {
      FileInputStream fis = new FileInputStream(file);
      DataInputStream dis = new DataInputStream(fis);
      byte[] bytes = new byte[(int) file.length()];
      dis.readFully(bytes);

      Map<String, String> newDocProps = new HashMap<String, String>();
      newDocProps.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
      newDocProps.put(PropertyIds.NAME, file.getName());

      List<Ace> addAces = new LinkedList<Ace>();
      List<Ace> removeAces = new LinkedList<Ace>();
      List<Policy> policies = new LinkedList<Policy>();

      try {

         ContentStream contentStream = new ContentStreamImpl(file.getAbsolutePath(), null, "application/pdf",
               new ByteArrayInputStream(bytes));
         org.apache.chemistry.opencmis.client.api.Document doc = folder.createDocument(newDocProps, contentStream,
               VersioningState.NONE, policies, removeAces, addAces, session.getDefaultContext());

      } catch (Exception e) {
         e.printStackTrace();
         /*
          * Same error here. org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException: Internal Server Error
          */
      }

   }

}
4 REPLIES 4

fmui
Champ in-the-making
Champ in-the-making
Hi,

Could you please catch a CmisBaseException and post the string that getErrorContent() returns?
It should contain a clue of why it is failing.

Thanks,

Florian

achillies
Champ in-the-making
Champ in-the-making
Thanks a lot for the prompt reply. I did as you asked me to.
While trying to create a new folder I get
Unable to retrieve content: Connection reset
and while trying to upload a pdf file I get
{cmisSmiley SurprisedbjectTypeId=cmis:document, cmis:name=a.pdf}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <title>Web Script Status 500 - Internal Error</title>
      <link rel="stylesheet" href="/css/webscripts.css" type="text/css" />
   </head>
   <body>
      <div>
         <table>
            <tr>
               <td><img src="/images/logo/AlfrescoLogo32.png" alt="Alfresco" /></td>
               <td><span class="title">Web Script Status 500 - Internal Error</span></td>
            </tr>
         </table>
         <br/>
         <table>
            <tr><td>The Web Script <a href="%2Fservice%2Fcmis%2Fs%2Fworkspace%3ASpacesStore%2Fi%2Ffba3c876-9682-4464-abb5-1e1c596fcc27%2Fchildren%3FversioningState%3Dnone">/service/cmis/s/workspaceSmiley FrustratedpacesStore/i/fba3c876-9682-4464-abb5-1e1c596fcc27/children</a> has responded with a status of 500 - Internal Error.</td></tr>
         </table>
         <br/>
         <table>
            <tr><td><b>500 Description:</b></td><td> An error inside the HTTP server which prevented it from fulfilling the request.</td></tr>
            <tr><td> </td></tr>
            <tr><td><b>Message:</b></td><td>02043559 Wrapped Exception (with status template): com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
at [row,col {unknown-source}]: [1,0]</td></tr>
            <tr><td></td><td> </td></tr>
   <tr><td><b>Exception:</b></td><td>com.ctc.wstx.exc.WstxEOFException - Unexpected EOF in prolog
at [row,col {unknown-source}]: [1,0]</td></tr>
   <tr><td></td><td> </td></tr>
         <tr><td></td><td>com.ctc.wstx.sr.StreamScanner.throwUnexpectedEOF(StreamScanner.java:661)</td></tr>
         <tr><td></td><td>com.ctc.wstx.sr.BasicStreamReader.handleEOF(BasicStreamReader.java:2134)</td></tr>
         <tr><td></td><td>com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2040)</td></tr>
         <tr><td></td><td>com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1069)</td></tr>
         <tr><td></td><td>org.apache.abdera.parser.stax.FOMBuilder.getNextElementToParse(FOMBuilder.java:163)</td></tr>
         <tr><td></td><td>org.apache.abdera.parser.stax.FOMBuilder.next(FOMBuilder.java:187)</td></tr>
         <tr><td></td><td>org.apache.abdera.parser.stax.FOMBuilder.getFomDocument(FOMBuilder.java:333)</td></tr>
         <tr><td></td><td>org.apache.abdera.parser.stax.FOMParser.getDocument(FOMParser.java:72)</td></tr>
         <tr><td></td><td>org.apache.abdera.parser.stax.FOMParser.parse(FOMParser.java:207)</td></tr>
         <tr><td></td><td>org.apache.abdera.parser.stax.FOMParser.parse(FOMParser.java:145)</td></tr>
         <tr><td></td><td>org.apache.abdera.util.AbstractParser.parse(AbstractParser.java:101)</td></tr>
         <tr><td></td><td>org.springframework.extensions.webscripts.atom.AbderaServiceImpl.parse(AbderaServiceImpl.java:152)</td></tr>
         <tr><td></td><td>org.springframework.extensions.webscripts.atom.AtomReader.read(AtomReader.java:85)</td></tr>
         <tr><td></td><td>org.springframework.extensions.webscripts.atom.AtomReader.createScriptParameters(AtomReader.java:99)</td></tr>
         <tr><td></td><td>org.springframework.extensions.webscripts.AbstractWebScript.createScriptParameters(AbstractWebScript.java:398)</td></tr>
         <tr><td></td><td>org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:81)</td></tr>
         <tr><td></td><td>org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:383)</td></tr>
         <tr><td></td><td>org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:381)</td></tr>
         <tr><td></td><td>org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:436)</td></tr>
         <tr><td></td><td>org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:466)</td></tr>
         <tr><td></td><td>org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:304)</td></tr>
         <tr><td></td><td>org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:333)</td></tr>
         <tr><td></td><td>org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:189)</td></tr>
         <tr><td></td><td>org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:118)</td></tr>
         <tr><td></td><td>javax.servlet.http.HttpServlet.service(HttpServlet.java:717)</td></tr>
         <tr><td></td><td>org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)</td></tr>
         <tr><td></td><td>org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)</td></tr>
         <tr><td></td><td>org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:58)</td></tr>
         <tr><td></td><td>org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)</td></tr>
         <tr><td></td><td>org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)</td></tr>
         <tr><td></td><td>org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)</td></tr>
         <tr><td></td><td>org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)</td></tr>
         <tr><td></td><td>org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)</td></tr>
         <tr><td></td><td>org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)</td></tr>
         <tr><td></td><td>org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)</td></tr>
         <tr><td></td><td>org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)</td></tr>
         <tr><td></td><td>org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)</td></tr>
         <tr><td></td><td>org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)</td></tr>
         <tr><td></td><td>org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)</td></tr>
         <tr><td></td><td>java.lang.Thread.run(Thread.java:619)</td></tr>
   <tr><td></td><td> </td></tr>
   <tr><td><b>Exception:</b></td><td>org.apache.abdera.parser.ParseException - com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
at [row,col {unknown-source}]: [1,0]</td></tr>
   <tr><td></td><td> </td></tr>
      <tr><td></td><td>org.apache.abdera.parser.stax.FOMBuilder.next(FOMBuilder.java:260)</td></tr>
   <tr><td></td><td> </td></tr>
   <tr><td><b>Exception:</b></td><td>org.springframework.extensions.webscripts.WebScriptException - 02043559 Wrapped Exception (with status template): com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
at [row,col {unknown-source}]: [1,0]</td></tr>
   <tr><td></td><td> </td></tr>
      <tr><td></td><td>org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:749)</td></tr>
   <tr><td></td><td> </td></tr>
            <tr><td><b>Server</b>:</td><td>Community v3.4.0 (b @build-number@) schema 4,111</td></tr>
            <tr><td><b>Time</b>:</td><td>04-Mar-2011 11:42:44</td></tr>
            <tr><td></td><td> </td></tr>
            <tr><td><b>Diagnostics</b>:</td><td><a href="/service/script/org/alfresco/cmis/children.post">Inspect Web Script (org/alfresco/cmis/children.post)</a></td></tr>
         </table>
      </div>
   </body>
</html>

It would be of great help if any of you could provide me a solution to it or correct way to create a folder or upload a document.

fmui
Champ in-the-making
Champ in-the-making
Are you using a (debug) proxy server? Some proxies don't handle chunking properly and lose content.

Can you create a file custom-log4j.properties in …/tomcat/shared/classes/alfresco/extension and add  the following line:
log4j.logger.org.alfresco.repo.transaction.RetryingTransactionHelper=debug

It should provide some more information in the log file.

- Florian

prakash_saurabh
Champ in-the-making
Champ in-the-making
Hi,
I have tested your code. The code has no issues.
May be you are using wrong versions of jar or just make sure you don't have "test" name folder in the
root of repository.
You should have alfresco-opencmis-extension-0.2 jar and chemistry-opencmis jars for 0.3.0 version.
Hope this will help you.