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.viewandinspect.servlet;
25 import java.io.IOException;
26 import java.io.PrintWriter;
27 import java.util.HashMap;
30 import javax.servlet.ServletException;
31 import javax.servlet.http.HttpServlet;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
35 import org.json.JSONException;
36 import org.json.JSONObject;
37 import org.onap.aai.sparky.config.oxm.OxmModelLoader;
38 import org.onap.aai.sparky.dal.elasticsearch.SearchAdapter;
39 import org.onap.aai.sparky.dal.rest.OperationResult;
40 import org.onap.aai.sparky.dal.sas.config.SearchServiceConfig;
41 import org.onap.aai.sparky.logging.AaiUiMsgs;
42 import org.onap.aai.sparky.search.VnfSearchService;
43 import org.onap.aai.sparky.search.config.SuggestionConfig;
44 import org.onap.aai.sparky.util.NodeUtils;
45 import org.onap.aai.sparky.viewandinspect.services.SearchServiceWrapper;
46 import org.onap.aai.cl.api.Logger;
47 import org.onap.aai.cl.eelf.LoggerFactory;
48 import org.onap.aai.cl.mdc.MdcContext;
51 * The Class SearchServlet.
54 public class SearchServlet extends HttpServlet {
56 private static final long serialVersionUID = 1L;
59 * @return the searchWrapper
61 public SearchServiceWrapper getSearchWrapper() {
66 * @param searchWrapper the searchWrapper to set
68 public void setSearchWrapper(SearchServiceWrapper searchWrapper) {
69 this.searchWrapper = searchWrapper;
73 * @return the serialversionuid
75 public static long getSerialversionuid() {
76 return serialVersionUID;
82 public static Logger getLog() {
87 * @return the keyPayload
89 public static String getKeyPayload() {
94 private static final Logger LOG = LoggerFactory.getInstance().getLogger(SearchServlet.class);
96 private SearchServiceWrapper searchWrapper = null;
98 private static final String KEY_PAYLOAD = "payload";
101 * Instantiates a new search servlet.
103 public SearchServlet() {
109 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
110 * javax.servlet.http.HttpServletResponse)
113 public void doGet(HttpServletRequest request, HttpServletResponse response)
114 throws ServletException, IOException {
115 doPost(request, response);
118 public void destroy() {
119 // TODO Auto-generated method stub
123 public void init() throws ServletException {
125 searchWrapper = new SearchServiceWrapper();
128 protected Map<String, String> getPayloadParams(JSONObject parameters) {
129 Map<String, String> payloadParams = new HashMap<String, String>();
131 JSONObject payload = parameters.getJSONObject(KEY_PAYLOAD);
132 if (payload.length() > 0) {
133 for (String key : JSONObject.getNames(payload)) {
134 payloadParams.put(key, payload.getString(key));
137 } catch (JSONException exc) {
138 LOG.error(AaiUiMsgs.ERROR_PARSING_PARAMS, exc);
140 return payloadParams;
146 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
147 * javax.servlet.http.HttpServletResponse)
150 public void doPost(HttpServletRequest request, HttpServletResponse response)
151 throws ServletException, IOException {
152 String txnID = request.getHeader("X-TransactionId");
154 txnID = NodeUtils.getRandomTxnId();
157 String partnerName = request.getHeader("X-FromAppId");
158 if (partnerName == null) {
159 partnerName = "Browser";
161 MdcContext.initialize(txnID, "AAI_UI", "", partnerName, request.getRemoteAddr());
162 searchWrapper.doPost(request, response);
166 * Generate json error response.
168 * @param message the message
172 * This is the manual approach, however we could also create an object container for the error
173 * then use the Jackson ObjectWrite to dump the object to json instead. If it gets any more
174 * complicated we could do that approach so we don't have to manually trip over the JSON
177 protected String generateJsonErrorResponse(String message) {
178 return String.format("{ \"errorMessage\" : %s }", message);
182 * Handle search servlet errors.
184 * @param errorMsg the error msg
186 * @param response the response
187 * @throws IOException Signals that an I/O exception has occurred.
189 public void handleSearchServletErrors(String errorMsg, Exception exc,
190 HttpServletResponse response) throws IOException {
193 (exc == null ? errorMsg : errorMsg + ". Error:" + exc.getLocalizedMessage());
195 LOG.error(AaiUiMsgs.SEARCH_SERVLET_ERROR, errorLogMsg);
197 response.setContentType("application/json");
198 PrintWriter out = response.getWriter();
199 out.println(generateJsonErrorResponse(errorMsg));
205 * Sets the servlet response.
207 * @param response the response
208 * @param postPayload the post payload
210 * @throws IOException Signals that an I/O exception has occurred.
212 private void setServletResponse(HttpServletResponse response, String postPayload)
215 if (postPayload != null) {
216 response.setContentType("application/json");
217 PrintWriter out = response.getWriter();
218 out.println(postPayload);