2 * ============LICENSE_START=======================================================
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
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 package org.onap.aai.sa.rest;
23 import com.fasterxml.jackson.annotation.JsonInclude.Include;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 //import javax.ws.rs.core.HttpHeaders;
29 //import javax.ws.rs.core.MediaType;
30 //import javax.ws.rs.core.Response;
31 //import javax.ws.rs.core.Response.Status;
33 import org.onap.aai.cl.api.LogFields;
34 import org.onap.aai.cl.api.LogLine;
35 import org.onap.aai.cl.api.Logger;
36 import org.onap.aai.cl.eelf.LoggerFactory;
37 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntityImpl;
38 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
39 import org.onap.aai.sa.searchdbabstraction.entity.AggregationResults;
40 import org.onap.aai.sa.searchdbabstraction.entity.DocumentOperationResult;
41 import org.onap.aai.sa.searchdbabstraction.entity.SearchOperationResult;
42 import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs;
43 import org.onap.aai.sa.searchdbabstraction.searchapi.SearchStatement;
44 import org.onap.aai.sa.searchdbabstraction.searchapi.SuggestionStatement;
45 import org.springframework.http.HttpHeaders;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.MediaType;
48 import org.springframework.http.ResponseEntity;
50 public class DocumentApi {
51 private static final String REQUEST_HEADER_RESOURCE_VERSION = "If-Match";
52 private static final String RESPONSE_HEADER_RESOURCE_VERSION = "ETag";
53 private static final String REQUEST_HEADER_ALLOW_IMPLICIT_INDEX_CREATION = "X-CreateIndex";
55 protected SearchServiceApi searchService = null;
57 private Logger logger = LoggerFactory.getInstance().getLogger(DocumentApi.class.getName());
58 private Logger auditLogger = LoggerFactory.getInstance()
59 .getAuditLogger(DocumentApi.class.getName());
61 public DocumentApi(SearchServiceApi searchService) {
62 this.searchService = searchService;
65 public ResponseEntity<String> processPost(String content, HttpServletRequest request, HttpHeaders headers,
66 HttpServletResponse httpResponse, String index,
67 DocumentStoreInterface documentStore) {
69 // Initialize the MDC Context for logging purposes.
70 ApiUtils.initMdcContext(request, headers);
73 ObjectMapper mapper = new ObjectMapper();
74 mapper.setSerializationInclusion(Include.NON_EMPTY);
75 if (content == null) {
76 return handleError(request, content, HttpStatus.BAD_REQUEST);
81 isValid = searchService.validateRequest(headers, request, ApiUtils.Action.POST,
82 ApiUtils.SEARCH_AUTH_POLICY_NAME);
83 } catch (Exception e) {
84 logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL,
85 "DocumentApi.processPost",
87 return handleError(request, content, HttpStatus.FORBIDDEN);
91 return handleError(request, content, HttpStatus.FORBIDDEN);
94 DocumentStoreDataEntityImpl document = new DocumentStoreDataEntityImpl();
95 document.setContent(content);
97 DocumentOperationResult result = documentStore.createDocument(index, document, implicitlyCreateIndex(headers));
99 if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
100 output = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getDocument());
102 output = result.getError() != null
103 ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getError())
104 : result.getFailureCause();
107 if (httpResponse != null) {
108 httpResponse.setHeader(RESPONSE_HEADER_RESOURCE_VERSION, result.getResultVersion());
110 ResponseEntity response = ResponseEntity.status(result.getResultCode()).contentType ( MediaType.APPLICATION_JSON ).body(output);
111 logResult(request, HttpStatus.valueOf ( response.getStatusCodeValue () ));
112 logResult(request, HttpStatus.valueOf ( response.getStatusCodeValue () ));
114 // Clear the MDC context so that no other transaction inadvertently
115 // uses our transaction id.
116 ApiUtils.clearMdcContext();
119 } catch (Exception e) {
120 return handleError(request, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
124 public ResponseEntity<String> processPut(String content, HttpServletRequest request, HttpHeaders headers,
125 HttpServletResponse httpResponse, String index,
126 String id, DocumentStoreInterface documentStore) {
128 // Initialize the MDC Context for logging purposes.
129 ApiUtils.initMdcContext(request, headers);
132 ObjectMapper mapper = new ObjectMapper();
133 mapper.setSerializationInclusion(Include.NON_EMPTY);
134 if (content == null) {
135 return handleError(request, content, HttpStatus.BAD_REQUEST);
140 isValid = searchService.validateRequest(headers, request, ApiUtils.Action.PUT,
141 ApiUtils.SEARCH_AUTH_POLICY_NAME);
142 } catch (Exception e) {
143 logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL,
144 "DocumentApi.processPut",
146 return handleError(request, content, HttpStatus.FORBIDDEN);
150 return handleError(request, content, HttpStatus.FORBIDDEN);
153 String resourceVersion = headers.getFirst(REQUEST_HEADER_RESOURCE_VERSION);
155 DocumentStoreDataEntityImpl document = new DocumentStoreDataEntityImpl();
157 document.setContent(content);
158 document.setVersion(resourceVersion);
160 DocumentOperationResult result = null;
161 if (resourceVersion == null) {
162 result = documentStore.createDocument(index, document, implicitlyCreateIndex(headers));
164 result = documentStore.updateDocument(index, document, implicitlyCreateIndex(headers));
167 String output = null;
168 if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
169 output = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getDocument());
171 output = result.getError() != null
172 ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getError())
173 : result.getFailureCause();
175 if (httpResponse != null) {
176 httpResponse.setHeader(RESPONSE_HEADER_RESOURCE_VERSION, result.getResultVersion());
178 ResponseEntity response = ResponseEntity.status(result.getResultCode()).contentType ( MediaType.APPLICATION_JSON ).body(output);
179 logResult(request, HttpStatus.valueOf ( response.getStatusCodeValue () ));
181 // Clear the MDC context so that no other transaction inadvertently
182 // uses our transaction id.
183 ApiUtils.clearMdcContext();
186 } catch (Exception e) {
187 return handleError(request, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
191 public ResponseEntity<String> processDelete(String content, HttpServletRequest request, HttpHeaders headers,
192 HttpServletResponse httpResponse, String index, String id,
193 DocumentStoreInterface documentStore) {
195 // Initialize the MDC Context for logging purposes.
196 ApiUtils.initMdcContext(request, headers);
199 ObjectMapper mapper = new ObjectMapper();
200 mapper.setSerializationInclusion(Include.NON_EMPTY);
203 isValid = searchService.validateRequest(headers, request, ApiUtils.Action.DELETE,
204 ApiUtils.SEARCH_AUTH_POLICY_NAME);
205 } catch (Exception e) {
206 logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL,
207 "DocumentApi.processDelete",
209 return handleError(request, content, HttpStatus.FORBIDDEN);
213 return handleError(request, content, HttpStatus.FORBIDDEN);
216 String resourceVersion = headers.getFirst(REQUEST_HEADER_RESOURCE_VERSION);
217 if (resourceVersion == null || resourceVersion.isEmpty()) {
218 return handleError(request, "Request header 'If-Match' missing",
219 HttpStatus.BAD_REQUEST);
222 DocumentStoreDataEntityImpl document = new DocumentStoreDataEntityImpl();
224 document.setVersion(resourceVersion);
226 DocumentOperationResult result = documentStore.deleteDocument(index, document);
227 String output = null;
228 if (!(result.getResultCode() >= 200 && result.getResultCode() <= 299)) { //
229 output = result.getError() != null
230 ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getError())
231 : result.getFailureCause();
234 if (httpResponse != null) {
235 httpResponse.setHeader(RESPONSE_HEADER_RESOURCE_VERSION, result.getResultVersion());
237 ResponseEntity response;
238 if (output == null) {
239 response = ResponseEntity.status(result.getResultCode()).build();
241 response = ResponseEntity.status(result.getResultCode()).contentType ( MediaType.APPLICATION_JSON ).body(output);
244 logResult(request, HttpStatus.valueOf ( response.getStatusCodeValue () ));
246 // Clear the MDC context so that no other transaction inadvertently
247 // uses our transaction id.
248 ApiUtils.clearMdcContext();
251 } catch (Exception e) {
252 return handleError(request, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
256 public ResponseEntity<String> processGet(String content, HttpServletRequest request, HttpHeaders headers,
257 HttpServletResponse httpResponse, String index, String id,
258 DocumentStoreInterface documentStore) {
260 // Initialize the MDC Context for logging purposes.
261 ApiUtils.initMdcContext(request, headers);
264 ObjectMapper mapper = new ObjectMapper();
265 mapper.setSerializationInclusion(Include.NON_EMPTY);
268 isValid = searchService.validateRequest(headers, request, ApiUtils.Action.GET,
269 ApiUtils.SEARCH_AUTH_POLICY_NAME);
270 } catch (Exception e) {
271 logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL,
272 "DocumentApi.processGet",
274 return handleError(request, content, HttpStatus.FORBIDDEN);
278 return handleError(request, content, HttpStatus.FORBIDDEN);
281 String resourceVersion = headers.getFirst(REQUEST_HEADER_RESOURCE_VERSION);
283 DocumentStoreDataEntityImpl document = new DocumentStoreDataEntityImpl();
285 document.setVersion(resourceVersion);
287 DocumentOperationResult result = documentStore.getDocument(index, document);
288 String output = null;
289 if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
290 output = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getDocument());
292 output = result.getError() != null
293 ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getError())
294 : result.getFailureCause();
296 if (httpResponse != null) {
297 httpResponse.setHeader(RESPONSE_HEADER_RESOURCE_VERSION, result.getResultVersion());
299 ResponseEntity response = ResponseEntity.status(result.getResultCode()).contentType ( MediaType.APPLICATION_JSON ).body(output);
300 logResult(request, HttpStatus.valueOf ( response.getStatusCodeValue () ));
302 // Clear the MDC context so that no other transaction inadvertently
303 // uses our transaction id.
304 ApiUtils.clearMdcContext();
307 } catch (Exception e) {
308 return handleError(request, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
312 public ResponseEntity<String> processSearchWithGet(String content, HttpServletRequest request,
313 HttpHeaders headers, String index,
314 String queryText, DocumentStoreInterface documentStore) {
316 // Initialize the MDC Context for logging purposes.
317 ApiUtils.initMdcContext(request, headers);
320 ObjectMapper mapper = new ObjectMapper();
321 mapper.setSerializationInclusion(Include.NON_EMPTY);
325 isValid = searchService.validateRequest(headers, request, ApiUtils.Action.GET,
326 ApiUtils.SEARCH_AUTH_POLICY_NAME);
327 } catch (Exception e) {
328 logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL,
329 "processSearchWithGet",
331 return handleError(request, content, HttpStatus.FORBIDDEN);
335 return handleError(request, content, HttpStatus.FORBIDDEN);
338 SearchOperationResult result = documentStore.search(index, queryText);
339 String output = null;
340 if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
341 output = mapper.writerWithDefaultPrettyPrinter()
342 .writeValueAsString(result.getSearchResult());
344 output = result.getError() != null
345 ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getError())
346 : result.getFailureCause();
348 ResponseEntity response = ResponseEntity.status(result.getResultCode()).contentType ( MediaType.APPLICATION_JSON ).body(output);
350 // Clear the MDC context so that no other transaction inadvertently
351 // uses our transaction id.
352 ApiUtils.clearMdcContext();
355 } catch (Exception e) {
356 return handleError(request, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
360 public ResponseEntity<String> queryWithGetWithPayload(String content, HttpServletRequest request,
361 HttpHeaders headers, String index,
362 DocumentStoreInterface documentStore) {
364 // Initialize the MDC Context for logging purposes.
365 ApiUtils.initMdcContext(request, headers);
367 logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "GET", (request != null)
368 ? request.getRequestURL ().toString () : "");
369 if (logger.isDebugEnabled()) {
370 logger.debug("Request Body: " + content);
372 return processQuery(index, content, request, headers, documentStore);
375 public ResponseEntity<String> processSearchWithPost(String content, HttpServletRequest request,
376 HttpHeaders headers, String index,
377 DocumentStoreInterface documentStore) {
379 // Initialize the MDC Context for logging purposes.
380 ApiUtils.initMdcContext(request, headers);
382 logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "POST", (request != null)
383 ? request.getRequestURL ().toString () : "");
384 if (logger.isDebugEnabled()) {
385 logger.debug("Request Body: " + content);
388 return processQuery(index, content, request, headers, documentStore);
392 public ResponseEntity<String> processSuggestQueryWithPost(String content, HttpServletRequest request,
393 HttpHeaders headers, String index, DocumentStoreInterface documentStore) {
395 // Initialize the MDC Context for logging purposes.
396 ApiUtils.initMdcContext(request, headers);
398 logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "POST",
399 (request != null) ? request.getRequestURL().toString() : "");
400 if (logger.isDebugEnabled()) {
401 logger.debug("Request Body: " + content);
404 return processSuggestQuery(index, content, request, headers, documentStore);
408 * Common handler for query requests. This is called by both the GET with
409 * payload and POST with payload variants of the query endpoint.
411 * @param index - The index to be queried against.
412 * @param content - The payload containing the query structure.
413 * @param request - The HTTP request.
414 * @param headers - The HTTP headers.
415 * @return - A standard HTTP response.
417 private ResponseEntity processQuery(String index, String content, HttpServletRequest request,
418 HttpHeaders headers, DocumentStoreInterface documentStore) {
421 ObjectMapper mapper = new ObjectMapper();
422 mapper.setSerializationInclusion(Include.NON_EMPTY);
424 // Make sure that we were supplied a payload before proceeding.
425 if (content == null) {
426 return handleError(request, content, HttpStatus.BAD_REQUEST);
429 // Validate that the request has the appropriate authorization.
432 isValid = searchService.validateRequest(headers, request, ApiUtils.Action.POST,
433 ApiUtils.SEARCH_AUTH_POLICY_NAME);
435 } catch (Exception e) {
436 logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL,
439 return handleError(request, content, HttpStatus.FORBIDDEN);
443 return handleError(request, content, HttpStatus.FORBIDDEN);
446 SearchStatement searchStatement;
449 // Marshall the supplied request payload into a search statement
451 searchStatement = mapper.readValue(content, SearchStatement.class);
453 } catch (Exception e) {
454 return handleError(request, e.getMessage(), HttpStatus.BAD_REQUEST);
457 // Now, submit the search statement, translated into
458 // ElasticSearch syntax, to the document store DAO.
459 SearchOperationResult result = documentStore.searchWithPayload(index,
460 searchStatement.toElasticSearch());
461 String output = null;
462 if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
463 output = prepareOutput(mapper, result);
465 output = result.getError() != null
466 ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getError())
467 : result.getFailureCause();
469 ResponseEntity response = ResponseEntity.status(result.getResultCode()).contentType ( MediaType.APPLICATION_JSON ).body(output);
471 // Clear the MDC context so that no other transaction inadvertently
472 // uses our transaction id.
473 ApiUtils.clearMdcContext();
477 } catch (Exception e) {
478 return handleError(request, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
484 * Common handler for query requests. This is called by both the GET with payload and POST with
485 * payload variants of the query endpoint.
487 * @param index - The index to be queried against.
488 * @param content - The payload containing the query structure.
489 * @param request - The HTTP request.
490 * @param headers - The HTTP headers.
491 * @return - A standard HTTP response.
493 private ResponseEntity<String> processSuggestQuery(String index, String content, HttpServletRequest request,
494 HttpHeaders headers, DocumentStoreInterface documentStore) {
497 ObjectMapper mapper = new ObjectMapper();
498 mapper.setSerializationInclusion(Include.NON_EMPTY);
500 // Make sure that we were supplied a payload before proceeding.
501 if (content == null) {
502 return handleError(request, content, HttpStatus.BAD_REQUEST);
505 // Validate that the request has the appropriate authorization.
508 isValid = searchService.validateRequest(headers, request, ApiUtils.Action.POST,
509 ApiUtils.SEARCH_AUTH_POLICY_NAME);
511 } catch (Exception e) {
512 logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "processQuery", e.getMessage());
513 return handleError(request, content, HttpStatus.FORBIDDEN);
517 return handleError(request, content, HttpStatus.FORBIDDEN);
520 SuggestionStatement suggestionStatement;
523 // Marshall the supplied request payload into a search statement
525 suggestionStatement = mapper.readValue(content, SuggestionStatement.class);
527 } catch (Exception e) {
528 return handleError(request, e.getMessage(), HttpStatus.BAD_REQUEST);
531 // Now, submit the search statement, translated into
532 // ElasticSearch syntax, to the document store DAO.
533 SearchOperationResult result =
534 documentStore.suggestionQueryWithPayload(index, suggestionStatement.toElasticSearch());
535 String output = null;
536 if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
537 output = prepareSuggestOutput(mapper, result);
539 output = result.getError() != null
540 ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getError())
541 : result.getFailureCause();
543 ResponseEntity<String> response = ResponseEntity.status(result.getResultCode()).body(output);
545 // Clear the MDC context so that no other transaction inadvertently
546 // uses our transaction id.
547 ApiUtils.clearMdcContext();
551 } catch (Exception e) {
552 return handleError(request, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
557 * Checks the supplied HTTP headers to see if we should allow the underlying document
558 * store to implicitly create the index referenced in a document PUT or POST if it
559 * does not already exist in the data store.
561 * @param headers - The HTTP headers to examine.
563 * @return - true if the headers indicate that missing indices should be implicitly created,
566 private boolean implicitlyCreateIndex(HttpHeaders headers) {
568 boolean createIndexIfNotPresent = false;
569 String implicitIndexCreationHeader =
570 headers.getFirst(REQUEST_HEADER_ALLOW_IMPLICIT_INDEX_CREATION);
572 if( (implicitIndexCreationHeader != null) && (implicitIndexCreationHeader.equals("true")) ) {
573 createIndexIfNotPresent = true;
576 return createIndexIfNotPresent;
579 private String prepareOutput(ObjectMapper mapper, SearchOperationResult result)
580 throws JsonProcessingException {
581 StringBuffer output = new StringBuffer();
582 output.append("{\r\n\"searchResult\":");
584 mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getSearchResult()));
585 AggregationResults aggs = result.getAggregationResult();
587 output.append(",\r\n\"aggregationResult\":");
588 output.append(mapper.setSerializationInclusion(Include.NON_NULL)
589 .writerWithDefaultPrettyPrinter().writeValueAsString(aggs));
591 output.append("\r\n}");
592 return output.toString();
595 private String prepareSuggestOutput(ObjectMapper mapper, SearchOperationResult result)
596 throws JsonProcessingException {
597 StringBuffer output = new StringBuffer();
598 output.append("{\r\n\"searchResult\":");
600 mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getSuggestResult()));
601 AggregationResults aggs = result.getAggregationResult();
603 output.append(",\r\n\"aggregationResult\":");
604 output.append(mapper.setSerializationInclusion(Include.NON_NULL)
605 .writerWithDefaultPrettyPrinter().writeValueAsString(aggs));
607 output.append("\r\n}");
608 return output.toString();
611 private ResponseEntity handleError( HttpServletRequest request, String message, HttpStatus status) {
612 logResult(request, status);
613 return ResponseEntity.status(status).contentType ( MediaType.APPLICATION_JSON ).body(message);
616 void logResult(HttpServletRequest request, HttpStatus status) {
618 logger.info(SearchDbMsgs.PROCESS_REST_REQUEST, (request != null) ? request.getMethod().toString () : "",
619 (request != null) ? request.getRequestURL ().toString () : "",
620 (request != null) ? request.getRemoteHost () : "", Integer.toString(status.value ()));
622 auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
624 .setField(LogLine.DefinedFields.RESPONSE_CODE, status.value())
625 .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, status.getReasonPhrase()),
626 (request != null) ? request.getMethod().toString () : "",
627 (request != null) ? request.getRequestURL ().toString () : "",
628 (request != null) ? request.getRemoteHost () : "", Integer.toString(status.value()));
630 // Clear the MDC context so that no other transaction inadvertently
631 // uses our transaction id.
632 ApiUtils.clearMdcContext();