99b461553fe3073d296021deec112fe01173f1eb
[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
28 import java.security.cert.X509Certificate;
29 import javax.security.auth.x500.X500Principal;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import javax.ws.rs.Consumes;
33 import javax.ws.rs.DELETE;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.core.Context;
40 import javax.ws.rs.core.HttpHeaders;
41 import javax.ws.rs.core.MediaType;
42 import javax.ws.rs.core.Response;
43
44 public class SearchServiceApi {
45
46   /**
47    * The Data Access Object that we will use to interact with the document store.
48    */
49   protected DocumentStoreInterface documentStore = null;
50   protected ApiUtils apiUtils = null;
51
52   /**
53    * Create a new instance of the end point.
54    */
55   public SearchServiceApi() {
56
57     // Perform one-time initialization.
58     init();
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   @PUT
73   @Path("/indexes/{index}")
74   @Consumes({MediaType.APPLICATION_JSON})
75   public Response processCreateIndex(String requestBody, @Context HttpServletRequest request,
76       @Context HttpHeaders headers, @PathParam("index") String index) {
77
78     // Forward the request to our index API to create the index.
79     IndexApi indexApi = new IndexApi(this);
80     return indexApi.processCreateIndex(requestBody, request, headers, index, documentStore);
81   }
82
83   @PUT
84   @Path("/indexes/dynamic/{index}")
85   @Consumes({MediaType.APPLICATION_JSON})
86   public Response processCreateDynamicIndex(String requestBody, @Context HttpServletRequest request,
87       @Context HttpHeaders headers, @PathParam("index") String index) {
88
89     // Forward the request to our index API to create the index.
90     IndexApi indexApi = new IndexApi(this);
91     return indexApi.processCreateDynamicIndex(requestBody, request, headers, index, documentStore);
92   }
93
94   @DELETE
95   @Path("/indexes/{index}")
96   @Consumes({MediaType.APPLICATION_JSON})
97   public Response processDeleteIndex(String requestBody, @Context HttpServletRequest request,
98       @Context HttpHeaders headers, @PathParam("index") String index) {
99
100     // Forward the request to our index API to delete the index.
101     IndexApi indexApi = new IndexApi(this);
102     return indexApi.processDelete(index, request, headers, documentStore);
103   }
104
105   @GET
106   @Path("/indexes/{index}/documents/{id}")
107   @Consumes({MediaType.APPLICATION_JSON})
108   public Response processGetDocument(String requestBody, @Context HttpServletRequest request,
109       @Context HttpServletResponse httpResponse, @Context HttpHeaders headers,
110       @PathParam("index") String index, @PathParam("id") String id) {
111
112     // Forward the request to our document API to retrieve the document.
113     DocumentApi documentApi = new DocumentApi(this);
114     return documentApi.processGet(requestBody, request, headers, httpResponse, index, id,
115         documentStore);
116   }
117
118   @POST
119   @Path("/indexes/{index}/documents")
120   @Consumes({MediaType.APPLICATION_JSON})
121   public Response processCreateDocWithoutId(String requestBody, @Context HttpServletRequest request,
122       @Context HttpServletResponse httpResponse, @Context HttpHeaders headers,
123       @PathParam("index") String index) {
124
125     // Forward the request to our document API to create the document.
126     DocumentApi documentApi = new DocumentApi(this);
127     return documentApi.processPost(requestBody, request, headers, httpResponse, index,
128         documentStore);
129   }
130
131   @PUT
132   @Path("/indexes/{index}/documents/{id}")
133   @Consumes({MediaType.APPLICATION_JSON})
134   public Response processUpsertDoc(String requestBody, @Context HttpServletRequest request,
135       @Context HttpServletResponse httpResponse, @Context HttpHeaders headers,
136       @PathParam("index") String index, @PathParam("id") String id) {
137
138     // Forward the request to our document API to upsert the document.
139     DocumentApi documentApi = new DocumentApi(this);
140     return documentApi.processPut(requestBody, request, headers, httpResponse, index, id,
141         documentStore);
142   }
143
144   @DELETE
145   @Path("/indexes/{index}/documents/{id}")
146   @Consumes({MediaType.APPLICATION_JSON})
147   public Response processDeleteDoc(String requestBody, @Context HttpServletRequest request,
148       @Context HttpServletResponse httpResponse, @Context HttpHeaders headers,
149       @PathParam("index") String index, @PathParam("id") String id) {
150
151     // Forward the request to our document API to delete the document.
152     DocumentApi documentApi = new DocumentApi(this);
153     return documentApi.processDelete(requestBody, request, headers, httpResponse, index, id,
154         documentStore);
155   }
156
157   @GET
158   @Path("/indexes/{index}/query/{queryText}")
159   @Consumes({MediaType.APPLICATION_JSON})
160   public Response processInlineQuery(String requestBody, @Context HttpServletRequest request,
161       @Context HttpHeaders headers, @PathParam("index") String index,
162       @PathParam("queryText") String queryText) {
163
164     // Forward the request to our document API to delete the document.
165     DocumentApi documentApi = new DocumentApi(this);
166     return documentApi.processSearchWithGet(requestBody, request, headers, index, queryText,
167         documentStore);
168   }
169
170   @GET
171   @Path("/indexes/{index}/query")
172   @Consumes({MediaType.APPLICATION_JSON})
173   public Response processQueryWithGet(String requestBody, @Context HttpServletRequest request,
174       @Context HttpHeaders headers, @PathParam("index") String index) {
175
176     // Forward the request to our document API to delete the document.
177     DocumentApi documentApi = new DocumentApi(this);
178     return documentApi.queryWithGetWithPayload(requestBody, request, headers, index, documentStore);
179   }
180
181   @POST
182   @Path("/indexes/{index}/query")
183   @Consumes({MediaType.APPLICATION_JSON})
184   public Response processQuery(String requestBody, @Context HttpServletRequest request,
185       @Context HttpHeaders headers, @PathParam("index") String index) {
186
187     // Forward the request to our document API to delete the document.
188     DocumentApi documentApi = new DocumentApi(this);
189     return documentApi.processSearchWithPost(requestBody, request, headers, index, documentStore);
190   }
191
192   @POST
193   @Path("/indexes/{index}/suggest")
194   @Consumes({MediaType.APPLICATION_JSON})
195   public Response processSuggestQuery(String requestBody, @Context HttpServletRequest request,
196       @Context HttpHeaders headers, @PathParam("index") String index) {
197
198     // Forward the request to our document API to query suggestions in the
199     // document.
200     DocumentApi documentApi = new DocumentApi(this);
201     return documentApi.processSuggestQueryWithPost(requestBody, request, headers, index,
202         documentStore);
203   }
204
205   @POST
206   @Path("/bulk")
207   @Consumes({MediaType.APPLICATION_JSON})
208   public Response processBulkRequest(String requestBody, @Context HttpServletRequest request,
209       @Context HttpHeaders headers, @PathParam("index") String index) {
210
211     // Forward the request to our document API to delete the document.
212     BulkApi bulkApi = new BulkApi(this);
213     return bulkApi.processPost(requestBody, request, headers, documentStore, apiUtils);
214   }
215
216   protected boolean validateRequest(HttpHeaders headers, HttpServletRequest req, Action action,
217       String authPolicyFunctionName) throws Exception {
218
219     SearchDbServiceAuth serviceAuth = new SearchDbServiceAuth();
220
221     String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
222     String authUser = null;
223     if (cipherSuite != null) {
224       Object x509CertAttribute = req.getAttribute("javax.servlet.request.X509Certificate");
225       if (x509CertAttribute != null) {
226         X509Certificate[] certChain = (X509Certificate[]) x509CertAttribute;
227         X509Certificate clientCert = certChain[0];
228         X500Principal subjectDn = clientCert.getSubjectX500Principal();
229         authUser = subjectDn.toString();
230       }
231     }
232
233     if (authUser == null) {
234       return false;
235     }
236
237     String status = serviceAuth.authUser(headers, authUser.toLowerCase(),
238         action.toString() + ":" + authPolicyFunctionName);
239     if (!status.equals("OK")) {
240       return false;
241     }
242
243     return true;
244   }
245 }