Update traversal from AJSC 2 to Spring Boot
[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 @Path("/util")
46 public class EchoResponse extends RESTAPI {
47         
48         protected static String authPolicyFunctionName = "util";
49                 
50         public static final String echoPath = "/echo";
51
52         /**
53          * Simple health-check API that echos back the X-FromAppId and X-TransactionId to clients.
54          * If there is a query string, a transaction gets logged into hbase, proving the application is connected to the data store.
55          * If there is no query string, no transacction logging is done to hbase.
56          *
57          * @param headers the headers
58          * @param req the req
59          * @param myAction if exists will cause transaction to be logged to hbase
60          * @return the response
61          */
62         @GET
63         @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
64         @Path(echoPath)
65         public Response echoResult(@Context HttpHeaders headers, @Context HttpServletRequest req,
66                         @QueryParam("action") String myAction) {
67                 Response response = null;
68                 
69                 AAIException ex = null;
70                 String fromAppId = null;
71                 String transId = null;
72                 
73                 try { 
74                         fromAppId = getFromAppId(headers );
75                         transId = getTransId(headers);
76                 } catch (AAIException e) { 
77                         ArrayList<String> templateVars = new ArrayList<String>();
78                         templateVars.add("PUT uebProvider");
79                         templateVars.add("addTopic");
80                         return Response
81                                         .status(e.getErrorObject().getHTTPResponseCode())
82                                         .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), e, templateVars))
83                                         .build();
84                 }
85                 
86                 try {
87                         
88                         HashMap<AAIException, ArrayList<String>> exceptionList = new HashMap<AAIException, ArrayList<String>>();
89                                         
90                         ArrayList<String> templateVars = new ArrayList<String>();
91                         templateVars.add(fromAppId);
92                         templateVars.add(transId);
93                 
94                         exceptionList.put(new AAIException("AAI_0002", "OK"), templateVars);
95                                 
96                         response = Response.status(Status.OK)
97                                         .entity(ErrorLogHelper.getRESTAPIInfoResponse(
98                                                         headers.getAcceptableMediaTypes(), exceptionList))
99                                                         .build();
100                         
101                 } catch (Exception e) {
102                         ex = new AAIException("AAI_4000", e);
103                         ArrayList<String> templateVars = new ArrayList<String>();
104                         templateVars.add(Action.GET.name());
105                         templateVars.add(fromAppId +" "+transId);
106
107                         response = Response
108                                         .status(Status.INTERNAL_SERVER_ERROR)
109                                         .entity(ErrorLogHelper.getRESTAPIErrorResponse(
110                                                         headers.getAcceptableMediaTypes(), ex,
111                                                         templateVars)).build();
112
113                 } finally {
114                         if (ex != null) {
115                                 ErrorLogHelper.logException(ex);
116                         }
117
118                 }
119                 
120                 return response;
121         }
122
123 }