2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.event.carrier.restserver;
23 import java.util.LinkedHashMap;
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.POST;
29 import javax.ws.rs.PUT;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The Class RestServerEndpoint is the end point servlet class for handling REST requests and responses to and from
43 * @author Liam Fallon (liam.fallon@ericsson.com)
45 @Path("/{eventInput}")
46 @Produces({ MediaType.APPLICATION_JSON })
47 @Consumes({ MediaType.APPLICATION_JSON })
48 public class RestServerEndpoint {
49 // Get a reference to the logger
50 private static final Logger LOGGER = LoggerFactory.getLogger(RestServerEndpoint.class);
52 // Statistics on the amount of HTTP messages handled
53 private static int getMessagesReceived = 0;
54 private static int postEventMessagesReceived = 0;
55 private static int putEventMessagesReceived = 0;
57 // This map is used to hold all the REST server event inputs. This is used to determine which consumer to send input
59 private static Map<String, ApexRestServerConsumer> consumerMap =
60 new LinkedHashMap<String, ApexRestServerConsumer>();
62 // The ID of this event input. This gets injected from the URL.
63 @PathParam("eventInput")
64 private String eventInputID = null;
67 * Register an Apex consumer with the REST server end point.
69 * @param consumerEventInputID The event input ID that indicates this consumer shoud be used
70 * @param consumer The consumer to register
72 public static void registerApexRestServerConsumer(final String consumerEventInputID,
73 final ApexRestServerConsumer consumer) {
74 consumerMap.put(consumerEventInputID, consumer);
78 * Get statistics on apex REST event handling.
80 * @return the response
84 public Response serviceGetStats() {
85 getMessagesReceived++;
86 return Response.status(Response.Status.OK.getStatusCode())
87 .entity("{\n" + "\"INPUTS\": \"" + consumerMap.keySet() + "\",\n" + "\"STAT\": " + getMessagesReceived
88 + ",\n" + "\"POST\": " + postEventMessagesReceived + ",\n" + "\"PUT\": "
89 + putEventMessagesReceived + "\n}")
94 * Service post request, an incoming event over RETS to Apex.
96 * @param jsonString the JSON string containing the data coming in on the REST call
97 * @return the response event to the request
101 public Response servicePostRequest(final String jsonString) {
102 postEventMessagesReceived++;
104 if (LOGGER.isDebugEnabled()) {
105 LOGGER.debug("event input " + eventInputID + ", received POST of event \"" + jsonString + "\"");
108 // Common handler method for POST and PUT requests
109 return handleEvent(jsonString);
113 * Service put request, an incoming event over RETS to Apex.
115 * @param jsonString the JSON string containing the data coming in on the REST call
116 * @return the response event to the request
120 public Response servicePutRequest(final String jsonString) {
121 putEventMessagesReceived++;
123 if (LOGGER.isDebugEnabled()) {
124 LOGGER.debug("event input \"" + eventInputID + "\", received PUT of event \"" + jsonString + "\"");
127 // Common handler method for POST and PUT requests
128 return handleEvent(jsonString);
132 * Common event handler for events received on POST and PUT messages.
134 * @param jsonString the JSON string containing the data coming in on the REST call
135 * @return the response event to the request
137 private Response handleEvent(final String jsonString) {
138 // Find the correct consumer for this REST message
139 final ApexRestServerConsumer eventConsumer = consumerMap.get(eventInputID);
140 if (eventConsumer == null) {
141 final String errorMessage =
142 "event input " + eventInputID + " is not defined in the Apex configuration file";
143 LOGGER.warn(errorMessage);
144 return Response.status(Response.Status.BAD_REQUEST.getStatusCode())
145 .entity("{'errorMessage', '" + errorMessage + "'}").build();
148 return eventConsumer.receiveEvent(jsonString);