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