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