1 2 Previous Next 24 Replies Latest reply: Aug 6, 2017 10:36 PM by William Zhou RSS

EMC RestFul Services -- Clients JAVA

astone

Hi,

I´ve been testing the new EMC RestFul Services and I haven´t found any java client in the web. The examples are written in python!!!!

I´ve spent a few hours to create the java client.

if anyone needs any specific client, please contact me through this thread.

 

I leave you the example of how to upload documents through rest, under folder:

 

Java Build Path: commons-codec-1.6.jar, commons-logging 1.1.1.jar, fluent-hc-4.2.5.jar, httpclient-4.2.5.jar, httpclient-cache-4.2.5, httpcore4.2.4.jar, httpmime-4.2.5.jar

 

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

 

public class CreateObjectsRest {

public static void main(String[] args) throws ClientProtocolException, IOException { 
  String path = "http://localhost:9080/dctm-rest/repositories/repository/folders/0b000406801e9ca8/objects";
  createObjectUnderFolder (path);
}

private static void createObjectUnderFolder(String path) throws ClientProtocolException, IOException {
  // TODO Auto-generated method stub
  System.out.println ("*****createObjectUnderFolder REST*****");
  HttpClient client = new DefaultHttpClient();
  HttpPost post = new HttpPost(path);
  //format:Basic user:password (encode Base 64)
  post.addHeader("Authorization", "Basic cGVwZTpwZXBl");
  //format
  List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
  nameValuePairs.add(new BasicNameValuePair("format", "pdf"));
  post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  //json y documento a importar
  String json = "{\"properties\" : {\"object_name\" : \"REST DOCUMENT- PDF\", \"r_object_type\" :\"dm_document\"}}"; 
  MultipartEntity reqEntity = new MultipartEntity();
  reqEntity.addPart("data", new StringBody (json,"application/vnd.emc.documentum",Charset.forName("UTF-8")));
  reqEntity.addPart("file", new FileBody( new File( "d:/doc.pdf" ) , "", "application/pdf", null));
  post.setEntity(reqEntity);
  HttpResponse response = client.execute(post);
  System.out.println ("response: " +  response.getStatusLine());
  BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    String line = "";
    while ((line = rd.readLine()) != null) {
     System.out.println(line);
    }
}

}

 

I hope they serve!

 

Regards.



  • 1. Re: EMC RestFul Services -- Clients JAVA
    jeremyprioux

    Nice, thank you. Do you have any plans considering REST services usage in Java ?

  • 2. Re: EMC RestFul Services -- Clients JAVA
    raghunaths

    Hi,

     

    I need to develop a Java dctm RESTful web services code to import multiple files of multiple formats (.jpg, .doc, .pdf etc.,) into a folder in a Documentum repository and also files should get imported in proper folder structure. Can you please share the code if you have.

  • 3. Re: EMC RestFul Services -- Clients JAVA
    nakulporwal

    Hi Aston,

    It is very useful, can u please help with file download through rest client.

     

    Thanks in advance.

  • 4. Re: EMC RestFul Services -- Clients JAVA
    William Zhou

    Are you going to write the client code or server code?

  • 6. Re: EMC RestFul Services -- Clients JAVA
    nakulporwal

    hi william,

    I went through it and upload rest client implemented also but I am not be able to implement download rest client, by which i want to download content file from repository .Please help if any code available to consume get method to retrieve content from repository.

  • 7. Re: Re: EMC RestFul Services -- Clients JAVA
    astone

    I use a java servlet in a web page to download content by REST:

     

     

     

    import java.io.IOException;

    import java.io.InputStreamReader;
    import java.util.Properties;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.log4j.Logger;
    import org.json.JSONArray;
    import org.json.JSONObject;

    import com.openemc.utils.Utils;


    @WebServlet (name = "GetContentServletOpenWebTop", urlPatterns = {"/GetContentServletOpenWebTop"})
    public class GetContentServletOpenWebTop extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private Logger logger = Logger.getLogger(GetContentServletOpenWebTop.class);

    public GetContentServletOpenWebTop() {
             // TODO Auto-generated constructor stub  
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      logger.debug("GetContentServletOpenWebTop. doPost()");
      String path = "";
      String user = request.getParameter("user");
      String token = request.getParameter("token");
      String identificador = request.getParameter("identificador");
      if (user!=null && !user.equals("") && token!=null && !token.equals("") && identificador!=null && !identificador.equals("")) {
       if (identificador.substring(0, 2).equals("09")) {
        logger.debug("------------------url de contenido------------------------");
        Utils utils = new Utils ();
        Properties pro = utils.readOpenWebTopProperties ();
        path = pro.getProperty("PARTIAL_URL_REST_SERVICES") + pro.getProperty("DOCBASE") + "/objects/"+ identificador + "/contents/content?page&format&modifier";
        logger.debug("path: " + path);
        HttpClient client = new DefaultHttpClient();
        HttpGet request_http = new HttpGet(path);
        request_http.addHeader("Authorization", "Basic " + token);
        HttpResponse response_http = client.execute(request_http);
        int status_code = response_http.getStatusLine().getStatusCode();
        logger.debug("status_line: " + response_http.getStatusLine());
        logger.debug("status_code: " + status_code);
        if (status_code==200) {
         InputStreamReader is = new InputStreamReader(response_http.getEntity().getContent(), "UTF-8");
         BufferedReader rd = new BufferedReader(is);
         String line = "";
         String resultado = "";
         while ((line = rd.readLine()) != null) {
          resultado = line;
         }
         try {
          logger.debug("resultado_ " + resultado);
          JSONObject obj = new JSONObject(resultado);
          JSONObject properties = obj.getJSONObject("properties");
          String mime_type = properties.getString("mime_type");
          String object_name = properties.getString("object_name");
          int size =  properties.getInt("full_content_size");
          logger.debug("mime_type: " + mime_type);
          logger.debug("object_name: " + object_name);
          logger.debug("size: " + size);
         
          logger.debug("gestionamos el contenido del json");
          String acs_url_object = "";
          JSONArray links_array = obj.getJSONArray("links");
          if (links_array.length()!=0) {
           for (int x = 0; x < links_array.length(); x++){
            JSONObject res = links_array.getJSONObject(x);
            if (res.get("rel").equals("enclosure")){
             acs_url_object = res.get("href").toString();
            }
           }
          }
         
          //guardamos el documento como attachment
          acs_url_object = acs_url_object + "&media-url-policy=all" +  "&transient=Content-Disposition::attachment;%20filename=" + object_name;
          logger.debug("acs_url_object:_ " + acs_url_object);
          response.getWriter().print(acs_url_object);
         
         }catch (Exception e) {
          response.getWriter().write("No se ha podido conseguir el contenido del objeto, consulte con el administrador");
          throw new ServletException ("No se ha podido conseguir el contenido del objeto, consulte con el administrador"); 
        
         }
       
       }
       else {
        response.getWriter().write("No se ha podido conseguir el contenido del objeto, consulte con el administrador");
        throw new ServletException ("No se ha podido conseguir el contenido del objeto, consulte con el administrador"); 
       }
       }else {
        response.getWriter().write("El elemento seleccionado no es un documento");
        throw new ServletException ("El elemento seleccionado no es un documento"); 
       
       }
         
      }else {
       response.getWriter().write("Los parametros de entrada no son validos, consulte con el administrador");
       throw new ServletException ("Los parametros de entrada no son validos, consulte con el administrador");

      }
     
     
    }

    }

  • 8. Re: EMC RestFul Services -- Clients JAVA
    William Zhou

    Please see the comment of Documentum REST Java Client Code Sample about how to download the content using Spring RestTemplate.

  • 9. Re: EMC RestFul Services -- Clients JAVA
    nakulporwal

    Hi Aston,

     

    I have implemented this code but it give me metadata of that requested

    document only in response, I want to retrieve content (image file byte

    array) along with the metadata.

     

    I want to view the file content not the metadata.

     

     

    Thanks & Regards

    Nakul Porwal

    Tata Consultancy Services

    Ph:- 91 22 67784687

    Buzz:- 4284687

    Mailto: nakul.porwal@tcs.com

    Website: http://www.tcs.com

  • 10. Re: EMC RestFul Services -- Clients JAVA
    nakulporwal

    Hi Aston,

     

    I am getting below attached output when I hit this  url

    http://172.17.178.175:7001/dctm-rest/repositories/UATPFILE/objects/0900301880e35c09/contents/content?format=pdf&modifier=&page=0&media-url-policy=local

     

     

     

    How can I retrieve content(pdf file nt metadata) from the local href="

    http://172.17.178.175:7001/dctm-rest/repositories/UATPFILE/objects/0900301880e35c09/content-media?format=pdf&modifier=&page=0

    "

    Please assis on the same.

     

    Thanks & Regards

    Nakul Porwal

    Tata Consultancy Services

    Ph:- 91 22 67784687

    Buzz:- 4284687

    Mailto: nakul.porwal@tcs.com

    Website: http://www.tcs.com

  • 11. Re: EMC RestFul Services -- Clients JAVA
    sreedhar_varanasi@satyam.com

    Hi Astone,

     

    Here,

     

    String json = "{\"properties\" : {\"object_name\" : \"REST DOCUMENT- PDF\", \"r_object_type\" :\"dm_document\"}}"; 

      MultipartEntity reqEntity = new MultipartEntity();

      reqEntity.addPart("data", new StringBody (json,"application/vnd.emc.documentum",Charset.forName("UTF-8")));

      reqEntity.addPart("file", new FileBody( new File( "d:/doc.pdf" ) , "", "application/pdf", null));


    How can we dynamically set the Filename and File format ("application/pdf") values ?


    Thanks,

    Sreedhar.

  • 12. Re: EMC RestFul Services -- Clients JAVA
    William Zhou

    Java 7 has an API to probe the MIME from file path, Files (Java Platform SE 7 ), but it may not always produce the acurate mime type for the local file. The mime to dos_extension map is actually saved in dm_format table of Content Server repository (you can get it via formats resource of Documentum REST Services). So the promising approach is to query the format registry in the repository and use the supported MIME type. The perfomrance may be a concern then if you query the format registry for each document import. Then the cache mechanism deserves consideration in your client app.

     

    Regards,

    William

  • 13. Re: EMC RestFul Services -- Clients JAVA
    SantoshJha

    Use ACS url to get the content instead of local url.

  • 14. Re: EMC RestFul Services -- Clients JAVA
    nakulporwal

    Hi

     

    I have written client to upload file through rest call . At the same time i want progress bar in sync with this upload call to show the progress if file size is more than 100 MB and my HTTP call takes more than 1 min to complete.

     

    Any idea to show sync progress bar with rest call in java.

     

    Thanks in advance

1 2 Previous Next