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.viewandinspect.servlet;
28 import java.io.IOException;
29 import java.io.PrintWriter;
30 import java.util.HashMap;
33 import javax.servlet.ServletException;
34 import javax.servlet.http.HttpServlet;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
38 import org.json.JSONException;
39 import org.json.JSONObject;
40 import org.openecomp.cl.api.Logger;
41 import org.openecomp.cl.eelf.LoggerFactory;
42 import org.openecomp.sparky.config.oxm.OxmModelLoader;
43 import org.openecomp.sparky.dal.elasticsearch.SearchAdapter;
44 import org.openecomp.sparky.dal.rest.OperationResult;
45 import org.openecomp.sparky.dal.sas.config.SearchServiceConfig;
46 import org.openecomp.sparky.logging.AaiUiMsgs;
47 import org.openecomp.sparky.search.VnfSearchService;
48 import org.openecomp.sparky.search.config.SuggestionConfig;
49 import org.openecomp.sparky.util.NodeUtils;
50 import org.openecomp.sparky.viewandinspect.services.SearchServiceWrapper;
52 import org.openecomp.cl.mdc.MdcContext;
55 * The Class SearchServlet.
58 public class SearchServlet extends HttpServlet {
60 private static final long serialVersionUID = 1L;
62 private static final Logger LOG = LoggerFactory.getInstance().getLogger(SearchServlet.class);
64 private SearchServiceWrapper searchWrapper = null;
66 private static final String KEY_PAYLOAD = "payload";
69 * Instantiates a new search servlet.
71 public SearchServlet() {
77 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
78 * javax.servlet.http.HttpServletResponse)
81 public void doGet(HttpServletRequest request, HttpServletResponse response)
82 throws ServletException, IOException {
83 doPost(request, response);
86 public void destroy() {
87 // TODO Auto-generated method stub
91 public void init() throws ServletException {
93 searchWrapper = new SearchServiceWrapper();
96 protected Map<String, String> getPayloadParams(JSONObject parameters) {
97 Map<String, String> payloadParams = new HashMap<String, String>();
99 JSONObject payload = parameters.getJSONObject(KEY_PAYLOAD);
100 if (payload.length() > 0) {
101 for (String key : JSONObject.getNames(payload)) {
102 payloadParams.put(key, payload.getString(key));
105 } catch (JSONException exc) {
106 LOG.error(AaiUiMsgs.ERROR_PARSING_PARAMS, exc);
108 return payloadParams;
114 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
115 * javax.servlet.http.HttpServletResponse)
118 public void doPost(HttpServletRequest request, HttpServletResponse response)
119 throws ServletException, IOException {
120 String txnID = request.getHeader("X-TransactionId");
122 txnID = NodeUtils.getRandomTxnId();
125 String partnerName = request.getHeader("X-FromAppId");
126 if (partnerName == null) {
127 partnerName = "Browser";
129 MdcContext.initialize(txnID, "AAI_UI", "", partnerName, request.getRemoteAddr());
130 searchWrapper.doPost(request, response);
134 * Generate json error response.
136 * @param message the message
140 * This is the manual approach, however we could also create an object container for the error
141 * then use the Jackson ObjectWrite to dump the object to json instead. If it gets any more
142 * complicated we could do that approach so we don't have to manually trip over the JSON
145 protected String generateJsonErrorResponse(String message) {
146 return String.format("{ \"errorMessage\" : %s }", message);
150 * Handle search servlet errors.
152 * @param errorMsg the error msg
154 * @param response the response
155 * @throws IOException Signals that an I/O exception has occurred.
157 public void handleSearchServletErrors(String errorMsg, Exception exc,
158 HttpServletResponse response) throws IOException {
161 (exc == null ? errorMsg : errorMsg + ". Error:" + exc.getLocalizedMessage());
163 LOG.error(AaiUiMsgs.SEARCH_SERVLET_ERROR, errorLogMsg);
165 response.setContentType("application/json");
166 PrintWriter out = response.getWriter();
167 out.println(generateJsonErrorResponse(errorMsg));
173 * Sets the servlet response.
175 * @param response the response
176 * @param postPayload the post payload
178 * @throws IOException Signals that an I/O exception has occurred.
180 private void setServletResponse(HttpServletResponse response, String postPayload)
183 if (postPayload != null) {
184 response.setContentType("application/json");
185 PrintWriter out = response.getWriter();
186 out.println(postPayload);