2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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 package org.onap.aai.sa.rest;
23 import java.security.cert.X509Certificate;
24 import javax.security.auth.x500.X500Principal;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import org.onap.aai.sa.auth.SearchDbServiceAuth;
28 import org.onap.aai.sa.rest.ApiUtils.Action;
29 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
30 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.ElasticSearchHttpController;
31 import org.springframework.http.HttpHeaders;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
34 import org.springframework.stereotype.Component;
35 import org.springframework.web.bind.annotation.PathVariable;
36 import org.springframework.web.bind.annotation.RequestBody;
37 import org.springframework.web.bind.annotation.RequestHeader;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.bind.annotation.RequestMethod;
40 import org.springframework.web.bind.annotation.RestController;
45 @RequestMapping("/services/search-data-service/v1/search")
46 public class SearchServiceApi {
49 * The Data Access Object that we will use to interact with the
52 protected DocumentStoreInterface documentStore = null;
53 protected ApiUtils apiUtils = null;
56 * Create a new instance of the end point.
58 public SearchServiceApi() {
60 // Perform one-time initialization.
66 * Performs all one-time initialization required for the end point.
70 // Instantiate our Document Store DAO.
71 documentStore = ElasticSearchHttpController.getInstance();
73 apiUtils = new ApiUtils();
76 @RequestMapping(value = "/indexes/{index}",
77 method = RequestMethod.PUT,
78 produces = { "application/json" })
79 public ResponseEntity<String> processCreateIndex(@RequestBody String requestBody,
80 HttpServletRequest request,
81 @RequestHeader HttpHeaders headers,
82 @PathVariable("index") String index) {
84 // Forward the request to our index API to create the index.
85 IndexApi indexApi = new IndexApi(this);
86 return indexApi.processCreateIndex(requestBody, request, headers, index, documentStore);
89 @RequestMapping(value = "/indexes/{index}",
90 method = RequestMethod.DELETE,
91 consumes = {"application/json"},
92 produces = {"application/json"})
93 public ResponseEntity<String> processDeleteIndex(HttpServletRequest request,
94 @RequestHeader HttpHeaders headers,
95 @PathVariable ("index") String index) {
97 // Forward the request to our index API to delete the index.
98 IndexApi indexApi = new IndexApi(this);
99 return indexApi.processDelete(index, request, headers, documentStore);
103 @RequestMapping(value = "/indexes/{index}/documents",
104 method = RequestMethod.POST,
105 consumes = {"application/json"})
106 public ResponseEntity<String> processCreateDocWithoutId(@RequestBody String requestBody,
107 HttpServletRequest request,
108 HttpServletResponse httpResponse,
109 @RequestHeader HttpHeaders headers,
110 @PathVariable ("index") String index) {
112 // Forward the request to our document API to create the document.
113 DocumentApi documentApi = new DocumentApi(this);
114 return documentApi.processPost(requestBody, request, headers, httpResponse,
115 index, documentStore);
118 @RequestMapping(value = "/indexes/{index}/documents/{id}",
119 method = RequestMethod.PUT,
120 consumes = {"application/json"})
121 public ResponseEntity<String> processUpsertDoc(@RequestBody String requestBody,
122 HttpServletRequest request,
123 HttpServletResponse httpResponse,
124 @RequestHeader HttpHeaders headers,
125 @PathVariable ("index") String index,
126 @PathVariable ("id") String id) {
128 // Forward the request to our document API to upsert the document.
129 DocumentApi documentApi = new DocumentApi(this);
130 return documentApi.processPut(requestBody, request, headers, httpResponse,
131 index, id, documentStore);
134 @RequestMapping(value = "/indexes/{index}/documents/{id}",
135 method = RequestMethod.GET)
136 public ResponseEntity<String> processGetDocument(HttpServletRequest request,
137 HttpServletResponse httpResponse,
138 @RequestHeader HttpHeaders headers,
139 @PathVariable ("index") String index,
140 @PathVariable ("id") String id) {
142 // Forward the request to our document API to retrieve the document.
143 DocumentApi documentApi = new DocumentApi(this);
144 return documentApi.processGet("", request, headers, httpResponse,
145 index, id, documentStore);
148 @RequestMapping(value = "/indexes/{index}/documents/{id}",
149 method = RequestMethod.DELETE,
150 consumes = {"application/json"})
151 public ResponseEntity<String> processDeleteDoc(HttpServletRequest request,
152 HttpServletResponse httpResponse,
153 @RequestHeader HttpHeaders headers,
154 @PathVariable ("index") String index,
155 @PathVariable ("id") String id) {
157 // Forward the request to our document API to delete the document.
158 DocumentApi documentApi = new DocumentApi(this);
159 return documentApi.processDelete("", request, headers, httpResponse,
160 index, id, documentStore);
163 @RequestMapping(value = "/indexes/{index}/query/{queryText}",
164 method = RequestMethod.GET)
165 public ResponseEntity<String> processInlineQuery(HttpServletRequest request,
166 @RequestHeader HttpHeaders headers,
167 @PathVariable ("index") String index,
168 @PathVariable ("queryText") String queryText) {
170 // Forward the request to our document API to delete the document.
171 DocumentApi documentApi = new DocumentApi(this);
172 return documentApi.processSearchWithGet("", request, headers,
173 index, queryText, documentStore);
176 @RequestMapping(value = "/indexes/{index}/query",
177 method = RequestMethod.GET,
178 consumes = {"application/json"})
179 public ResponseEntity<String> processQueryWithGet(@RequestBody String requestBody,
180 HttpServletRequest request,
181 @RequestHeader HttpHeaders headers,
182 @PathVariable ("index") String index) {
184 // Forward the request to our document API to delete the document.
185 DocumentApi documentApi = new DocumentApi(this);
186 return documentApi.queryWithGetWithPayload(requestBody, request, headers, index, documentStore);
189 @RequestMapping(value = "/indexes/{index}/query",
190 method = RequestMethod.POST,
191 consumes = {"application/json"})
192 public ResponseEntity<String> processQuery(@RequestBody String requestBody,
193 HttpServletRequest request,
194 @RequestHeader HttpHeaders headers,
195 @PathVariable ("index") String index) {
197 // Forward the request to our document API to delete the document.
198 DocumentApi documentApi = new DocumentApi(this);
199 return documentApi.processSearchWithPost(requestBody, request, headers, index, documentStore);
202 @RequestMapping(value = "/indexes/{index}/suggest",
203 method = RequestMethod.POST,
204 consumes = {"application/json"})
205 public ResponseEntity<String> processSuggestQuery(@RequestBody String requestBody, HttpServletRequest request,
206 @RequestHeader HttpHeaders headers, @PathVariable("index") String index) {
207 // Forward the request to our document API to query suggestions in the
209 DocumentApi documentApi = new DocumentApi(this);
210 return documentApi.processSuggestQueryWithPost(requestBody, request, headers, index,
214 @RequestMapping(value = "/indexes/dynamic/{index}",
215 method = RequestMethod.PUT,
216 consumes = {"application/json"})
217 public ResponseEntity<String> processCreateDynamicIndex(@RequestBody String requestBody,
218 HttpServletRequest request,
219 @RequestHeader HttpHeaders headers,
220 @PathVariable ("index") String index) {
222 // Forward the request to our index API to create the index.
223 IndexApi indexApi = new IndexApi(this);
224 return indexApi.processCreateDynamicIndex(requestBody, request, headers, index, documentStore);
227 @RequestMapping(value = "/bulk",
228 method = RequestMethod.POST,
229 consumes = {"application/json"},
230 produces = { "application/json"})
231 public ResponseEntity<String> processBulkRequest(@RequestBody String requestBody,
232 HttpServletRequest request,
233 @RequestHeader HttpHeaders headers) {
235 // Forward the request to our document API to delete the document.
236 BulkApi bulkApi = new BulkApi(this);
237 ResponseEntity<String> dbugResp = bulkApi.processPost(requestBody, request, headers, documentStore, apiUtils);
241 protected boolean validateRequest(HttpHeaders headers,
242 HttpServletRequest req,
244 String authPolicyFunctionName) throws Exception {
246 SearchDbServiceAuth serviceAuth = new SearchDbServiceAuth();
248 String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
249 String authUser = null;
250 if (cipherSuite != null) {
251 Object x509CertAttribute = req.getAttribute("javax.servlet.request.X509Certificate");
252 if (x509CertAttribute != null) {
253 X509Certificate[] certChain = (X509Certificate[]) x509CertAttribute;
254 X509Certificate clientCert = certChain[0];
255 X500Principal subjectDn = clientCert.getSubjectX500Principal();
256 authUser = subjectDn.toString();
260 if (authUser == null) {
264 String status = serviceAuth.authUser(headers, authUser.toLowerCase(),
265 action.toString() + ":" + authPolicyFunctionName);
266 if (!status.equals("OK")) {