Está en la página 1de 6

Alfresco

Upload Content notes 004


Created 01/09/2011 T. Patrick Bailey package src.com._10x13.alfrescotesting; import java.io.FileInputStream; import java.io.IOException; import java.rmi.RemoteException; import java.util.Properties; import org.alfresco.webservice.authentication.AuthenticationFau lt; import org.alfresco.webservice.content.Content; import org.alfresco.webservice.content.ContentServiceSoapBindin gStub; import org.alfresco.webservice.repository.UpdateResult; import org.alfresco.webservice.types.CML; 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.Store; import org.alfresco.webservice.util.AuthenticationUtils; import org.alfresco.webservice.util.Constants; import org.alfresco.webservice.util.Utils; import org.alfresco.webservice.util.WebServiceFactory; import org.alfresco.webservice.types.Reference; import org.alfresco.webservice.util.ISO9075; /** * * @author patman */ public class UploadContent { public UploadContent(Properties prop) { String fileName = System.currentTimeMillis() + "_myFile.txt";

Uploading Content
First, it is important to note that the alfresco SDK with all its included jars is missing one. I had to download wss4j-1.5.10.jar from http://www.apache.org/dyn/closer.cgi/ws/wss4j/ . Here is a full list off all the jar files I am using that alfresco needs. activation.jar alfresco-web-service-client-3.4.b.jar axis-1.4.jar commons-discovery-0.2.jar commons-logging-1.1.jar jaxrpc.jar mail.jar opensaml-1.0.1.jar wsdl4j-1.6.2.jar wss4j-1.5.10.jar xalan.jar xercesImpl-2.8.0.jar xmlsec-1.4.1.jar

Java Code
Page 1 of 6

Store STORE = new Store(Constants.WORKSPACE_STORE, "SpacesStore");

Alfresco
String ASSOC_CONTAINS =
"{http://www.alfresco.org/model/content/1.0}contains";

update(cml); Reference newContentNode = result[0].getDestination(); Content content = contService.write( newContentNode, Constants.PROP_CONTENT, "Just Some Text".getBytes(), contentFormat); } catch (AuthenticationFault e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } } public static void main(String...args) { Properties prop = new Properties(); try { prop.load(new FileInputStream( "properties/alfresco.properties")); UploadContent upCont = new UploadContent(prop); } catch (IOException ex) { ex.printStackTrace(); } } }

try { WebServiceFactory.setEndpointAddress( prop.getProperty("repository.location")); AuthenticationUtils.startSession( prop.getProperty("dev.username"), prop.getProperty("dev.password")); // Get the content service ContentServiceSoapBindingStub contService = WebServiceFactory.getContentService(); // Create a parent reference ParentReference parRef = new ParentReference(STORE, null, "/app:company_home/st:sites" + "/cm:mySite/cm:documentLibrary/" + "cm:Recording/cm:Jan/cm:" + ISO9075.encode("20101130"), ASSOC_CONTAINS, "{" + Constants.NAMESPACE_CONTENT_MODEL + "}" + fileName); // Define the content format for the content //we are adding ContentFormat contentFormat = new ContentFormat("text/plain", "UTF-8"); NamedValue[] properties = new NamedValue[]{Utils.createNamedValue( Constants.PROP_NAME, fileName)}; CMLCreate create = new CMLCreate("1", parRef, null, null, null, Constants.TYPE_CONTENT, properties); CML cml = new CML(); cml.setCreate(new CMLCreate[]{create}); UpdateResult[] result = WebServiceFactory.getRepositoryService().

Page 2 of 6

Alfresco And here is the properties file alfresco.properties (enter in your information) repository.location=http://localhost:8080/alfresco/api dev.username=admin dev.password=adminPassword We cant give it a simple path like this as the path it wants is an xPath. The only things to concern yourself with at this point is that company_name requires app: before it, sites requires st: before it and all others require cm: before their names. Also there are some issues with numbers, and I believe a few other things To deal with this you can encode a string to the ISO9075 format using the ISO9075.encode(String_to_encode) method. In this first example I am uploading a simple text file. In fact I am doing it inline without an actual text file.

Explaining the Java Code


One of the first things you do is get a reference to the parent node where you want to place this file (the folder where it will be contained).
ParentReference parRef = new ParentReference(STORE, null, "/app:company_home/st:sites" + "/cm:mySite/cm:documentLibrary/" + "cm:Month/cm:Jan/cm:" + ISO9075.encode("20101130"),

ContentFormat contentFormat = new ContentFormat("text/plain", "UTF-8");

I need to define the mime-type and the encoding.


Content content = contService.write( newContentNode, Constants.PROP_CONTENT, "Just Some Text".getBytes(), contentFormat);

And here is where the content is uploaded. Instead of reading from a file we are just feeding it the bytes from a normal String. One thing you will notice is that to use this method you need to have the entire file in memory (all the bytes). This is fine for small files but for larger files you need to use a streaming service like UploadContentServlet which is explained somewhat here
http://wiki.alfresco.com/wiki/URL_Addressability#UploadContentServlet [1] and here http://forums.alfresco.com/en/viewtopic.php?f=27&t=24427 [2].

In this example I created a site using the alfresco share interface and I called the site mySite and in the Document Library I created a folder called Month, in that folder I have a folder called Jan, and within that folder I have another folder called 20101130. If this was a normal file directory it would be /company_home/sites/mySite/documentLibrary/Month/Jan/20101130/

I will cover this topic another time.

Page 3 of 6

Alfresco Running this code will place a text file with the text Just Some Text in the given location on the alfresco share site. Now what if we wanted to upload information from an actual text file?

public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length();

I added this little bit of code to read a file entirely into a byte stream

if (length > Integer.MAX_VALUE) { throw new IOException( "File is too large for this method"); } // Create the byte array to hold the data byte[] bytes = new byte[(int)length]; // Read in the bytes int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException( "Could not completely read file" +file.getName()); } // Close the input stream and return bytes is.close(); return bytes; }

Page 4 of 6

Alfresco Then I updated this to


Content content = contService.write(newContentNode, Constants.PROP_CONTENT, getBytesFromFile(file), contentFormat);

Different File Types


What if we are trying to upload a jpeg file or an mp3 how does this need to be changed? I am going to use my new method getBytesFromFile(file) but I need to adjust some other code. Without adjusting some other code the files will upload just fine but the Content Type and encoding will be wrong as shown in this image of a png I uploaded without changing the mimetype.

Also had to add a catch for the IOException.


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

Build and ran the new code and it worked like a charm.

Page 5 of 6

Alfresco

I updated the mime type.


ContentFormat contentFormat = new ContentFormat("image/png", "UTF-8");

References
[1] UploadContentServlet
http://wiki.alfresco.com/wiki/URL_Addressability#UploadContentServlet

Visited 01/2011 [2] Upload large files using Alfresco, Forum topic http://forums.alfresco.com/en/viewtopic.php?f=27&t=24427 Visited 01/2011

Here are some mime types for different types of content Content type Text Image png Image jpeg PDF MP3 (MPEG Audio) Mime type text/plain image/png image/jpeg application/pdf audio/x-mpeg

I also ran across this page with some code that seems to outline a lot of mime types alfresco uses. http://forge.alfresco.com/plugins/scmsvn/viewcvs.php/trunk/Record.cs ?root=outlook-addin&view=markup

Well that is it a first primer on uploading content to alfresco.

Page 6 of 6

También podría gustarte