Merge "Fix Blocker/Critical sonar issues"
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / viewandinspect / servlet / SearchServlet.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.viewandinspect.servlet;
24
25 import java.io.IOException;
26 import java.io.PrintWriter;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import javax.servlet.ServletException;
31 import javax.servlet.http.HttpServlet;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
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;
49
50 /**
51  * The Class SearchServlet.
52  */
53
54 public class SearchServlet extends HttpServlet {
55
56   private static final long serialVersionUID = 1L;
57
58   private static final Logger LOG = LoggerFactory.getInstance().getLogger(SearchServlet.class);
59
60   private SearchServiceWrapper searchWrapper = null;
61
62   private static final String KEY_PAYLOAD = "payload";
63   
64   /**
65    * Instantiates a new search servlet.
66    */
67   public SearchServlet() {
68   }
69
70   /*
71    * (non-Javadoc)
72    * 
73    * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
74    * javax.servlet.http.HttpServletResponse)
75    */
76   @Override
77   public void doGet(HttpServletRequest request, HttpServletResponse response)
78       throws ServletException, IOException {
79     doPost(request, response);
80   }
81
82    public void destroy() {
83     // TODO Auto-generated method stub
84     super.destroy();
85   }
86   
87   public void init() throws ServletException {
88     super.init();
89     searchWrapper = new SearchServiceWrapper();
90   }
91
92   protected Map<String, String> getPayloadParams(JSONObject parameters) {
93     Map<String, String> payloadParams = new HashMap<String, String>();
94     try {
95       JSONObject payload = parameters.getJSONObject(KEY_PAYLOAD);
96       if (payload.length() > 0) {
97         for (String key : JSONObject.getNames(payload)) {
98           payloadParams.put(key, payload.getString(key));
99         }
100       }
101     } catch (JSONException exc) {
102       LOG.error(AaiUiMsgs.ERROR_PARSING_PARAMS, exc);
103     }
104     return payloadParams;
105   }
106
107   /*
108    * (non-Javadoc)
109    * 
110    * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
111    * javax.servlet.http.HttpServletResponse)
112    */
113   @Override
114   public void doPost(HttpServletRequest request, HttpServletResponse response)
115       throws ServletException, IOException {
116     String txnID = request.getHeader("X-TransactionId");
117     if (txnID == null) { 
118       txnID = NodeUtils.getRandomTxnId();
119     }
120
121     String partnerName = request.getHeader("X-FromAppId");
122     if (partnerName == null) {
123       partnerName = "Browser";
124     }
125     MdcContext.initialize(txnID, "AAI_UI", "", partnerName, request.getRemoteAddr());
126     searchWrapper.doPost(request, response);
127   }
128
129   /**
130    * Generate json error response.
131    *
132    * @param message the message
133    * @return the string
134    */
135   /*
136    * This is the manual approach, however we could also create an object container for the error
137    * then use the Jackson ObjectWrite to dump the object to json instead. If it gets any more
138    * complicated we could do that approach so we don't have to manually trip over the JSON
139    * formatting.
140    */
141   protected String generateJsonErrorResponse(String message) {
142     return String.format("{ \"errorMessage\" : %s }", message);
143   }
144
145   /**
146    * Handle search servlet errors.
147    *
148    * @param errorMsg the error msg
149    * @param exc the exc
150    * @param response the response
151    * @throws IOException Signals that an I/O exception has occurred.
152    */
153   public void handleSearchServletErrors(String errorMsg, Exception exc,
154       HttpServletResponse response) throws IOException {
155
156     String errorLogMsg =
157         (exc == null ? errorMsg : errorMsg + ". Error:" + exc.getLocalizedMessage());
158
159     LOG.error(AaiUiMsgs.SEARCH_SERVLET_ERROR, errorLogMsg);
160
161     response.setContentType("application/json");
162     PrintWriter out = response.getWriter();
163     out.println(generateJsonErrorResponse(errorMsg));
164     out.close();
165   }
166
167
168   /**
169    * Sets the servlet response.
170    * 
171    * @param response the response
172    * @param postPayload the post payload
173    *
174    * @throws IOException Signals that an I/O exception has occurred.
175    */
176   private void setServletResponse(HttpServletResponse response, String postPayload)
177       throws IOException {
178
179     if (postPayload != null) {
180       response.setContentType("application/json");
181       PrintWriter out = response.getWriter();
182       out.println(postPayload);
183       out.close();
184     }
185   }
186
187   
188
189   
190 }