Adding interfaces in documentation
[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-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 package org.onap.aai.sparky.logging.util;
22
23 import java.io.IOException;
24 import java.io.PrintWriter;
25
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.restclient.client.OperationResult;
30 import org.onap.aai.sparky.logging.AaiUiMsgs;
31 import org.onap.aai.sparky.search.SearchServiceAdapter;
32
33 /**
34  * The Class ServletUtils.
35  */
36 public class ServletUtils {
37
38   /**
39    * Execute get query.
40    *
41    * @param logger the logger
42    * @param search the search
43    * @param response the response
44    * @param requestUrl the request url
45    * @return the operation result
46    * @throws Exception the exception
47    */
48   public static OperationResult executeGetQuery(Logger logger, SearchServiceAdapter search,
49       HttpServletResponse response, String requestUrl) throws Exception {
50
51     OperationResult opResult = search.doGet(requestUrl, "application/json");
52
53     if (opResult.getResultCode() > 300) {
54       setServletResponse(logger, true, opResult.getResultCode(), response, opResult.getResult());
55     } else {
56       response.setStatus(opResult.getResultCode());
57     }
58
59     return opResult;
60
61   }
62
63   /**
64    * Execute post query.
65    *
66    * @param logger the logger
67    * @param search the search
68    * @param response the response
69    * @param requestUrl the request url
70    * @param requestJsonPayload the request json payload
71    * @return the operation result
72    * @throws Exception the exception
73    */
74   public static OperationResult executePostQuery(Logger logger, SearchServiceAdapter search,
75       HttpServletResponse response, String requestUrl, String requestJsonPayload) throws Exception {
76
77     OperationResult opResult = search.doPost(requestUrl, requestJsonPayload, "application/json");
78
79     if (opResult.getResultCode() > 300) {
80       setServletResponse(logger, true, opResult.getResultCode(), response, opResult.getResult());
81
82     } else {
83       response.setStatus(opResult.getResultCode());
84     }
85
86     return opResult;
87   }
88
89   /**
90    * Handle search servlet errors.
91    *
92    * @param logger the logger
93    * @param errorMsg the error msg
94    * @param exc the exc
95    * @param response the response
96    * @throws IOException Signals that an I/O exception has occurred.
97    */
98   public static void handleSearchServletErrors(Logger logger, String errorMsg, Exception exc,
99       HttpServletResponse response) throws IOException {
100     String errorLogMsg = (exc == null ? errorMsg : errorMsg + ". Error:" 
101         + exc.getLocalizedMessage());
102     logger.error(AaiUiMsgs.ERROR_GENERIC, errorLogMsg);
103     response.setContentType("application/json");
104     PrintWriter out = response.getWriter();
105     out.println(generateJsonErrorResponse(errorMsg));
106     out.close();
107   }
108
109   /**
110    * Generate json error response.
111    *
112    * @param message the message
113    * @return the string
114    */
115   public static String generateJsonErrorResponse(String message) {
116     return String.format("{ \"errorMessage\" : %s }", message);
117   }
118
119   /**
120    * Sets the servlet response.
121    *
122    * @param logger the logger
123    * @param isError the is error
124    * @param responseCode the response code
125    * @param response the response
126    * @param postPayload the post payload
127    * @throws IOException Signals that an I/O exception has occurred.
128    */
129   public static void setServletResponse(Logger logger, boolean isError, int responseCode,
130       HttpServletResponse response, String postPayload) throws IOException {
131
132     if (isError) {
133       logger.error(AaiUiMsgs.ERROR_GENERIC, postPayload);
134     }
135
136     response.setStatus(responseCode);
137
138     if (postPayload != null) {
139       response.setContentType("application/json");
140       PrintWriter out = response.getWriter();
141       out.println(postPayload);
142       out.close();
143     }
144   }
145
146   /**
147    * Gets the full url.
148    *
149    * @param elasticConfig the elastic config
150    * @param resourceUrl the resource url
151    * @return the full url
152    */
153   public static String getFullUrl(String eHost,String ePort, String resourceUrl) {
154     final String host = eHost;
155     final String port = ePort;
156     return String.format("http://%s:%s%s", host, port, resourceUrl);
157   }
158 }