Class ProvidenceHttpServlet<RequestType extends net.morimekta.providence.PMessage<RequestType>,​ResponseType extends net.morimekta.providence.PMessage<ResponseType>>

  • Type Parameters:
    RequestType - The request type.
    ResponseType - The response type.
    All Implemented Interfaces:
    java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

    public abstract class ProvidenceHttpServlet<RequestType extends net.morimekta.providence.PMessage<RequestType>,​ResponseType extends net.morimekta.providence.PMessage<ResponseType>>
    extends javax.servlet.http.HttpServlet
    A simple HTTP POST servlet that simply deserializes the POST body as a providence message, and serializes the response message using the requested content type or accept type.

    Note that the ProvidenceHttpServlet is NOT usable for thrift services, but is meant to be used to make simple POST based HTTP servlets.

    
     public class MyServlet extends ProvidenceHttpServlet<
             MyRequest, MyRequest._Field,
             MyResponse, MyResponse._Field> {
         {@[email protected]}Override
         protected MyResponse handle(HttpServletRequest httpRequest,
                                     MyRequest request)
                  throws MyException, InternalFailureException {
             // ... do stuff with request, or throw MyException or IFE.
             return MyResponse.builder()
                              .setFieldOut("yes")
                              .build();
         }
    
         {@[email protected]}Override
         protected int statusCodeForException({@[email protected]}Nonnull Throwable exception) {
             if (exception instanceof MyException) {
                 return HttpStatus.BAD_REQUEST_400;
             }
             return super.statusCodeForException(ex);
         }
     }
     
    This will result in a simple HTTP servlet that can be queries with CURL e.g. like this:
    
     # Simple success
     $ curl -sS -X POST -d "{\"field_in\": \"value\"}" \
     >     -H "Content-Type: application/json" \
     >     localhost:8080/my/servlet
     {\"field_out\":\"yes\"}
    
     # Simple Error
     $ curl -sSv -X POST -d "{\"field_in\": \"not valid\"}" \
     >     -H "Content-Type: application/json" \
     >     localhost:8080/my/servlet
     ...
     > Content-Type: application/json
     > Accept: *{@literal/}*
     ...
     < HTTP/1.1 400 Bad Request
     < Content-Type: application/json
     ...
     {\"text\":\"not valid value\"}
     
    Alternatively you can hijack the whole exception / error response handling, which might be needed where custom headers etc are needed, e.g. with Unauthorized (401):
    
     public class MyServlet extends ProvidenceHttpServlet&lt;
             MyRequest, MyRequest._Field,
             MyResponse, MyResponse._Field&gt; {
         public MyServlet() {
             super(MyRequest.kDescriptor, new MyExceptionHandler(), DefaultSerializerProvider.INSTANCE);
         }
    
         {@[email protected]}Override
         protected MyResponse handle(HttpServletRequest httpRequest,
                                     MyRequest request)
                  throws MyException, InternalFailureException {
             // ... do stuff with request, or throw MyException or IFE.
             return MyResponse.builder()
                              .setFieldOut("yes")
                              .build();
         }
     }
     
    
     public class MyExceptionHandler extends ExceptionHandler {
         {@[email protected]}Override
         protected void writeErrorResponse(
                 {@[email protected]}Nonnull Throwable exception,
                 {@[email protected]}Nonnull Serializer responseSerializer,
                 {@[email protected]}Nonnull HttpServletRequest httpRequest,
                 {@[email protected]}Nonnull HttpServletResponse httpResponse)
                 throws IOException {
             if (exception instanceof MyException) {
                 httpResponse.setStatus(HttpStatus.UNAUTHORIZED_401);
                 httpResponse.setHeader(HttpHeaders.WWW_AUTHENTICATE, "www.my-domain.com");
                 responseSerializer.serialize(httpResponse.getOutputStream(), (MyException) exception);
                 return;
             }
             super.writeErrorResponse(exception);
         }
     }
     

    Overridable Methods

    Since:
    1.6.0
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      ProvidenceHttpServlet​(net.morimekta.providence.descriptor.PMessageDescriptor<RequestType> requestDescriptor, ExceptionHandler exceptionHandler, net.morimekta.providence.serializer.SerializerProvider serializerProvider)  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected void doPost​(javax.servlet.http.HttpServletRequest httpRequest, javax.servlet.http.HttpServletResponse httpResponse)  
      protected abstract ResponseType handle​(javax.servlet.http.HttpServletRequest httpRequest, RequestType request)
      Handle the request itself as a simple called method.
      • Methods inherited from class javax.servlet.http.HttpServlet

        doDelete, doGet, doHead, doOptions, doPut, doTrace, getLastModified, service, service
      • Methods inherited from class javax.servlet.GenericServlet

        destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, init, log, log
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ProvidenceHttpServlet

        public ProvidenceHttpServlet​(@Nonnull
                                     net.morimekta.providence.descriptor.PMessageDescriptor<RequestType> requestDescriptor,
                                     @Nullable
                                     ExceptionHandler exceptionHandler,
                                     @Nullable
                                     net.morimekta.providence.serializer.SerializerProvider serializerProvider)
    • Method Detail

      • handle

        @Nonnull
        protected abstract ResponseType handle​(@Nonnull
                                               javax.servlet.http.HttpServletRequest httpRequest,
                                               @Nonnull
                                               RequestType request)
                                        throws java.lang.Exception
        Handle the request itself as a simple called method.
        Parameters:
        httpRequest - The HTTP request.
        request - The parsed providence request.
        Returns:
        The response object.
        Throws:
        java.lang.Exception - On any internal exception.
      • doPost

        protected final void doPost​(javax.servlet.http.HttpServletRequest httpRequest,
                                    javax.servlet.http.HttpServletResponse httpResponse)
                             throws java.io.IOException
        Overrides:
        doPost in class javax.servlet.http.HttpServlet
        Throws:
        java.io.IOException