Search in sources :

Example 1 with DefaultConnectionReuseStrategy

use of org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy in project geo-platform by geosdi.

the class GPServerProxy method doPost.

/**
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.log("@@@@@@@@@@@@@@@@@@@@@Called " + this.getClass().getSimpleName() + " {}#doPost.");
    try {
        if ((request.getParameter("targetURL") != null) && (request.getParameter("targetURL") != "")) {
            CloseableHttpClient httpClient = HttpClients.custom().setConnectionReuseStrategy(new DefaultConnectionReuseStrategy()).setConnectionManager(createClientConnectionManager()).setDefaultCredentialsProvider(new BasicCredentialsProvider()).build();
            String targetUrl = request.getParameter("targetURL");
            URIBuilder uriBuilder = new URIBuilder(targetUrl);
            Enumeration<String> parametersName = request.getParameterNames();
            while (parametersName.hasMoreElements()) {
                String requestParamName = parametersName.nextElement();
                switch(requestParamName) {
                    case "targetURL":
                    case "v":
                    case "p":
                        break;
                    default:
                        uriBuilder.addParameter(requestParamName, request.getParameter(requestParamName));
                }
            }
            URI uri = uriBuilder.build();
            this.log("############################URI to call : " + uri.toString());
            HttpPost httpPost = new HttpPost(uri);
            if (request.getHeader("Content-Type") != null) {
                httpPost.setHeader("Content-Type", request.getContentType());
            }
            if (((request.getParameter("v") != null) && (request.getParameter("v") != "")) && ((request.getParameter("p") != null) && (request.getParameter("p") != ""))) {
                String userName = request.getParameter("v");
                String password = request.getParameter("p");
                String userpass = userName + ":" + password;
                this.log("@@@@@@@@@@@@@@@@@Trying to inject basicAuth with parameter : " + userpass);
                String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userpass.getBytes()));
                httpPost.setHeader(HttpHeaders.AUTHORIZATION, basicAuth);
            }
            int contentLength = request.getContentLength();
            if (contentLength > 0) {
                httpPost.setEntity(new StringEntity(new BufferedReader(new InputStreamReader(request.getInputStream(), UTF_8)).lines().collect(joining("\n"))));
            }
            CloseableHttpResponse httpClientResponse = httpClient.execute(httpPost);
            this.log("###########################STATUS_CODE : " + httpClientResponse.getCode());
            if (httpClientResponse.getCode() != 200) {
                String exceptionMessage = IOUtils.toString(httpClientResponse.getEntity().getContent(), UTF_8);
                this.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ExecutionError : " + exceptionMessage);
                response.setStatus(500);
            } else {
                OutputStream ostream = response.getOutputStream();
                copy(httpClientResponse.getEntity().getContent(), ostream);
                this.log("#########################EXECUTE SUCCESS for POST.");
            }
        }
    } catch (Exception ex) {
        response.setStatus(500);
        ex.printStackTrace();
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) DefaultConnectionReuseStrategy(org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy) URI(java.net.URI) ServletException(javax.servlet.ServletException) URIBuilder(org.apache.hc.core5.net.URIBuilder) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)

Example 2 with DefaultConnectionReuseStrategy

use of org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy in project geo-platform by geosdi.

the class GPServerProxy method doGet.

/**
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.log("@@@@@@@@@@@@@@@@@@@@@Called " + this.getClass().getSimpleName() + "#doGet.");
    try {
        if ((request.getParameter("targetURL") != null) && (request.getParameter("targetURL") != "")) {
            CloseableHttpClient httpClient = HttpClients.custom().setConnectionReuseStrategy(new DefaultConnectionReuseStrategy()).setConnectionManager(createClientConnectionManager()).setDefaultCredentialsProvider(new BasicCredentialsProvider()).build();
            String targetUrl = request.getParameter("targetURL");
            URIBuilder uriBuilder = new URIBuilder(targetUrl);
            Enumeration<String> parametersName = request.getParameterNames();
            while (parametersName.hasMoreElements()) {
                String requestParamName = parametersName.nextElement();
                switch(requestParamName) {
                    case "targetURL":
                    case "v":
                    case "p":
                        break;
                    default:
                        uriBuilder.addParameter(requestParamName, request.getParameter(requestParamName));
                }
            }
            URI uri = uriBuilder.build();
            this.log("############################URI to call : " + uri.toString());
            HttpGet httpGet = new HttpGet(uri);
            if (((request.getParameter("v") != null) && (request.getParameter("v") != "")) && ((request.getParameter("p") != null) && (request.getParameter("p") != ""))) {
                String userName = request.getParameter("v");
                String password = request.getParameter("p");
                String userpass = userName + ":" + password;
                this.log("@@@@@@@@@@@@@@@@@Trying to inject basicAuth with parameter : " + userpass);
                String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userpass.getBytes()));
                httpGet.setHeader(HttpHeaders.AUTHORIZATION, basicAuth);
            }
            CloseableHttpResponse httpClientResponse = httpClient.execute(httpGet);
            this.log("###########################STATUS_CODE : " + httpClientResponse.getCode());
            if (httpClientResponse.getCode() != 200) {
                String exceptionMessage = IOUtils.toString(httpClientResponse.getEntity().getContent(), UTF_8);
                this.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ExecutionError : " + exceptionMessage);
                response.setStatus(500);
            } else {
                OutputStream ostream = response.getOutputStream();
                copy(httpClientResponse.getEntity().getContent(), ostream);
                this.log("#########################EXECUTE SUCCESS for GET.");
            }
        }
    } catch (Exception ex) {
        response.setStatus(500);
        ex.printStackTrace();
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) DefaultConnectionReuseStrategy(org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) URI(java.net.URI) ServletException(javax.servlet.ServletException) URIBuilder(org.apache.hc.core5.net.URIBuilder)

Aggregations

URI (java.net.URI)2 ServletException (javax.servlet.ServletException)2 BasicCredentialsProvider (org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider)2 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)2 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)2 DefaultConnectionReuseStrategy (org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy)2 URIBuilder (org.apache.hc.core5.net.URIBuilder)2 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)1 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)1 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)1