diff options
Diffstat (limited to 'datarouter-prov/src/main/java')
20 files changed, 565 insertions, 370 deletions
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/BaseServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/BaseServlet.java index 047043b1..3838cadf 100755 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/BaseServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/BaseServlet.java @@ -121,7 +121,6 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { private static final int DEFAULT_POKETIMER2 = 30; private static final String DEFAULT_DOMAIN = "onap"; private static final String DEFAULT_PROVSRVR_NAME = "dmaap-dr-prov"; - private static final String RESEARCH_SUBNET = "10.42.0.0/16"; private static final String STATIC_ROUTING_NODES = ""; //Adding new param for static Routing - Rally:US664862-1610 /** @@ -139,11 +138,11 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { /** * The set of authorized addresses and networks; pulled from the DB (PROV_AUTH_ADDRESSES) */ - private static Set<String> authorizedAddressesAndNetworks = new HashSet<String>(); + private static Set<String> authorizedAddressesAndNetworks = new HashSet<>(); /** * The set of authorized names; pulled from the DB (PROV_AUTH_SUBJECTS) */ - private static Set<String> authorizedNames = new HashSet<String>(); + private static Set<String> authorizedNames = new HashSet<>(); /** * The FQDN of the initially "active" provisioning server in this Data Router ecosystem */ @@ -199,19 +198,13 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { /** * The standard FQDN of the provisioning server in this Data Router ecosystem */ - public static String provName = "feeds-drtr.web.att.com"; + private static String provName = "feeds-drtr.web.att.com"; + /** * The standard FQDN of the ACTIVE provisioning server in this Data Router ecosystem */ - public static String activeProvName = "feeds-drtr.web.att.com"; - /** - * Special subnet that is allowed access to /internal - */ - private static String researchSubnet = RESEARCH_SUBNET; - /** - * Special subnet that is allowed access to /internal to Lab Machine - */ - private static String researchSubnet1 = RESEARCH_SUBNET; + private static String activeProvName = "feeds-drtr.web.att.com"; + private static String staticRoutingNodes = STATIC_ROUTING_NODES; //Adding new param for static Routing - Rally:US664862-1610 /** @@ -389,13 +382,6 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { if (loopback != null && ip.equals(loopback)) { return true; } - // Also allow the "special subnet" access - if (addressMatchesNetwork(ip, researchSubnet1)) { - return true; - } - if (addressMatchesNetwork(ip, researchSubnet)) { - return true; - } } catch (UnknownHostException e) { // ignore } @@ -473,7 +459,6 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { provDomain = getString(map, Parameters.PROV_DOMAIN, DEFAULT_DOMAIN); provName = getString(map, Parameters.PROV_NAME, DEFAULT_PROVSRVR_NAME); activeProvName = getString(map, Parameters.PROV_ACTIVE_NAME, provName); - researchSubnet = getString(map, Parameters.PROV_SPECIAL_SUBNET, RESEARCH_SUBNET); staticRoutingNodes = getString(map, Parameters.STATIC_ROUTING_NODES, ""); //Adding new param for static Routing - Rally:US664862-1610 initialActivePod = getString(map, Parameters.ACTIVE_POD, ""); @@ -630,6 +615,13 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { } } + public static String getProvName() { + return provName; + } + + public static String getActiveProvName() { + return activeProvName; + } /** * Get an array of all node names in the DR network. @@ -784,7 +776,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { } private static Set<String> getSet(Map<String, String> map, String name) { - Set<String> set = new HashSet<String>(); + Set<String> set = new HashSet<>(); String s = map.get(name); if (s != null) { String[] pp = s.split("\\|"); @@ -807,7 +799,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { public class ContentHeader { private String type = ""; - private Map<String, String> map = new HashMap<String, String>(); + private Map<String, String> map = new HashMap<>(); ContentHeader() { this("", "1.0"); diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/DRFeedsServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/DRFeedsServlet.java index 47156d28..d7da4dc1 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/DRFeedsServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/DRFeedsServlet.java @@ -41,6 +41,8 @@ import org.onap.dmaap.datarouter.provisioning.utils.JSONUtilities; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError; + /** * This servlet handles provisioning for the <drFeedsURL> which is the URL on the provisioning server used to * create new feeds. It supports POST to create new feeds, and GET to support the Feeds Collection Query function. @@ -59,7 +61,7 @@ public class DRFeedsServlet extends ProxyServlet { * DELETE on the <drFeedsURL> -- not supported. */ @Override - public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doDelete(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doDelete"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); String message = "DELETE not allowed for the drFeedsURL."; @@ -67,7 +69,7 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message); + sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); } /** @@ -75,7 +77,7 @@ public class DRFeedsServlet extends ProxyServlet { * Queries</i> section in the <b>Provisioning API</b> document for details on how this method should be invoked. */ @Override - public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doGet(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doGet"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); EventLogRecord elr = new EventLogRecord(req); @@ -84,11 +86,15 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } if (isProxyServer()) { - super.doGet(req, resp); + try { + super.doGet(req, resp); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } return; } String bhdr = req.getHeader(BEHALF_HEADER); @@ -97,7 +103,7 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } String path = req @@ -107,7 +113,7 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_NOT_FOUND); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_NOT_FOUND, message); + sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, message, eventlogger); return; } // Check with the Authorizer @@ -117,7 +123,7 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } @@ -133,14 +139,18 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); } else { // send response elr.setResult(HttpServletResponse.SC_OK); eventlogger.info(elr); resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(FEEDFULL_CONTENT_TYPE); - resp.getOutputStream().print(feed.asJSONObject(true).toString()); + try { + resp.getOutputStream().print(feed.asJSONObject(true).toString()); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } } } else { // Display a list of URLs @@ -160,7 +170,11 @@ public class DRFeedsServlet extends ProxyServlet { eventlogger.info(elr); resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(FEEDLIST_CONTENT_TYPE); - resp.getOutputStream().print(t); + try { + resp.getOutputStream().print(t); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } } } @@ -168,7 +182,7 @@ public class DRFeedsServlet extends ProxyServlet { * PUT on the <drFeedsURL> -- not supported. */ @Override - public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doPut(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doPut"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); String message = "PUT not allowed for the drFeedsURL."; @@ -176,7 +190,7 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message); + sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); } /** @@ -184,7 +198,7 @@ public class DRFeedsServlet extends ProxyServlet { * <b>Provisioning API</b> document for details on how this method should be invoked. */ @Override - public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doPost(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doPost"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER)); EventLogRecord elr = new EventLogRecord(req); @@ -193,11 +207,15 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } if (isProxyServer()) { - super.doPost(req, resp); + try { + super.doPost(req, resp); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } return; } String bhdr = req.getHeader(BEHALF_HEADER); @@ -206,7 +224,7 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } String path = req @@ -216,7 +234,7 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_NOT_FOUND); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_NOT_FOUND, message); + sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, message, eventlogger); return; } // check content type is FEED_CONTENT_TYPE, version 1.0 @@ -227,7 +245,7 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message); + sendResponseError(resp, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message, eventlogger); return; } // Check with the Authorizer @@ -237,7 +255,7 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } JSONObject jo = getJSONfromInput(req); @@ -246,7 +264,7 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } if (intlogger.isDebugEnabled()) { @@ -258,7 +276,7 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_CONFLICT); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_CONFLICT, message); + sendResponseError(resp, HttpServletResponse.SC_CONFLICT, message, eventlogger); return; } Feed feed = null; @@ -269,7 +287,7 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } feed.setPublisher(bhdr); // set from X-ATT-DR-ON-BEHALF-OF header @@ -281,7 +299,7 @@ public class DRFeedsServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } @@ -293,13 +311,17 @@ public class DRFeedsServlet extends ProxyServlet { resp.setStatus(HttpServletResponse.SC_CREATED); resp.setContentType(FEEDFULL_CONTENT_TYPE); resp.setHeader("Location", feed.getLinks().getSelf()); - resp.getOutputStream().print(feed.asLimitedJSONObject().toString()); + try { + resp.getOutputStream().print(feed.asLimitedJSONObject().toString()); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } provisioningDataChanged(); } else { // Something went wrong with the INSERT elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG); + sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, eventlogger); } } } diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/FeedServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/FeedServlet.java index 3f8929e7..d2452e7d 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/FeedServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/FeedServlet.java @@ -30,6 +30,7 @@ import java.io.InvalidObjectException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.json.JSONException; import org.json.JSONObject; import org.onap.dmaap.datarouter.authz.AuthorizationResponse; import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord; @@ -39,6 +40,8 @@ import org.onap.dmaap.datarouter.provisioning.eelf.EelfMsgs; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError; + /** * This servlet handles provisioning for the <feedURL> which is generated by the provisioning * server to handle a particular feed. It supports DELETE to mark the feed as deleted, @@ -59,7 +62,7 @@ public class FeedServlet extends ProxyServlet { * document for details on how this method should be invoked. */ @Override - public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doDelete(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doDelete"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); EventLogRecord elr = new EventLogRecord(req); @@ -68,11 +71,15 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } if (isProxyServer()) { - super.doDelete(req, resp); + try { + super.doDelete(req, resp); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } return; } String bhdr = req.getHeader(BEHALF_HEADER); @@ -81,7 +88,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } int feedid = getIdFromPath(req); @@ -90,7 +97,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } Feed feed = Feed.getFeedById(feedid); @@ -99,7 +106,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_NOT_FOUND); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_NOT_FOUND, message); + sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, message, eventlogger); return; } // Check with the Authorizer @@ -109,7 +116,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } @@ -126,7 +133,7 @@ public class FeedServlet extends ProxyServlet { // Something went wrong with the UPDATE elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG); + sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, eventlogger); } } /** @@ -135,7 +142,7 @@ public class FeedServlet extends ProxyServlet { * document for details on how this method should be invoked. */ @Override - public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doGet(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doGet"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); EventLogRecord elr = new EventLogRecord(req); @@ -144,11 +151,15 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } if (isProxyServer()) { - super.doGet(req, resp); + try { + super.doGet(req, resp); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } return; } String bhdr = req.getHeader(BEHALF_HEADER); @@ -157,7 +168,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } int feedid = getIdFromPath(req); @@ -166,7 +177,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } Feed feed = Feed.getFeedById(feedid); @@ -175,7 +186,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_NOT_FOUND); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_NOT_FOUND, message); + sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, message, eventlogger); return; } // Check with the Authorizer @@ -185,7 +196,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } @@ -194,7 +205,11 @@ public class FeedServlet extends ProxyServlet { eventlogger.info(elr); resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(FEEDFULL_CONTENT_TYPE); - resp.getOutputStream().print(feed.asJSONObject(true).toString()); + try { + resp.getOutputStream().print(feed.asJSONObject(true).toString()); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } } /** * PUT on the <feedURL> for a feed. @@ -202,7 +217,7 @@ public class FeedServlet extends ProxyServlet { * document for details on how this method should be invoked. */ @Override - public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doPut(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doPut"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); EventLogRecord elr = new EventLogRecord(req); @@ -211,11 +226,15 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } if (isProxyServer()) { - super.doPut(req, resp); + try { + super.doPut(req, resp); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } return; } String bhdr = req.getHeader(BEHALF_HEADER); @@ -224,7 +243,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } int feedid = getIdFromPath(req); @@ -233,7 +252,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } Feed oldFeed = Feed.getFeedById(feedid); @@ -242,7 +261,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_NOT_FOUND); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_NOT_FOUND, message); + sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, message, eventlogger); return; } // check content type is FEED_CONTENT_TYPE, version 1.0 @@ -253,7 +272,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message); + sendResponseError(resp, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message, eventlogger); return; } JSONObject jo = getJSONfromInput(req); @@ -262,7 +281,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } if (intlogger.isDebugEnabled()) @@ -275,7 +294,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } feed.setFeedid(feedid); @@ -287,7 +306,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } if (!oldFeed.getName().equals(feed.getName())) { @@ -295,7 +314,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } if (!oldFeed.getVersion().equals(feed.getVersion())) { @@ -303,7 +322,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } // Check with the Authorizer @@ -313,7 +332,7 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } @@ -324,15 +343,23 @@ public class FeedServlet extends ProxyServlet { eventlogger.info(elr); resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(FEEDFULL_CONTENT_TYPE); - resp.getOutputStream().print(feed.asLimitedJSONObject().toString()); + try { + resp.getOutputStream().print(feed.asLimitedJSONObject().toString()); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } /**Change Owner ship of Feed //Adding for group feature:Rally US708115*/ if (jo.has("changeowner") && subjectgroup != null) { - Boolean changeowner = (Boolean) jo.get("changeowner"); - if (changeowner != null && changeowner.equals(true)) { - feed.setPublisher(req.getHeader(BEHALF_HEADER)); - feed.changeOwnerShip(); + try { + Boolean changeowner = (Boolean) jo.get("changeowner"); + if (changeowner != null && changeowner.equals(true)) { + feed.setPublisher(req.getHeader(BEHALF_HEADER)); + feed.changeOwnerShip(); + } + } catch (JSONException je) { + eventlogger.error("JSONException" + je.getMessage()); } } /***End of change ownership*/ @@ -342,14 +369,14 @@ public class FeedServlet extends ProxyServlet { // Something went wrong with the UPDATE elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG); + sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, eventlogger); } } /** * POST on the <feedURL> -- not supported. */ @Override - public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doPost(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doPost"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER)); String message = "POST not allowed for the feedURL."; @@ -357,6 +384,6 @@ public class FeedServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message); + sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); } } diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/GroupServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/GroupServlet.java index 8537ff4b..96a9924f 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/GroupServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/GroupServlet.java @@ -26,18 +26,15 @@ package org.onap.dmaap.datarouter.provisioning; import java.io.IOException; import java.io.InvalidObjectException; -import java.util.Collection; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONObject; -import org.onap.dmaap.datarouter.authz.AuthorizationResponse; -import org.onap.dmaap.datarouter.provisioning.BaseServlet.ContentHeader; import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord; import org.onap.dmaap.datarouter.provisioning.beans.Group; -import org.onap.dmaap.datarouter.provisioning.beans.Subscription; -import org.onap.dmaap.datarouter.provisioning.utils.JSONUtilities; + +import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError; /** * This servlet handles provisioning for the <groups> which is generated by the provisioning @@ -53,13 +50,13 @@ public class GroupServlet extends ProxyServlet { * DELETE on the <GRUPS> -- not supported. */ @Override - public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doDelete(HttpServletRequest req, HttpServletResponse resp) { String message = "DELETE not allowed for the GROUPS."; EventLogRecord elr = new EventLogRecord(req); elr.setMessage(message); elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message); + sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); } /** * GET on the the list of groups to a feed/sub. @@ -67,18 +64,22 @@ public class GroupServlet extends ProxyServlet { * document for details on how this method should be invoked. */ @Override - public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doGet(HttpServletRequest req, HttpServletResponse resp) { EventLogRecord elr = new EventLogRecord(req); String message = isAuthorizedForProvisioning(req); if (message != null) { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } if (isProxyServer()) { - super.doGet(req, resp); + try { + super.doGet(req, resp); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } return; } String bhdr = req.getHeader(BEHALF_HEADER); @@ -87,7 +88,7 @@ public class GroupServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } @@ -122,7 +123,7 @@ public class GroupServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } @@ -132,7 +133,11 @@ public class GroupServlet extends ProxyServlet { eventlogger.info(elr); resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(GROUPFULL_CONTENT_TYPE); - resp.getOutputStream().print(gup.asJSONObject().toString()); + try { + resp.getOutputStream().print(gup.asJSONObject().toString()); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } // Display a list of Groups /*Collection<Group> list = Group.getGroupById(groupid); @@ -149,18 +154,22 @@ public class GroupServlet extends ProxyServlet { * PUT on the <GROUPS> -- not supported. */ @Override - public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doPut(HttpServletRequest req, HttpServletResponse resp) { EventLogRecord elr = new EventLogRecord(req); String message = isAuthorizedForProvisioning(req); if (message != null) { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } if (isProxyServer()) { - super.doPut(req, resp); + try { + super.doPut(req, resp); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } return; } String bhdr = req.getHeader(BEHALF_HEADER); @@ -169,7 +178,7 @@ public class GroupServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } int groupid = getIdFromPath(req); @@ -178,7 +187,7 @@ public class GroupServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } Group oldgup = Group.getGroupById(groupid); @@ -187,7 +196,7 @@ public class GroupServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_NOT_FOUND); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_NOT_FOUND, message); + sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, message, eventlogger); return; } // Check with the Authorizer @@ -208,7 +217,7 @@ public class GroupServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message); + sendResponseError(resp, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message, eventlogger); return; } JSONObject jo = getJSONfromInput(req); @@ -217,12 +226,12 @@ public class GroupServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } if (intlogger.isDebugEnabled()) intlogger.debug(jo.toString()); - Group gup = null; + Group gup; try { gup = new Group(jo); } catch (InvalidObjectException e) { @@ -230,17 +239,15 @@ public class GroupServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } gup.setGroupid(oldgup.getGroupid()); - - Group gb2 = Group.getGroupMatching(gup, oldgup.getGroupid()); if (gb2 != null) { eventlogger.warn("PROV0011 Creating a duplicate Group: "+gup.getName()); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Duplicate Group:"+gup.getName()); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, "Duplicate Group:"+gup.getName(), eventlogger); return; } @@ -251,13 +258,17 @@ public class GroupServlet extends ProxyServlet { eventlogger.info(elr); resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(GROUPFULL_CONTENT_TYPE); - resp.getOutputStream().print(gup.asJSONObject().toString()); + try { + resp.getOutputStream().print(gup.asJSONObject().toString()); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } provisioningDataChanged(); } else { // Something went wrong with the UPDATE elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG); + sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, eventlogger); } } /** @@ -266,18 +277,22 @@ public class GroupServlet extends ProxyServlet { * document for details on how this method should be invoked. */ @Override - public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doPost(HttpServletRequest req, HttpServletResponse resp) { EventLogRecord elr = new EventLogRecord(req); String message = isAuthorizedForProvisioning(req); if (message != null) { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } if (isProxyServer()) { - super.doPost(req, resp); + try { + super.doPost(req, resp); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } return; } String bhdr = req.getHeader(BEHALF_HEADER); @@ -286,7 +301,7 @@ public class GroupServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } /*int feedid = getIdFromPath(req); @@ -327,7 +342,7 @@ public class GroupServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message); + sendResponseError(resp, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message, eventlogger); return; } JSONObject jo = getJSONfromInput(req); @@ -336,13 +351,13 @@ public class GroupServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } if (intlogger.isDebugEnabled()) intlogger.debug(jo.toString()); - Group gup = null; + Group gup; try { gup = new Group(jo); } catch (InvalidObjectException e) { @@ -350,7 +365,7 @@ public class GroupServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } //gup.setFeedid(feedid); @@ -361,7 +376,7 @@ public class GroupServlet extends ProxyServlet { if (gb2 != null) { eventlogger.warn("PROV0011 Creating a duplicate Group: "+gup.getName()); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Duplicate Group:"+gup.getName()); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, "Duplicate Group:"+gup.getName(), eventlogger); return; } @@ -373,13 +388,17 @@ public class GroupServlet extends ProxyServlet { eventlogger.info(elr); resp.setStatus(HttpServletResponse.SC_CREATED); resp.setContentType(GROUPFULL_CONTENT_TYPE); - resp.getOutputStream().print(gup.asJSONObject().toString()); + try { + resp.getOutputStream().print(gup.asJSONObject().toString()); + } catch (IOException ioe) { + eventlogger.error("IOException" + ioe.getMessage()); + } provisioningDataChanged(); } else { // Something went wrong with the INSERT elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG); + sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, eventlogger); } } } diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/InternalServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/InternalServlet.java index 61845cef..685d5528 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/InternalServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/InternalServlet.java @@ -51,6 +51,8 @@ import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError; + /** * <p> * This servlet handles requests to URLs under /internal on the provisioning server. These include: @@ -154,6 +156,7 @@ import com.att.eelf.configuration.EELFManager; @SuppressWarnings("serial") public class InternalServlet extends ProxyServlet { + private static final Object lock = new Object(); private static Integer logseq = 0; // another piece of info to make log spool file names unique //Adding EELF Logger Rally:US664892 private static EELFLogger eelflogger = EELFManager.getInstance() @@ -164,7 +167,7 @@ public class InternalServlet extends ProxyServlet { * details on how this method should be invoked. */ @Override - public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doDelete(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doDelete"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); EventLogRecord elr = new EventLogRecord(req); @@ -172,14 +175,18 @@ public class InternalServlet extends ProxyServlet { elr.setMessage("Unauthorized."); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized."); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, "Unauthorized.", eventlogger); return; } String path = req.getPathInfo(); if (path.startsWith("/api/")) { if (isProxyOK(req) && isProxyServer()) { - super.doDelete(req, resp); + try { + super.doDelete(req, resp); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } return; } String key = path.substring(5); @@ -196,13 +203,13 @@ public class InternalServlet extends ProxyServlet { // Something went wrong with the DELETE elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG); + sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, eventlogger); } return; } } } - resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL."); + sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Bad URL.", eventlogger); } /** @@ -210,14 +217,15 @@ public class InternalServlet extends ProxyServlet { * document for details on how this method should be invoked. */ @Override - public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doGet(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doGet"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); String path = req.getPathInfo(); + Properties props = (new DB()).getProperties(); if (path.equals("/halt") && !req.isSecure()) { // request to halt the server - can ONLY come from localhost String remote = req.getRemoteAddr(); - if (remote.equals("127.0.0.1")) { + if (remote.equals(props.getProperty("org.onap.dmaap.datarouter.provserver.localhost"))) { intlogger.info("PROV0009 Request to HALT received."); resp.setStatus(HttpServletResponse.SC_OK); Main.shutdown(); @@ -233,7 +241,7 @@ public class InternalServlet extends ProxyServlet { elr.setMessage("Unauthorized."); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized."); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, "Unauthorized.", eventlogger); return; } if (path.equals("/fetchProv") && !req.isSecure()) { @@ -245,8 +253,12 @@ public class InternalServlet extends ProxyServlet { } if (path.equals("/prov")) { if (isProxyOK(req) && isProxyServer()) { - if (super.doGetWithFallback(req, resp)) { - return; + try { + if (super.doGetWithFallback(req, resp)) { + return; + } + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); } // fall back to returning the local data if the remote is unreachable intlogger.info("Active server unavailable; falling back to local copy."); @@ -254,18 +266,25 @@ public class InternalServlet extends ProxyServlet { Poker p = Poker.getPoker(); resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(PROVFULL_CONTENT_TYPE2); - resp.getOutputStream().print(p.getProvisioningString()); + try { + resp.getOutputStream().print(p.getProvisioningString()); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } return; } if (path.equals("/logs") || path.equals("/logs/")) { resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType("application/json"); - resp.getOutputStream().print(generateLogfileList().toString()); + try { + resp.getOutputStream().print(generateLogfileList().toString()); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } return; } if (path.startsWith("/logs/")) { - Properties p = (new DB()).getProperties(); - String logdir = p.getProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir"); + String logdir = props.getProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir"); String logfile = path.substring(6); if (logdir != null && logfile != null && logfile.indexOf('/') < 0) { File log = new File(logdir + "/" + logfile); @@ -273,16 +292,24 @@ public class InternalServlet extends ProxyServlet { resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType("text/plain"); Path logpath = Paths.get(log.getAbsolutePath()); - Files.copy(logpath, resp.getOutputStream()); + try { + Files.copy(logpath, resp.getOutputStream()); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } return; } } - resp.sendError(HttpServletResponse.SC_NO_CONTENT, "No file."); + sendResponseError(resp, HttpServletResponse.SC_NO_CONTENT, "No file.", eventlogger); return; } if (path.startsWith("/api/")) { if (isProxyOK(req) && isProxyServer()) { - super.doGet(req, resp); + try { + super.doGet(req, resp); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } return; } String key = path.substring(5); @@ -291,7 +318,11 @@ public class InternalServlet extends ProxyServlet { if (param != null) { resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType("text/plain"); - resp.getOutputStream().print(param.getValue() + "\n"); + try { + resp.getOutputStream().print(param.getValue() + "\n"); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } return; } } @@ -301,10 +332,14 @@ public class InternalServlet extends ProxyServlet { LogfileLoader lfl = LogfileLoader.getLoader(); resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType("text/plain"); - resp.getOutputStream().print(lfl.getBitSet().toString()); + try { + resp.getOutputStream().print(lfl.getBitSet().toString()); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } return; } - resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL."); + sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Bad URL.", eventlogger); } /** @@ -312,7 +347,7 @@ public class InternalServlet extends ProxyServlet { * details on how this method should be invoked. */ @Override - public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doPut(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doPut"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); EventLogRecord elr = new EventLogRecord(req); @@ -320,13 +355,17 @@ public class InternalServlet extends ProxyServlet { elr.setMessage("Unauthorized."); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized."); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, "Unauthorized.", eventlogger); return; } String path = req.getPathInfo(); if (path.startsWith("/api/")) { if (isProxyOK(req) && isProxyServer()) { - super.doPut(req, resp); + try { + super.doPut(req, resp); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } return; } String key = path.substring(5); @@ -345,13 +384,13 @@ public class InternalServlet extends ProxyServlet { // Something went wrong with the UPDATE elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG); + sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, eventlogger); } return; } } } - resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL."); + sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Bad URL.", eventlogger); } /** @@ -360,7 +399,7 @@ public class InternalServlet extends ProxyServlet { */ @SuppressWarnings("resource") @Override - public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doPost(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doPost"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER)); EventLogRecord elr = new EventLogRecord(req); @@ -368,14 +407,18 @@ public class InternalServlet extends ProxyServlet { elr.setMessage("Unauthorized."); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized."); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, "Unauthorized.", eventlogger); return; } String path = req.getPathInfo(); if (path.startsWith("/api/")) { if (isProxyOK(req) && isProxyServer()) { - super.doPost(req, resp); + try { + super.doPost(req, resp); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } return; } String key = path.substring(5); @@ -394,7 +437,7 @@ public class InternalServlet extends ProxyServlet { // Something went wrong with the INSERT elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG); + sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, eventlogger); } return; } @@ -412,7 +455,7 @@ public class InternalServlet extends ProxyServlet { } String spooldir = (new DB()).getProperties().getProperty("org.onap.dmaap.datarouter.provserver.spooldir"); String spoolname = String.format("%d-%d-", System.currentTimeMillis(), Thread.currentThread().getId()); - synchronized (logseq) { + synchronized (lock) { // perhaps unnecessary, but it helps make the name unique spoolname += logseq.toString(); logseq++; @@ -433,8 +476,12 @@ public class InternalServlet extends ProxyServlet { long total = 0; long avail = 0; for (FileStore store : fs.getFileStores()) { - total += store.getTotalSpace(); - avail += store.getUsableSpace(); + try { + total += store.getTotalSpace(); + avail += store.getUsableSpace(); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } } try { fs.close(); @@ -448,12 +495,16 @@ public class InternalServlet extends ProxyServlet { } Path tmppath = Paths.get(spooldir, spoolname); Path donepath = Paths.get(spooldir, "IN." + spoolname); - Files.copy(req.getInputStream(), Paths.get(spooldir, spoolname), StandardCopyOption.REPLACE_EXISTING); - Files.move(tmppath, donepath, StandardCopyOption.REPLACE_EXISTING); - elr.setResult(HttpServletResponse.SC_CREATED); - resp.setStatus(HttpServletResponse.SC_CREATED); - eventlogger.info(elr); - LogfileLoader.getLoader(); // This starts the logfile loader "task" + try { + Files.copy(req.getInputStream(), Paths.get(spooldir, spoolname), StandardCopyOption.REPLACE_EXISTING); + Files.move(tmppath, donepath, StandardCopyOption.REPLACE_EXISTING); + elr.setResult(HttpServletResponse.SC_CREATED); + resp.setStatus(HttpServletResponse.SC_CREATED); + eventlogger.info(elr); + LogfileLoader.getLoader(); // This starts the logfile loader "task" + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } return; } @@ -467,23 +518,27 @@ public class InternalServlet extends ProxyServlet { eventlogger.info(elr); return; } - InputStream is = req.getInputStream(); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - int ch = 0; - while ((ch = is.read()) >= 0) { - bos.write(ch); + try { + InputStream is = req.getInputStream(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + int ch; + while ((ch = is.read()) >= 0) { + bos.write(ch); + } + RLEBitSet bs = new RLEBitSet(bos.toString()); // The set of records to retrieve + elr.setResult(HttpServletResponse.SC_OK); + resp.setStatus(HttpServletResponse.SC_OK); + resp.setContentType("text/plain"); + LogRecord.printLogRecords(resp.getOutputStream(), bs); + eventlogger.info(elr); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); } - RLEBitSet bs = new RLEBitSet(bos.toString()); // The set of records to retrieve - elr.setResult(HttpServletResponse.SC_OK); - resp.setStatus(HttpServletResponse.SC_OK); - resp.setContentType("text/plain"); - LogRecord.printLogRecords(resp.getOutputStream(), bs); - eventlogger.info(elr); return; } elr.setResult(HttpServletResponse.SC_NOT_FOUND); - resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL."); + sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Bad URL.", eventlogger); eventlogger.info(elr); } diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Poker.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Poker.java index f2c511c1..563e6f7f 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Poker.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Poker.java @@ -64,6 +64,8 @@ public class Poker extends TimerTask { */ private static final String POKE_URL_TEMPLATE = "http://%s/internal/fetchProv"; + private static final Object lock = new Object(); + /** * This is a singleton -- there is only one Poker object in the server */ @@ -109,7 +111,7 @@ public class Poker extends TimerTask { * @param t2 the second timer set the outer bound on how long to wait. It cannot be reset. */ public void setTimers(long t1, long t2) { - synchronized (thisPod) { + synchronized (lock) { if (timer1 == 0 || t1 > timer1) { timer1 = t1; } @@ -144,7 +146,7 @@ public class Poker extends TimerTask { if (timer1 > 0) { long now = System.currentTimeMillis(); boolean fire = false; - synchronized (thisPod) { + synchronized (lock) { if (now > timer1 || now > timer2) { timer1 = timer2 = 0; fire = true; 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 8d6bfcf0..66a9d42b 100755 --- 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 @@ -109,8 +109,7 @@ public class ProxyServlet extends BaseServlet { try (FileInputStream instream = new FileInputStream(new File(store))) { ks.load(instream, pass.toCharArray()); } catch (FileNotFoundException fileNotFoundException) { - System.err.println("ProxyServlet: " + fileNotFoundException); - fileNotFoundException.printStackTrace(); + intlogger.error("ProxyServlet: " + fileNotFoundException.getMessage()); } catch (Exception x) { System.err.println("READING TRUSTSTORE: " + x); } diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/PublishServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/PublishServlet.java index d500bb31..05502760 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/PublishServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/PublishServlet.java @@ -64,6 +64,7 @@ public class PublishServlet extends BaseServlet { private List<IngressRoute> irt; //Adding EELF Logger Rally:US664892 private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.PublishServlet"); + private static final Object lock = new Object(); @Override @@ -138,7 +139,7 @@ public class PublishServlet extends BaseServlet { // Check to see if the IRT needs to be updated Poker p = Poker.getPoker(); String s = p.getProvisioningString(); - synchronized (provstring) { + synchronized (lock) { if (irt == null || (s.length() != provstring.length()) || !s.equals(provstring)) { // Provisioning string has changed -- update the IRT provstring = s; diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/RouteServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/RouteServlet.java index aa0d6f57..a9b22ec0 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/RouteServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/RouteServlet.java @@ -30,6 +30,7 @@ import java.util.Set; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.json.JSONException;
import org.json.JSONObject;
import org.onap.dmaap.datarouter.provisioning.beans.Deleteable;
import org.onap.dmaap.datarouter.provisioning.beans.EgressRoute;
@@ -39,6 +40,8 @@ import org.onap.dmaap.datarouter.provisioning.beans.Insertable; import org.onap.dmaap.datarouter.provisioning.beans.NetworkRoute;
import org.onap.dmaap.datarouter.provisioning.beans.NodeClass;
+import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError;
+
/**
* <p>
* This servlet handles requests to URLs under /internal/route/ on the provisioning server.
@@ -130,17 +133,21 @@ public class RouteServlet extends ProxyServlet { * DELETE route table entries by deleting part of the route table tree.
*/
@Override
- public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ public void doDelete(HttpServletRequest req, HttpServletResponse resp) {
EventLogRecord elr = new EventLogRecord(req);
if (!isAuthorizedForInternal(req)) {
elr.setMessage("Unauthorized.");
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized.");
+ sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, "Unauthorized.", eventlogger);
return;
}
if (isProxyOK(req) && isProxyServer()) {
- super.doDelete(req, resp);
+ try {
+ super.doDelete(req, resp);
+ } catch (IOException ioe) {
+ eventlogger.error("IOException" + ioe.getMessage());
+ }
return;
}
@@ -154,12 +161,12 @@ public class RouteServlet extends ProxyServlet { int feedid = Integer.parseInt(parts[1]);
IngressRoute er = IngressRoute.getIngressRoute(feedid, parts[2], parts[3].replaceAll("!", "/"));
if (er == null) {
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, "The specified ingress route does not exist.");
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "The specified ingress route does not exist.", eventlogger);
return;
}
d = new Deleteable[] { er };
} catch (NumberFormatException e) {
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid feed ID in 'delete ingress' command.");
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Invalid feed ID in 'delete ingress' command.", eventlogger);
return;
}
} else if (parts.length == 2) {
@@ -169,11 +176,11 @@ public class RouteServlet extends ProxyServlet { Set<IngressRoute> set = IngressRoute.getIngressRoutesForSeq(seq);
d = set.toArray(new Deleteable[0]);
} catch (NumberFormatException e) {
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid sequence number in 'delete ingress' command.");
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Invalid sequence number in 'delete ingress' command.", eventlogger);
return;
}
} else {
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid number of arguments in 'delete ingress' command.");
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Invalid number of arguments in 'delete ingress' command.", eventlogger);
return;
}
} else if (parts[0].equals("egress")) {
@@ -183,16 +190,16 @@ public class RouteServlet extends ProxyServlet { int subid = Integer.parseInt(parts[1]);
EgressRoute er = EgressRoute.getEgressRoute(subid);
if (er == null) {
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, "The specified egress route does not exist.");
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "The specified egress route does not exist.", eventlogger);
return;
}
d = new Deleteable[] { er };
} catch (NumberFormatException e) {
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid sub ID in 'delete egress' command.");
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Invalid sub ID in 'delete egress' command.", eventlogger);
return;
}
} else {
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid number of arguments in 'delete egress' command.");
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Invalid number of arguments in 'delete egress' command.", eventlogger);
return;
}
} else if (parts[0].equals("network")) {
@@ -205,16 +212,16 @@ public class RouteServlet extends ProxyServlet { );
d = new Deleteable[] { nr };
} catch (IllegalArgumentException e) {
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, "The specified network route does not exist.");
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "The specified network route does not exist.", eventlogger);
return;
}
} else {
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid number of arguments in 'delete network' command.");
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Invalid number of arguments in 'delete network' command.", eventlogger);
return;
}
}
if (d == null) {
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Bad URL.", eventlogger);
return;
}
boolean rv = true;
@@ -231,24 +238,28 @@ public class RouteServlet extends ProxyServlet { // Something went wrong with the DELETE
elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG);
+ sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, eventlogger);
}
}
/**
* GET route table entries from the route table tree specified by the URL path.
*/
@Override
- public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ public void doGet(HttpServletRequest req, HttpServletResponse resp) {
EventLogRecord elr = new EventLogRecord(req);
if (!isAuthorizedForInternal(req)) {
elr.setMessage("Unauthorized.");
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized.");
+ sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, "Unauthorized.", eventlogger);
return;
}
if (isProxyOK(req) && isProxyServer()) {
- super.doGet(req, resp);
+ try {
+ super.doGet(req, resp);
+ } catch (IOException ioe) {
+ eventlogger.error("IOException" + ioe.getMessage());
+ }
return;
}
@@ -256,7 +267,7 @@ public class RouteServlet extends ProxyServlet { if (!path.endsWith("/"))
path += "/";
if (!path.equals("/") && !path.equals("/ingress/") && !path.equals("/egress/") && !path.equals("/network/")) {
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Bad URL.", eventlogger);
return;
}
@@ -283,7 +294,11 @@ public class RouteServlet extends ProxyServlet { for (String key : jx.keySet()) {
sb.append(pfx);
sb.append(" \"").append(key).append("\": ");
- sb.append("\"").append(jx.getString(key)).append("\"");
+ try {
+ sb.append("\"").append(jx.getString(key)).append("\"");
+ } catch (JSONException je) {
+ eventlogger.error("JSONException" + je.getMessage());
+ }
pfx = ",\n";
}
}
@@ -305,38 +320,46 @@ public class RouteServlet extends ProxyServlet { sb.append("}\n");
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("application/json");
- resp.getOutputStream().print(sb.toString());
+ try {
+ resp.getOutputStream().print(sb.toString());
+ } catch (IOException ioe) {
+ eventlogger.error("IOException" + ioe.getMessage());
+ }
}
/**
* PUT on </internal/route/*> -- not supported.
*/
@Override
- public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ public void doPut(HttpServletRequest req, HttpServletResponse resp) {
EventLogRecord elr = new EventLogRecord(req);
if (!isAuthorizedForInternal(req)) {
elr.setMessage("Unauthorized.");
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized.");
+ sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, "Unauthorized.", eventlogger);
return;
}
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Bad URL.", eventlogger);
}
/**
* POST - modify existing route table entries in the route table tree specified by the URL path.
*/
@Override
- public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ public void doPost(HttpServletRequest req, HttpServletResponse resp) {
EventLogRecord elr = new EventLogRecord(req);
if (!isAuthorizedForInternal(req)) {
elr.setMessage("Unauthorized.");
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized.");
+ sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, "Unauthorized.", eventlogger);
return;
}
if (isProxyOK(req) && isProxyServer()) {
- super.doPost(req, resp);
+ try {
+ super.doPost(req, resp);
+ } catch (IOException ioe) {
+ intlogger.error("IOException" + ioe.getMessage());
+ }
return;
}
String path = req.getPathInfo();
@@ -358,7 +381,7 @@ public class RouteServlet extends ProxyServlet { ins = new Insertable[] { new IngressRoute(seq, feedid, user, subnet, NodeClass.lookupNodeNames(nodepatt)) };
} catch (Exception e) {
intlogger.info(e);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments in 'add ingress' command.");
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments in 'add ingress' command.", intlogger);
return;
}
} else if (path.startsWith("/egress/")) {
@@ -367,14 +390,14 @@ public class RouteServlet extends ProxyServlet { int subid = Integer.parseInt(req.getParameter("sub"));
EgressRoute er = EgressRoute.getEgressRoute(subid);
if (er != null) {
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "An egress route already exists for that subscriber.");
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, "An egress route already exists for that subscriber.", intlogger);
return;
}
String node = NodeClass.normalizeNodename(req.getParameter("node"));
ins = new Insertable[] { new EgressRoute(subid, node) };
} catch (Exception e) {
intlogger.info(e);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments in 'add egress' command.");
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments in 'add egress' command.", intlogger);
return;
}
} else if (path.startsWith("/network/")) {
@@ -384,7 +407,7 @@ public class RouteServlet extends ProxyServlet { String nto = req.getParameter("to");
String nvia = req.getParameter("via");
if (nfrom == null || nto == null || nvia == null) {
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing arguments in 'add network' command.");
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, "Missing arguments in 'add network' command.", intlogger);
return;
}
nfrom = NodeClass.normalizeNodename(nfrom);
@@ -393,19 +416,19 @@ public class RouteServlet extends ProxyServlet { NetworkRoute nr = new NetworkRoute(nfrom, nto, nvia);
for (NetworkRoute route : NetworkRoute.getAllNetworkRoutes()) {
if (route.getFromnode() == nr.getFromnode() && route.getTonode() == nr.getTonode()) {
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Network route table already contains a route for "+nfrom+" and "+nto);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, "Network route table already contains a route for " + nfrom + " and " + nto, intlogger);
return;
}
}
ins = new Insertable[] { nr };
} catch (IllegalArgumentException e) {
intlogger.info(e);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments in 'add network' command.");
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments in 'add network' command.", intlogger);
return;
}
}
if (ins == null) {
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, "Bad URL.", intlogger);
return;
}
boolean rv = true;
@@ -422,7 +445,7 @@ public class RouteServlet extends ProxyServlet { // Something went wrong with the INSERT
elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG);
+ sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, intlogger);
}
}
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java index 4917402c..b77062fa 100755 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java @@ -28,7 +28,6 @@ import java.sql.Connection; import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
@@ -44,6 +43,8 @@ import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord; import org.onap.dmaap.datarouter.provisioning.utils.DB;
import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject;
+import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError;
+
/**
* This Servlet handles requests to the <Statistics API> and <Statistics consilidated
* resultset>,
@@ -64,13 +65,13 @@ public class StatisticsServlet extends BaseServlet { * DELETE a logging URL -- not supported.
*/
@Override
- public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ public void doDelete(HttpServletRequest req, HttpServletResponse resp) {
String message = "DELETE not allowed for the logURL.";
EventLogRecord elr = new EventLogRecord(req);
elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);
+ sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger);
}
/**
@@ -78,24 +79,27 @@ public class StatisticsServlet extends BaseServlet { * <b>Statistics API</b> document for details on how this method should be invoked.
*/
@Override
- public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ public void doGet(HttpServletRequest req, HttpServletResponse resp) {
Map<String, String> map = buildMapFromRequest(req);
if (map.get("err") != null) {
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments: " + map.get("err"));
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments: " + map.get("err"), eventlogger);
return;
}
// check Accept: header??
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType(LOGLIST_CONTENT_TYPE);
- ServletOutputStream out = resp.getOutputStream();
String outputType = "json";
String feedids = null;
if (req.getParameter("feedid") == null && req.getParameter("groupid") == null) {
- out.print("Invalid request, Feedid or Group ID is required.");
+ try {
+ resp.getOutputStream().print("Invalid request, Feedid or Group ID is required.");
+ } catch (IOException ioe) {
+ eventlogger.error("IOException: " + ioe.getMessage());
+ }
}
if (req.getParameter("feedid") != null && req.getParameter("groupid") == null) {
@@ -114,10 +118,8 @@ public class StatisticsServlet extends BaseServlet { System.out.println("groupid1" + groupid1.toString());
- } catch (NumberFormatException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
+ } catch (NumberFormatException | SQLException e) {
+ eventlogger.error(e.getMessage());
}
}
if (req.getParameter("groupid") != null && req.getParameter("feedid") != null) {
@@ -135,10 +137,8 @@ public class StatisticsServlet extends BaseServlet { System.out.println("groupid1" + groupid1.toString());
- } catch (NumberFormatException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
+ } catch (NumberFormatException | SQLException e) {
+ eventlogger.error(e.getMessage());
}
}
@@ -179,8 +179,11 @@ public class StatisticsServlet extends BaseServlet { if (req.getParameter("output_type") != null) {
outputType = req.getParameter("output_type");
}
-
- this.getRecordsForSQL(map, outputType, out, resp);
+ try {
+ this.getRecordsForSQL(map, outputType, resp.getOutputStream(), resp);
+ } catch (IOException ioe) {
+ eventlogger.error("IOException: " + ioe.getMessage());
+ }
}
@@ -288,7 +291,7 @@ public class StatisticsServlet extends BaseServlet { System.out.println("feedIds" + feedIds.toString());
}
} catch (SQLException e) {
- e.printStackTrace();
+ eventlogger.error(e.getMessage());
} finally {
try {
if (resultSet != null) {
@@ -299,7 +302,7 @@ public class StatisticsServlet extends BaseServlet { db.release(conn);
}
} catch (Exception e) {
- e.printStackTrace();
+ eventlogger.error(e.getMessage());
}
}
return feedIds;
@@ -399,26 +402,26 @@ public class StatisticsServlet extends BaseServlet { * PUT a Statistics URL -- not supported.
*/
@Override
- public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ public void doPut(HttpServletRequest req, HttpServletResponse resp) {
String message = "PUT not allowed for the StatisticsURL.";
EventLogRecord elr = new EventLogRecord(req);
elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);
+ sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger);
}
/**
* POST a Statistics URL -- not supported.
*/
@Override
- public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ public void doPost(HttpServletRequest req, HttpServletResponse resp) {
String message = "POST not allowed for the StatisticsURL.";
EventLogRecord elr = new EventLogRecord(req);
elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);
+ sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger);
}
private Map<String, String> buildMapFromRequest(HttpServletRequest req) {
@@ -543,10 +546,8 @@ public class StatisticsServlet extends BaseServlet { intlogger.debug(filterQuery);
long start = System.currentTimeMillis();
DB db = new DB();
- ResultSet rs = null;
try (Connection conn = db.getConnection()) {
- try (PreparedStatement pst = conn.prepareStatement(filterQuery)) {
- rs = pst.executeQuery();
+ try (ResultSet rs = conn.prepareStatement(filterQuery).executeQuery()) {
if (outputType.equals("csv")) {
resp.setContentType("application/octet-stream");
Date date = new Date();
@@ -562,18 +563,15 @@ public class StatisticsServlet extends BaseServlet { }
}
} catch (SQLException e) {
- e.printStackTrace();
+ eventlogger.error("SQLException:" + e);
}
intlogger.debug("Time: " + (System.currentTimeMillis() - start) + " ms");
} catch (IOException e) {
eventlogger.error("IOException - Generating JSON/CSV:" + e);
- e.printStackTrace();
} catch (JSONException e) {
eventlogger.error("JSONException - executing SQL query:" + e);
- e.printStackTrace();
} catch (ParseException e) {
eventlogger.error("ParseException - executing SQL query:" + e);
- e.printStackTrace();
}
}
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscribeServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscribeServlet.java index 21d391e5..e8828f12 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscribeServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscribeServlet.java @@ -42,6 +42,8 @@ import org.onap.dmaap.datarouter.provisioning.utils.JSONUtilities; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError; + /** * This servlet handles provisioning for the <subscribeURL> which is generated by the provisioning server to * handle the creation and inspection of subscriptions to a specific feed. @@ -60,7 +62,7 @@ public class SubscribeServlet extends ProxyServlet { * DELETE on the <subscribeUrl> -- not supported. */ @Override - public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doDelete(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doDelete"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); String message = "DELETE not allowed for the subscribeURL."; @@ -68,7 +70,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message); + sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); } /** @@ -76,7 +78,7 @@ public class SubscribeServlet extends ProxyServlet { * Query</i> section in the <b>Provisioning API</b> document for details on how this method should be invoked. */ @Override - public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doGet(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doGet"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); EventLogRecord elr = new EventLogRecord(req); @@ -85,11 +87,15 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } if (isProxyServer()) { - super.doGet(req, resp); + try { + super.doGet(req, resp); + } catch (IOException ioe) { + eventlogger.error("IOException: " + ioe.getMessage()); + } return; } String bhdr = req.getHeader(BEHALF_HEADER); @@ -98,7 +104,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } int feedid = getIdFromPath(req); @@ -107,7 +113,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } Feed feed = Feed.getFeedById(feedid); @@ -116,7 +122,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_NOT_FOUND); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_NOT_FOUND, message); + sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, message, eventlogger); return; } // Check with the Authorizer @@ -126,7 +132,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } @@ -139,14 +145,18 @@ public class SubscribeServlet extends ProxyServlet { eventlogger.info(elr); resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(SUBLIST_CONTENT_TYPE); - resp.getOutputStream().print(t); + try { + resp.getOutputStream().print(t); + } catch (IOException ioe) { + eventlogger.error("IOException: " + ioe.getMessage()); + } } /** * PUT on the <subscribeUrl> -- not supported. */ @Override - public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException { + public void doPut(HttpServletRequest req, HttpServletResponse resp) { setIpAndFqdnForEelf("doPut"); eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); String message = "PUT not allowed for the subscribeURL."; @@ -154,7 +164,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message); + sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); } /** @@ -171,11 +181,15 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } if (isProxyServer()) { - super.doPost(req, resp); + try { + super.doPost(req, resp); + } catch (IOException ioe) { + eventlogger.error("IOException: " + ioe.getMessage()); + } return; } String bhdr = req.getHeader(BEHALF_HEADER); @@ -184,7 +198,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } int feedid = getIdFromPath(req); @@ -193,7 +207,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } Feed feed = Feed.getFeedById(feedid); @@ -202,7 +216,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_NOT_FOUND); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_NOT_FOUND, message); + sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, message, eventlogger); return; } // Check with the Authorizer @@ -212,7 +226,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); + sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger); return; } @@ -225,7 +239,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message); + sendResponseError(resp, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message, eventlogger); return; } JSONObject jo = getJSONfromInput(req); @@ -234,7 +248,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } if (intlogger.isDebugEnabled()) { @@ -246,7 +260,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_CONFLICT); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_CONFLICT, message); + sendResponseError(resp, HttpServletResponse.SC_CONFLICT, message, eventlogger); return; } Subscription sub = null; @@ -258,7 +272,7 @@ public class SubscribeServlet extends ProxyServlet { elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger); return; } sub.setFeedid(feedid); @@ -280,7 +294,11 @@ public class SubscribeServlet extends ProxyServlet { resp.setStatus(HttpServletResponse.SC_CREATED); resp.setContentType(SUBFULL_CONTENT_TYPE); resp.setHeader("Location", sub.getLinks().getSelf()); - resp.getOutputStream().print(sub.asLimitedJSONObject().toString()); + try { + resp.getOutputStream().print(sub.asLimitedJSONObject().toString()); + } catch (IOException ioe) { + eventlogger.error("IOException: " + ioe.getMessage()); + } provisioningDataChanged(); } else { @@ -288,7 +306,7 @@ public class SubscribeServlet extends ProxyServlet { activeSubs--; elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG); + sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, eventlogger); } } } diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServlet.java index 3294580b..3bfa7507 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServlet.java @@ -44,6 +44,8 @@ import org.onap.dmaap.datarouter.provisioning.eelf.EelfMsgs; import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
+import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError;
+
/**
* This servlet handles provisioning for the <subscriptionURL> which is generated by the provisioning server to
* handle the inspection, modification, and deletion of a particular subscription to a feed. It supports DELETE to
@@ -66,7 +68,7 @@ public class SubscriptionServlet extends ProxyServlet { * the <b>Provisioning API</b> document for details on how this method should be invoked.
*/
@Override
- public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ public void doDelete(HttpServletRequest req, HttpServletResponse resp) {
setIpAndFqdnForEelf("doDelete");
eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");
EventLogRecord elr = new EventLogRecord(req);
@@ -75,11 +77,15 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_FORBIDDEN, message);
+ sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger);
return;
}
if (isProxyServer()) {
- super.doDelete(req, resp);
+ try {
+ super.doDelete(req, resp);
+ } catch (IOException ioe) {
+ eventlogger.error("IOException: " + ioe.getMessage());
+ }
return;
}
String bhdr = req.getHeader(BEHALF_HEADER);
@@ -88,7 +94,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger);
return;
}
int subid = getIdFromPath(req);
@@ -97,7 +103,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger);
return;
}
Subscription sub = Subscription.getSubscriptionById(subid);
@@ -106,7 +112,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_NOT_FOUND);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, message);
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, message, eventlogger);
return;
}
// Check with the Authorizer
@@ -116,7 +122,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_FORBIDDEN, message);
+ sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger);
return;
}
@@ -132,7 +138,7 @@ public class SubscriptionServlet extends ProxyServlet { // Something went wrong with the DELETE
elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG);
+ sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, intlogger);
}
}
@@ -142,7 +148,7 @@ public class SubscriptionServlet extends ProxyServlet { * invoked.
*/
@Override
- public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ public void doGet(HttpServletRequest req, HttpServletResponse resp) {
setIpAndFqdnForEelf("doGet");
eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");
EventLogRecord elr = new EventLogRecord(req);
@@ -151,11 +157,15 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_FORBIDDEN, message);
+ sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger);
return;
}
if (isProxyServer()) {
- super.doGet(req, resp);
+ try {
+ super.doGet(req, resp);
+ } catch (IOException ioe) {
+ eventlogger.error("IOException: " + ioe.getMessage());
+ }
return;
}
String bhdr = req.getHeader(BEHALF_HEADER);
@@ -164,7 +174,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger);
return;
}
int subid = getIdFromPath(req);
@@ -173,7 +183,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger);
return;
}
Subscription sub = Subscription.getSubscriptionById(subid);
@@ -182,7 +192,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_NOT_FOUND);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, message);
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, message, eventlogger);
return;
}
// Check with the Authorizer
@@ -192,7 +202,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_FORBIDDEN, message);
+ sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger);
return;
}
@@ -201,7 +211,11 @@ public class SubscriptionServlet extends ProxyServlet { eventlogger.info(elr);
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType(SUBFULL_CONTENT_TYPE);
- resp.getOutputStream().print(sub.asJSONObject(true).toString());
+ try {
+ resp.getOutputStream().print(sub.asJSONObject(true).toString());
+ } catch (IOException ioe) {
+ eventlogger.error("IOException: " + ioe.getMessage());
+ }
}
/**
@@ -209,7 +223,7 @@ public class SubscriptionServlet extends ProxyServlet { * the <b>Provisioning API</b> document for details on how this method should be invoked.
*/
@Override
- public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ public void doPut(HttpServletRequest req, HttpServletResponse resp) {
setIpAndFqdnForEelf("doPut");
eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");
EventLogRecord elr = new EventLogRecord(req);
@@ -218,11 +232,15 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_FORBIDDEN, message);
+ sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger);
return;
}
if (isProxyServer()) {
- super.doPut(req, resp);
+ try {
+ super.doPut(req, resp);
+ } catch (IOException ioe) {
+ eventlogger.error("IOException: " + ioe.getMessage());
+ }
return;
}
String bhdr = req.getHeader(BEHALF_HEADER);
@@ -231,7 +249,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger);
return;
}
int subid = getIdFromPath(req);
@@ -240,7 +258,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger);
return;
}
Subscription oldsub = Subscription.getSubscriptionById(subid);
@@ -249,7 +267,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_NOT_FOUND);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_NOT_FOUND, message);
+ sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, message, eventlogger);
return;
}
// Check with the Authorizer
@@ -259,7 +277,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_FORBIDDEN, message);
+ sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger);
return;
}
// check content type is SUB_CONTENT_TYPE, version 1.0
@@ -270,7 +288,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message);
+ sendResponseError(resp, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message, eventlogger);
return;
}
JSONObject jo = getJSONfromInput(req);
@@ -279,7 +297,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger);
return;
}
if (intlogger.isDebugEnabled()) {
@@ -293,7 +311,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger);
return;
}
sub.setSubid(oldsub.getSubid());
@@ -306,7 +324,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger);
return;
}
@@ -317,14 +335,22 @@ public class SubscriptionServlet extends ProxyServlet { eventlogger.info(elr);
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType(SUBFULL_CONTENT_TYPE);
- resp.getOutputStream().print(sub.asLimitedJSONObject().toString());
+ try {
+ resp.getOutputStream().print(sub.asLimitedJSONObject().toString());
+ } catch (IOException ioe) {
+ eventlogger.error("IOException: " + ioe.getMessage());
+ }
/**Change Owner ship of Subscriber Adding for group feature:Rally US708115*/
if (jo.has("changeowner") && subjectgroup != null) {
- Boolean changeowner = (Boolean) jo.get("changeowner");
- if (changeowner != null && changeowner.equals(true)) {
- sub.setSubscriber(req.getHeader(BEHALF_HEADER));
- sub.changeOwnerShip();
+ try {
+ Boolean changeowner = (Boolean) jo.get("changeowner");
+ if (changeowner != null && changeowner.equals(true)) {
+ sub.setSubscriber(req.getHeader(BEHALF_HEADER));
+ sub.changeOwnerShip();
+ }
+ } catch (JSONException je) {
+ eventlogger.error("JSONException: " + je.getMessage());
}
}
/***End of change ownership*/
@@ -334,7 +360,7 @@ public class SubscriptionServlet extends ProxyServlet { // Something went wrong with the UPDATE
elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG);
+ sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, intlogger);
}
}
@@ -343,7 +369,7 @@ public class SubscriptionServlet extends ProxyServlet { * Schedule</i> section in the <b>Provisioning API</b> document for details on how this method should be invoked.
*/
@Override
- public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ public void doPost(HttpServletRequest req, HttpServletResponse resp) {
// OLD pre-3.0 code
// String message = "POST not allowed for the subscriptionURL.";
// EventLogRecord elr = new EventLogRecord(req);
@@ -360,11 +386,15 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_FORBIDDEN, message);
+ sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger);
return;
}
if (isProxyServer()) {
- super.doPost(req, resp);
+ try {
+ super.doPost(req, resp);
+ } catch (IOException ioe) {
+ eventlogger.error("IOException: " + ioe.getMessage());
+ }
return;
}
String bhdr = req.getHeader(BEHALF_HEADER);
@@ -373,7 +403,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger);
return;
}
final int subid = getIdFromPath(req);
@@ -382,7 +412,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger);
return;
}
// check content type is SUBCNTRL_CONTENT_TYPE, version 1.0
@@ -393,7 +423,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message);
+ sendResponseError(resp, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message, eventlogger);
return;
}
// Check with the Authorizer
@@ -403,7 +433,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_FORBIDDEN, message);
+ sendResponseError(resp, HttpServletResponse.SC_FORBIDDEN, message, eventlogger);
return;
}
JSONObject jo = getJSONfromInput(req);
@@ -412,7 +442,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger);
return;
}
try {
@@ -434,7 +464,7 @@ public class SubscriptionServlet extends ProxyServlet { elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
+ sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, message, eventlogger);
}
}
@@ -474,7 +504,6 @@ public class SubscriptionServlet extends ProxyServlet { }
} catch (Exception e) {
intlogger.warn("Caught exception in SubscriberNotifyThread: " + e);
- e.printStackTrace();
}
}
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java index e1eeb786..f9d0867d 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java @@ -302,7 +302,7 @@ public class SynchronizerTask extends TimerTask { String thisPod = myaddr.getHostName(); Set<String> pods = new TreeSet<>(Arrays.asList(BaseServlet.getPods())); if (pods.contains(thisPod)) { - InetAddress pserver = InetAddress.getByName(BaseServlet.activeProvName); + InetAddress pserver = InetAddress.getByName(BaseServlet.getActiveProvName()); newstate = myaddr.equals(pserver) ? ACTIVE : STANDBY; if (logger.isDebugEnabled() && System.currentTimeMillis() >= nextMsg) { logger.debug("Active POD = " + pserver + ", Current state is " + stnames[newstate]); diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java index c08bce57..9c060d5e 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java @@ -72,8 +72,9 @@ public class Feed extends Syncable { try {
DB db = new DB();
Connection conn = db.getConnection();
- try(Statement stmt = conn.createStatement()) {
- try(ResultSet rs = stmt.executeQuery("select COUNT(*) from FEEDS where FEEDID = " + id)) {
+ try(PreparedStatement stmt = conn.prepareStatement("select COUNT(*) from FEEDS where FEEDID = ?")) {
+ stmt.setInt(1, id);
+ try(ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
count = rs.getInt(1);
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java index a460d647..91d6c1b4 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java @@ -133,14 +133,15 @@ public class Group extends Syncable { }
public static Collection<String> getGroupsByClassfication(String classfication) {
- List<String> list = new ArrayList<String>();
- String sql = "select * from GROUPS where classification = '" + classfication + "'";
+ List<String> list = new ArrayList<>();
+ String sql = "select * from GROUPS where classification = ?";
try {
DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- try(Statement stmt = conn.createStatement()) {
- try(ResultSet rs = stmt.executeQuery(sql)) {
+ try(PreparedStatement stmt = conn.prepareStatement(sql)) {
+ stmt.setString(1, classfication);
+ try(ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
int groupid = rs.getInt("groupid");
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Parameters.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Parameters.java index 3e8c90b4..b2378218 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Parameters.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Parameters.java @@ -118,9 +118,9 @@ public class Parameters extends Syncable { DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- try(Statement stmt = conn.createStatement()) {
- String sql = "select KEYNAME, VALUE from PARAMETERS where KEYNAME = '" + k + "'";
- try(ResultSet rs = stmt.executeQuery(sql)) {
+ try(PreparedStatement stmt = conn.prepareStatement("select KEYNAME, VALUE from PARAMETERS where KEYNAME = ?")) {
+ stmt.setString(1, k);
+ try(ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
v = new Parameters(rs);
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/HttpServletUtils.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/HttpServletUtils.java new file mode 100644 index 00000000..ce287f4d --- /dev/null +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/HttpServletUtils.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * ============LICENSE_START================================================== + * * org.onap.dmaap + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package org.onap.dmaap.datarouter.provisioning.utils; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +import org.apache.log4j.Logger; + +public class HttpServletUtils { + public static void sendResponseError(HttpServletResponse response, int errorCode, String message, Logger intlogger) { + try { + response.sendError(errorCode, message); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } + } +} diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObject.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObject.java index dec3cc13..1518859a 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObject.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObject.java @@ -1446,7 +1446,7 @@ public class LOGJSONObject { try { return this.toString(0); } catch (Exception e) { - return null; + return ""; } } @@ -1495,7 +1495,7 @@ public class LOGJSONObject { */ @SuppressWarnings("unchecked") public static String valueToString(Object value) throws JSONException { - if (value == null || value.equals(null)) { + if (value == null) { return "null"; } if (value instanceof JSONString) { @@ -1601,7 +1601,7 @@ public class LOGJSONObject { @SuppressWarnings("unchecked") static final Writer writeValue(Writer writer, Object value, int indentFactor, int indent) throws JSONException, IOException { - if (value == null || value.equals(null)) { + if (value == null) { writer.write("null"); } else if (value instanceof LOGJSONObject) { ((LOGJSONObject) value).write(writer, indentFactor, indent); diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/URLUtilities.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/URLUtilities.java index 4576bd26..0c6afdd7 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/URLUtilities.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/URLUtilities.java @@ -45,7 +45,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateFeedURL(int feedid) {
- return "https://" + BaseServlet.provName + "/feed/" + feedid;
+ return "https://" + BaseServlet.getProvName() + "/feed/" + feedid;
}
/**
@@ -55,7 +55,7 @@ public class URLUtilities { * @return the URL
*/
public static String generatePublishURL(int feedid) {
- return "https://" + BaseServlet.provName + "/publish/" + feedid;
+ return "https://" + BaseServlet.getProvName() + "/publish/" + feedid;
}
/**
@@ -65,7 +65,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateSubscribeURL(int feedid) {
- return "https://" + BaseServlet.provName + "/subscribe/" + feedid;
+ return "https://" + BaseServlet.getProvName() + "/subscribe/" + feedid;
}
/**
@@ -75,7 +75,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateFeedLogURL(int feedid) {
- return "https://" + BaseServlet.provName + "/feedlog/" + feedid;
+ return "https://" + BaseServlet.getProvName() + "/feedlog/" + feedid;
}
/**
@@ -85,7 +85,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateSubscriptionURL(int subid) {
- return "https://" + BaseServlet.provName + "/subs/" + subid;
+ return "https://" + BaseServlet.getProvName() + "/subs/" + subid;
}
/**
@@ -95,7 +95,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateSubLogURL(int subid) {
- return "https://" + BaseServlet.provName + "/sublog/" + subid;
+ return "https://" + BaseServlet.getProvName() + "/sublog/" + subid;
}
/**
@@ -115,7 +115,7 @@ public class URLUtilities { public static String generatePeerLogsURL() {
//Fixes for Itrack ticket - DATARTR-4#Fixing if only one Prov is configured, not to give exception to fill logs.
String peerPodUrl = getPeerPodName();
- if (peerPodUrl.equals("") || peerPodUrl==null) {
+ if (peerPodUrl == null || peerPodUrl.equals("")) {
return "";
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/FeedReport.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/FeedReport.java index 4346a0ec..fe9c4601 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/FeedReport.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/FeedReport.java @@ -72,36 +72,12 @@ public class FeedReport extends ReportBase { try( PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
- if (alg1) {
- String date = rs.getString("date");
- String type = rs.getString("type");
- int feedid = rs.getInt("feedid");
- int subid = type.equals("del") ? rs.getInt("delivery_subid") : 0;
- int count = rs.getInt("count");
- sb.append(date + "," + type + "," + feedid + "," + subid + "," + count + "\n");
- } else {
- String date = rs.getString("date");
- JSONObject datemap = jo.optJSONObject(date);
- if (datemap == null) {
- datemap = new JSONObject();
- jo.put(date, datemap);
- }
- int feed = rs.getInt("FEEDID");
- JSONObject feedmap = datemap.optJSONObject("" + feed);
- if (feedmap == null) {
- feedmap = new JSONObject();
- feedmap.put("pubcount", 0);
- datemap.put("" + feed, feedmap);
- }
- String type = rs.getString("TYPE");
- int count = rs.getInt("count");
- if (type.equals("pub")) {
- feedmap.put("pubcount", count);
- } else if (type.equals("del")) {
- String subid = "" + rs.getInt("DELIVERY_SUBID");
- feedmap.put(subid, count);
- }
- }
+ String date = rs.getString("date");
+ String type = rs.getString("type");
+ int feedid = rs.getInt("feedid");
+ int subid = type.equals("del") ? rs.getInt("delivery_subid") : 0;
+ int count = rs.getInt("count");
+ sb.append(date + "," + type + "," + feedid + "," + subid + "," + count + "\n");
}
}
}
@@ -110,15 +86,9 @@ public class FeedReport extends ReportBase { e.printStackTrace();
}
logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");
- try {
- PrintWriter os = new PrintWriter(outfile);
- if (alg1) {
- os.print("date,type,feedid,subid,count\n");
- os.print(sb.toString());
- } else {
- os.println(toHTML(jo));
- }
- os.close();
+ try (PrintWriter os = new PrintWriter(outfile)) {
+ os.print("date,type,feedid,subid,count\n");
+ os.print(sb.toString());
} catch (FileNotFoundException e) {
System.err.println("File cannot be written: " + outfile);
}
|