2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sparky.logging.util;
23 import java.io.IOException;
24 import java.io.PrintWriter;
26 import javax.servlet.http.HttpServletResponse;
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;
34 * The Class ServletUtils.
36 public class ServletUtils {
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
48 public static OperationResult executeGetQuery(Logger logger, SearchServiceAdapter search,
49 HttpServletResponse response, String requestUrl) throws Exception {
51 OperationResult opResult = search.doGet(requestUrl, "application/json");
53 if (opResult.getResultCode() > 300) {
54 setServletResponse(logger, true, opResult.getResultCode(), response, opResult.getResult());
56 response.setStatus(opResult.getResultCode());
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
74 public static OperationResult executePostQuery(Logger logger, SearchServiceAdapter search,
75 HttpServletResponse response, String requestUrl, String requestJsonPayload) throws Exception {
77 OperationResult opResult = search.doPost(requestUrl, requestJsonPayload, "application/json");
79 if (opResult.getResultCode() > 300) {
80 setServletResponse(logger, true, opResult.getResultCode(), response, opResult.getResult());
83 response.setStatus(opResult.getResultCode());
90 * Handle search servlet errors.
92 * @param logger the logger
93 * @param errorMsg the error msg
95 * @param response the response
96 * @throws IOException Signals that an I/O exception has occurred.
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));
110 * Generate json error response.
112 * @param message the message
115 public static String generateJsonErrorResponse(String message) {
116 return String.format("{ \"errorMessage\" : %s }", message);
120 * Sets the servlet response.
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.
129 public static void setServletResponse(Logger logger, boolean isError, int responseCode,
130 HttpServletResponse response, String postPayload) throws IOException {
133 logger.error(AaiUiMsgs.ERROR_GENERIC, postPayload);
136 response.setStatus(responseCode);
138 if (postPayload != null) {
139 response.setContentType("application/json");
140 PrintWriter out = response.getWriter();
141 out.println(postPayload);
149 * @param elasticConfig the elastic config
150 * @param resourceUrl the resource url
151 * @return the full url
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);