Add instructions to invoke the linter and code formatter plugins to the README and...
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / healthcheck / EchoResource.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.schemaservice.healthcheck;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.core.*;
31 import javax.ws.rs.core.Response.Status;
32
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.logging.ErrorLogHelper;
35 import org.onap.aai.restcore.RESTAPI;
36
37 /**
38  * The Class EchoResponse.
39  */
40 @Path("/util")
41 public class EchoResource extends RESTAPI {
42
43     /**
44      * Simple health-check API that echos back the X-FromAppId and X-TransactionId to clients.
45      * If there is a query string, a transaction gets logged into hbase, proving the application is
46      * connected to the data store.
47      * If there is no query string, no transaction logging is done to hbase.
48      *
49      * @param headers the headers
50      * @param req the req
51      * @return the response
52      */
53     @GET
54     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
55     @Path("/echo")
56     public Response echoResult(@Context HttpHeaders headers, @Context HttpServletRequest req,
57         @Context UriInfo uriInfo) {
58         Response response = null;
59
60         AAIException ex = null;
61         String fromAppId = null;
62         String transId = null;
63
64         try {
65             fromAppId = getFromAppId(headers);
66             transId = getTransId(headers);
67         } catch (AAIException e) {
68             ArrayList<String> templateVars = new ArrayList<String>();
69             templateVars.add("Headers missing");
70             return Response
71                 .status(e.getErrorObject().getHTTPResponseCode()).entity(ErrorLogHelper
72                     .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), e, templateVars))
73                 .build();
74         }
75
76         try {
77
78             HashMap<AAIException, ArrayList<String>> exceptionList =
79                 new HashMap<AAIException, ArrayList<String>>();
80
81             ArrayList<String> templateVars = new ArrayList<String>();
82             templateVars.add(fromAppId);
83             templateVars.add(transId);
84
85             exceptionList.put(new AAIException("AAI_0002", "OK"), templateVars);
86
87             response = Response
88                 .status(Status.OK).entity(ErrorLogHelper
89                     .getRESTAPIInfoResponse(headers.getAcceptableMediaTypes(), exceptionList))
90                 .build();
91
92         } catch (Exception e) {
93             ex = new AAIException("AAI_4000", e);
94             ArrayList<String> templateVars = new ArrayList<String>();
95             templateVars.add(Action.GET.name());
96             templateVars.add(fromAppId + " " + transId);
97
98             response = Response
99                 .status(Status.INTERNAL_SERVER_ERROR).entity(ErrorLogHelper
100                     .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
101                 .build();
102
103         } finally {
104             if (ex != null) {
105                 ErrorLogHelper.logException(ex);
106             }
107
108         }
109
110         return response;
111     }
112
113 }