55a07e48d5ad79ceea0e05fa38bc2d2ce791bebc
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / rest / util / EchoResponse.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.rest.util;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.QueryParam;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.HttpHeaders;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.Response.Status;
37
38 import org.onap.aai.exceptions.AAIException;
39 import org.onap.aai.logging.ErrorLogHelper;
40 import org.onap.aai.restcore.RESTAPI;
41
42 /**
43  * The Class EchoResponse.
44  */
45 public class EchoResponse extends RESTAPI {
46         
47         protected static String authPolicyFunctionName = "util";
48                 
49         public static final String echoPath = "/util/echo";
50
51         /**
52          * Simple health-check API that echos back the X-FromAppId and X-TransactionId to clients.
53          * If there is a query string, a transaction gets logged into hbase, proving the application is connected to the data store.
54          * If there is no query string, no transacction logging is done to hbase.
55          *
56          * @param headers the headers
57          * @param req the req
58          * @param myAction if exists will cause transaction to be logged to hbase
59          * @return the response
60          */
61         @GET
62         @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
63         @Path(echoPath)
64         public Response echoResult(@Context HttpHeaders headers, @Context HttpServletRequest req,
65                         @QueryParam("action") String myAction) {
66                 Response response = null;
67                 
68                 AAIException ex = null;
69                 String fromAppId = null;
70                 String transId = null;
71                 
72                 try { 
73                         fromAppId = getFromAppId(headers );
74                         transId = getTransId(headers);
75                 } catch (AAIException e) { 
76                         ArrayList<String> templateVars = new ArrayList<String>();
77                         templateVars.add("PUT uebProvider");
78                         templateVars.add("addTopic");
79                         return Response
80                                         .status(e.getErrorObject().getHTTPResponseCode())
81                                         .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), e, templateVars))
82                                         .build();
83                 }
84                 
85                 try {
86                         
87                         HashMap<AAIException, ArrayList<String>> exceptionList = new HashMap<AAIException, ArrayList<String>>();
88                                         
89                         ArrayList<String> templateVars = new ArrayList<String>();
90                         templateVars.add(fromAppId);
91                         templateVars.add(transId);
92                 
93                         exceptionList.put(new AAIException("AAI_0002", "OK"), templateVars);
94                                 
95                         response = Response.status(Status.OK)
96                                         .entity(ErrorLogHelper.getRESTAPIInfoResponse(
97                                                         headers.getAcceptableMediaTypes(), exceptionList))
98                                                         .build();
99                         
100                 } catch (Exception e) {
101                         ex = new AAIException("AAI_4000", e);
102                         ArrayList<String> templateVars = new ArrayList<String>();
103                         templateVars.add(Action.GET.name());
104                         templateVars.add(fromAppId +" "+transId);
105
106                         response = Response
107                                         .status(Status.INTERNAL_SERVER_ERROR)
108                                         .entity(ErrorLogHelper.getRESTAPIErrorResponse(
109                                                         headers.getAcceptableMediaTypes(), ex,
110                                                         templateVars)).build();
111
112                 } finally {
113                         if (ex != null) {
114                                 ErrorLogHelper.logException(ex);
115                         }
116
117                 }
118                 
119                 return response;
120         }
121
122 }