Class ProxyRequestMethods
- java.lang.Object
-
- io.toro.martini.ProxyRequestMethods
-
public final class ProxyRequestMethods extends Object
Contains one-liner methods for proxying HTTP requests to another server.
HTTP request data from the proxied HTTP request is for the most part, automatically injected and forwarded to the target server.
-
-
Constructor Summary
Constructors Constructor Description ProxyRequestMethods(io.toro.martini.http.HttpProxyService httpProxyService)
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
getMatchingPathPart(HttpServletRequest request)
Get the part of the request URL that matches the path pattern assigned to the endpoint in code.static String
parseUrl(String targetUrlTemplate, HttpServletRequest request)
Create a new URL whose format follows the provided template.static String
parseUrl(String targetUrlTemplate, HttpServletRequest request, Map variables)
Create a new URL whose format follows the provided template.static String
parseUrl(HttpServletRequest request, String targetUrlTemplate)
Create a new URL whose format follows the provided template.static String
parseUrl(HttpServletRequest request, String targetUrlTemplate, Map variables)
Create a new URL whose format follows the provided template.static HttpServletRequest
proxy(String targetUrl, HttpServletRequest request, HttpServletResponse response)
Proxy a request to another server.static HttpServletRequest
proxy(String targetUrl, HttpServletRequest request, HttpServletResponse response, boolean followRedirects)
Proxy a request to another server.static HttpServletRequest
proxy(String targetUrl, HttpServletRequest request, HttpServletResponse response, boolean followRedirect, Closure<HttpServletRequest> requestClosure)
Proxy a request to another server.static HttpServletRequest
proxy(HttpServletRequest request, HttpServletResponse response, String targetUrl)
Proxy a request to another server.static HttpServletRequest
proxy(HttpServletRequest request, HttpServletResponse response, String targetUrl, boolean followRedirects)
Proxy a request to another server.
-
-
-
Constructor Detail
-
ProxyRequestMethods
@Autowired ProxyRequestMethods(io.toro.martini.http.HttpProxyService httpProxyService)
-
-
Method Detail
-
proxy
public static HttpServletRequest proxy(HttpServletRequest request, HttpServletResponse response, String targetUrl) throws Exception
Proxy a request to another server.
targetURL
is atemplate
that defines the URL where the request will be directed to. Variables are supported, and will be substituted in the template according to the values ofrequest
. The following variables can be used in the template:variables
, which is a map containing all the variables below:request
, which will substitute the value ofrequest
params
, which is aMap
of the query parameters and their valuesmethod
, which is the HTTP method used to make the requestqueryString
, which is the query string contained in the request URL after the pathuri
, which is the request URIuriParts
, which is an array of all the paths in the request URIurl
, which is the request URLprotocol
, which is the protocol used by the requestlocalPort
, which is the port number of the interface on which the request was receivedserverPort
, which is the port number to which the request was sentremotePort
, which is the source port of the client or last proxy that sent the request-
secure
, which is a boolean value indicating whether the request was made on a secured channel, like HTTPs matchingPath
, which is the pattern-mapped part of the URLmatchingPaths
, which is an array derived frommatchingPath
, split by /serverName
, which is the host name of the server to which the request was sentheaders
, which is aMap
of all headers in the request
Example usage:
def request = ... def response = ... def proxyUrl = request.getRequestURL() + request.getQueryString() assert proxyUrl.equals( 'http://my.new.host/api/resource?page=1&size=50' ) def template = 'http://my.legacy.host/0a/sa2/179/$matchingPaths[1]?$queryString' request.proxy( response, template )
This one-liner invocation will proxy the request received at http://my.new.host/api/resource?page=1&size=50, and then direct it to http://my.legacy.host/0a/sa2/179/resource?page=1&size=50. Redirects will also be followed.The response of the target server will be written over to
response
. The provided response body is automatically closed if and only if, the proxied request receives a response from the server, regardless of the response code. Otherwise if the underlying HTTP client that proxies the request throws an exception (e.g., thetargetUrl
is down, or the remote server times out) the provided response body is kept open to give control to the original caller.If you are to call this one-liner in Gloop, it is recommended to declare an output property
$requestHandled
, and set it totrue
. This will prevent Martini from further writing to the response.- Parameters:
request
- the proxied HTTP requestresponse
- the proxied HTTP responsetargetUrl
- a template defining the URL of the web service which will actually handle the request- Returns:
- the proxied request
- Throws:
Exception
- Since:
- 1.0
-
proxy
public static HttpServletRequest proxy(HttpServletRequest request, HttpServletResponse response, String targetUrl, boolean followRedirects) throws Exception
Proxy a request to another server.
targetURL
is atemplate
that defines the URL where the request will be directed to. Variables are supported, and will be substituted in the template according to the values ofrequest
. The following variables can be used in the template:variables
, which is a map containing all the variables below:request
, which will substitute the value ofrequest
params
, which is aMap
of the query parameters and their valuesmethod
, which is the HTTP method used to make the requestqueryString
, which is the query string contained in the request URL after the pathuri
, which is the request URIuriParts
, which is an array of all the paths in the request URIurl
, which is the request URLprotocol
, which is the protocol used by the requestlocalPort
, which is the port number of the interface on which the request was receivedserverPort
, which is the port number to which the request was sentremotePort
, which is the source port of the client or last proxy that sent the request-
secure
, which is a boolean value indicating whether the request was made on a secured channel, like HTTPs matchingPath
, which is the pattern-mapped part of the URLmatchingPaths
, which is an array derived frommatchingPath
, split by /serverName
, which is the host name of the server to which the request was sentheaders
, which is aMap
of all headers in the request
Example usage:
def request = ... def response = ... def proxyUrl = request.getRequestURL() + request.getQueryString() assert proxyUrl.equals( 'http://my.new.host/api/resource?page=1&size=50' ) def template = 'http://my.legacy.host/0a/sa2/179/$matchingPaths[1]?$queryString' request.proxy( response, template, false )
This one-liner invocation will proxy the request received at http://my.new.host/api/resource?page=1&size=50, and then direct it to http://my.legacy.host/0a/sa2/179/resource?page=1&size=50. Redirects will also not be followed.The response of the target server will be written over to
response
. The provided response body is automatically closed if and only if, the proxied request receives a response from the server, regardless of the response code. Otherwise if the underlying HTTP client that proxies the request throws an exception (e.g., thetargetUrl
is down, or the remote server times out) the provided response body is kept open to give control to the original caller.If you are to call this one-liner in Gloop, it is recommended to declare an output property
$requestHandled
, and set it totrue
. This will prevent Martini from further writing to the response.- Parameters:
request
- the proxied HTTP requestresponse
- the proxied HTTP responsetargetUrl
- a template defining the URL of the web service which will actually handle the requestfollowRedirects
- a boolean value determining whether or not the proxy will follow a redirect response- Returns:
- the proxied request
- Throws:
Exception
- Since:
- 1.0
-
proxy
public static HttpServletRequest proxy(String targetUrl, HttpServletRequest request, HttpServletResponse response) throws Exception
Proxy a request to another server.
targetURL
is atemplate
that defines the URL where the request will be directed to. Variables are supported, and will be substituted in the template according to the values ofrequest
. The following variables can be used in the template:variables
, which is a map containing all the variables below:request
, which will substitute the value ofrequest
params
, which is aMap
of the query parameters and their valuesmethod
, which is the HTTP method used to make the requestqueryString
, which is the query string contained in the request URL after the pathuri
, which is the request URIuriParts
, which is an array of all the paths in the request URIurl
, which is the request URLprotocol
, which is the protocol used by the requestlocalPort
, which is the port number of the interface on which the request was receivedserverPort
, which is the port number to which the request was sentremotePort
, which is the source port of the client or last proxy that sent the request-
secure
, which is a boolean value indicating whether the request was made on a secured channel, like HTTPs matchingPath
, which is the pattern-mapped part of the URLmatchingPaths
, which is an array derived frommatchingPath
, split by /serverName
, which is the host name of the server to which the request was sentheaders
, which is aMap
of all headers in the request
Example usage:
def request = ... def response = ... def proxyUrl = request.getRequestURL() + request.getQueryString() assert proxyUrl.equals( 'http://my.new.host/api/resource?page=1&size=50' ) def template = 'http://my.legacy.host/0a/sa2/179/$matchingPaths[1]?$queryString' template.proxy( request, response )
This one-liner invocation will proxy the request received at http://my.new.host/api/resource?page=1&size=50, and then direct it to http://my.legacy.host/0a/sa2/179/resource?page=1&size=50. Redirects will be followed.The response of the target server will be written over to
response
. The provided response body is automatically closed if and only if, the proxied request receives a response from the server, regardless of the response code. Otherwise if the underlying HTTP client that proxies the request throws an exception (e.g., thetargetUrl
is down, or the remote server times out) the provided response body is kept open to give control to the original caller.If you are to call this one-liner in Gloop, it is recommended to declare an output property
$requestHandled
, and set it totrue
. This will prevent Martini from further writing to the response.- Parameters:
targetUrl
- a template defining the URL of the web service which will actually handle the requestrequest
- the proxied HTTP requestresponse
- the proxied HTTP response- Returns:
- the proxied request
- Throws:
Exception
- Since:
- 1.0
-
proxy
public static HttpServletRequest proxy(String targetUrl, HttpServletRequest request, HttpServletResponse response, boolean followRedirects) throws Exception
Proxy a request to another server.
targetURL
is atemplate
that defines the URL where the request will be directed to. Variables are supported, and will be substituted in the template according to the values ofrequest
. The following variables can be used in the template:variables
, which is a map containing all the variables below:request
, which will substitute the value ofrequest
params
, which is aMap
of the query parameters and their valuesmethod
, which is the HTTP method used to make the requestqueryString
, which is the query string contained in the request URL after the pathuri
, which is the request URIuriParts
, which is an array of all the paths in the request URIurl
, which is the request URLprotocol
, which is the protocol used by the requestlocalPort
, which is the port number of the interface on which the request was receivedserverPort
, which is the port number to which the request was sentremotePort
, which is the source port of the client or last proxy that sent the request-
secure
, which is a boolean value indicating whether the request was made on a secured channel, like HTTPs matchingPath
, which is the pattern-mapped part of the URLmatchingPaths
, which is an array derived frommatchingPath
, split by /serverName
, which is the host name of the server to which the request was sentheaders
, which is aMap
of all headers in the request
Example usage:
def request = ... def response = ... def proxyUrl = request.getRequestURL() + request.getQueryString() assert proxyUrl.equals( 'http://my.new.host/api/resource?page=1&size=50' ) def template = 'http://my.legacy.host/0a/sa2/179/$matchingPaths[1]?$queryString' template.proxy( request, response, false )
This one-liner invocation will proxy the request received at http://my.new.host/api/resource?page=1&size=50, and then direct it to http://my.legacy.host/0a/sa2/179/resource?page=1&size=50. Redirects will also not be followed.The response of the target server will be written over to
response
. The provided response body is automatically closed if and only if, the proxied request receives a response from the server, regardless of the response code. Otherwise if the underlying HTTP client that proxies the request throws an exception (e.g., thetargetUrl
is down, or the remote server times out) the provided response body is kept open to give control to the original caller.If you are to call this one-liner in Gloop, it is recommended to declare an output property
$requestHandled
, and set it totrue
. This will prevent Martini from further writing to the response.- Parameters:
targetUrl
- a template defining the URL of the web service which will actually handle the requestrequest
- the proxied HTTP requestresponse
- the proxied HTTP responsefollowRedirects
- a boolean value determining whether or not the proxy will follow a redirect response- Returns:
- the proxied request
- Throws:
Exception
- Since:
- 1.0
-
proxy
public static HttpServletRequest proxy(String targetUrl, HttpServletRequest request, HttpServletResponse response, boolean followRedirect, Closure<HttpServletRequest> requestClosure) throws Exception
Proxy a request to another server.
targetURL
is atemplate
that defines the URL where the request will be directed to. Variables are supported, and will be substituted in the template according to the values ofrequest
. The following variables can be used in the template:variables
, which is a map containing all the variables below:request
, which will substitute the value ofrequest
params
, which is aMap
of the query parameters and their valuesmethod
, which is the HTTP method used to make the requestqueryString
, which is the query string contained in the request URL after the pathuri
, which is the request URIuriParts
, which is an array of all the paths in the request URIurl
, which is the request URLprotocol
, which is the protocol used by the requestlocalPort
, which is the port number of the interface on which the request was receivedserverPort
, which is the port number to which the request was sentremotePort
, which is the source port of the client or last proxy that sent the request-
secure
, which is a boolean value indicating whether the request was made on a secured channel, like HTTPs matchingPath
, which is the pattern-mapped part of the URLmatchingPaths
, which is an array derived frommatchingPath
, split by /serverName
, which is the host name of the server to which the request was sentheaders
, which is aMap
of all headers in the request
Example usage:
def request = ... def response = ... def proxyUrl = request.getRequestURL() + request.getQueryString() assert proxyUrl.equals( 'http://my.new.host/api/resource?page=1&size=50' ) def template = 'http://my.legacy.host/0a/sa2/179/$matchingPaths[1]?$queryString' template.proxy( request, response, false, { it.putHeader( 'Authorization', null ) // removes the 'Authorization' header } )
This one-liner invocation will proxy the request received at http://my.new.host/api/resource?page=1&size=50, and then direct it to http://my.legacy.host/0a/sa2/179/resource?page=1&size=50 without the Authorization header. Redirects will also not be followed.The response of the target server will be written over to
response
. The provided response body is automatically closed if and only if, the proxied request receives a response from the server, regardless of the response code. Otherwise if the underlying HTTP client that proxies the request throws an exception (e.g., thetargetUrl
is down, or the remote server times out) the provided response body is kept open to give control to the original caller.If you are to call this one-liner in Gloop, it is recommended to declare an output property
$requestHandled
, and set it totrue
. This will prevent Martini from further writing to the response.- Parameters:
targetUrl
- a template defining the URL of the web service which will actually handle the requestrequest
- the proxied HTTP requestresponse
- the proxied HTTP responsefollowRedirect
- a boolean value determining whether or not the proxy will follow a redirect responserequestClosure
- a closure for modifying the proxied request before it is sent- Returns:
- the proxied request
- Throws:
Exception
- Since:
- 1.0
-
getMatchingPathPart
public static String getMatchingPathPart(HttpServletRequest request)
Get the part of the request URL that matches the path pattern assigned to the endpoint in code.
Example usage:
def request = ... def requestUrl = request.getRequestURL() def urlPattern = request.getAttribute( HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE ) assert urlPattern.equals( '/api/resource/*' ) assert requestUrl.equals( '/api/resource/1' ) def part = request.getMatchingPathPart() assert part.equals( '1' )
- Parameters:
request
- the HTTP request- Returns:
- part of the URL matching the assigned pattern
- Since:
- 1.0
-
parseUrl
public static String parseUrl(HttpServletRequest request, String targetUrlTemplate, Map variables) throws Exception
Create a new URL whose format follows the provided template. Variables in the template will be substituted with data from the request and the provided map of variables.
Example usage:
def request = ... def proxyUrl = request.getRequestURL() + request.getQueryString() assert proxyUrl.equals( 'http://my.new.host/api/resource?page=1&size=50' ) def template = 'http://my.legacy.host/0a/sa2/179/$matchingPaths[1]?$queryString&sort=$sort' def legacyUrl = template.parseUrl( request, [ 'sort': 'name' ] ) assert legacyUrl.equals( 'http://my.legacy.host/0a/sa2/179/resource?page=1&size=50&sort=name' )
- Parameters:
request
- the request whose data will be usedtargetUrlTemplate
- the template to usevariables
- an optional map of variables whose values will be substituted in the provided template when used- Returns:
- the resulting URL string
- Throws:
Exception
- Since:
- 1.0
-
parseUrl
public static String parseUrl(HttpServletRequest request, String targetUrlTemplate) throws Exception
Create a new URL whose format follows the provided template. Variables in the template will be substituted with data from the request.
Example usage:
def request = ... def proxyUrl = request.getRequestURL() + request.getQueryString() assert proxyUrl.equals( 'http://my.new.host/api/resource?page=1&size=50' ) def template = 'http://my.legacy.host/0a/sa2/179/$matchingPaths[1]?$queryString' def legacyUrl = template.parseUrl( request ) assert legacyUrl.equals( 'http://my.legacy.host/0a/sa2/179/resource?page=1&size=50' )
- Parameters:
request
- the request whose data will be usedtargetUrlTemplate
- the template to use- Returns:
- the resulting URL string
- Throws:
Exception
- Since:
- 1.0
-
parseUrl
public static String parseUrl(String targetUrlTemplate, HttpServletRequest request, Map variables) throws Exception
Create a new URL whose format follows the provided template. Variables in the template will be substituted with data from the request and the provided map of variables.
Example usage:
def request = ... def proxyUrl = request.getRequestURL() + request.getQueryString() assert proxyUrl.equals( 'http://my.new.host/api/resource?page=1&size=50' ) def template = 'http://my.legacy.host/0a/sa2/179/$matchingPaths[1]?$queryString&sort=$sort' def legacyUrl = template.parseUrl( request, [ 'sort': 'name' ] ) assert legacyUrl.equals( 'http://my.legacy.host/0a/sa2/179/resource?page=1&size=50&sort=name' )
- Parameters:
targetUrlTemplate
- the template to userequest
- the request whose data will be usedvariables
- an optional map of variables whose values will be substituted in the provided template when used- Returns:
- the resulting URL string
- Throws:
Exception
- Since:
- 1.0
-
parseUrl
public static String parseUrl(String targetUrlTemplate, HttpServletRequest request) throws Exception
Create a new URL whose format follows the provided template. Variables in the template will be substituted with data from the request.
Example usage:
def request = ... def proxyUrl = request.getRequestURL() + request.getQueryString() assert proxyUrl.equals( 'http://my.new.host/api/resource?page=1&size=50' ) def template = 'http://my.legacy.host/0a/sa2/179/$matchingPaths[1]?$queryString' def legacyUrl = template.parseUrl( request ) assert legacyUrl.equals( 'http://my.legacy.host/0a/sa2/179/resource?page=1&size=50' )
- Parameters:
targetUrlTemplate
- the template to userequest
- the request whose data will be used- Returns:
- the resulting URL string
- Throws:
Exception
- Since:
- 1.0
-
-