Class HttpMethods
- java.lang.Object
-
- io.toro.martini.HttpMethods
-
public final class HttpMethods extends Object
Provides one-liners for mapping HTTP-based operations using Gloop.
-
-
Constructor Summary
Constructors Constructor Description 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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
authenticated(HttpServletRequest request)
Checks if the currentHttpServletRequest
is authenticated.static boolean
authenticated(HttpServletRequest request, boolean throwException)
Checks if the currentHttpServletRequest
is authenticated.static GloopModel
http(GloopModel requestModel, GloopModel authModel, String returnAs, String responseType, boolean throwHttpExceptions, GloopModel trackableModel, GloopExecutionContext context)
Send an HTTP request to a web service, and then parse the response into a model.static boolean
prepareResponse(HttpServletResponse response, GloopModel content, String format, int responseCode)
Set the appropriateresponseCode
and write the givenGloopModel
content
to theHttpServletResponse
in the specifiedformat
.static GloopModel
soap(String url, GloopModel authModel, String soapAction, String style, String soapProtocol, GloopModel messageHeader, GloopModel messageBody, GloopModel trackable, GloopExecutionContext context)
Send a request to a SOAP web service, and then parse the response into a model.
-
-
-
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
'suri
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 theauthModel
. 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 therequestModel
'sparameters
field.The server's response is returned as a model.
responseType
is used to indicate the expected response content format. Through thereturnAs
argument, one can specify the data type of the response content – which can be accessed by the response model'scontent
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
. ThetrackableModel
must at least have itsinternalId
andtrackingScope
properties set.- Parameters:
requestModel
- describes the HTTP request to sendauthModel
- authentication model to be used for the requestreturnAs
- determines the desired data type for the response contentresponseType
- determines the content type of the server's responsethrowHttpExceptions
- 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 enginecontext
- 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. Thetrackable
model must at least have itsinternalId
andtrackingScope
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 endpointauthModel
- authentication model to be used for the requestsoapAction
- the SOAP action to usestyle
- determines whether or not to use an RPC style model; set totrue
to use RPC stylesoapProtocol
- determines whether or not the service is implemented in SOAP v1.2; set totrue
to use SOAP v1.2messageHeader
- the message header to be sent along with the requestmessageBody
- the message body to be sent along with the requesttrackable
- tracking metadata to be used by Martini's Tracker enginecontext
- 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 abortedException
- 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 appropriateresponseCode
and write the givenGloopModel
content
to theHttpServletResponse
in the specifiedformat
.- Parameters:
response
- the response object of the requestcontent
- a model representing the content to be written to the response bodyformat
- the format (json, xml, or yaml) in which the content will be writtenresponseCode
- the code indicating the status of the request- Since:
- 1.1
-
authenticated
public static boolean authenticated(@GloopParameter(allowNull=false) HttpServletRequest request)
Checks if the currentHttpServletRequest
is authenticated.- Parameters:
request
- the http request- Since:
- 1.1
-
authenticated
public static boolean authenticated(@GloopParameter(allowNull=false) HttpServletRequest request, @GloopParameter(defaultValue="false") boolean throwException)
Checks if the currentHttpServletRequest
is authenticated. If the parameterthrowException
is set totrue
, this will throw aRequestUnauthorizedException
if the currentHttpServletRequest
is not authenticated.- Parameters:
request
- the http requestthrowException
- the flag that tells whether to throw an exception if the request failed to authenticate- Since:
- 1.1
-
-