Class 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 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 a template 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 of request. 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 of request
        • params, which is a Map of the query parameters and their values
        • method, which is the HTTP method used to make the request
        • queryString, which is the query string contained in the request URL after the path
        • uri, which is the request URI
        • uriParts, which is an array of all the paths in the request URI
        • url, which is the request URL
        • protocol, which is the protocol used by the request
        • localPort, which is the port number of the interface on which the request was received
        • serverPort, which is the port number to which the request was sent
        • remotePort, 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 URL
        • matchingPaths, which is an array derived from matchingPath, split by /
        • serverName, which is the host name of the server to which the request was sent
        • headers, which is a Map 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., the targetUrl 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 to true. This will prevent Martini from further writing to the response.

        Parameters:
        request - the proxied HTTP request
        response - the proxied HTTP response
        targetUrl - 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 a template 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 of request. 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 of request
        • params, which is a Map of the query parameters and their values
        • method, which is the HTTP method used to make the request
        • queryString, which is the query string contained in the request URL after the path
        • uri, which is the request URI
        • uriParts, which is an array of all the paths in the request URI
        • url, which is the request URL
        • protocol, which is the protocol used by the request
        • localPort, which is the port number of the interface on which the request was received
        • serverPort, which is the port number to which the request was sent
        • remotePort, 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 URL
        • matchingPaths, which is an array derived from matchingPath, split by /
        • serverName, which is the host name of the server to which the request was sent
        • headers, which is a Map 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., the targetUrl 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 to true. This will prevent Martini from further writing to the response.

        Parameters:
        request - the proxied HTTP request
        response - the proxied HTTP response
        targetUrl - a template defining the URL of the web service which will actually handle the request
        followRedirects - 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 a template 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 of request. 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 of request
        • params, which is a Map of the query parameters and their values
        • method, which is the HTTP method used to make the request
        • queryString, which is the query string contained in the request URL after the path
        • uri, which is the request URI
        • uriParts, which is an array of all the paths in the request URI
        • url, which is the request URL
        • protocol, which is the protocol used by the request
        • localPort, which is the port number of the interface on which the request was received
        • serverPort, which is the port number to which the request was sent
        • remotePort, 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 URL
        • matchingPaths, which is an array derived from matchingPath, split by /
        • serverName, which is the host name of the server to which the request was sent
        • headers, which is a Map 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., the targetUrl 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 to true. 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 request
        request - the proxied HTTP request
        response - 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 a template 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 of request. 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 of request
        • params, which is a Map of the query parameters and their values
        • method, which is the HTTP method used to make the request
        • queryString, which is the query string contained in the request URL after the path
        • uri, which is the request URI
        • uriParts, which is an array of all the paths in the request URI
        • url, which is the request URL
        • protocol, which is the protocol used by the request
        • localPort, which is the port number of the interface on which the request was received
        • serverPort, which is the port number to which the request was sent
        • remotePort, 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 URL
        • matchingPaths, which is an array derived from matchingPath, split by /
        • serverName, which is the host name of the server to which the request was sent
        • headers, which is a Map 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., the targetUrl 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 to true. 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 request
        request - the proxied HTTP request
        response - the proxied HTTP response
        followRedirects - 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 a template 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 of request. 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 of request
        • params, which is a Map of the query parameters and their values
        • method, which is the HTTP method used to make the request
        • queryString, which is the query string contained in the request URL after the path
        • uri, which is the request URI
        • uriParts, which is an array of all the paths in the request URI
        • url, which is the request URL
        • protocol, which is the protocol used by the request
        • localPort, which is the port number of the interface on which the request was received
        • serverPort, which is the port number to which the request was sent
        • remotePort, 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 URL
        • matchingPaths, which is an array derived from matchingPath, split by /
        • serverName, which is the host name of the server to which the request was sent
        • headers, which is a Map 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., the targetUrl 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 to true. 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 request
        request - the proxied HTTP request
        response - the proxied HTTP response
        followRedirect - a boolean value determining whether or not the proxy will follow a redirect response
        requestClosure - 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 used
        targetUrlTemplate - the template to use
        variables - 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 used
        targetUrlTemplate - 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 use
        request - the request whose data will be used
        variables - 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 use
        request - the request whose data will be used
        Returns:
        the resulting URL string
        Throws:
        Exception
        Since:
        1.0