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 org.onap.aai.sa.auth.SearchDbServiceAuth;
24 import org.onap.aai.sa.rest.ApiUtils.Action;
25 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
26 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.ElasticSearchHttpController;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
30 import org.springframework.stereotype.Component;
31 import org.springframework.web.bind.annotation.*;
33 import javax.security.auth.x500.X500Principal;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import java.security.cert.X509Certificate;
41 @RequestMapping("/services/search-data-service/v1/search")
42 public class SearchServiceApi {
45 * The Data Access Object that we will use to interact with the
48 protected DocumentStoreInterface documentStore = null;
49 protected ApiUtils apiUtils = null;
52 * Create a new instance of the end point.
54 public SearchServiceApi() {
56 // Perform one-time initialization.
62 * Performs all one-time initialization required for the end point.
66 // Instantiate our Document Store DAO.
67 documentStore = ElasticSearchHttpController.getInstance();
69 apiUtils = new ApiUtils();
72 @RequestMapping(value = "/indexes/{index}",
73 method = RequestMethod.PUT,
74 produces = { "application/json" })
75 public ResponseEntity<String> processCreateIndex(@RequestBody String requestBody,
76 HttpServletRequest request,
77 @RequestHeader HttpHeaders headers,
78 @PathVariable("index") String index) {
80 // Forward the request to our index API to create the index.
81 IndexApi indexApi = new IndexApi(this);
82 return indexApi.processCreateIndex(requestBody, request, headers, index, documentStore);
85 @RequestMapping(value = "/indexes/{index}",
86 method = RequestMethod.DELETE,
87 consumes = {"application/json"},
88 produces = {"application/json"})
89 public ResponseEntity<String> processDeleteIndex(HttpServletRequest request,
90 @RequestHeader HttpHeaders headers,
91 @PathVariable ("index") String index) {
93 // Forward the request to our index API to delete the index.
94 IndexApi indexApi = new IndexApi(this);
95 return indexApi.processDelete(index, request, headers, documentStore);
99 @RequestMapping(value = "/indexes/{index}/documents",
100 method = RequestMethod.POST,
101 consumes = {"application/json"})
102 public ResponseEntity<String> processCreateDocWithoutId(@RequestBody String requestBody,
103 HttpServletRequest request,
104 HttpServletResponse httpResponse,
105 @RequestHeader HttpHeaders headers,
106 @PathVariable ("index") String index) {
108 // Forward the request to our document API to create the document.
109 DocumentApi documentApi = new DocumentApi(this);
110 return documentApi.processPost(requestBody, request, headers, httpResponse,
111 index, documentStore);
114 @RequestMapping(value = "/indexes/{index}/documents/{id}",
115 method = RequestMethod.PUT,
116 consumes = {"application/json"})
117 public ResponseEntity<String> processUpsertDoc(@RequestBody String requestBody,
118 HttpServletRequest request,
119 HttpServletResponse httpResponse,
120 @RequestHeader HttpHeaders headers,
121 @PathVariable ("index") String index,
122 @PathVariable ("id") String id) {
124 // Forward the request to our document API to upsert the document.
125 DocumentApi documentApi = new DocumentApi(this);
126 return documentApi.processPut(requestBody, request, headers, httpResponse,
127 index, id, documentStore);
130 @RequestMapping(value = "/indexes/{index}/documents/{id}",
131 method = RequestMethod.GET)
132 public ResponseEntity<String> processGetDocument(HttpServletRequest request,
133 HttpServletResponse httpResponse,
134 @RequestHeader HttpHeaders headers,
135 @PathVariable ("index") String index,
136 @PathVariable ("id") String id) {
138 // Forward the request to our document API to retrieve the document.
139 DocumentApi documentApi = new DocumentApi(this);
140 return documentApi.processGet("", request, headers, httpResponse,
141 index, id, documentStore);
144 @RequestMapping(value = "/indexes/{index}/documents/{id}",
145 method = RequestMethod.DELETE,
146 consumes = {"application/json"})
147 public ResponseEntity<String> processDeleteDoc(HttpServletRequest request,
148 HttpServletResponse httpResponse,
149 @RequestHeader HttpHeaders headers,
150 @PathVariable ("index") String index,
151 @PathVariable ("id") String id) {
153 // Forward the request to our document API to delete the document.
154 DocumentApi documentApi = new DocumentApi(this);
155 return documentApi.processDelete("", request, headers, httpResponse,
156 index, id, documentStore);
159 @RequestMapping(value = "/indexes/{index}/query/{queryText}",
160 method = RequestMethod.GET)
161 public ResponseEntity<String> processInlineQuery(HttpServletRequest request,
162 @RequestHeader HttpHeaders headers,
163 @PathVariable ("index") String index,
164 @PathVariable ("queryText") String queryText) {
166 // Forward the request to our document API to delete the document.
167 DocumentApi documentApi = new DocumentApi(this);
168 return documentApi.processSearchWithGet("", request, headers,
169 index, queryText, documentStore);
172 @RequestMapping(value = "/indexes/{index}/query",
173 method = RequestMethod.GET,
174 consumes = {"application/json"})
175 public ResponseEntity<String> processQueryWithGet(@RequestBody String requestBody,
176 HttpServletRequest request,
177 @RequestHeader HttpHeaders headers,
178 @PathVariable ("index") String index) {
180 // Forward the request to our document API to delete the document.
181 DocumentApi documentApi = new DocumentApi(this);
182 return documentApi.queryWithGetWithPayload(requestBody, request, headers, index, documentStore);
185 @RequestMapping(value = "/indexes/{index}/query",
186 method = RequestMethod.POST,
187 consumes = {"application/json"})
188 public ResponseEntity<String> processQuery(@RequestBody String requestBody,
189 HttpServletRequest request,
190 @RequestHeader HttpHeaders headers,
191 @PathVariable ("index") String index) {
193 // Forward the request to our document API to delete the document.
194 DocumentApi documentApi = new DocumentApi(this);
195 return documentApi.processSearchWithPost(requestBody, request, headers, index, documentStore);
198 @RequestMapping(value = "/indexes/{index}/suggest",
199 method = RequestMethod.POST,
200 consumes = {"application/json"})
201 public ResponseEntity<String> processSuggestQuery(@RequestBody String requestBody, HttpServletRequest request,
202 @RequestHeader HttpHeaders headers, @PathVariable("index") String index) {
203 // Forward the request to our document API to query suggestions in the
205 DocumentApi documentApi = new DocumentApi(this);
206 return documentApi.processSuggestQueryWithPost(requestBody, request, headers, index,
210 @RequestMapping(value = "/indexes/dynamic/{index}",
211 method = RequestMethod.PUT,
212 consumes = {"application/json"})
213 public ResponseEntity<String> processCreateDynamicIndex(@RequestBody String requestBody,
214 HttpServletRequest request,
215 @RequestHeader HttpHeaders headers,
216 @PathVariable ("index") String index) {
218 // Forward the request to our index API to create the index.
219 IndexApi indexApi = new IndexApi(this);
220 return indexApi.processCreateDynamicIndex(requestBody, request, headers, index, documentStore);
223 @RequestMapping(value = "/bulk",
224 method = RequestMethod.POST,
225 consumes = {"application/json"},
226 produces = { "application/json"})
227 public ResponseEntity<String> processBulkRequest(@RequestBody String requestBody,
228 HttpServletRequest request,
229 @RequestHeader HttpHeaders headers) {
231 // Forward the request to our document API to delete the document.
232 BulkApi bulkApi = new BulkApi(this);
233 ResponseEntity<String> dbugResp = bulkApi.processPost(requestBody, request, headers, documentStore, apiUtils);
237 protected boolean validateRequest(HttpHeaders headers,
238 HttpServletRequest req,
240 String authPolicyFunctionName) throws Exception {
242 SearchDbServiceAuth serviceAuth = new SearchDbServiceAuth();
244 String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
245 String authUser = null;
246 if (cipherSuite != null) {
247 Object x509CertAttribute = req.getAttribute("javax.servlet.request.X509Certificate");
248 if (x509CertAttribute != null) {
249 X509Certificate[] certChain = (X509Certificate[]) x509CertAttribute;
250 X509Certificate clientCert = certChain[0];
251 X500Principal subjectDn = clientCert.getSubjectX500Principal();
252 authUser = subjectDn.toString();
256 if (authUser == null) {
260 String status = serviceAuth.authUser(headers, authUser.toLowerCase(),
261 action.toString() + ":" + authPolicyFunctionName);
262 if (!status.equals("OK")) {