cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to create site programmatically

nancyaggarwal
Champ in-the-making
Champ in-the-making

Hi ,

I trying to create a site in alfresco share using a webscript in java and this i am doing with the help of below code:

HttpClient client = new HttpClient();


          PostMethod  httpPost = new PostMethod ("http://localhost:8080/share/service/modules/create-site?authority="+username+"&alf_ticket="+alf_tkt);


           JSONObject site = new JSONObject();
           try {
               site.put("shortName", "java_test");
               site.put("Visiblity", "Public");
               site.put("sitePreset", "site-dashboard");
               site.put("title", "java_test");
               site.put("description", "test");
               httpPost.setDoAuthentication(true);
               httpPost.setRequestHeader("Content-Type", "application/json");
               httpPost.setRequestEntity( new StringRequestEntity(site.toString(), "application/json", "UTF-8"));


               int status1 = client.executeMethod(httpPost);

               if (status1 != HttpStatus.SC_OK) {
               System.err.println("Method failed: " + httpPost.getStatusLine());
               }



          } catch (JSONException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
          }

It is giving me the error as HTTP/1.1 401 Unauthorized.

Please help me in this.

Regards,
Nancy

11 REPLIES 11

niketapatel
Star Contributor
Star Contributor

Hi Nancy, Please make sure your ticket is not expired and you are calling Share script which again internally calls Alfresco data script to create a site and that script also required authentication. So I believe here authentication chain is breaking.

Why dont you directly call Alfresco data script. Just change your script URL to data script as below and it should work.

PostMethod  httpPost = new PostMethod ("http://localhost:8080/alfresco/service/api/sites?&alf_ticket="+alf_tkt);

Niketa Patel
Senior Alfresco Consultant
CIGNEX Datamatics

Blog:http://niketa-alfresco3.blogspot.in/

Hi Nikita,

Thanks for your reply. But the above url only creates the site at repository level, do not create a fully functional site in share interface.


Regards,
Nancy Aggarwal

niketapatel
Star Contributor
Star Contributor
Hi Yes true. It will be only repository level folder structure. Need to create surf preset object for fully functional site.
Either you can create new share side script to create site-dashboard preset or if you need to use share create site module script then as per below wiki you need to pass JSESSIONID for repository authentication.

" if you call a Surf Web Script URL directly in a browser address bar there is no authentication (i.e. no context). You would need to have a JSESSIONID that is already authenticated (i.e. by the page). Surf ties the given JSESSIONID to the Alfresco TICKET that is stored in the session for that user for that connector (i.e. alfresco, alfresco-api). When a client-side library on an authenticated page makes an XHR call to a /service URL it will be passing the JSESSIONID automatically." Reference - http://docs.alfresco.com/5.1/concepts/dev-extensions-share-surf-web-scripts.html

hi Nikita,
It helped me. I got the jsession id and its working, giving me no exception but in alfresco share site is not created.
can you tell where it is breaking?





Regards
Nancy Aggarwal

Hi Nancy ,

I have posted solution here. Please check it  - http://niketa-alfresco3.blogspot.in/2016/04/create-alfresco-share-sites-through.html

Hi Niketa,

Thanks for the reply. Actually i am authenticating user by using alf_ticket and creating a session using alf_ticket and username using CMIS api and the part i was missing is making a touch call for validating session. I make the touch call for initiating session and stills the site is not created. I also run your code as a single entity with username and password and it gives me 400 error status for creating site.

What is that i am missing?



Thanks & Regards,
Nancy

Hi Nancy, not sure for 400 . Can you please post your error.
Also can you post your entire code using CMIS API.

To call/authenticate share script , need to pass JSESSIONID and to get it need to use dologin script. Not sure if there is any other way.
And when you execute dologin script it starts authentication handshale and internally it gets Alfresco ticket. and for next all consecutive Repository level webscript call it internally does append alf_ticket.

Hi Niketa,

I tried your code as a single code also and it is giving me bad request(the request sent was syntactically incorrect) and when i am trying it in my code it gives me 401 error for touch call share webscript.


HttpClient http = new HttpClient();
      
       PostMethod loginPost = new PostMethod("http://localhost:8080/share/page?pt=login");
       String logindata = "username="+user+"&password="+alf_tkt;
         loginPost.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         
         
         loginPost.setRequestEntity(new StringRequestEntity(logindata, "text/plain", "UTF-8"));
         System.out.println("dologin script status: " + http.executeMethod(loginPost));
         org.apache.commons.httpclient.Header[] headers = loginPost.getResponseHeaders();
         Header cookies1 =loginPost.getResponseHeader("Set-Cookie");
         
            
   // getting JSESSIONID from the header      
   
            
            System.out.println("session id ============" +sessionid);
            GetMethod authenticationGet = new GetMethod("http://localhost:8080/share/service/modules/authenticated?a=user");
            authenticationGet.setRequestHeader("JSESSIONID", sessionid);
            
            
            System.out.println("User is authenticated script status : " + http.executeMethod(authenticationGet));
                  
            PostMethod  httpPost = new PostMethod ("http://localhost:8080/share/service/modules/create-site");
            
             JSONObject site = new JSONObject();
            try{
               site.put("shortName", "java");
               site.put("Visiblity", "PUBLIC");
               site.put("sitePreset", "site-dashboard");
               site.put("title", "java");
               site.put("description", "Google-cURL_next");
               
         
               httpPost.setRequestHeader("Content-Type", "application/json");
               httpPost.setRequestHeader("Accept", "application/json");
               httpPost.setRequestEntity(new StringRequestEntity(site.toString(), "application/json", "UTF-8"));
               
               int status = http.executeMethod(httpPost);
               
               
               if (status != HttpStatus.SC_OK) {
                  System.err.println("Method failed: " + httpPost.getStatusLine());
                  }
                  else {
                  System.out.println("site created successfully");
}



Regards,
Nancy

Nancy, Please use share/page/dologin for authentication and to get jsessionId NOT share/page?pt=login in your code


    //PostMethod loginPost = new PostMethod("http://localhost:8080/share/page?pt=login");
           PostMethod loginPost = new PostMethod("http://localhost:8080/share/page/dologin"); //USE THIS ONE