Search in sources :

Example 1 with HttpProxyException

use of org.craftercms.engine.exception.HttpProxyException in project engine by craftercms.

the class HttpProxyImpl method proxyMethod.

protected void proxyMethod(String url, boolean isGet, HttpServletRequest request, HttpServletResponse response) throws HttpProxyException {
    url = createTargetUrl(url, request);
    HttpMethod httpMethod = null;
    try {
        if (isGet) {
            httpMethod = createGetMethod(url, request);
        } else {
            httpMethod = createPostMethod(url, request);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Proxying to " + getMethodDescription(httpMethod));
        }
        int status = httpClient.executeMethod(httpMethod);
        response.setStatus(status);
        if (status >= 400 && logger.isDebugEnabled()) {
            logger.debug("Received error response from " + getMethodDescription(httpMethod) + ": status = " + httpMethod.getStatusText() + ", response body = \n" + httpMethod.getResponseBodyAsString());
        }
        copyMethodResponseHeadersToResponse(httpMethod, response);
        copyMethodResponseBodyToResponse(httpMethod, response);
    } catch (Exception e) {
        String errorMsg;
        if (httpMethod != null) {
            errorMsg = "Error while proxying to " + getMethodDescription(httpMethod);
        } else {
            errorMsg = "Error while proxing to " + (isGet ? "GET[" : "POST[") + url + "]";
        }
        logger.error(errorMsg, e);
        throw new HttpProxyException(errorMsg, e);
    } finally {
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }
}
Also used : HttpProxyException(org.craftercms.engine.exception.HttpProxyException) IOException(java.io.IOException) HttpProxyException(org.craftercms.engine.exception.HttpProxyException)

Example 2 with HttpProxyException

use of org.craftercms.engine.exception.HttpProxyException in project engine by craftercms.

the class HttpProxyImpl method proxyRequest.

protected void proxyRequest(String url, boolean isGet, HttpServletRequest request, HttpServletResponse response) throws HttpProxyException {
    String targetUrl = createTargetUrl(url, request);
    HttpRequestBase httpRequest = null;
    CloseableHttpResponse httpResponse = null;
    try {
        if (isGet) {
            httpRequest = createGetRequest(targetUrl, request);
        } else {
            httpRequest = createPostRequest(targetUrl, request);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Proxying to " + getRequestDescription(httpRequest));
        }
        httpResponse = httpClient.execute(httpRequest);
        response.setStatus(httpResponse.getStatusLine().getStatusCode());
        String responseBody = IOUtils.toString(httpResponse.getEntity().getContent());
        if (httpResponse.getStatusLine().getStatusCode() >= 400 && logger.isDebugEnabled()) {
            logger.debug("Received error response from " + getRequestDescription(httpRequest) + ": status = " + httpResponse.getStatusLine().getReasonPhrase() + ", response body = \n" + responseBody);
        }
        copyActualResponseHeaders(httpRequest, response);
        copyActualResponseBody(responseBody, response);
    } catch (Exception e) {
        String errorMsg;
        if (httpRequest != null) {
            errorMsg = "Error while proxying to " + getRequestDescription(httpRequest);
        } else {
            errorMsg = "Error while proxing to " + (isGet ? "GET[" : "POST[") + targetUrl + "]";
        }
        logger.error(errorMsg, e);
        throw new HttpProxyException(errorMsg, e);
    } finally {
        if (httpRequest != null) {
            httpRequest.releaseConnection();
        }
    }
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpProxyException(org.craftercms.engine.exception.HttpProxyException) IOException(java.io.IOException) HttpProxyException(org.craftercms.engine.exception.HttpProxyException)

Aggregations

IOException (java.io.IOException)2 HttpProxyException (org.craftercms.engine.exception.HttpProxyException)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1