2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.util;
28 import java.io.IOException;
29 import java.io.PrintWriter;
31 import javax.servlet.http.HttpServletResponse;
33 import org.openecomp.cl.api.Logger;
34 import org.openecomp.sparky.logging.AaiUiMsgs;
35 import org.openecomp.sparky.dal.elasticsearch.SearchAdapter;
36 import org.openecomp.sparky.dal.elasticsearch.config.ElasticSearchConfig;
37 import org.openecomp.sparky.dal.rest.OperationResult;
40 * The Class ServletUtils.
42 public class ServletUtils {
47 * @param logger the logger
48 * @param search the search
49 * @param response the response
50 * @param requestUrl the request url
51 * @return the operation result
52 * @throws Exception the exception
54 public static OperationResult executeGetQuery(Logger logger, SearchAdapter search,
55 HttpServletResponse response, String requestUrl) throws Exception {
57 OperationResult opResult = search.doGet(requestUrl, "application/json");
59 if (opResult.getResultCode() > 300) {
60 setServletResponse(logger, true, opResult.getResultCode(), response, opResult.getResult());
62 response.setStatus(opResult.getResultCode());
72 * @param logger the logger
73 * @param search the search
74 * @param response the response
75 * @param requestUrl the request url
76 * @param requestJsonPayload the request json payload
77 * @return the operation result
78 * @throws Exception the exception
80 public static OperationResult executePostQuery(Logger logger, SearchAdapter search,
81 HttpServletResponse response, String requestUrl, String requestJsonPayload) throws Exception {
83 OperationResult opResult = search.doPost(requestUrl, requestJsonPayload, "application/json");
85 if (opResult.getResultCode() > 300) {
86 setServletResponse(logger, true, opResult.getResultCode(), response, opResult.getResult());
89 response.setStatus(opResult.getResultCode());
96 * Handle search servlet errors.
98 * @param logger the logger
99 * @param errorMsg the error msg
101 * @param response the response
102 * @throws IOException Signals that an I/O exception has occurred.
104 public static void handleSearchServletErrors(Logger logger, String errorMsg, Exception exc,
105 HttpServletResponse response) throws IOException {
106 String errorLogMsg = (exc == null ? errorMsg : errorMsg + ". Error:"
107 + exc.getLocalizedMessage());
108 logger.error(AaiUiMsgs.ERROR_GENERIC, errorLogMsg);
109 response.setContentType("application/json");
110 PrintWriter out = response.getWriter();
111 out.println(generateJsonErrorResponse(errorMsg));
116 * Generate json error response.
118 * @param message the message
121 public static String generateJsonErrorResponse(String message) {
122 return String.format("{ \"errorMessage\" : %s }", message);
126 * Sets the servlet response.
128 * @param logger the logger
129 * @param isError the is error
130 * @param responseCode the response code
131 * @param response the response
132 * @param postPayload the post payload
133 * @throws IOException Signals that an I/O exception has occurred.
135 public static void setServletResponse(Logger logger, boolean isError, int responseCode,
136 HttpServletResponse response, String postPayload) throws IOException {
139 logger.error(AaiUiMsgs.ERROR_GENERIC, postPayload);
142 response.setStatus(responseCode);
144 if (postPayload != null) {
145 response.setContentType("application/json");
146 PrintWriter out = response.getWriter();
147 out.println(postPayload);
155 * @param elasticConfig the elastic config
156 * @param resourceUrl the resource url
157 * @return the full url
159 public static String getFullUrl(ElasticSearchConfig elasticConfig, String resourceUrl) {
160 final String host = elasticConfig.getIpAddress();
161 final String port = elasticConfig.getHttpPort();
162 return String.format("http://%s:%s%s", host, port, resourceUrl);