如何使用REST在Documentum 7.x中复制对象

时间:2015-12-08 14:19:02

标签: java documentum spring-rest

我希望使用DCTM REST API创建子类型的dm_document的物理副本。我对DCTM世界很陌生,特别是在涉及REST API时。

我的用例是当某个动作发生时,我需要克隆文档(元和内容)并更改1或2个元并将该新文档保存回存储库。

我已经查看了位于此处的所有示例:https://community.emc.com/docs/DOC-34346但是,我无法弄清楚如何检索文档&使用这个例子(Spring Rest)只有r_object_id的内容。我已经使用这个例子来创建内容,因此,如果我可以下载内容&在我的申请中,我可以做其余的事情。但无论我尝试什么 - 我似乎无法使用r_object_id获取数据。

如果有人能提供指针,它会帮助我很多。

感谢。

1 个答案:

答案 0 :(得分:1)

以下是获取对象信息的方式,以及它的内容。我真的很想知道如何做这些事情,所以我努力工作,最后让它充分发挥作用。

要发出请求,您需要使用documentum中的xmlconfigeditor.jar。其他base64编码无效...

我使用java-json.jar来轻松使用jsons。它不是“必须”的。但它会帮助你很多。否则你需要自己读取json字符串。

我使用来自Java: Reading a pdf file from URL into Byte array/ByteBuffer in an applet的@Eddie的答案来将url中的文件作为字节数组。

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONArray;
import org.json.JSONObject;
public class test {

public static void main(String[] args) throws Exception {

    String repository_name = "";
    String r_object_id = "";

    JSONObject theObject = makeRequest("http://localhost:8000/dctm-rest/repositories/"
            + repository_name + "/objects/" + r_object_id);

    if (theObject != null) {

        // you have all information about the object now
        // you can access the attributes of the object by
        // theObject.getJSONObject("properties") then like
        // .getString("object_name")

        JSONObject contentJSON = makeRequest("http://localhost:8000/dctm-rest/repositories/"
                + repository_name
                + "/objects/"
                + r_object_id
                + "/contents/content");

        JSONArray js = contentJSON.getJSONArray("links");
        for (int i = 0; i < js.length(); i++) {
            JSONObject jo = js.getJSONObject(i);
            if (jo.has("title") && "ACS".equals(jo.getString("title"))) {

                // here is your byte array
                byte[] bytes = getAsByteArray(new URL(jo.getString("href")))
                        .array();

            }
        }
    }

}

public static JSONObject makeRequest(String url) {

    JSONObject json = null;

    String username = "";
    String password = "";

    String strResponse = "";

    CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    BufferedReader rd = null;
    CloseableHttpResponse cls = null;

    HttpGet request = new HttpGet(url);

    RequestConfig config = RequestConfig.custom()
            .setSocketTimeout(120 * 1000).setConnectTimeout(30 * 1000)
            .setConnectionRequestTimeout(30 * 1000).build();

    request.setConfig(config);

    request.addHeader("Accept", "application/vnd.emc.documentum+json");

    request.addHeader(
            "Authorization",
            "Basic "
                    + com.documentum.xmlconfig.util.Base64.encode(username
                            + ":" + password));

    try {

        cls = httpClient.execute(request);

        HttpEntity entity = cls.getEntity();

        rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String line = "";
        while (line != null) {
            line = rd.readLine();
            strResponse += line;
        }

        strResponse = strResponse.trim().replace("\n", "");

        String statusline = cls.getStatusLine().toString();
        if (!statusline.contains("200") && !statusline.contains("201")) {

            System.out.println(strResponse);

        } else {

            json = new JSONObject(strResponse);
        }

    } catch (Exception e) {

        e.printStackTrace();
    }

    return json;

}

public static ByteBuffer getAsByteArray(URL url) throws IOException {
    URLConnection connection = url.openConnection();
    // Since you get a URLConnection, use it to get the InputStream
    InputStream in = connection.getInputStream();
    // Now that the InputStream is open, get the content length
    int contentLength = connection.getContentLength();

    // To avoid having to resize the array over and over and over as
    // bytes are written to the array, provide an accurate estimate of
    // the ultimate size of the byte array
    ByteArrayOutputStream tmpOut;
    if (contentLength != -1) {
        tmpOut = new ByteArrayOutputStream(contentLength);
    } else {
        tmpOut = new ByteArrayOutputStream(16384); // Pick some appropriate
                                                    // size
    }

    byte[] buf = new byte[512];
    while (true) {
        int len = in.read(buf);
        if (len == -1) {
            break;
        }
        tmpOut.write(buf, 0, len);
    }
    in.close();
    tmpOut.close(); // No effect, but good to do anyway to keep the metaphor
                    // alive

    byte[] array = tmpOut.toByteArray();

    // Lines below used to test if file is corrupt
    // FileOutputStream fos = new FileOutputStream("C:\\abc.pdf");
    // fos.write(array);
    // fos.close();

    return ByteBuffer.wrap(array);
}

}

相关问题