Update the dependencies to use project version
[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   /**
59    * @return the searchWrapper
60    */
61   public SearchServiceWrapper getSearchWrapper() {
62     return searchWrapper;
63   }
64
65   /**
66    * @param searchWrapper the searchWrapper to set
67    */
68   public void setSearchWrapper(SearchServiceWrapper searchWrapper) {
69     this.searchWrapper = searchWrapper;
70   }
71
72   /**
73    * @return the serialversionuid
74    */
75   public static long getSerialversionuid() {
76     return serialVersionUID;
77   }
78
79   /**
80    * @return the log
81    */
82   public static Logger getLog() {
83     return LOG;
84   }
85
86   /**
87    * @return the keyPayload
88    */
89   public static String getKeyPayload() {
90     return KEY_PAYLOAD;
91   }
92
93
94   private static final Logger LOG = LoggerFactory.getInstance().getLogger(SearchServlet.class);
95
96   private SearchServiceWrapper searchWrapper = null;
97
98   private static final String KEY_PAYLOAD = "payload";
99
100   /**
101    * Instantiates a new search servlet.
102    */
103   public SearchServlet() {}
104
105   /*
106    * (non-Javadoc)
107    * 
108    * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
109    * javax.servlet.http.HttpServletResponse)
110    */
111   @Override
112   public void doGet(HttpServletRequest request, HttpServletResponse response)
113       throws ServletException, IOException {
114     doPost(request, response);
115   }
116
117   public void destroy() {
118     // TODO Auto-generated method stub
119     super.destroy();
120   }
121
122   public void init() throws ServletException {
123     super.init();
124     searchWrapper = new SearchServiceWrapper();
125   }
126
127   protected Map<String, String> getPayloadParams(JSONObject parameters) {
128     Map<String, String> payloadParams = new HashMap<String, String>();
129     try {
130       JSONObject payload = parameters.getJSONObject(KEY_PAYLOAD);
131       if (payload.length() > 0) {
132         for (String key : JSONObject.getNames(payload)) {
133           payloadParams.put(key, payload.getString(key));
134         }
135       }
136     } catch (JSONException exc) {
137       LOG.error(AaiUiMsgs.ERROR_PARSING_PARAMS, exc);
138     }
139     return payloadParams;
140   }
141
142   /*
143    * (non-Javadoc)
144    * 
145    * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
146    * javax.servlet.http.HttpServletResponse)
147    */
148   @Override
149   public void doPost(HttpServletRequest request, HttpServletResponse response)
150       throws ServletException, IOException {
151     String txnID = request.getHeader("X-TransactionId");
152     if (txnID == null) {
153       txnID = NodeUtils.getRandomTxnId();
154     }
155
156     String partnerName = request.getHeader("X-FromAppId");
157     if (partnerName == null) {
158       partnerName = "Browser";
159     }
160     MdcContext.initialize(txnID, "AAI_UI", "", partnerName, request.getRemoteAddr());
161     searchWrapper.doPost(request, response);
162   }
163
164   /**
165    * Generate json error response.
166    *
167    * @param message the message
168    * @return the string
169    */
170   /*
171    * This is the manual approach, however we could also create an object container for the error
172    * then use the Jackson ObjectWrite to dump the object to json instead. If it gets any more
173    * complicated we could do that approach so we don't have to manually trip over the JSON
174    * formatting.
175    */
176   protected String generateJsonErrorResponse(String message) {
177     return String.format("{ \"errorMessage\" : %s }", message);
178   }
179
180   /**
181    * Handle search servlet errors.
182    *
183    * @param errorMsg the error msg
184    * @param exc the exc
185    * @param response the response
186    * @throws IOException Signals that an I/O exception has occurred.
187    */
188   public void handleSearchServletErrors(String errorMsg, Exception exc,
189       HttpServletResponse response) throws IOException {
190
191     String errorLogMsg =
192         (exc == null ? errorMsg : errorMsg + ". Error:" + exc.getLocalizedMessage());
193
194     LOG.error(AaiUiMsgs.SEARCH_SERVLET_ERROR, errorLogMsg);
195
196     response.setContentType("application/json");
197     PrintWriter out = response.getWriter();
198     out.println(generateJsonErrorResponse(errorMsg));
199     out.close();
200   }
201
202
203   /**
204    * Sets the servlet response.
205    * 
206    * @param response the response
207    * @param postPayload the post payload
208    *
209    * @throws IOException Signals that an I/O exception has occurred.
210    */
211   private void setServletResponse(HttpServletResponse response, String postPayload)
212       throws IOException {
213
214     if (postPayload != null) {
215       response.setContentType("application/json");
216       PrintWriter out = response.getWriter();
217       out.println(postPayload);
218       out.close();
219     }
220   }
221
222
223
224 }