cancel
Showing results for 
Search instead for 
Did you mean: 

calling a java backed webscript

ucf
Champ on-the-rise
Champ on-the-rise
hi everybody,
I made a java backed web script that extends AbstractWebScript(with a java class and a description file, no .ftl) and I want to know how to call it from another java project, one more qquestion: how to return a JSONArray from the overrided method execute in that kind of webscript
thank you
1 REPLY 1

kaynezhang
World-Class Innovator
World-Class Innovator
You can call your webscript using apache commons httpclient,like below

      HttpClient client = new HttpClient();
      client.getState().setCredentials(
            new AuthScope("localhost", 8080, "Alfresco"),
            new UsernamePasswordCredentials("admin", "admin"));
      String apiurl = "your webscript url";
      PostMethod post = new PostMethod(apiurl);
   JSONObject data = new JSONObject();
         data.put("title", "this is a test title");
         data.put("content", "this is a test content");

         post.setDoAuthentication(true);
         post.setRequestHeader("Content-Type", "application/json");
         post.setRequestEntity(new StringRequestEntity(data.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);