user739115
user739115

Reputation: 1217

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

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
}

Upvotes: 1

Views: 1390

Answers (2)

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!

Upvotes: 0

Jeff Potts
Jeff Potts

Reputation: 10538

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.

Upvotes: 2

Related Questions