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();
}
}
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();
}
}
Aggregations