-/**
+/**
* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
package org.onap.aai.sa.rest;
import com.fasterxml.jackson.databind.ObjectMapper;
-
-import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
-import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
-import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
-import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import javax.servlet.http.HttpServletRequest;
import org.onap.aai.cl.api.LogFields;
import org.onap.aai.cl.api.LogLine;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
-import org.onap.aai.sa.rest.DocumentFieldSchema;
-import org.onap.aai.sa.rest.DocumentSchema;
-import org.slf4j.MDC;
-
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import javax.servlet.http.HttpServletRequest;
-
-// Spring Imports
+import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
+import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
+import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
+import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs;
import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
-import org.springframework.http.HttpStatus;
-// import org.springframework.http.server.HttpServletRequest;
-
/**
- * This class encapsulates the REST end points associated with manipulating
- * indexes in the document store.
+ * This class encapsulates the REST end points associated with manipulating indexes in the document store.
*/
public class IndexApi {
protected AnalysisConfiguration analysisConfig;
// Set up the loggers.
- private static Logger logger = LoggerFactory.getInstance()
- .getLogger(IndexApi.class.getName());
- private static Logger auditLogger = LoggerFactory.getInstance()
- .getAuditLogger(IndexApi.class.getName());
+ private static Logger logger = LoggerFactory.getInstance().getLogger(IndexApi.class.getName());
+ private static Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(IndexApi.class.getName());
public IndexApi(SearchServiceApi searchService) {
/**
- * Processes client requests to create a new index and document type in the
- * document store.
+ * Processes client requests to create a new index and document type in the document store.
*
- * @param documentSchema - The contents of the request body which is expected
- * to be a JSON structure which corresponds to the
- * schema defined in document.schema.json
+ * @param documentSchema - The contents of the request body which is expected to be a JSON structure which
+ * corresponds to the schema defined in document.schema.json
* @param index - The name of the index to create.
* @return - A Standard REST response
*/
- public ResponseEntity<String> processCreateIndex (String documentSchema,
- HttpServletRequest request,
- HttpHeaders headers,
- String index,
- DocumentStoreInterface documentStore) {
+ public ResponseEntity<String> processCreateIndex(String documentSchema, HttpServletRequest request,
+ HttpHeaders headers, String index, DocumentStoreInterface documentStore) {
int resultCode = 500;
- String resultString = "Unexpected error";
// Initialize the MDC Context for logging purposes.
ApiUtils.initMdcContext(request, headers);
// any further.
try {
- if (!searchService.validateRequest(headers, request,
- ApiUtils.Action.POST, ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
+ if (!searchService.validateRequest(headers, request, ApiUtils.Action.POST,
+ ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, "Authentication failure.");
return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
}
return errorResponse(HttpStatus.valueOf(resultCode), "Missing payload", request);
}
- try {
+ String resultString;
+ try {
// Marshal the supplied json string into a document schema object.
ObjectMapper mapper = new ObjectMapper();
DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
// create, so if we get back a 200 from the document store,
// translate that int a 201.
resultCode = (result.getResultCode() == 200) ? 201 : result.getResultCode();
- resultString = (result.getFailureCause() == null)
- ? result.getResult() : result.getFailureCause();
+ resultString = (result.getFailureCause() == null) ? result.getResult() : result.getFailureCause();
} catch (com.fasterxml.jackson.core.JsonParseException
| com.fasterxml.jackson.databind.JsonMappingException e) {
resultString = "IO Failure: " + e.getMessage();
}
- ResponseEntity<String> response = ResponseEntity.status(resultCode).contentType ( MediaType.APPLICATION_JSON ).body(resultString);
+ ResponseEntity<String> response =
+ ResponseEntity.status(resultCode).contentType(MediaType.APPLICATION_JSON).body(resultString);
// Log the result.
// Generate our audit log.
auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
- new LogFields()
- .setField(LogLine.DefinedFields.RESPONSE_CODE, resultCode)
- .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION,
- HttpStatus.valueOf(resultCode).toString()),
- (request != null) ? request.getMethod().toString () : "Unknown",
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, resultCode).setField(
+ LogLine.DefinedFields.RESPONSE_DESCRIPTION, HttpStatus.valueOf(resultCode).toString()),
+ (request != null) ? request.getMethod() : "Unknown",
(request != null) ? request.getRequestURL ().toString () : "Unknown",
(request != null) ? request.getRemoteHost () : "Unknown",
Integer.toString(response.getStatusCodeValue ()));
}
/**
- * This function accepts any JSON and will "blindly" write it to the
- * document store.
+ * This function accepts any JSON and will "blindly" write it to the document store.
*
- * Note, eventually this "dynamic" flow should follow the same JSON-Schema
- * validation procedure as the normal create index flow.
+ * Note, eventually this "dynamic" flow should follow the same JSON-Schema validation procedure as the normal create
+ * index flow.
*
* @param dynamicSchema - The JSON string that will be sent to the document store.
* @param index - The name of the index to be created.
ResponseEntity<String> response = null;
- ResponseEntity<String> validationResponse = validateRequest(request, headers, index, SearchDbMsgs.INDEX_CREATE_FAILURE);
+ ResponseEntity<String> validationResponse =
+ validateRequest(request, headers, index, SearchDbMsgs.INDEX_CREATE_FAILURE);
if (validationResponse.getStatusCodeValue () != HttpStatus.OK.value ()) {
}
/**
- * Processes a client request to remove an index from the document store.
- * Note that this implicitly deletes all documents contained within that index.
+ * Processes a client request to remove an index from the document store. Note that this implicitly deletes all
+ * documents contained within that index.
*
* @param index - The index to be deleted.
* @return - A standard REST response.
*/
- public ResponseEntity<String> processDelete(String index,
- HttpServletRequest request,
- HttpHeaders headers,
+ public ResponseEntity<String> processDelete(String index, HttpServletRequest request, HttpHeaders headers,
DocumentStoreInterface documentStore) {
// Initialize the MDC Context for logging purposes.
ApiUtils.initMdcContext(request, headers);
- // Set a default response in case something unexpected goes wrong.
- ResponseEntity<String> response = ResponseEntity.status ( HttpStatus.INTERNAL_SERVER_ERROR ).body ( "Unknown" );
+ ResponseEntity<String> response;
// Validate that the request is correctly authenticated before going
// any further.
try {
-
if (!searchService.validateRequest(headers, request, ApiUtils.Action.POST,
ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, "Authentication failure.");
}
} catch (Exception e) {
-
logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index,
"Unexpected authentication failure - cause: " + e.getMessage());
return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
}
-
try {
// Send the request to the document store.
response = responseFromOperationResult(documentStore.deleteIndex(index));
-
} catch (DocumentStoreOperationException e) {
- response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType ( MediaType.APPLICATION_JSON ).body(e.getMessage());
+ response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON)
+ .body(e.getMessage());
}
// Log the result.
if ((response.getStatusCodeValue() >= 200) && (response.getStatusCodeValue() < 300)) {
logger.info(SearchDbMsgs.DELETED_INDEX, index);
} else {
- logger.warn(SearchDbMsgs.INDEX_DELETE_FAILURE, index, (String) response.getBody ());
+ logger.warn(SearchDbMsgs.INDEX_DELETE_FAILURE, index, response.getBody());
}
// Generate our audit log.
auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
- new LogFields()
- .setField(LogLine.DefinedFields.RESPONSE_CODE, response.getStatusCodeValue())
- .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION,
- response.getStatusCode ().getReasonPhrase()),
- (request != null) ? request.getMethod().toString () : "Unknown",
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, response.getStatusCodeValue()).setField(
+ LogLine.DefinedFields.RESPONSE_DESCRIPTION, response.getStatusCode().getReasonPhrase()),
+ (request != null) ? request.getMethod() : "Unknown",
(request != null) ? request.getRequestURL ().toString () : "Unknown",
(request != null) ? request.getRemoteHost () : "Unknown",
Integer.toString(response.getStatusCodeValue()));
/**
- * This method takes a JSON format document schema and produces a set of
- * field mappings in the form that Elastic Search expects.
+ * This method takes a JSON format document schema and produces a set of field mappings in the form that Elastic
+ * Search expects.
*
* @param documentSchema - A document schema expressed as a JSON string.
* @return - A JSON string expressing an Elastic Search mapping configuration.
* @throws com.fasterxml.jackson.databind.JsonMappingException
* @throws IOException
*/
- public String generateDocumentMappings(String documentSchema)
- throws com.fasterxml.jackson.core.JsonParseException,
- com.fasterxml.jackson.databind.JsonMappingException, IOException {
+ public String generateDocumentMappings(String documentSchema) throws IOException {
// Unmarshal the json content into a document schema object.
ObjectMapper mapper = new ObjectMapper();
// If the index field was specified, then append it.
if (field.getSearchable() != null) {
- sb.append(", \"index\": \"").append(field.getSearchable()
- ? "analyzed" : "not_analyzed").append("\"");
+ sb.append(", \"index\": \"").append(field.getSearchable() ? "analyzed" : "not_analyzed").append("\"");
}
// If a search analyzer was specified, then append it.
/**
- * Converts an {@link OperationResult} to a standard REST {@link ResponseEntity}
- * object.
+ * Converts an {@link OperationResult} to a standard REST {@link ResponseEntity} object.
*
* @param result - The {@link OperationResult} to be converted.
* @return - The equivalent {@link ResponseEntity} object.
public ResponseEntity<String> responseFromOperationResult(OperationResult result) {
if ((result.getResultCode() >= 200) && (result.getResultCode() < 300)) {
- return ResponseEntity.status(result.getResultCode()).contentType ( MediaType.APPLICATION_JSON ).body(result.getResult());
+ return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
+ .body(result.getResult());
} else {
if (result.getFailureCause() != null) {
- return ResponseEntity.status(result.getResultCode()).contentType ( MediaType.APPLICATION_JSON ).body(result.getFailureCause());
+ return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
+ .body(result.getFailureCause());
} else {
- return ResponseEntity.status(result.getResultCode()).contentType ( MediaType.APPLICATION_JSON ).body(result.getResult());
+ return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
+ .body(result.getResult());
}
}
}
// Generate our audit log.
auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
- new LogFields()
- .setField(LogLine.DefinedFields.RESPONSE_CODE, status.value ())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, status.value())
.setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, status.getReasonPhrase()),
- (request != null) ? request.getMethod().toString () : "Unknown",
+ (request != null) ? request.getMethod() : "Unknown",
(request != null) ? request.getRequestURL ().toString () : "Unknown",
- (request != null) ? request.getRemoteHost () : "Unknown",
- Integer.toString(status.value ()));
+ (request != null) ? request.getRemoteHost() : "Unknown", Integer.toString(status.value()));
// Clear the MDC context so that no other transaction inadvertently
// uses our transaction id.
* @param failureMsgEnum - The logging message to be used upon validation failure.
* @return A success or failure response
*/
- private ResponseEntity<String> validateRequest(HttpServletRequest request, HttpHeaders headers, String index, SearchDbMsgs failureMsgEnum) {
+ private ResponseEntity<String> validateRequest(HttpServletRequest request, HttpHeaders headers, String index,
+ SearchDbMsgs failureMsgEnum) {
try {
- if (!searchService.validateRequest(headers, request, ApiUtils.Action.POST, ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
+ if (!searchService.validateRequest(headers, request, ApiUtils.Action.POST,
+ ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
logger.warn(failureMsgEnum, index, "Authentication failure.");
return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
}