Writing a Serverless HelloWorld function (part 2)

After a Twitter exchange with David Delabassée from Oracle (thanks David) I realized that I did not take the example far enough. So we are going to rewrite the previous code so the binary will process standard input and produce standard output.

The code below describes a wrapper class that makes it so the Servlet can be executed without it knowing it is NOT really listening on a port. The wrapper class processes standard input and writes the result on standard output.


public class HelloWorldFunctionWrapper {

    /**
     * Process method.
     *
     * @param inputStream the (standard in) input stream.
     * @param outputStream the (standard out) output stream.
     */
    public void process(InputStream inputStream, PrintStream outputStream) {

        DefaultWebApplication webApp = new DefaultWebApplication();
        webApp.setContextPath("");
        HelloWorldFunction function = new HelloWorldFunction();
        webApp.addServlet("function", function);
        webApp.addServletMapping("function", "/*");
        webApp.initialize();
        webApp.start();

        ByteBufferHttpServletRequest request = new ByteBufferHttpServletRequest();
        request.setWebApplication(webApp);
        request.setContextPath("");
        request.setServletPath("/helloWorld");

        ByteBufferHttpServletResponse response = new ByteBufferHttpServletResponse();
        ByteBufferServletOutputStream bufferedOutputStream = new ByteBufferServletOutputStream();
        response.setOutputStream(bufferedOutputStream);
        bufferedOutputStream.setResponse(response);

        try {
            webApp.service(request, response);
            response.flushBuffer();
            outputStream.write(bufferedOutputStream.getBytes());
            outputStream.flush();
        } catch (ServletException | IOException e) {
            e.printStackTrace(outputStream);
        }

        webApp.stop();
    }

    /**
     * Main method.
     *
     * @param arguments the arguments.
     */
    public static void main(String[] arguments) {
        HelloWorldFunctionWrapper wrapper = new HelloWorldFunctionWrapper();
        wrapper.process(System.in, System.out);
    }
}

        

And this simplifies the Servlet itself to be just the code below.



public class HelloWorldFunction extends HttpServlet {
    
    /**
     * Process the request.
     *
     * @param request the request.
     * @param response the response.
     * @throws IOException when an I/O error occurs.
     * @throws ServletException when a Servlet error occurs.
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        response.setContentType("text/plain");
        PrintWriter writer = response.getWriter();
        writer.println("Hello World");
    }
}

        

This change should make it more suitable to run it within any good serverless framework.

Project resources

If you want a copy of the entire project click here to download it.

Posted December 16th, 2018

Up