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