Format Java code to ONAP standard
[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 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;
41
42 @Component
43 @EnableWebSecurity
44 @RestController
45 @RequestMapping("/services/search-data-service/v1/search")
46 public class SearchServiceApi {
47
48     /**
49      * The Data Access Object that we will use to interact with the document store.
50      */
51     protected DocumentStoreInterface documentStore = null;
52     protected ApiUtils apiUtils = null;
53
54     /**
55      * Create a new instance of the end point.
56      */
57     public SearchServiceApi() {
58
59         // Perform one-time initialization.
60         init();
61     }
62
63
64     /**
65      * Performs all one-time initialization required for the end point.
66      */
67     public void init() {
68
69         // Instantiate our Document Store DAO.
70         documentStore = ElasticSearchHttpController.getInstance();
71
72         apiUtils = new ApiUtils();
73     }
74
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) {
78
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);
82     }
83
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) {
88
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);
92     }
93
94
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) {
98
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);
102     }
103
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) {
109
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);
113     }
114
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) {
118
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);
122     }
123
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) {
128
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);
132     }
133
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) {
137
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);
141     }
142
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) {
146
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);
150     }
151
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) {
155
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);
159     }
160
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
165         // document.
166         DocumentApi documentApi = new DocumentApi(this);
167         return documentApi.processSuggestQueryWithPost(requestBody, request, headers, index, documentStore);
168     }
169
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) {
173
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);
177     }
178
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) {
183
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);
187         return dbugResp;
188     }
189
190     protected boolean validateRequest(HttpHeaders headers, HttpServletRequest req, Action action,
191             String authPolicyFunctionName) throws Exception {
192
193         SearchDbServiceAuth serviceAuth = new SearchDbServiceAuth();
194
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();
204             }
205         }
206
207         if (authUser == null) {
208             return false;
209         }
210
211         String status =
212                 serviceAuth.authUser(headers, authUser.toLowerCase(), action.toString() + ":" + authPolicyFunctionName);
213         if (!status.equals("OK")) {
214             return false;
215         }
216
217         return true;
218     }
219 }