How can the client code calling Java-backed webscript's execute method get a return value, because the execute method returns void? The method is GET.
I have Java-backed webscript code similar to:
public void execute(WebScriptRequest request, WebScriptResponse response) throws IOException{ // some more code to get argument from the URL int numOfFiles = myClass.getCountOfFiles(..some args..) } The above webscript is called by a client code similar to: try { URL url = new URL("https://myhost:myport/alfresco/service/fileNum"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String output; while ((output = br.readLine()) != null) { System.out.println(output); } conn.disconnect(); }catch(Exception e) { logger.error("Exception in callling web service" + e.toString()); }
Inside the execute method, I indeed know the num files. But how do I propagate that value to the response obtained in the Client? The output received by the client can be a simple String value (e.g. 30 where 30 is the numOfFiles), and it does not have to be JSON.
Do I need to write any additional code inside execute method to propagate the value of numOfFiles to the client code? And if yes, how to get that value of numOfFiles in the client code?
Is there a way to add values in the WebScriptResponse inside execute, which can be obtained by the client code?
Creating a java backed WebScript by extending AbstractWebScript class gives similar flavor as Http Servlet. You have request and response objects available to process the request and write the result using response object.
To write something to WebScript response you can call write method by getting the instance or Writer.
response.getWriter().write("hello")
It can be used to write any type of response such as text, xml, json, html etc. considering you prepare the response within WebScript and write it.
On the other hand their is another implementation of Java backed WebScript which called "DeclarativeWebScript" which you can extend to return a model (basically a java map) object.
This mode object will be used to pouplate the response in any of the extpected format based on your implementation.
Check these posts for more details:
Alfresco Javabacked Webscripts
Ask for and offer help to other Alfresco Content Services Users and members of the Alfresco team.
Related links:
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.