1

how to call another webscript inside one webscript in java controller if both are in same repository.

//hellowebscript
 public void execute(WebScriptRequest request, WebScriptResponse response)
{

 //need to call another webscript
}
user739115
  • 1,117
  • 5
  • 20
  • 41
  • 1
    Is it in the same tier? Or are you trying to call a repo webscript from the share tier? – Younes Regaieg Jun 10 '16 at 06:24
  • 3
    What does the second webscript do? You should refactor the logic into a service class that can then be used by both webscripts. There is no easy way to call a repository webscript from another repository webscript. –  Jun 10 '16 at 07:04
  • 1
    If they're in the same tier, you could always grab the Java bean for the other webscript and call it, but refactoring is much more the recommended way! – Gagravarr Jun 10 '16 at 08:31
  • 2nd webscript is having only get.desc.xml file & ftl files .There is no Java controller and JS controller for 2nd webscript. In that case how to call that 2nd webscript from 1st webscript.both are in the same tier. – user739115 Jun 10 '16 at 09:49
  • if my 2nd script is somewhat looks like defined in this url where there is no java/js controller;http://babyalfresco.blogspot.in/2014/05/creating-sample-webscript-without-using.html;in this how to call in 1st webscript java controller; – user739115 Jun 10 '16 at 10:15

2 Answers2

2

It sounds like you are trying to invoke a web script on the same tier and that web script does not have a Java controller. If it did have a Java controller you'd want to just invoke that logic from your Java class.

I agree with the commenters that the best thing to do is port that logic to a Java class and call it.

But if you can't or don't want to do that, grab an HTTP client (here's one) and invoke the URL like you would any other URL from a Java class. Depending on the web script you are calling you may have to grab the user's current ticket (see AuthenticationUtils.getTicket()) and pass that to the web script using the alf_ticket argument.

Jeff Potts
  • 10,468
  • 17
  • 40
0

My solution:

Two WebScripts, one call as redirect to second.

File: RedirectHelloWorldWebScript.java:

public class RedirectHelloWorldWebScript extends AbstractWebScript {
@Override
public void execute(WebScriptRequest wsreq, WebScriptResponse wsres)
        throws IOException {
    HttpServletResponse httpResponse = WebScriptServletRuntime
            .getHttpServletResponse(wsres);

    httpResponse.sendRedirect("/alfresco/service/helloworld");
}
}

File: HelloWorldWebScript.java:

public class HelloWorldWebScript extends AbstractWebScript {
@Override
public void execute(WebScriptRequest req, WebScriptResponse res)
        throws IOException {
    try {
        JSONObject obj = new JSONObject();
        obj.put("message", "Hello Word!");
        String jsonString = obj.toString();
        res.getWriter().write(jsonString);
    } 

    catch (JSONException e) {
        throw new WebScriptException("Unable to serialize JSON");
    }

    catch (org.json.JSONException e) {
        e.printStackTrace();
    }
}
}

Descriptors:

File: redirecthelloworld.get.desc.xml:

<webscript>
   <shortname>RedirectHelloWorld</shortname>
   <description>Redirect to Hello World</description>
   <url>/redirecthelloworld</url>
   <authentication>none</authentication>
   <family>Java-Backed WebScripts</family>
</webscript>

File: helloworld.get.desc.xml:

<webscript>
   <shortname>helloworld</shortname>
   <description>Hello World</description>
   <url>/helloworld</url>
   <url>/helloworld.json</url>
   <authentication>none</authentication>
   <family>Java-Backed WebScripts</family>
</webscript>

And, context to Spring:

File: webscript-context.xml:

<bean id="webscript.helloworld.get"
  class="com.fegor.HelloWorldWebScript"
  parent="webscript">       
</bean>

<bean id="webscript.redirecthelloworld.get"
  class="com.fegor.RedirectHelloWorldWebScript"
  parent="webscript">       
</bean>

God luck!