Initial search service commit
[aai/search-data-service.git] / src / main / java / org / openecomp / sa / rest / SearchServiceApi.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Search Data Service
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License ati
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.sa.rest;
26
27 import org.openecomp.sa.auth.SearchDbServiceAuth;
28 import org.openecomp.sa.rest.ApiUtils.Action;
29 import org.openecomp.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
30 import org.openecomp.sa.searchdbabstraction.elasticsearch.dao.ElasticSearchHttpController;
31
32 import java.security.cert.X509Certificate;
33 import javax.security.auth.x500.X500Principal;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import javax.ws.rs.Consumes;
37 import javax.ws.rs.DELETE;
38 import javax.ws.rs.GET;
39 import javax.ws.rs.POST;
40 import javax.ws.rs.PUT;
41 import javax.ws.rs.Path;
42 import javax.ws.rs.PathParam;
43 import javax.ws.rs.core.Context;
44 import javax.ws.rs.core.HttpHeaders;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
47
48
49 public class SearchServiceApi {
50
51   /**
52    * The Data Access Object that we will use to interact with the
53    * document store.
54    */
55   protected DocumentStoreInterface documentStore = null;
56   protected ApiUtils apiUtils = null;
57
58
59   /**
60    * Create a new instance of the end point.
61    */
62   public SearchServiceApi() {
63
64     // Perform one-time initialization.
65     init();
66   }
67
68
69   /**
70    * Performs all one-time initialization required for the end point.
71    */
72   public void init() {
73
74     // Instantiate our Document Store DAO.
75     documentStore = ElasticSearchHttpController.getInstance();
76
77     apiUtils = new ApiUtils();
78   }
79
80   @PUT
81   @Path("/indexes/{index}")
82   @Consumes({MediaType.APPLICATION_JSON})
83   public Response processCreateIndex(String requestBody,
84                                      @Context HttpServletRequest request,
85                                      @Context HttpHeaders headers,
86                                      @PathParam("index") String index) {
87
88     // Forward the request to our index API to create the index.
89     IndexApi indexApi = new IndexApi(this);
90     return indexApi.processCreateIndex(requestBody, request, headers, index, documentStore);
91   }
92
93
94   @DELETE
95   @Path("/indexes/{index}")
96   @Consumes({MediaType.APPLICATION_JSON})
97   public Response processDeleteIndex(String requestBody,
98                                      @Context HttpServletRequest request,
99                                      @Context HttpHeaders headers,
100                                      @PathParam("index") String index) {
101
102     // Forward the request to our index API to delete the index.
103     IndexApi indexApi = new IndexApi(this);
104     return indexApi.processDelete(index, request, headers, documentStore);
105   }
106
107
108   @GET
109   @Path("/indexes/{index}/documents/{id}")
110   @Consumes({MediaType.APPLICATION_JSON})
111   public Response processGetDocument(String requestBody,
112                                      @Context HttpServletRequest request,
113                                      @Context HttpServletResponse httpResponse,
114                                      @Context HttpHeaders headers,
115                                      @PathParam("index") String index,
116                                      @PathParam("id") String id) {
117
118     // Forward the request to our document API to retrieve the document.
119     DocumentApi documentApi = new DocumentApi(this);
120     return documentApi.processGet(requestBody, request, headers, httpResponse,
121         index, id, documentStore);
122   }
123
124   @POST
125   @Path("/indexes/{index}/documents")
126   @Consumes({MediaType.APPLICATION_JSON})
127   public Response processCreateDocWithoutId(String requestBody,
128                                             @Context HttpServletRequest request,
129                                             @Context HttpServletResponse httpResponse,
130                                             @Context HttpHeaders headers,
131                                             @PathParam("index") String index) {
132
133     // Forward the request to our document API to create the document.
134     DocumentApi documentApi = new DocumentApi(this);
135     return documentApi.processPost(requestBody, request, headers, httpResponse,
136         index, documentStore);
137   }
138
139   @PUT
140   @Path("/indexes/{index}/documents/{id}")
141   @Consumes({MediaType.APPLICATION_JSON})
142   public Response processUpsertDoc(String requestBody,
143                                    @Context HttpServletRequest request,
144                                    @Context HttpServletResponse httpResponse,
145                                    @Context HttpHeaders headers,
146                                    @PathParam("index") String index,
147                                    @PathParam("id") String id) {
148
149     // Forward the request to our document API to upsert the document.
150     DocumentApi documentApi = new DocumentApi(this);
151     return documentApi.processPut(requestBody, request, headers, httpResponse,
152         index, id, documentStore);
153   }
154
155   @DELETE
156   @Path("/indexes/{index}/documents/{id}")
157   @Consumes({MediaType.APPLICATION_JSON})
158   public Response processDeleteDoc(String requestBody,
159                                    @Context HttpServletRequest request,
160                                    @Context HttpServletResponse httpResponse,
161                                    @Context HttpHeaders headers,
162                                    @PathParam("index") String index,
163                                    @PathParam("id") String id) {
164
165     // Forward the request to our document API to delete the document.
166     DocumentApi documentApi = new DocumentApi(this);
167     return documentApi.processDelete(requestBody, request, headers, httpResponse,
168         index, id, documentStore);
169   }
170
171
172   @GET
173   @Path("/indexes/{index}/query/{queryText}")
174   @Consumes({MediaType.APPLICATION_JSON})
175   public Response processInlineQuery(String requestBody,
176                                      @Context HttpServletRequest request,
177                                      @Context HttpHeaders headers,
178                                      @PathParam("index") String index,
179                                      @PathParam("queryText") String queryText) {
180
181     // Forward the request to our document API to delete the document.
182     DocumentApi documentApi = new DocumentApi(this);
183     return documentApi.processSearchWithGet(requestBody, request, headers,
184         index, queryText, documentStore);
185   }
186
187
188   @GET
189   @Path("/indexes/{index}/query")
190   @Consumes({MediaType.APPLICATION_JSON})
191   public Response processQueryWithGet(String requestBody,
192                                       @Context HttpServletRequest request,
193                                       @Context HttpHeaders headers,
194                                       @PathParam("index") String index) {
195
196     // Forward the request to our document API to delete the document.
197     DocumentApi documentApi = new DocumentApi(this);
198     return documentApi.queryWithGetWithPayload(requestBody, request, headers, index, documentStore);
199   }
200
201   @POST
202   @Path("/indexes/{index}/query")
203   @Consumes({MediaType.APPLICATION_JSON})
204   public Response processQuery(String requestBody,
205                                @Context HttpServletRequest request,
206                                @Context HttpHeaders headers,
207                                @PathParam("index") String index) {
208
209     // Forward the request to our document API to delete the document.
210     DocumentApi documentApi = new DocumentApi(this);
211     return documentApi.processSearchWithPost(requestBody, request, headers, index, documentStore);
212   }
213
214
215   @POST
216   @Path("/bulk")
217   @Consumes({MediaType.APPLICATION_JSON})
218   public Response processBulkRequest(String requestBody,
219                                      @Context HttpServletRequest request,
220                                      @Context HttpHeaders headers,
221                                      @PathParam("index") String index) {
222
223     // Forward the request to our document API to delete the document.
224     BulkApi bulkApi = new BulkApi(this);
225     return bulkApi.processPost(requestBody, request, headers, documentStore, apiUtils);
226   }
227
228   protected boolean validateRequest(HttpHeaders headers,
229                                     HttpServletRequest req,
230                                     Action action,
231                                     String authPolicyFunctionName) throws Exception {
232
233     SearchDbServiceAuth serviceAuth = new SearchDbServiceAuth();
234
235     String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
236     String authUser = null;
237     if (cipherSuite != null) {
238       Object x509CertAttribute = req.getAttribute("javax.servlet.request.X509Certificate");
239       if (x509CertAttribute != null) {
240         X509Certificate[] certChain = (X509Certificate[]) x509CertAttribute;
241         X509Certificate clientCert = certChain[0];
242         X500Principal subjectDn = clientCert.getSubjectX500Principal();
243         authUser = subjectDn.toString();
244       }
245     }
246
247     if (authUser == null) {
248       return false;
249     }
250
251     String status = serviceAuth.authUser(headers, authUser.toLowerCase(),
252         action.toString() + ":" + authPolicyFunctionName);
253     if (!status.equals("OK")) {
254       return false;
255     }
256
257     return true;
258   }
259 }