Changing the license and trademark
[aai/sparky-be.git] / src / main / java / org / openecomp / 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.openecomp.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.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;
48
49 import org.openecomp.cl.mdc.MdcContext;
50
51 /**
52  * The Class SearchServlet.
53  */
54
55 public class SearchServlet extends HttpServlet {
56
57   private static final long serialVersionUID = 1L;
58
59   private static final Logger LOG = LoggerFactory.getInstance().getLogger(SearchServlet.class);
60
61   private SearchServiceWrapper searchWrapper = null;
62
63   private static final String KEY_PAYLOAD = "payload";
64   
65   /**
66    * Instantiates a new search servlet.
67    */
68   public SearchServlet() {
69   }
70
71   /*
72    * (non-Javadoc)
73    * 
74    * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
75    * javax.servlet.http.HttpServletResponse)
76    */
77   @Override
78   public void doGet(HttpServletRequest request, HttpServletResponse response)
79       throws ServletException, IOException {
80     doPost(request, response);
81   }
82
83    public void destroy() {
84     // TODO Auto-generated method stub
85     super.destroy();
86   }
87   
88   public void init() throws ServletException {
89     super.init();
90     searchWrapper = new SearchServiceWrapper();
91   }
92
93   protected Map<String, String> getPayloadParams(JSONObject parameters) {
94     Map<String, String> payloadParams = new HashMap<String, String>();
95     try {
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));
100         }
101       }
102     } catch (JSONException exc) {
103       LOG.error(AaiUiMsgs.ERROR_PARSING_PARAMS, exc);
104     }
105     return payloadParams;
106   }
107
108   /*
109    * (non-Javadoc)
110    * 
111    * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
112    * javax.servlet.http.HttpServletResponse)
113    */
114   @Override
115   public void doPost(HttpServletRequest request, HttpServletResponse response)
116       throws ServletException, IOException {
117     String txnID = request.getHeader("X-TransactionId");
118     if (txnID == null) { 
119       txnID = NodeUtils.getRandomTxnId();
120     }
121
122     String partnerName = request.getHeader("X-FromAppId");
123     if (partnerName == null) {
124       partnerName = "Browser";
125     }
126     MdcContext.initialize(txnID, "AAI_UI", "", partnerName, request.getRemoteAddr());
127     searchWrapper.doPost(request, response);
128   }
129
130   /**
131    * Generate json error response.
132    *
133    * @param message the message
134    * @return the string
135    */
136   /*
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
140    * formatting.
141    */
142   protected String generateJsonErrorResponse(String message) {
143     return String.format("{ \"errorMessage\" : %s }", message);
144   }
145
146   /**
147    * Handle search servlet errors.
148    *
149    * @param errorMsg the error msg
150    * @param exc the exc
151    * @param response the response
152    * @throws IOException Signals that an I/O exception has occurred.
153    */
154   public void handleSearchServletErrors(String errorMsg, Exception exc,
155       HttpServletResponse response) throws IOException {
156
157     String errorLogMsg =
158         (exc == null ? errorMsg : errorMsg + ". Error:" + exc.getLocalizedMessage());
159
160     LOG.error(AaiUiMsgs.SEARCH_SERVLET_ERROR, errorLogMsg);
161
162     response.setContentType("application/json");
163     PrintWriter out = response.getWriter();
164     out.println(generateJsonErrorResponse(errorMsg));
165     out.close();
166   }
167
168
169   /**
170    * Sets the servlet response.
171    * 
172    * @param response the response
173    * @param postPayload the post payload
174    *
175    * @throws IOException Signals that an I/O exception has occurred.
176    */
177   private void setServletResponse(HttpServletResponse response, String postPayload)
178       throws IOException {
179
180     if (postPayload != null) {
181       response.setContentType("application/json");
182       PrintWriter out = response.getWriter();
183       out.println(postPayload);
184       out.close();
185     }
186   }
187
188   
189
190   
191 }