Handling POST requests from remote website/Sigfox with Jetty 9.4.1

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Handling POST requests from remote website/Sigfox with Jetty 9.4.1

This post has NOT been accepted by the mailing list yet.
This post was updated on .
I'm new to Jetty and networking with Java so please bear with me and my beginners questions.

What I want to do is process a payload sent from Sigfox in the application/x-www-form-urlencoded format i.e. the data will come as "device={device}&data={data}&time={time}".

I've learnt that I can actually make a Sigfox Callback to a Java app server. Correct me if I'm wrong but I was thinking that a servlet can serve just the same purpose? This is why I decided to embed Jetty into my Java application (I'm using the latest Jetty 9.4.1).

Following the Embedded Jetty tutorial I've made this program:

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.net.InetSocketAddress;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;

public class MinimalServlets
    public static void main( String[] args ) throws Exception
        // Create a basic jetty server object that will listen on port 9081.
        // Note that if you set this to port 0 then a randomly available port
        // will be assigned that you can either look in the logs for the port,
        // or programmatically obtain it for use in test cases.
        Server server = new Server(new InetSocketAddress("MY_IP", 9081));

        // The ServletHandler is a dead simple way to create a context handler
        // that is backed by an instance of a Servlet.
        // This handler then needs to be registered with the Server object.
        ServletHandler handler = new ServletHandler();

        // Passing in the class for the Servlet allows jetty to instantiate an
        // instance of that Servlet and mount it on a given context path.

        // IMPORTANT:
        // This is a raw Servlet, not a Servlet that has been configured
        // through a web.xml @WebServlet annotation, or anything similar.
        handler.addServletWithMapping(HelloServlet.class, "/*");

        // Start things up!

        // The use of server.join() the will make the current thread join and
        // wait until the server is done executing.
        // See
        // http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()

    public static class HelloServlet extends HttpServlet
        protected void doGet( HttpServletRequest request,
                              HttpServletResponse response ) throws ServletException,
            response.getWriter().println("Hello from HelloServlet");
//            System.out.println("\nConnected");
//     System.out.println(request.getParameter("data"));

        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
            // Get POST parameters
            String val = req.getParameter("data");
            System.out.println(val);    //print value of 'data'
            // Write a response
            resp.setStatus(HttpServletResponse.SC_OK);  //respond with "200"          
            resp.getWriter().printf("data = %s",val);

However it doesn't receive the payload from Sigfox nor does it respond with "200". On the Sigfox end it says the "connection has timed out":

Screenshot of callback error:

This is similar to what I want to do but not in json code, just the normal x-www-form-urlencoded. Currently when the page loads the Message from doGet() "Hello from HelloServlet" is printed which tells me my doGet is executed instead of my doPost. Inside doGet, request.getRequestURI()  also says the request is coming from "/favicon.io"?

From my reading of the Jetty architecture, HttpServlet and ServletHandler is the right way to go? So where am I going wrong with my understanding and code?

Thanks in advance!