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.
25 package org.onap.aai.sparky.logging.util;
27 import java.io.IOException;
28 import java.io.PrintWriter;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
37 import org.apache.camel.Exchange;
38 import org.onap.aai.cl.api.Logger;
39 import org.onap.aai.cl.mdc.MdcContext;
40 import org.onap.aai.restclient.client.OperationResult;
41 import org.onap.aai.sparky.logging.AaiUiMsgs;
42 import org.onap.aai.sparky.search.SearchServiceAdapter;
43 import org.onap.aai.sparky.util.NodeUtils;
44 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
48 * The Class ServletUtils.
50 public class ServletUtils {
55 * @param logger the logger
56 * @param search the search
57 * @param response the response
58 * @param requestUrl the request url
59 * @return the operation result
60 * @throws Exception the exception
62 public static OperationResult executeGetQuery(Logger logger, SearchServiceAdapter search,
63 HttpServletResponse response, String requestUrl) throws Exception {
65 OperationResult opResult = search.doGet(requestUrl, "application/json");
67 if (opResult.getResultCode() > 300) {
68 setServletResponse(logger, true, opResult.getResultCode(), response, opResult.getResult());
70 response.setStatus(opResult.getResultCode());
80 * @param logger the logger
81 * @param search the search
82 * @param response the response
83 * @param requestUrl the request url
84 * @param requestJsonPayload the request json payload
85 * @return the operation result
86 * @throws Exception the exception
88 public static OperationResult executePostQuery(Logger logger, SearchServiceAdapter search,
89 HttpServletResponse response, String requestUrl, String requestJsonPayload) throws Exception {
91 OperationResult opResult = search.doPost(requestUrl, requestJsonPayload, "application/json");
93 if (opResult.getResultCode() > 300) {
94 setServletResponse(logger, true, opResult.getResultCode(), response, opResult.getResult());
97 response.setStatus(opResult.getResultCode());
104 * Handle search servlet errors.
106 * @param logger the logger
107 * @param errorMsg the error msg
109 * @param response the response
110 * @throws IOException Signals that an I/O exception has occurred.
112 public static void handleSearchServletErrors(Logger logger, String errorMsg, Exception exc,
113 HttpServletResponse response) throws IOException {
114 String errorLogMsg = (exc == null ? errorMsg : errorMsg + ". Error:"
115 + exc.getLocalizedMessage());
116 logger.error(AaiUiMsgs.ERROR_GENERIC, errorLogMsg);
117 response.setContentType("application/json");
118 PrintWriter out = response.getWriter();
119 out.println(generateJsonErrorResponse(errorMsg));
124 * Generate json error response.
126 * @param message the message
129 public static String generateJsonErrorResponse(String message) {
130 return String.format("{ \"errorMessage\" : %s }", message);
134 * Sets the servlet response.
136 * @param logger the logger
137 * @param isError the is error
138 * @param responseCode the response code
139 * @param response the response
140 * @param postPayload the post payload
141 * @throws IOException Signals that an I/O exception has occurred.
143 public static void setServletResponse(Logger logger, boolean isError, int responseCode,
144 HttpServletResponse response, String postPayload) throws IOException {
147 logger.error(AaiUiMsgs.ERROR_GENERIC, postPayload);
150 response.setStatus(responseCode);
152 if (postPayload != null) {
153 response.setContentType("application/json");
154 PrintWriter out = response.getWriter();
155 out.println(postPayload);
163 * @param elasticConfig the elastic config
164 * @param resourceUrl the resource url
165 * @return the full url
167 public static String getFullUrl(String eHost,String ePort, String resourceUrl) {
168 final String host = eHost;
169 final String port = ePort;
170 return String.format("http://%s:%s%s", host, port, resourceUrl);
173 public static void setUpMdcContext(final Exchange exchange, final HttpServletRequest request) {
177 Object xTransactionId = exchange.getIn().getHeader("X-TransactionId");
178 if (xTransactionId == null) {
179 txnId = NodeUtils.getRandomTxnId();
181 txnId = (String) xTransactionId;
186 Object partnerName = exchange.getIn().getHeader("X-FromAppId");
187 if (partnerName == null) {
188 fromAppId = SparkyConstants.APP_NAME;
190 fromAppId = (String) partnerName;
193 MdcContext.initialize(txnId, "AAI-UI", "", fromAppId,
194 request.getRequestURI() + ":" + request.getLocalPort());
197 public static Map<String, List<String>> getTxnHeaders() {
198 Map<String, List<String>> headers = new HashMap<String, List<String>>();
199 headers.put("X-TransactionId", Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
200 headers.put("X-FromAppId", Arrays.asList(MDC.get(MdcContext.MDC_PARTNER_NAME)));