cancel
Showing results for 
Search instead for 
Did you mean: 

How to use webscript to get SiteService object for Site related operations

smita
Champ in-the-making
Champ in-the-making
    I want to get SiteService object for some site related functions like creation, deletion etc through java code. eg. I want following should be working

siteService.deleteSite(shortName);

As I researched on net, I have to use webscript to get SiteService objcet. I have seen on alfresco wiki pages, for running webscripts we need to hit webscript url given in webscript discritor.
1. My first question is, what are the minimum and proper steps to get this SiteService object? Also, please refer the location where I can put my webscript discriptor file in src which could be read as webscript.
2. To fulfill my requirement, do  need to call this webscript url through apache's http api in some java code? If not, how could I can use above siteService which could perform oprations on alfresco server.
Please reply, its urgent.

Thanks,
Smita
5 REPLIES 5

kaynezhang
World-Class Innovator
World-Class Innovator
siteService an object in javascript api.
If you just want to operate site using existing webscript api you don't need to get siteService object

alfresco webscript api is a REST API.so to  make a call, You can use apache commons httpclient to make an HTTP  request to webscript's URI:

Take site creation  as an example

package com.kayne.cmis.webscript;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.json.JSONObject;

public class CreateSiteTest {

   public static void main(String[] args) {
      HttpClient client = new HttpClient();
      client.getState().setCredentials(
            new AuthScope("localhost", 8080, "Alfresco"),
            new UsernamePasswordCredentials("admin", "admin"));

      String apiurl = "http://localhost:8080/alfresco/service/api/sites";
      PostMethod post = new PostMethod(apiurl);
      try {
         JSONObject site = new JSONObject();
         site.put("shortName", "newsite");
         site.put("sitePreset", "site-dashboard");
         site.put("title", "newsite");
         site.put("description", "newsite");
         site.put("visibility", "PUBLIC");

         System.out.println(site.toString());
         post.setDoAuthentication(true);
         post.setRequestHeader("Content-Type", "application/json");
         post.setRequestEntity(new StringRequestEntity(site.toString(),
               "application/json", "UTF-8"));

         int status = client.executeMethod(post);
         if (status != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + post.getStatusLine());
         }
         String resultString = post.getResponseBodyAsString();
         System.out.println(resultString);
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         post.releaseConnection();
      }
   }
}


smita
Champ in-the-making
Champ in-the-making
Thank you for your help. But the code you have provided here is not creating site properly. When I click on the site created with this code, it returns 'A server error has occurred'. Somehow I have managed the create site with following url
'http://alfresco-server:8080/share/service/modules/create-site'

I have tried the same for deleting the site with url 'http://alfresco-server:8080/share/service/modules/delete-site' but that doesn't work at my end and I am not able to find the delete site url and their parameters. Could please provide me the deleteSite webscipt url and a code snippet for deleting site, if there is any.

Also, if I have to perform more operations for site, like cloning etc. from where I can find the existing webscript urls? This was the reason, I have chosen the SiteService object for performing site related tasks.

Thanks in advance.

Smita

smita
Champ in-the-making
Champ in-the-making
I have got the webscript url for delete site, it is '/alfresco/service/api/sites/{shortname}' and this is working fine with java code now. But, as I have noticed earlier also, if I create a site using create-site webscript url '/alfresco/service/api/sites' in java, the site click is throwing server error. This might due to the AVM store issue as I have seen in many blogs. Is there any work around for this in java?

Site node is created but when redirecting to site dashboard its giving the following error message

ERROR [alfresco.web.site] [http-bio-8080-exec-12] javax.servlet.ServletException: Could not resolve view with name 'site/sample123/dashboard' in servlet with name 'Spring Surf Dispatcher Servlet'

kaynezhang
World-Class Innovator
World-Class Innovator
I have already tested the site creation code ,It works fine in my envirement.
What error did you get ? Could you paste your error here?