From 27bba9734e136e06c3562767c1c54b330cd49f75 Mon Sep 17 00:00:00 2001 From: Amaresh Kumar Date: Wed, 29 Aug 2018 18:50:48 +0530 Subject: Fixed Blocker Sonar issues in ProxyServlet.java Fixed 2 Sonar blocker issues in proxyServelet.java https://sonar.onap.org/project/issues?id=org.onap.dmaap.datarouter%3Aparent&open=AWHpc14DRGy6eclHCoS-&resolved=false&severities=BLOCKER&types=BUG https://sonar.onap.org/project/issues?id=org.onap.dmaap.datarouter%3Aparent&open=AWHpc14DRGy6eclHCoS9&resolved=false&severities=BLOCKER&types=BUG Issue-ID: DMAAP-673 Change-Id: Iafa0e0cfedb3fbb29b202d069c2bc0c90d962bfa Signed-off-by: Amaresh Kumar --- .../datarouter/provisioning/ProxyServlet.java | 65 +++++++++++----------- 1 file changed, 34 insertions(+), 31 deletions(-) (limited to 'datarouter-prov') 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"); -- cgit 1.2.3-korg