Garvit Jain
Garvit Jain

Reputation: 21

File created insted of Folder in Alfresco

I am trying to create a folder in Alfresco using XMSI below is the code i am using .

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");
}

Instead of creating a folder it is creating a file of size 0.

Thanks Garvit

Upvotes: 0

Views: 484

Answers (1)

Will Abson
Will Abson

Reputation: 1572

Your property is of the wrong type. You are incorrectly using cmis:propertyString instead of cmis:propertyId Try the following

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

As @Gagravarr says, problems like this are easily avoided if you can use well-known client libraries. If you are constructing the HTTP requests yourself you'd better have a good reason for that.

Upvotes: 2

Related Questions