Blog‎ > ‎

A batch to upload files in Gatein

posted May 23, 2013, 1:05 AM by Luca Stancapiano   [ updated May 23, 2013, 7:35 AM ]

Gatein allows to access with webdav remotely. It is supported in all 3.x versions of Gatein, EPP 5 and EPP 6. You can write a simple batch using this protocol. Here a sample of a java main:


          import java.io.FileInputStream;

          import java.io.FileNotFoundException;

          import java.io.IOException;

          import java.io.InputStream;


          import com.github.sardine.Sardine;

          import com.github.sardine.SardineFactory;

                        …


  String remote_path = "http://localhost:8080/rest/private/jcr/repository/portal-work";

  Sardine sardine = SardineFactory.begin("utente", "password");

  try {

  sardine.delete(remote_path + "/prova");

  } catch (IOException e1) {

  e1.printStackTrace();

  }

  try {

  System.out.println("inserimento");

  sardine.createDirectory(remote_path + "/prova");

  sardine.put(remote_path + "/prova/nuovo", new FileInputStream(FILE_PATH), "html/text");

  InputStream resource = sardine.get(remote_path);

  System.out.println(resource);

  } catch (FileNotFoundException e) {

  e.printStackTrace();

  } catch (IOException e) {

  e.printStackTrace();

  }


This code uses a webdav implementation library, called Sardine. Sardine 5.0, actually the last version, can be downloaded here: http://repo1.maven.org/maven2/com/github/lookfirst/sardine/5.0/sardine-5.0.jar for the binary with maven and here: https://github.com/lookfirst/sardine for the sources with github. The binary file is compiled with java 7, so care attention if you use java 6. For it you need to compile it from the sources. Here the runtime dependencies mandatory to execute the code: 


httpclient 4.2.3

httpcore 4.2.3

commons-codec 1.7

 

The client access to the workspace 'portal-work' of Gatein through the user 'utente'.  The user must be inserted in the User administration of Gatein as administrator. The permission settings of the 'portal-work' workspace are configured in the deploy/gatein.ear/02portal.war/WEB-INF/conf/jcr/repository-configuration.xml' file. Here the snippet code:


          <initializer class="org.exoplatform.services.jcr.impl.core.ScratchWorkspaceInitializer">

            <properties>

              <property name="root-nodetype" value="nt:unstructured"/>

              <property name="root-permissions" value="*:/platform/administrators read;*:/platform/administrators add_node;*:/platform/administrators set_property;*:/platform/administrators remove"/>

            </properties>


So, to access with that user, remember to add almost one of the configured roles or to add the desired role in this file.


Then the code delete, if it is, a directory 'prova' directly under the portal-work workspace, and then it recreate it.

After,  the  method sardine.put create the file from a n existent FILE_PATH directory.


In this case the uploaded file can be downloaded connecting with the browser, rest or webdav to this url: http://localhost:8080/rest/private/jcr/repository/portal-work/prova/nuovo


If you are inside a container (EJB, servlet, ecc) you don't need to use a remote protocol. An example can be found in the chapter 8 of Gatein Cookbook written by me. Here the url: http://www.packtpub.com/building-portals-with-gatein-cookbook/book

Comments