cancel
Showing results for 
Search instead for 
Did you mean: 

executing a java script through web script ..

linton
Champ in-the-making
Champ in-the-making
Hi,
         I have a javascript which assigns a work-flow to a user. I placed this script in side the scripts folder.
I use the webscript command/script/execute interface to execute this.
for example ..
if i type the following url in the browser the asignworkflow.js script is executed and a review workflow is assigned to admin_signup.txt doc..

http://localhost:8080/alfresco/command/script/execute?scriptPath=/Company%20Home/Data%20Dictionary/S...

This works fine when i type the url in the browser and presses enter key..

But i want to make in automatic that is when ever a new document is added , i want this script to be called with the document name so the workflow is attached to that file.

SO i wrote the following java code…

HttpClient client = new HttpClient();
       client.getHostConfiguration().setHost("localhost", 8080);
       //Servlet.getContext("URL");
      
       String strURL="http://localhost:8080/alfresco/command/script/execute?scriptPath=/Company%20Home/Data%20Dictionary/S...";
       System.out.println("strURL"+strURL);
      
      HostConfiguration h = new HostConfiguration();
      h.setHost("localhost", 8080);
      GetMethod get = new GetMethod(strURL);
      try {
         int result = client.executeMethod(h,get);
         System.out.println("Servelt Call for Excute java script Result == " + result);
      } catch (HttpException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }


but it always throws 500 status code. which means bad request…

with the same java code if i call
String strURL = "http://localhost:8080/alfresco/wcservice/ui/myspaces?f=0&p=/Company%20Home";
i get 200 as status which means success..

I am not able to fix this.
can anyone help me…

waiting for your replies..
Thank you
3 REPLIES 3

zaizi
Champ in-the-making
Champ in-the-making
The error is most likely to do with authentication. When calling the URL from your java code does login to Alfresco and thus provides no user context.

The Alfresco way to implement this functionality is to create a simple javascript file and load it into Data Dictionary -> Scripts folder. Create a content rule to execute this script when new document is added to your folder. The new document is available "document" root object within your script.

linton
Champ in-the-making
Champ in-the-making
hi Zaizi,
          Thanks for the reply, But i do run the above code in the current session.

linton
Champ in-the-making
Champ in-the-making
Getting the ticket and attaching it with the url done the trick.

 NameValuePair[] kvps = 
               new NameValuePair[]{new NameValuePair("ticket", ticket),
              new NameValuePair("scriptPath", "/Company Home/Data Dictionary/Scripts/assignworkflow.js"),
               new NameValuePair("contextPath", "/Company Home/admin_signup.txt")};
         get.setQueryString(kvps);