Adding UI extensibility
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / logging / util / ServletUtils.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.logging.util;
24
25 import java.io.IOException;
26 import java.io.PrintWriter;
27
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.onap.aai.cl.api.Logger;
31 import org.onap.aai.restclient.client.OperationResult;
32 import org.onap.aai.sparky.dal.elasticsearch.SearchAdapter;
33 import org.onap.aai.sparky.dal.elasticsearch.config.ElasticSearchConfig;
34 import org.onap.aai.sparky.logging.AaiUiMsgs;
35
36 /**
37  * The Class ServletUtils.
38  */
39 public class ServletUtils {
40
41   /**
42    * Execute get query.
43    *
44    * @param logger the logger
45    * @param search the search
46    * @param response the response
47    * @param requestUrl the request url
48    * @return the operation result
49    * @throws Exception the exception
50    */
51   public static OperationResult executeGetQuery(Logger logger, SearchAdapter search,
52       HttpServletResponse response, String requestUrl) throws Exception {
53
54     OperationResult opResult = search.doGet(requestUrl, "application/json");
55
56     if (opResult.getResultCode() > 300) {
57       setServletResponse(logger, true, opResult.getResultCode(), response, opResult.getResult());
58     } else {
59       response.setStatus(opResult.getResultCode());
60     }
61
62     return opResult;
63
64   }
65
66   /**
67    * Execute post query.
68    *
69    * @param logger the logger
70    * @param search the search
71    * @param response the response
72    * @param requestUrl the request url
73    * @param requestJsonPayload the request json payload
74    * @return the operation result
75    * @throws Exception the exception
76    */
77   public static OperationResult executePostQuery(Logger logger, SearchAdapter search,
78       HttpServletResponse response, String requestUrl, String requestJsonPayload) throws Exception {
79
80     OperationResult opResult = search.doPost(requestUrl, requestJsonPayload, "application/json");
81
82     if (opResult.getResultCode() > 300) {
83       setServletResponse(logger, true, opResult.getResultCode(), response, opResult.getResult());
84
85     } else {
86       response.setStatus(opResult.getResultCode());
87     }
88
89     return opResult;
90   }
91
92   /**
93    * Handle search servlet errors.
94    *
95    * @param logger the logger
96    * @param errorMsg the error msg
97    * @param exc the exc
98    * @param response the response
99    * @throws IOException Signals that an I/O exception has occurred.
100    */
101   public static void handleSearchServletErrors(Logger logger, String errorMsg, Exception exc,
102       HttpServletResponse response) throws IOException {
103     String errorLogMsg =
104         (exc == null ? errorMsg : errorMsg + ". Error:" + exc.getLocalizedMessage());
105     logger.error(AaiUiMsgs.ERROR_GENERIC, errorLogMsg);
106     response.setContentType("application/json");
107     PrintWriter out = response.getWriter();
108     out.println(generateJsonErrorResponse(errorMsg));
109     out.close();
110   }
111
112   /**
113    * Generate json error response.
114    *
115    * @param message the message
116    * @return the string
117    */
118   public static String generateJsonErrorResponse(String message) {
119     return String.format("{ \"errorMessage\" : %s }", message);
120   }
121
122   /**
123    * Sets the servlet response.
124    *
125    * @param logger the logger
126    * @param isError the is error
127    * @param responseCode the response code
128    * @param response the response
129    * @param postPayload the post payload
130    * @throws IOException Signals that an I/O exception has occurred.
131    */
132   public static void setServletResponse(Logger logger, boolean isError, int responseCode,
133       HttpServletResponse response, String postPayload) throws IOException {
134
135     if (isError) {
136       logger.error(AaiUiMsgs.ERROR_GENERIC, postPayload);
137     }
138
139     response.setStatus(responseCode);
140
141     if (postPayload != null) {
142       response.setContentType("application/json");
143       PrintWriter out = response.getWriter();
144       out.println(postPayload);
145       out.close();
146     }
147   }
148
149   /**
150    * Gets the full url.
151    *
152    * @param elasticConfig the elastic config
153    * @param resourceUrl the resource url
154    * @return the full url
155    */
156   public static String getFullUrl(ElasticSearchConfig elasticConfig, String resourceUrl) {
157     final String host = elasticConfig.getIpAddress();
158     final String port = elasticConfig.getHttpPort();
159     return String.format("http://%s:%s%s", host, port, resourceUrl);
160   }
161 }