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 document store.
51 protected DocumentStoreInterface documentStore = null;
52 protected ApiUtils apiUtils = null;
55 * Create a new instance of the end point.
57 public SearchServiceApi() {
59 // Perform one-time initialization.
65 * Performs all one-time initialization required for the end point.
69 // Instantiate our Document Store DAO.
70 documentStore = ElasticSearchHttpController.getInstance();
72 apiUtils = new ApiUtils();
75 @RequestMapping(value = "/indexes/{index}", method = RequestMethod.PUT, produces = {"application/json"})
76 public ResponseEntity<String> processCreateIndex(@RequestBody String requestBody, HttpServletRequest request,
77 @RequestHeader HttpHeaders headers, @PathVariable("index") String index) {
79 // Forward the request to our index API to create the index.
80 IndexApi indexApi = new IndexApi(this);
81 return indexApi.processCreateIndex(requestBody, request, headers, index, documentStore);
84 @RequestMapping(value = "/indexes/{index}", method = RequestMethod.DELETE, consumes = {"application/json"},
85 produces = {"application/json"})
86 public ResponseEntity<String> processDeleteIndex(HttpServletRequest request, @RequestHeader HttpHeaders headers,
87 @PathVariable("index") String index) {
89 // Forward the request to our index API to delete the index.
90 IndexApi indexApi = new IndexApi(this);
91 return indexApi.processDelete(index, request, headers, documentStore);
95 @RequestMapping(value = "/indexes/{index}/documents", method = RequestMethod.POST, consumes = {"application/json"})
96 public ResponseEntity<String> processCreateDocWithoutId(@RequestBody String requestBody, HttpServletRequest request,
97 HttpServletResponse httpResponse, @RequestHeader HttpHeaders headers, @PathVariable("index") String index) {
99 // Forward the request to our document API to create the document.
100 DocumentApi documentApi = new DocumentApi(this);
101 return documentApi.processPost(requestBody, request, headers, httpResponse, index, documentStore);
104 @RequestMapping(value = "/indexes/{index}/documents/{id}", method = RequestMethod.PUT,
105 consumes = {"application/json"})
106 public ResponseEntity<String> processUpsertDoc(@RequestBody String requestBody, HttpServletRequest request,
107 HttpServletResponse httpResponse, @RequestHeader HttpHeaders headers, @PathVariable("index") String index,
108 @PathVariable("id") String id) {
110 // Forward the request to our document API to upsert the document.
111 DocumentApi documentApi = new DocumentApi(this);
112 return documentApi.processPut(requestBody, request, headers, httpResponse, index, id, documentStore);
115 @RequestMapping(value = "/indexes/{index}/documents/{id}", method = RequestMethod.GET)
116 public ResponseEntity<String> processGetDocument(HttpServletRequest request, HttpServletResponse httpResponse,
117 @RequestHeader HttpHeaders headers, @PathVariable("index") String index, @PathVariable("id") String id) {
119 // Forward the request to our document API to retrieve the document.
120 DocumentApi documentApi = new DocumentApi(this);
121 return documentApi.processGet("", request, headers, httpResponse, index, id, documentStore);
124 @RequestMapping(value = "/indexes/{index}/documents/{id}", method = RequestMethod.DELETE,
125 consumes = {"application/json"})
126 public ResponseEntity<String> processDeleteDoc(HttpServletRequest request, HttpServletResponse httpResponse,
127 @RequestHeader HttpHeaders headers, @PathVariable("index") String index, @PathVariable("id") String id) {
129 // Forward the request to our document API to delete the document.
130 DocumentApi documentApi = new DocumentApi(this);
131 return documentApi.processDelete("", request, headers, httpResponse, index, id, documentStore);
134 @RequestMapping(value = "/indexes/{index}/query/{queryText}", method = RequestMethod.GET)
135 public ResponseEntity<String> processInlineQuery(HttpServletRequest request, @RequestHeader HttpHeaders headers,
136 @PathVariable("index") String index, @PathVariable("queryText") String queryText) {
138 // Forward the request to our document API to delete the document.
139 DocumentApi documentApi = new DocumentApi(this);
140 return documentApi.processSearchWithGet("", request, headers, index, queryText, documentStore);
143 @RequestMapping(value = "/indexes/{index}/query", method = RequestMethod.GET, consumes = {"application/json"})
144 public ResponseEntity<String> processQueryWithGet(@RequestBody String requestBody, HttpServletRequest request,
145 @RequestHeader HttpHeaders headers, @PathVariable("index") String index) {
147 // Forward the request to our document API to delete the document.
148 DocumentApi documentApi = new DocumentApi(this);
149 return documentApi.queryWithGetWithPayload(requestBody, request, headers, index, documentStore);
152 @RequestMapping(value = "/indexes/{index}/query", method = RequestMethod.POST, consumes = {"application/json"})
153 public ResponseEntity<String> processQuery(@RequestBody String requestBody, HttpServletRequest request,
154 @RequestHeader HttpHeaders headers, @PathVariable("index") String index) {
156 // Forward the request to our document API to delete the document.
157 DocumentApi documentApi = new DocumentApi(this);
158 return documentApi.processSearchWithPost(requestBody, request, headers, index, documentStore);
161 @RequestMapping(value = "/indexes/{index}/suggest", method = RequestMethod.POST, consumes = {"application/json"})
162 public ResponseEntity<String> processSuggestQuery(@RequestBody String requestBody, HttpServletRequest request,
163 @RequestHeader HttpHeaders headers, @PathVariable("index") String index) {
164 // Forward the request to our document API to query suggestions in the
166 DocumentApi documentApi = new DocumentApi(this);
167 return documentApi.processSuggestQueryWithPost(requestBody, request, headers, index, documentStore);
170 @RequestMapping(value = "/indexes/dynamic/{index}", method = RequestMethod.PUT, consumes = {"application/json"})
171 public ResponseEntity<String> processCreateDynamicIndex(@RequestBody String requestBody, HttpServletRequest request,
172 @RequestHeader HttpHeaders headers, @PathVariable("index") String index) {
174 // Forward the request to our index API to create the index.
175 IndexApi indexApi = new IndexApi(this);
176 return indexApi.processCreateDynamicIndex(requestBody, request, headers, index, documentStore);
179 @RequestMapping(value = "/bulk", method = RequestMethod.POST, consumes = {"application/json"},
180 produces = {"application/json"})
181 public ResponseEntity<String> processBulkRequest(@RequestBody String requestBody, HttpServletRequest request,
182 @RequestHeader HttpHeaders headers) {
184 // Forward the request to our document API to delete the document.
185 BulkApi bulkApi = new BulkApi(this);
186 ResponseEntity<String> dbugResp = bulkApi.processPost(requestBody, request, headers, documentStore, apiUtils);
190 protected boolean validateRequest(HttpHeaders headers, HttpServletRequest req, Action action,
191 String authPolicyFunctionName) throws Exception {
193 SearchDbServiceAuth serviceAuth = new SearchDbServiceAuth();
195 String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
196 String authUser = null;
197 if (cipherSuite != null) {
198 Object x509CertAttribute = req.getAttribute("javax.servlet.request.X509Certificate");
199 if (x509CertAttribute != null) {
200 X509Certificate[] certChain = (X509Certificate[]) x509CertAttribute;
201 X509Certificate clientCert = certChain[0];
202 X500Principal subjectDn = clientCert.getSubjectX500Principal();
203 authUser = subjectDn.toString();
207 if (authUser == null) {
212 serviceAuth.authUser(headers, authUser.toLowerCase(), action.toString() + ":" + authPolicyFunctionName);
213 if (!status.equals("OK")) {