2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.logging.util;
25 import java.io.IOException;
26 import java.io.PrintWriter;
28 import javax.servlet.http.HttpServletResponse;
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;
37 * The Class ServletUtils.
39 public class ServletUtils {
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
51 public static OperationResult executeGetQuery(Logger logger, SearchAdapter search,
52 HttpServletResponse response, String requestUrl) throws Exception {
54 OperationResult opResult = search.doGet(requestUrl, "application/json");
56 if (opResult.getResultCode() > 300) {
57 setServletResponse(logger, true, opResult.getResultCode(), response, opResult.getResult());
59 response.setStatus(opResult.getResultCode());
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
77 public static OperationResult executePostQuery(Logger logger, SearchAdapter search,
78 HttpServletResponse response, String requestUrl, String requestJsonPayload) throws Exception {
80 OperationResult opResult = search.doPost(requestUrl, requestJsonPayload, "application/json");
82 if (opResult.getResultCode() > 300) {
83 setServletResponse(logger, true, opResult.getResultCode(), response, opResult.getResult());
86 response.setStatus(opResult.getResultCode());
93 * Handle search servlet errors.
95 * @param logger the logger
96 * @param errorMsg the error msg
98 * @param response the response
99 * @throws IOException Signals that an I/O exception has occurred.
101 public static void handleSearchServletErrors(Logger logger, String errorMsg, Exception exc,
102 HttpServletResponse response) throws IOException {
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));
113 * Generate json error response.
115 * @param message the message
118 public static String generateJsonErrorResponse(String message) {
119 return String.format("{ \"errorMessage\" : %s }", message);
123 * Sets the servlet response.
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.
132 public static void setServletResponse(Logger logger, boolean isError, int responseCode,
133 HttpServletResponse response, String postPayload) throws IOException {
136 logger.error(AaiUiMsgs.ERROR_GENERIC, postPayload);
139 response.setStatus(responseCode);
141 if (postPayload != null) {
142 response.setContentType("application/json");
143 PrintWriter out = response.getWriter();
144 out.println(postPayload);
152 * @param elasticConfig the elastic config
153 * @param resourceUrl the resource url
154 * @return the full url
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);