Add the versions api with the echo api
[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 package org.onap.aai.schemaservice.healthcheck;
21
22 import org.onap.aai.exceptions.AAIException;
23 import org.onap.aai.logging.ErrorLogHelper;
24 import org.onap.aai.restcore.RESTAPI;
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 import java.util.ArrayList;
33 import java.util.HashMap;
34
35 /**
36  * The Class EchoResponse.
37  */
38 @Path("/util")
39 public class EchoResource extends RESTAPI {
40
41     /**
42      * Simple health-check API that echos back the X-FromAppId and X-TransactionId to clients.
43      * If there is a query string, a transaction gets logged into hbase, proving the application is connected to the data store.
44      * If there is no query string, no transaction logging is done to hbase.
45      *
46      * @param headers the headers
47      * @param req the req
48      * @return the response
49      */
50     @GET
51     @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
52     @Path("/echo")
53     public Response echoResult(@Context HttpHeaders headers, @Context HttpServletRequest req, @Context UriInfo uriInfo) {
54         Response response = null;
55
56         AAIException ex = null;
57         String fromAppId = null;
58         String transId = null;
59
60         try {
61             fromAppId = getFromAppId(headers );
62             transId = getTransId(headers);
63         } catch (AAIException e) {
64             ArrayList<String> templateVars = new ArrayList<String>();
65             templateVars.add("Headers missing");
66             return Response
67                 .status(e.getErrorObject().getHTTPResponseCode())
68                 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), e, templateVars))
69                 .build();
70         }
71
72         try {
73
74             HashMap<AAIException, ArrayList<String>> exceptionList = new HashMap<AAIException, ArrayList<String>>();
75
76             ArrayList<String> templateVars = new ArrayList<String>();
77             templateVars.add(fromAppId);
78             templateVars.add(transId);
79
80             exceptionList.put(new AAIException("AAI_0002", "OK"), templateVars);
81
82             response = Response.status(Status.OK)
83                 .entity(ErrorLogHelper.getRESTAPIInfoResponse(
84                     headers.getAcceptableMediaTypes(), exceptionList))
85                 .build();
86
87         } catch (Exception e) {
88             ex = new AAIException("AAI_4000", e);
89             ArrayList<String> templateVars = new ArrayList<String>();
90             templateVars.add(Action.GET.name());
91             templateVars.add(fromAppId +" "+transId);
92
93             response = Response
94                 .status(Status.INTERNAL_SERVER_ERROR)
95                 .entity(ErrorLogHelper.getRESTAPIErrorResponse(
96                     headers.getAcceptableMediaTypes(), ex,
97                     templateVars)).build();
98
99         } finally {
100             if (ex != null) {
101                 ErrorLogHelper.logException(ex);
102             }
103
104         }
105
106         return response;
107     }
108
109 }