Add cosumes and produces type where necessary
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / rest / SearchServiceApi.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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 package org.onap.aai.sa.rest;
22
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.*;
32
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;
37
38 @Component
39 @EnableWebSecurity
40 @RestController
41 @RequestMapping("/services/search-data-service/v1/search")
42 public class SearchServiceApi {
43
44   /**
45    * The Data Access Object that we will use to interact with the
46    * document store.
47    */
48   protected DocumentStoreInterface documentStore = null;
49   protected ApiUtils apiUtils = null;
50
51   /**
52    * Create a new instance of the end point.
53    */
54   public SearchServiceApi() {
55
56     // Perform one-time initialization.
57     init();
58   }
59
60
61   /**
62    * Performs all one-time initialization required for the end point.
63    */
64   public void init() {
65
66     // Instantiate our Document Store DAO.
67     documentStore = ElasticSearchHttpController.getInstance();
68
69     apiUtils = new ApiUtils();
70   }
71
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) {
79
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);
83   }
84
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) {
92
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);
96   }
97
98
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) {
107
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);
112   }
113
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) {
123
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);
128   }
129
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) {
137
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);
142   }
143
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) {
152
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);
157   }
158
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) {
165
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);
170   }
171
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) {
179
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);
183   }
184
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) {
192
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);
196   }
197
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
204     // document.
205     DocumentApi documentApi = new DocumentApi(this);
206     return documentApi.processSuggestQueryWithPost(requestBody, request, headers, index,
207             documentStore);
208   }
209
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) {
217
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);
221   }
222
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) {
230
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);
234     return dbugResp;
235   }
236
237   protected boolean validateRequest(HttpHeaders headers,
238                                     HttpServletRequest req,
239                                     Action action,
240                                     String authPolicyFunctionName) throws Exception {
241
242     SearchDbServiceAuth serviceAuth = new SearchDbServiceAuth();
243
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();
253       }
254     }
255
256     if (authUser == null) {
257       return false;
258     }
259
260     String status = serviceAuth.authUser(headers, authUser.toLowerCase(),
261                                          action.toString() + ":" + authPolicyFunctionName);
262     if (!status.equals("OK")) {
263       return false;
264     }
265
266     return true;
267   }
268 }