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.openecomp.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.openecomp.cl.api.Logger;
38 import org.openecomp.cl.eelf.LoggerFactory;
39 import org.openecomp.sparky.config.oxm.OxmModelLoader;
40 import org.openecomp.sparky.dal.elasticsearch.SearchAdapter;
41 import org.openecomp.sparky.dal.rest.OperationResult;
42 import org.openecomp.sparky.dal.sas.config.SearchServiceConfig;
43 import org.openecomp.sparky.logging.AaiUiMsgs;
44 import org.openecomp.sparky.search.VnfSearchService;
45 import org.openecomp.sparky.search.config.SuggestionConfig;
46 import org.openecomp.sparky.util.NodeUtils;
47 import org.openecomp.sparky.viewandinspect.services.SearchServiceWrapper;
49 import org.openecomp.cl.mdc.MdcContext;
52 * The Class SearchServlet.
55 public class SearchServlet extends HttpServlet {
57 private static final long serialVersionUID = 1L;
59 private static final Logger LOG = LoggerFactory.getInstance().getLogger(SearchServlet.class);
61 private SearchServiceWrapper searchWrapper = null;
63 private static final String KEY_PAYLOAD = "payload";
66 * Instantiates a new search servlet.
68 public SearchServlet() {
74 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
75 * javax.servlet.http.HttpServletResponse)
78 public void doGet(HttpServletRequest request, HttpServletResponse response)
79 throws ServletException, IOException {
80 doPost(request, response);
83 public void destroy() {
84 // TODO Auto-generated method stub
88 public void init() throws ServletException {
90 searchWrapper = new SearchServiceWrapper();
93 protected Map<String, String> getPayloadParams(JSONObject parameters) {
94 Map<String, String> payloadParams = new HashMap<String, String>();
96 JSONObject payload = parameters.getJSONObject(KEY_PAYLOAD);
97 if (payload.length() > 0) {
98 for (String key : JSONObject.getNames(payload)) {
99 payloadParams.put(key, payload.getString(key));
102 } catch (JSONException exc) {
103 LOG.error(AaiUiMsgs.ERROR_PARSING_PARAMS, exc);
105 return payloadParams;
111 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
112 * javax.servlet.http.HttpServletResponse)
115 public void doPost(HttpServletRequest request, HttpServletResponse response)
116 throws ServletException, IOException {
117 String txnID = request.getHeader("X-TransactionId");
119 txnID = NodeUtils.getRandomTxnId();
122 String partnerName = request.getHeader("X-FromAppId");
123 if (partnerName == null) {
124 partnerName = "Browser";
126 MdcContext.initialize(txnID, "AAI_UI", "", partnerName, request.getRemoteAddr());
127 searchWrapper.doPost(request, response);
131 * Generate json error response.
133 * @param message the message
137 * This is the manual approach, however we could also create an object container for the error
138 * then use the Jackson ObjectWrite to dump the object to json instead. If it gets any more
139 * complicated we could do that approach so we don't have to manually trip over the JSON
142 protected String generateJsonErrorResponse(String message) {
143 return String.format("{ \"errorMessage\" : %s }", message);
147 * Handle search servlet errors.
149 * @param errorMsg the error msg
151 * @param response the response
152 * @throws IOException Signals that an I/O exception has occurred.
154 public void handleSearchServletErrors(String errorMsg, Exception exc,
155 HttpServletResponse response) throws IOException {
158 (exc == null ? errorMsg : errorMsg + ". Error:" + exc.getLocalizedMessage());
160 LOG.error(AaiUiMsgs.SEARCH_SERVLET_ERROR, errorLogMsg);
162 response.setContentType("application/json");
163 PrintWriter out = response.getWriter();
164 out.println(generateJsonErrorResponse(errorMsg));
170 * Sets the servlet response.
172 * @param response the response
173 * @param postPayload the post payload
175 * @throws IOException Signals that an I/O exception has occurred.
177 private void setServletResponse(HttpServletResponse response, String postPayload)
180 if (postPayload != null) {
181 response.setContentType("application/json");
182 PrintWriter out = response.getWriter();
183 out.println(postPayload);