在Alfresco中创建的文件而不是文件夹

时间:2013-05-20 11:05:29

标签: alfresco cmis

我正在尝试使用XMSI在Alfresco中创建一个文件夹,这是我正在使用的代码。

public static void main(String [] args)throws Exception {         long start = System.currentTimeMillis();

    String host = "127.0.0.1";
    int port = 9080;
    String username = "admin";
    String password = "admin";
    String parentFolder = "Company%20Home";
    String folderName = "sales3";
    String description = "sales space3";

    RulesRequest request  = new RulesRequest();
    //request.setRemediationProfileObj(remediationProfile);
    Gson gs = new Gson();
    String json = gs.toJson(request);
    DefaultHttpClient client = new DefaultHttpClient();
    RestClient rstClnt = new RestClient();
    String  ticket = rstClnt.restClientPost(json, "http://127.0.0.10:9080/alfresco/service/api/login?u=admin&pw=admin", client);
    //String url = "http://" + host + ":" + port + "/alfresco/service/api/path/workspace/SpacesStore/" + parentFolder + "/children";
    String url = "http://localhost:9080/alfresco/service/cmis/s/workspace:SpacesStore/i/078b05c6-14bd-439c-a1ae-db032c5d98fc/children?alf_ticket="+ticket;

    String contentType = "application/atom+xml;type=entry";

    String xml = 
        "<?xml version='1.0' encoding='utf-8'?>\n" +
        "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:cmis='http://www.cmis.org/2008/05'>\n" +
        "<title>" + folderName + "</title>\n" +
        "<summary>" + description + "</summary>\n" +
        "<cmis:object>\n"+
        "<cmis:properties>\n" +
        "<cmis:propertyString cmis:name='cmis:objectTypeId'>\n" +
        "<cmis:value>cmis:folder</cmis:value>\n" +
        "</cmis:propertyString>\n" +
        "</cmis:properties>\n" +
        "</cmis:object>\n" +
        "</entry>\n"; 




    DefaultHttpClient httpclient = new DefaultHttpClient();

    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(host, port),
            new UsernamePasswordCredentials(username, password));

    HttpPost httppost = new HttpPost(url);
    httppost.setHeader("Content-type", contentType);

    StringEntity requestEntity = new StringEntity(xml, "UTF-8");
    httppost.setEntity(requestEntity);


    System.out.println("executing request" + httppost.getRequestLine());
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();

    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    if (entity != null) {
        System.out.println("Response content type: " + entity.getContentType());
        long contentLength = entity.getContentLength();
        System.out.println("Response content length: "
                + entity.getContentLength());

        if (contentLength > 0) {
            byte [] b = new byte[(int) contentLength];
            entity.getContent().read(b);
            System.out.println("Response content: " + new String(b));
        }

        entity.writeTo(System.out);
    }

    // When HttpClient instance is no longer needed,
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();
    long end = System.currentTimeMillis();
    System.out.println("Time spend: " + (end-start) + "ms");
}

创建一个大小为0的文件,而不是创建文件夹。

由于 Garvit

1 个答案:

答案 0 :(得分:2)

您的财产类型错误。您错误地使用cmis:propertyString而不是cmis:propertyId请尝试以下

<cmis:propertyId propertyDefinitionId="cmis:objectTypeId">
    <cmis:value>cmis:folder</cmis:value>
</cmis:propertyId>

正如@Gagravarr所说,如果你可以使用众所周知的客户端库,很容易避免这样的问题。如果你自己构建HTTP请求,那么你最好有充分的理由。