public ResponseEntity<?> doProxy(HttpServletRequest request, String baseUrl, Map<String, String> params,
            boolean useWhitelist) {
        LOG.debug("Intercepting a request against service '" + baseUrl + "' with parameters: " + params);

        if (StringUtils.isEmpty(baseUrl) || request == null) {
            LOG.warn(ERR_MSG_400_NO_URL);
            return RESPONSE_400_BAD_REQUEST_NO_URL;
        }

        // transform to URL
        URL url;
        try {
            url = new URL(baseUrl);
        } catch (MalformedURLException use) {
            LOG.error(RESPONSE_500_INTERNAL_SERVER_ERROR);
            return RESPONSE_500_INTERNAL_SERVER_ERROR;
        }

        // Could not parse URI properly
        if (url == null) {
            LOG.warn(ERR_MSG_404);
            return RESPONSE_404_NOT_FOUND;
        }

        if (useWhitelist) {
            // check if URI is contained in whitelist
            final boolean isInWhiteList = isInWhiteList(url);

            if (!isInWhiteList) {
                LOG.warn(ERR_MSG_502);
                return RESPONSE_502_BAD_GATEWAY;
            }
        }

        // build request for params and baseUrl;
        try {
            url = buildUriWithParameters(url, params);
        } catch (URISyntaxException | MalformedURLException excep) {
            LOG.error(RESPONSE_500_INTERNAL_SERVER_ERROR);
            return RESPONSE_500_INTERNAL_SERVER_ERROR;
        }

        // Proxy the request
        Response response;
        if (HttpUtil.isHttpGetRequest(request)) {
            try {
                LOG.debug("Forwarding as GET to: " + url);
                response = HttpUtil.forwardGet(url.toURI(), request, false);
            } catch (URISyntaxException | HttpException e) {
                String errorMessage = "Error forwarding GET request: " + e.getMessage();
                LOG.error(errorMessage);
                return RESPONSE_400_BAD_REQUEST_COMMON;
            }
        } else if (HttpUtil.isHttpPostRequest(request)) {
            if (HttpUtil.isFormMultipartPost(request)) {
                try {
                    LOG.debug("Forwarding as form/multipart POST");
                    response = HttpUtil.forwardFormMultipartPost(url.toURI(), request, false);
                } catch (URISyntaxException | HttpException | IllegalStateException | IOException | ServletException e) {
                    String errorMessage = "Error forwarding form/multipart POST request: " + e.getMessage();
                    LOG.error(errorMessage);
                    return RESPONSE_400_BAD_REQUEST_COMMON;
                }
            } else {
                try {
                    LOG.debug("Forwarding as POST");
                    response = HttpUtil.forwardPost(url.toURI(), request, false);
                } catch (URISyntaxException | HttpException e) {
                    String errorMessage = "Error forwarding POST request: " + e.getMessage();
                    LOG.error(errorMessage);
                    return RESPONSE_400_BAD_REQUEST_COMMON;
                }
            }
        } else {
            LOG.error("Proxy does not yet support HTTP method: " + request.getMethod());
            return RESPONSE_405_METHOD_NOT_ALLOWED;
        }

        byte[] bytes = response.getBody();
        final HttpHeaders responseHeadersToForward = response.getHeaders();
        //getResponseHeadersToForward(response); // TODO adapt headers in the future!

        // LOG response headers
        Set<Entry<String, List<String>>> headerEntries = responseHeadersToForward.entrySet();
        for (Entry<String, List<String>> headerEntry : headerEntries) {
            String headerKey = headerEntry.getKey();
            List<String> headerValues = headerEntry.getValue();
            String joinedHeaderValues = StringUtils.join(headerValues, "; ");

            LOG.debug("Got the following response header: " + headerKey + "=" + joinedHeaderValues);
        }

        final HttpStatus responseHttpStatus = response.getStatusCode();

        return new ResponseEntity<>(bytes, responseHeadersToForward, responseHttpStatus);
    }
public ResponseEntity < ? > doProxy ( HttpServletRequest request , String baseUrl , Map < String , String > params , boolean useWhitelist ) { LOG . debug ( "Intercepting a request against service '" + baseUrl + "' with parameters: " + params ) ; if ( StringUtils . isEmpty ( baseUrl ) || request == null ) { LOG . warn ( ERR_MSG_400_NO_URL ) ; return RESPONSE_400_BAD_REQUEST_NO_URL ; } URL url ; try { url = new URL ( baseUrl ) ; } catch ( MalformedURLException use ) { LOG . error ( RESPONSE_500_INTERNAL_SERVER_ERROR ) ; return RESPONSE_500_INTERNAL_SERVER_ERROR ; } if ( url == null ) { LOG . warn ( ERR_MSG_404 ) ; return RESPONSE_404_NOT_FOUND ; } if ( useWhitelist ) { final boolean isInWhiteList = isInWhiteList ( url ) ; if ( ! isInWhiteList ) { LOG . warn ( ERR_MSG_502 ) ; return RESPONSE_502_BAD_GATEWAY ; } } try { url = buildUriWithParameters ( url , params ) ; } catch ( URISyntaxException | MalformedURLException excep ) { LOG . error ( RESPONSE_500_INTERNAL_SERVER_ERROR ) ; return RESPONSE_500_INTERNAL_SERVER_ERROR ; } Response response ; if ( HttpUtil . isHttpGetRequest ( request ) ) { try { LOG . debug ( "Forwarding as GET to: " + url ) ; response = HttpUtil . forwardGet ( url . toURI ( ) , request , false ) ; } catch ( URISyntaxException | HttpException e ) { String errorMessage = "Error forwarding GET request: " + e . getMessage ( ) ; LOG . error ( errorMessage ) ; return RESPONSE_400_BAD_REQUEST_COMMON ; } } else if ( HttpUtil . isHttpPostRequest ( request ) ) { if ( HttpUtil . isFormMultipartPost ( request ) ) { try { LOG . debug ( "Forwarding as form/multipart POST" ) ; response = HttpUtil . forwardFormMultipartPost ( url . toURI ( ) , request , false ) ; } catch ( URISyntaxException | HttpException | IllegalStateException | IOException | ServletException e ) { String errorMessage = "Error forwarding form/multipart POST request: " + e . getMessage ( ) ; LOG . error ( errorMessage ) ; return RESPONSE_400_BAD_REQUEST_COMMON ; } } else { try { LOG . debug ( "Forwarding as POST" ) ; response = HttpUtil . forwardPost ( url . toURI ( ) , request , false ) ; } catch ( URISyntaxException | HttpException e ) { String errorMessage = "Error forwarding POST request: " + e . getMessage ( ) ; LOG . error ( errorMessage ) ; return RESPONSE_400_BAD_REQUEST_COMMON ; } } } else { LOG . error ( "Proxy does not yet support HTTP method: " + request . getMethod ( ) ) ; return RESPONSE_405_METHOD_NOT_ALLOWED ; } byte [ ] bytes = response . getBody ( ) ; final HttpHeaders responseHeadersToForward = response . getHeaders ( ) ; Set < Entry < String , List < String > > > headerEntries = responseHeadersToForward . entrySet ( ) ; for ( Entry < String , List < String > > headerEntry : headerEntries ) { String headerKey = headerEntry . getKey ( ) ; List < String > headerValues = headerEntry . getValue ( ) ; String joinedHeaderValues = StringUtils . join ( headerValues , "; " ) ; LOG . debug ( "Got the following response header: " + headerKey + "=" + joinedHeaderValues ) ; } final HttpStatus responseHttpStatus = response . getStatusCode ( ) ; return new ResponseEntity < > ( bytes , responseHeadersToForward , responseHttpStatus ) ; }
@param request
@param baseUrl
@param params
@param useWhitelist
@return