4ff658bcd10adb268bb85a0b0d1fef3c3fe9445c
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / service / EchoService.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.datarouter.service;
22
23 import org.onap.aai.cl.api.LogFields;
24 import org.onap.aai.cl.api.LogLine;
25 import org.onap.aai.cl.api.Logger;
26 import org.onap.aai.cl.eelf.LoggerFactory;
27 import org.onap.aai.cl.mdc.MdcContext;
28 import org.onap.aai.datarouter.logging.DataRouterMsgs;
29 import org.onap.aai.datarouter.util.DataRouterConstants;
30 import org.slf4j.MDC;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.HttpHeaders;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.UriInfo;
41
42 public class EchoService {
43
44   private static Logger logger = LoggerFactory.getInstance().getLogger(EchoService.class.getName());
45   private static Logger auditLogger =
46       LoggerFactory.getInstance().getAuditLogger(EchoService.class.getName());
47
48   @GET
49   @Path("echo/{input}")
50   @Produces("text/plain")
51   public String ping(@PathParam("input") String input, @Context HttpHeaders headers,
52       @Context UriInfo info, @Context HttpServletRequest req) {
53
54         String fromIp = req.getRemoteAddr();
55     String fromAppId = "";
56     String transId;
57
58     if (headers.getRequestHeaders().getFirst("X-FromAppId") != null) {
59       fromAppId = headers.getRequestHeaders().getFirst("X-FromAppId");
60     }
61
62     if ((headers.getRequestHeaders().getFirst("X-TransactionId") == null)
63         || headers.getRequestHeaders().getFirst("X-TransactionId").isEmpty()) {
64       transId = java.util.UUID.randomUUID().toString();
65     } else {
66       transId = headers.getRequestHeaders().getFirst("X-TransactionId");
67     }
68
69     MdcContext.initialize(transId, DataRouterConstants.DATA_ROUTER_SERVICE_NAME, "", fromAppId,
70         fromIp);
71     
72     int status = 200;
73     String respStatusString = "";
74     if (Response.Status.fromStatusCode(status) != null) {
75       respStatusString = Response.Status.fromStatusCode(status).toString();
76     }
77
78     // Generate error log
79     logger.info(DataRouterMsgs.PROCESS_REST_REQUEST, req.getMethod(),
80         req.getRequestURL().toString(), req.getRemoteHost(), Integer.toString(status));
81
82     // Generate audit log.
83     auditLogger.info(DataRouterMsgs.PROCESS_REST_REQUEST,
84         new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, status)
85             .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, respStatusString),
86         (req != null) ? req.getMethod() : "Unknown",
87         (req != null) ? req.getRequestURL().toString() : "Unknown",
88         (req != null) ? req.getRemoteHost() : "Unknown", Integer.toString(status));
89     MDC.clear();
90
91     return "Hello, " + input + ".";
92   }
93 }