Class HttpMethods


  • public final class HttpMethods
    extends Object
    Provides one-liners for mapping HTTP-based operations using Gloop.
    • Constructor Detail

      • HttpMethods

        public HttpMethods​(io.toro.martini.service.UrlRewriter urlRewriter,
                           io.toro.martini.gloop.http.GloopHttpRequestFactory requestFactory,
                           io.toro.martini.gloop.http.GloopResponseHandlerFactory responseHandlerFactory,
                           io.toro.martini.gloop.http.GloopHttpContextFactory httpClientContextFactory,
                           Supplier<org.apache.http.impl.client.HttpClientBuilder> httpClientBuilder,
                           Predicate<HttpServletRequest> requestAuthenticator)
    • Method Detail

      • http

        @GloopObjectParameter("output{\nresponse#io.toro.martini.http.Response{\n}\n}")
        public static GloopModel http​(@GloopObjectParameter("request#io.toro.martini.http.Request{\n}")
                                      GloopModel requestModel,
                                      @GloopObjectParameter("auth#io.toro.martini.http.Auth{\n}")
                                      GloopModel authModel,
                                      @GloopParameter(choices={"InputStream","String","byte[]","GloopObject"},defaultValue="String")
                                      String returnAs,
                                      @GloopParameter(choices={"Xml","Json","Auto Detect"},defaultValue="Auto Detect")
                                      String responseType,
                                      @GloopParameter(defaultValue="false")
                                      boolean throwHttpExceptions,
                                      @GloopObjectParameter("trackable#io.toro.martini.tracker.Trackable{\n}")
                                      GloopModel trackableModel,
                                      GloopExecutionContext context)
                               throws IOException

        Send an HTTP request to a web service, and then parse the response into a model.

        This one-liner sends an HTTP request with the same HTTP method, URI, content type, headers, cookies, parameters, and body as the provided requestModel.

        At a minimum, the requestModel's uri field must be provided.

        For requests that require authorization, the authModel must be provided. When provided, the request will be sent with the same authorization details specified by the authModel. Else, the one-liner assumes authorization is not needed to access the web service.

        For requests that send a file, provide a io.toro.martini.http.MultipartFile GloopModel with a content-type, file name, and input stream then add it to the requestModel's parameters field.

        The server's response is returned as a model. responseType is used to indicate the expected response content format. Through the returnAs argument, one can specify the data type of the response content – which can be accessed by the response model's content property.

        If the response received has an HTTP status code of 5xx, then an error will be thrown by this one-liner if throwHttpExceptions is set to true. Else, the one-liner will return the response as usual.

        To log your request call to Tracker, populate the trackableModel. The trackableModel must at least have its internalId and trackingScope properties set.

        Parameters:
        requestModel - describes the HTTP request to send
        authModel - authentication model to be used for the request
        returnAs - determines the desired data type for the response content
        responseType - determines the content type of the server's response
        throwHttpExceptions - determines whether or not exceptions should be thrown when the server receives a response code >= 500.
        trackableModel - tracking metadata to be used by Martini's Tracker engine
        context - the Gloop execution context (automatically mapped by Gloop)
        Returns:
        a model representing the response of the server to the request sent
        Throws:
        IOException - in case of a problem or if the connection was aborted
        Since:
        1.0
        See Also:
        Tracker search index
      • soap

        @GloopObjectParameter("output{\nresponse#io.toro.martini.http.Response{\n}\n}")
        public static GloopModel soap​(String url,
                                      @GloopObjectParameter("auth#io.toro.martini.http.Auth{\n}")
                                      GloopModel authModel,
                                      String soapAction,
                                      @GloopParameter(defaultValue="false")
                                      String style,
                                      @GloopParameter(defaultValue="false")
                                      String soapProtocol,
                                      GloopModel messageHeader,
                                      GloopModel messageBody,
                                      @GloopObjectParameter("trackable#io.toro.martini.tracker.Trackable{\n}")
                                      GloopModel trackable,
                                      GloopExecutionContext context)
                               throws Exception

        Send a request to a SOAP web service, and then parse the response into a model.

        To log your request call to Tracker, populate trackable model. The trackable model must at least have its internalId and trackingScope properties set.

        Example:

         url = 'http://localhost:8080/soap/apis.soapApi.HelloYouSOAP?wsdl='
         soapAction = 'apis.service.HelloYouService'
         messageBody = messageBody {
             HelloYouServiceInput {
                 name 'world'
             }
         }
        
         HttpMethods.soap(url, null, soapAction, false, false, null, messageBody, null)
         

        Parameters:
        url - the URL of the recipient SOAP endpoint
        authModel - authentication model to be used for the request
        soapAction - the SOAP action to use
        style - determines whether or not to use an RPC style model; set to true to use RPC style
        soapProtocol - determines whether or not the service is implemented in SOAP v1.2; set to true to use SOAP v1.2
        messageHeader - the message header to be sent along with the request
        messageBody - the message body to be sent along with the request
        trackable - tracking metadata to be used by Martini's Tracker engine
        context - the Gloop execution context (automatically mapped by Gloop)
        Returns:
        a model representing the response of the server to the request sent
        Throws:
        IOException - in case of a problem or if the connection was aborted
        Exception
        Since:
        1.0
      • prepareResponse

        @GloopParameter(name="requestHandled")
        public static boolean prepareResponse​(HttpServletResponse response,
                                              GloopModel content,
                                              @GloopParameter(choices={"json","xml","yaml"},defaultValue="json",allowNull=false)
                                              String format,
                                              int responseCode)
        Set the appropriate responseCode and write the given GloopModel content to the HttpServletResponse in the specified format.
        Parameters:
        response - the response object of the request
        content - a model representing the content to be written to the response body
        format - the format (json, xml, or yaml) in which the content will be written
        responseCode - the code indicating the status of the request
        Since:
        1.1
      • authenticated

        public static boolean authenticated​(@GloopParameter(allowNull=false)
                                            HttpServletRequest request,
                                            @GloopParameter(defaultValue="false")
                                            boolean throwException)
        Checks if the current HttpServletRequest is authenticated. If the parameter throwException is set to true, this will throw a RequestUnauthorizedException if the current HttpServletRequest is not authenticated.
        Parameters:
        request - the http request
        throwException - the flag that tells whether to throw an exception if the request failed to authenticate
        Since:
        1.1