aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRam Koya <rk541m@att.com>2018-08-29 13:55:39 +0000
committerGerrit Code Review <gerrit@onap.org>2018-08-29 13:55:39 +0000
commita38724b18d1b0382966a845469caf0668a33af1c (patch)
treed424327dbc73bf71d0b08f2e90824af125ae7a3b
parent8a27631d961de4dfcace31a0107a701b735f6959 (diff)
parent27bba9734e136e06c3562767c1c54b330cd49f75 (diff)
Merge "Fixed Blocker Sonar issues in ProxyServlet.java"
-rw-r--r--datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/ProxyServlet.java65
1 files changed, 34 insertions, 31 deletions
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/ProxyServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/ProxyServlet.java
index 6564e175..c560299a 100644
--- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/ProxyServlet.java
+++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/ProxyServlet.java
@@ -181,7 +181,7 @@ public class ProxyServlet extends BaseServlet {
if (inited) {
String url = buildUrl(req);
intlogger.info("ProxyServlet: proxying with fallback GET "+url);
- AbstractHttpClient httpclient = new DefaultHttpClient();
+ try(AbstractHttpClient httpclient = new DefaultHttpClient()){
HttpRequestBase proxy = new HttpGet(url);
try {
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
@@ -204,6 +204,7 @@ public class ProxyServlet extends BaseServlet {
in.close();
}
rv = true;
+
} catch (IOException e) {
System.err.println("ProxyServlet: "+e);
e.printStackTrace();
@@ -211,6 +212,7 @@ public class ProxyServlet extends BaseServlet {
proxy.releaseConnection();
httpclient.getConnectionManager().shutdown();
}
+ }
} else {
intlogger.warn("ProxyServlet: proxy disabled");
}
@@ -220,41 +222,42 @@ public class ProxyServlet extends BaseServlet {
if (inited && isProxyServer()) {
String url = buildUrl(req);
intlogger.info("ProxyServlet: proxying "+method + " "+url);
- AbstractHttpClient httpclient = new DefaultHttpClient();
- ProxyHttpRequest proxy = new ProxyHttpRequest(method, url);
- try {
- httpclient.getConnectionManager().getSchemeRegistry().register(sch);
+ try(AbstractHttpClient httpclient = new DefaultHttpClient()) {
+ ProxyHttpRequest proxy = new ProxyHttpRequest(method, url);
+ try {
+ httpclient.getConnectionManager().getSchemeRegistry().register(sch);
- // Copy request headers and request body
- copyRequestHeaders(req, proxy);
- if (method.equals("POST") || method.equals("PUT")){
- BasicHttpEntity body = new BasicHttpEntity();
- body.setContent(req.getInputStream());
- body.setContentLength(-1); // -1 = unknown
- proxy.setEntity(body);
- }
+ // Copy request headers and request body
+ copyRequestHeaders(req, proxy);
+ if (method.equals("POST") || method.equals("PUT")) {
+ BasicHttpEntity body = new BasicHttpEntity();
+ body.setContent(req.getInputStream());
+ body.setContentLength(-1); // -1 = unknown
+ proxy.setEntity(body);
+ }
- // Execute the request
- HttpResponse pxy_response = httpclient.execute(proxy);
+ // Execute the request
+ HttpResponse pxy_response = httpclient.execute(proxy);
- // Get response headers and body
- int code = pxy_response.getStatusLine().getStatusCode();
- resp.setStatus(code);
- copyResponseHeaders(pxy_response, resp);
+ // Get response headers and body
+ int code = pxy_response.getStatusLine().getStatusCode();
+ resp.setStatus(code);
+ copyResponseHeaders(pxy_response, resp);
- HttpEntity entity = pxy_response.getEntity();
- if (entity != null) {
- InputStream in = entity.getContent();
- IOUtils.copy(in, resp.getOutputStream());
- in.close();
+ HttpEntity entity = pxy_response.getEntity();
+ if (entity != null) {
+ InputStream in = entity.getContent();
+ IOUtils.copy(in, resp.getOutputStream());
+ in.close();
+ }
+ } catch (IOException e) {
+ intlogger.warn("ProxyServlet: " + e);
+ resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
+ e.printStackTrace();
+ } finally {
+ proxy.releaseConnection();
+ httpclient.getConnectionManager().shutdown();
}
- } catch (IOException e) {
- intlogger.warn("ProxyServlet: "+e);
- resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
- e.printStackTrace();
- } finally {
- proxy.releaseConnection();
- httpclient.getConnectionManager().shutdown();
}
} else {
intlogger.warn("ProxyServlet: proxy disabled");