From 5c205833e4b2e8f6cfe938641cdbceaf680da92b Mon Sep 17 00:00:00 2001 From: liamfallon Date: Tue, 20 Mar 2018 17:28:29 +0000 Subject: Fix bugs identified by Sonar on drools-pdp Three bugs fixed: 1. Set of static variable in DMaaPSimulatorJaxR 2. Use of opssibly null variable "output" in RepositoryAudit 3. Unreachable statement error in RepositoryAudit Change-Id: I72e028cfc51a82250afd02fb4109d3dea08072dc Issue-ID: POLICY-691 Signed-off-by: liamfallon --- .../drools/simulators/DMaaPSimulatorJaxRs.java | 122 ++++++++++++--------- 1 file changed, 68 insertions(+), 54 deletions(-) (limited to 'feature-simulators/src') diff --git a/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java b/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java index 2513a040..44700890 100644 --- a/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java +++ b/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; +import java.util.function.Function; import java.util.concurrent.BlockingQueue; import javax.servlet.http.HttpServletResponse; @@ -43,83 +44,96 @@ import org.slf4j.LoggerFactory; @Path("/events") public class DMaaPSimulatorJaxRs { - private static final String NO_DATA_MSG = "No Data"; private static final Map> queues = new ConcurrentHashMap<>(); private static final Logger logger = LoggerFactory.getLogger(DMaaPSimulatorJaxRs.class); private static int responseCode = 200; - + @GET @Path("/{topicName}/{consumeGroup}/{consumerId}") public String subscribe(@DefaultValue("0") @QueryParam("timeout") int timeout, @PathParam("topicName") String topicName, - @Context final HttpServletResponse httpResponse) { - int currentRespCode = responseCode; - httpResponse.setStatus(currentRespCode); - try { - httpResponse.flushBuffer(); - } catch (IOException e) { - logger.error("flushBuffer threw: ", e); - return "Got an error"; - } - - if (currentRespCode < 200 || currentRespCode >= 300) - { - return "You got response code: " + currentRespCode; - } + @Context final HttpServletResponse httpResponse) { + int currentRespCode = responseCode; + httpResponse.setStatus(currentRespCode); + try { + httpResponse.flushBuffer(); + } catch (IOException e) { + logger.error("flushBuffer threw: ", e); + return "Got an error"; + } + + if (currentRespCode < 200 || currentRespCode >= 300) + { + return "You got response code: " + currentRespCode; + } + if (queues.containsKey(topicName)) { - BlockingQueue queue = queues.get(topicName); - String response = NO_DATA_MSG; - try { - response = queue.poll(timeout, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - logger.debug("error in DMaaP simulator", e); - Thread.currentThread().interrupt(); - } - if (response == null) { - response = NO_DATA_MSG; - } - return response; + return getNextMessageFromQueue(timeout, topicName); } else if (timeout > 0) { - try { - Thread.sleep(timeout); - if (queues.containsKey(topicName)) { - BlockingQueue queue = queues.get(topicName); - String response = queue.poll(); - if (response == null) { - response = NO_DATA_MSG; - } - return response; + return waitForNextMessageFromQueue(timeout, topicName); + } + return "No topic"; + } + + private String getNextMessageFromQueue(final int timeout, final String topicName) { + BlockingQueue queue = queues.get(topicName); + String response = NO_DATA_MSG; + try { + response = queue.poll(timeout, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + logger.debug("error in DMaaP simulator", e); + Thread.currentThread().interrupt(); + } + if (response == null) { + response = NO_DATA_MSG; + } + return response; + } + + private String waitForNextMessageFromQueue(int timeout, String topicName) { + try { + Thread.sleep(timeout); + if (queues.containsKey(topicName)) { + BlockingQueue queue = queues.get(topicName); + String response = queue.poll(); + if (response == null) { + response = NO_DATA_MSG; } - } catch (InterruptedException e) { - logger.debug("error in DMaaP simulator", e); - Thread.currentThread().interrupt(); + return response; } + } catch (InterruptedException e) { + logger.debug("error in DMaaP simulator", e); + Thread.currentThread().interrupt(); } return "No topic"; } - + @POST @Path("/{topicName}") @Consumes(MediaType.TEXT_PLAIN) - public String publish(@PathParam("topicName") String topicName, String body) { - if (queues.containsKey(topicName)) { - BlockingQueue queue = queues.get(topicName); - queue.offer(body); - } - else { - BlockingQueue queue = new LinkedBlockingQueue<>(); - queue.offer(body); - queues.put(topicName, queue); - } + public String publish(@PathParam("topicName") String topicName, String body) { + BlockingQueue queue = queues.computeIfAbsent(topicName, entry -> new LinkedBlockingQueue<>()); + if (!queue.offer(body)) { + logger.warn("error on topic {}, failed to place body {} on queue", topicName, body); + } + return ""; } - + @POST @Path("/setStatus") public String setStatus(@QueryParam("statusCode") int statusCode) { - responseCode = statusCode; - return "Status code set"; + setResponseCode(statusCode); + return "Status code set"; + } + + /** + * Static method to set static response code, synchronized for multiple possible uses + * @param incomingResponseCode + */ + private static synchronized void setResponseCode(final int incomingResponseCode) { + responseCode = incomingResponseCode; } } -- cgit 1.2.3-korg