X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fsa%2Fsearchdbabstraction%2Felasticsearch%2Fdao%2FElasticSearchHttpController.java;h=98a254c836af4bf67570a0d878342899465c1592;hb=82658daad05f982a8358581623f44a1554238a48;hp=019d867a3a60f70bf26801d86703f32a51f89dea;hpb=8b971cd42183793e3363e850456282081b2c09ec;p=aai%2Fsearch-data-service.git diff --git a/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java b/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java index 019d867..98a254c 100644 --- a/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java +++ b/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java @@ -48,6 +48,7 @@ import org.onap.aai.sa.searchdbabstraction.entity.SearchOperationResult; import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs; import org.onap.aai.sa.searchdbabstraction.util.AggregationParsingUtil; import org.onap.aai.sa.searchdbabstraction.util.DocumentSchemaUtil; +import org.onap.aai.sa.searchdbabstraction.util.ElasticSearchPayloadTranslator; import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants; import org.onap.aai.cl.api.LogFields; import org.onap.aai.cl.api.LogLine; @@ -79,7 +80,9 @@ import java.util.Arrays; import java.util.List; import java.util.Properties; import java.util.concurrent.atomic.AtomicBoolean; -import javax.ws.rs.core.Response.Status; + +import org.springframework.http.HttpStatus; + /** @@ -177,7 +180,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { //result.setResult("{\"index\": \"" + index + ", \"type\": \"" + DEFAULT_TYPE + "\"}"); } - } catch (DocumentStoreOperationException e) { + } catch (DocumentStoreOperationException | IOException e) { result.setFailureCause("Document store operation failure. Cause: " + e.getMessage()); } @@ -348,6 +351,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { try { conn.setRequestMethod("PUT"); + conn.setRequestProperty("Content-Type", "application/json"); } catch (ProtocolException e) { shutdownConnection(conn); throw new DocumentStoreOperationException("Failed to set HTTP request method to PUT.", e); @@ -363,7 +367,12 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { sb.append(indexMappings); sb.append("}}"); - attachContent(conn, sb.toString()); + try { + attachContent(conn, ElasticSearchPayloadTranslator.translateESPayload(sb.toString())); + } catch(IOException e) { + logger.error(SearchDbMsgs.INDEX_CREATE_FAILURE, e); + throw new DocumentStoreOperationException(e.getMessage(), e); + } logger.debug("\ncreateTable(), Sending 'PUT' request to URL : " + conn.getURL()); logger.debug("Request content: " + sb.toString()); @@ -405,12 +414,18 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { try { conn.setRequestMethod("PUT"); + conn.setRequestProperty("Content-Type", "application/json"); } catch (ProtocolException e) { shutdownConnection(conn); throw new DocumentStoreOperationException("Failed to set HTTP request method to PUT.", e); } - attachContent(conn, settingsAndMappings); + try { + attachContent(conn, ElasticSearchPayloadTranslator.translateESPayload(settingsAndMappings)); + } catch(IOException e) { + logger.error(SearchDbMsgs.INDEX_CREATE_FAILURE, e); + throw new DocumentStoreOperationException(e.getMessage()); + } handleResponse(conn, result); // Generate a metrics log so we can track how long the operation took. @@ -440,7 +455,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { if ((indexExistsResult.getResultCode() < 200) || (indexExistsResult.getResultCode() >= 300)) { DocumentOperationResult opResult = new DocumentOperationResult(); - opResult.setResultCode(Status.NOT_FOUND.getStatusCode()); + opResult.setResultCode(HttpStatus.NOT_FOUND.value()); opResult.setResult("Document Index '" + indexName + "' does not exist."); opResult.setFailureCause("Document Index '" + indexName + "' does not exist."); return opResult; @@ -461,13 +476,13 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { DocumentOperationResult opResult = checkDocumentExistence(indexName, document.getId()); - if (opResult.getResultCode() != Status.NOT_FOUND.getStatusCode()) { - if (opResult.getResultCode() == Status.OK.getStatusCode()) { + if (opResult.getResultCode() != HttpStatus.NOT_FOUND.value()) { + if (opResult.getResultCode() == HttpStatus.CONFLICT.value()) { opResult.setFailureCause("A document with the same id already exists."); } else { opResult.setFailureCause("Failed to verify a document with the specified id does not already exist."); } - opResult.setResultCode(Status.CONFLICT.getStatusCode()); + opResult.setResultCode(HttpStatus.CONFLICT.value()); return opResult; } @@ -555,9 +570,9 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { private void attachDocument(HttpURLConnection conn, DocumentStoreDataEntity doc) throws DocumentStoreOperationException { - conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); +// conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Connection", "Close"); - attachContent(conn, doc.getContentInJson()); } @@ -626,7 +641,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { if ((indexExistsResult.getResultCode() < 200) || (indexExistsResult.getResultCode() >= 300)) { DocumentOperationResult opResult = new DocumentOperationResult(); - opResult.setResultCode(Status.NOT_FOUND.getStatusCode()); + opResult.setResultCode(HttpStatus.NOT_FOUND.value()); opResult.setResult("Document Index '" + indexName + "' does not exist."); opResult.setFailureCause("Document Index '" + indexName + "' does not exist."); return opResult; @@ -648,6 +663,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { try { conn.setRequestMethod("PUT"); + conn.setRequestProperty("Content-Type", "application/json"); } catch (ProtocolException e) { shutdownConnection(conn); throw new DocumentStoreOperationException("Failed to set HTTP request method to PUT.", e); @@ -821,6 +837,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { try { conn.setRequestMethod("POST"); + conn.setRequestProperty("Content-Type", "application/json"); } catch (ProtocolException e) { shutdownConnection(conn); throw new DocumentStoreOperationException("Failed to set HTTP request method to POST.", e); @@ -870,6 +887,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { try { conn.setRequestMethod("POST"); + conn.setRequestProperty("Content-Type", "application/json"); } catch (ProtocolException e) { shutdownConnection(conn); throw new DocumentStoreOperationException("Failed to set HTTP request method to POST.", e); @@ -979,8 +997,8 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { throw new DocumentStoreOperationException("Failed getting the response body payload.", e); } - if (resultCode == Status.CONFLICT.getStatusCode()) { - opResult.setResultCode(Status.PRECONDITION_FAILED.getStatusCode()); + if (resultCode == HttpStatus.CONFLICT.value()) { + opResult.setResultCode(HttpStatus.PRECONDITION_FAILED.value()); } else { opResult.setResultCode(resultCode); }