AnsweredAssumed Answered

CMIS REST upload problem with non octet-stream content

Question asked by iblanco on May 21, 2009
Latest reply on Jun 2, 2009 by syberyan
I've developed ( copy pasted from different examples ) a little proof of concept that uploads a file to an Alfresco repository using CMIS's REST API.

It works for content that has type "application/octet-stream" (which is the default content type), but for other content types the file is created in alfresco but its content is empty. If I always set the content type as "application/octet-stream" all the files upload right, but obviously their content type is not correctly set, and of course rules and behavoiurs related to the content type are not triggered.

I haven't found this bug in Jira but I wanted to check here before reporting it as a bug, I might be doing something wrong.

Here you have the code in case you are interested in ( I use mime-utils to obtain the mime type ):


package es.binovo.cmis;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;

import eu.medsea.mimeutil.MimeType;
import eu.medsea.mimeutil.MimeUtil;

import sun.misc.BASE64Encoder;

public class ProofOfConcept {

   /**
    * @param args
    */
   public static void main(String[] args) {
      ProofOfConcept poc;

      if (args.length != 4) {
         System.err.println("Error: número de parámetros inadecuado.");
         System.out
               .println("Modo de uso: ProofOfConcept <usuario> <contraseña> http://<cmis_server>:<port>/<cmis-api>/<folder-url>/<children> <fichero_a_subir>");
         System.out
               .println("Ejemplo: java es.binovo.cmis.ProofOfConcept admin admin http://pruebas.binovo:8180/alfresco/service/api/path/workspace/SpacesStore/Company%20Home/children /usr/share/pixmaps/gnome-term-tiger.png");
         return;
      }

      try {
         poc = new ProofOfConcept(args[0], args[1], new URL(args[2]),
               new File(args[3]));
         poc.createContent();
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

   private URL url;

   private File file;

   private static final String mensaje = "<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:cmis=\"http://www.cmis.org/2008/05\">\n"
         + "\t<title>FILE_NAME</title>\n"
         + "\t<content type=\"MIME_TYPE\">CONTENT</content>\n"
         + "\t<cmis:object>\n"
         + "\t\t<cmis:properties>\n"
         + "\t\t\t<cmis:propertyString cmis:name=\"ObjectTypeId\"><cmis:value>document</cmis:value></cmis:propertyString>\n"
         + "\t\t</cmis:properties>\n" + "\t</cmis:object>\n" + "</entry>\n";
   private String usuario;

   private String password;

   public ProofOfConcept(String usuario, String password, URL url, File file) {
      super();
      this.usuario = usuario;
      this.password = password;
      this.url = url;
      this.file = file;
   }

   @SuppressWarnings("unchecked")
   private void createContent() throws IOException {
      BASE64Encoder encoder = new BASE64Encoder();
      FileInputStream fis = new FileInputStream(file);
      DataInputStream dis = new DataInputStream(fis);
      // Create the byte array to hold the data
      byte[] bytes = new byte[(int) file.length()];
      dis.readFully(bytes);

      Collection<MimeType> mimeTypes = MimeUtil.getMimeTypes(file);
      MimeType mimeType = MimeUtil.getMostSpecificMimeType(mimeTypes);
      if(mimeType == null) mimeType = MimeUtil.UNKNOWN_MIME_TYPE;
      
      System.out.println("Content mime type is: " + mimeType.toString());
      
      String datos = ProofOfConcept.mensaje;
      datos = datos.replaceFirst("FILE_NAME", file.getName());
      datos = datos.replaceFirst("MIME_TYPE", mimeType.toString());
      datos = datos.replaceFirst("CONTENT", encoder.encode(bytes));
      String auth = encoder.encode((usuario + ":" + password).getBytes());

      URLConnection conn = url.openConnection();
      conn.setDoOutput(true);
      conn.setRequestProperty("Authorization", "Basic " + auth);
      conn.setRequestProperty("Content-Type",
            "application/atom+xml;type=entry");
      OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
      out.write(datos);
      out.close();

      // A ver que nos cuentan.
      String decodedString;

      BufferedReader in = new BufferedReader(new InputStreamReader(conn
            .getInputStream()));

      while ((decodedString = in.readLine()) != null) {
         System.out.println(decodedString);
      }
      in.close();

   }

}



UPDATE: It seems that it also works for image/png content type, but not for application/xml or or text/xml , it seems like something related to text formats.

Outcomes