2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.sa.rest;
25 import org.openecomp.sa.auth.SearchDbServiceAuth;
26 import org.openecomp.sa.rest.ApiUtils.Action;
27 import org.openecomp.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
28 import org.openecomp.sa.searchdbabstraction.elasticsearch.dao.ElasticSearchHttpController;
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;
47 public class SearchServiceApi {
50 * The Data Access Object that we will use to interact with the
53 protected DocumentStoreInterface documentStore = null;
54 protected ApiUtils apiUtils = null;
58 * Create a new instance of the end point.
60 public SearchServiceApi() {
62 // Perform one-time initialization.
68 * Performs all one-time initialization required for the end point.
72 // Instantiate our Document Store DAO.
73 documentStore = ElasticSearchHttpController.getInstance();
75 apiUtils = new ApiUtils();
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) {
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);
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) {
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);
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) {
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);
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) {
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);
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) {
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);
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) {
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);
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) {
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);
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) {
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);
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) {
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);
215 @Consumes({MediaType.APPLICATION_JSON})
216 public Response processBulkRequest(String requestBody,
217 @Context HttpServletRequest request,
218 @Context HttpHeaders headers,
219 @PathParam("index") String index) {
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);
226 protected boolean validateRequest(HttpHeaders headers,
227 HttpServletRequest req,
229 String authPolicyFunctionName) throws Exception {
231 SearchDbServiceAuth serviceAuth = new SearchDbServiceAuth();
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();
245 if (authUser == null) {
249 String status = serviceAuth.authUser(headers, authUser.toLowerCase(),
250 action.toString() + ":" + authPolicyFunctionName);
251 if (!status.equals("OK")) {