1

我正在寻找使用 DCTM REST API 创建子类型 dm_document 的物理副本。我对 DCTM 世界很陌生,尤其是在 REST API 方面。

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

我已经查看了此处的所有示例:https ://community.emc.com/docs/DOC-34346但是,我无法弄清楚如何使用只有 r_object_id 的示例(Spring Rest)来检索文档和内容. 我已经使用这个示例来创建内容,因此如果我可以将内容和元数据下载到我的应用程序中,剩下的就可以完成了。但是,无论我尝试什么 - 我似乎都无法使用 r_object_id 获取数据。

如果有人可以提供指针,那将对我有很大帮助。

谢谢。

4

1 回答 1

2

这是获取对象信息及其内容的方式。我真的很想知道如何做这些事情,所以我很努力,终于让它完全正常工作。

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

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

我使用了来自 Java 的@Eddie 的答案:Reading a pdf file from URL into Byte array/ByteBuffer in an applet以从 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);
}

}

于 2015-12-17T23:38:21.253 回答