* ============LICENSE_START==========================================
* org.onap.aai
* ===================================================================
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017 Amdocs
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 Amdocs
* ===================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END============================================
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
*/
package org.onap.champ;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Timer;
-
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
+import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
-
import org.json.JSONException;
import org.json.JSONObject;
import org.onap.aai.champcore.ChampTransaction;
import org.onap.champ.service.logging.LoggingUtil;
import org.onap.champ.util.ChampProperties;
import org.onap.champ.util.ChampServiceConstants;
-
+import org.onap.champ.util.HttpHeadersValidator;
+import org.onap.champ.util.etag.EtagGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
-@Path(value = "/")
+@Path(value = "/services/champ-service/v1/")
public class ChampRESTAPI {
private ObjectMapper mapper;
private ChampDataService champDataService;
+ private EtagGenerator etagGenerator;
+ private HttpHeadersValidator httpHeadersValidator;
private String TRANSACTION_METHOD = "method";
private Timer timer;
private Logger logger = LoggerFactory.getInstance().getLogger(ChampRESTAPI.class);
Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(ChampRESTAPI.class.getName());
private static Logger metricsLogger = LoggerFactory.getInstance().getMetricsLogger(ChampRESTAPI.class.getName());
+ private static final Pattern QUERY_OBJECT_ID_URL_MATCH = Pattern.compile("_reserved_(.*)");
- public ChampRESTAPI(ChampDataService champDataService, ChampAsyncRequestProcessor champAsyncRequestProcessor) {
+ public ChampRESTAPI(ChampDataService champDataService, ChampAsyncRequestProcessor champAsyncRequestProcessor) throws NoSuchAlgorithmException {
this.champDataService = champDataService;
- // Async request handling is optional.
+ // Async request handling is optional.
if (champAsyncRequestProcessor != null) {
timer = new Timer("ChampAsyncRequestProcessor-1");
timer.schedule(champAsyncRequestProcessor, champAsyncRequestProcessor.getRequestPollingTimeSeconds(),
module.addSerializer(ChampRelationship.class, new ChampRelationshipSerializer());
module.addDeserializer(ChampRelationship.class, new ChampRelationshipDeserializer());
mapper.registerModule(module);
+
+ etagGenerator = new EtagGenerator();
+ httpHeadersValidator = new HttpHeadersValidator();
}
@GET
ChampObject retrieved;
try {
+ httpHeadersValidator.validateRequestHeaders(headers);
ChampTransaction transaction = champDataService.getTransaction(tId);
if (tId != null && transaction == null) {
if (retrieved == null) {
response = Response.status(Status.NOT_FOUND).entity(objectId + " not found").build();
} else {
- response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).build();
+ EntityTag etag = new EntityTag(etagGenerator.computeHashForChampObject(retrieved));
+ response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).tag(etag).build();
}
} catch (JsonProcessingException e) {
response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
} catch (ChampServiceException ce) {
response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
+ } catch (Exception e) {
+ response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
+ LoggingUtil.logInternalError(logger, e);
} finally {
- logger.debug(response.getEntity().toString());
+ if (response != null) {
+ logger.debug(response.getEntity().toString());
+ }
LoggingUtil.logRestRequest(logger, auditLogger, req, response);
metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
}
LoggingUtil.initMdcContext(req, headers);
long startTimeInMs = System.currentTimeMillis();
logger.info(ChampMsgs.INCOMING_REQUEST, tId, objectId);
- ChampObject retrieved;
Response response = null;
try {
+ httpHeadersValidator.validateRequestHeaders(headers);
ChampTransaction transaction = champDataService.getTransaction(tId);
if (tId != null && transaction == null) {
logger.info(ChampMsgs.INCOMING_REQUEST, tId, champObj);
Response response = null;
try {
+ httpHeadersValidator.validateRequestHeaders(headers);
ChampTransaction transaction = champDataService.getTransaction(tId);
if (tId != null && transaction == null) {
throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
ChampObject champObject = mapper.readValue(champObj, ChampObject.class);
ChampObject created = champDataService.storeObject(champObject, Optional.ofNullable(transaction));
- response = Response.status(Status.CREATED).entity(mapper.writeValueAsString(created)).build();
+ EntityTag eTag = new EntityTag(etagGenerator.computeHashForChampObject(created));
+ response = Response.status(Status.CREATED).entity(mapper.writeValueAsString(created)).tag(eTag).build();
} catch (IOException e) {
response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
} catch (ChampServiceException ce) {
response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
+ } catch (IllegalArgumentException e) {
+ response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
} catch (Exception e) {
response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
LoggingUtil.logInternalError(logger, e);
Response response = null;
try {
+ httpHeadersValidator.validateRequestHeaders(headers);
ChampTransaction transaction = champDataService.getTransaction(tId);
if (tId != null && transaction == null) {
throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
ChampObject co = mapper.readValue(champObj, ChampObject.class);
// check if key is present or if it equals the key that is in the URI
ChampObject updated = champDataService.replaceObject(co, objectId, Optional.ofNullable(transaction));
-
- response = Response.status(Status.OK).entity(mapper.writeValueAsString(updated)).build();
+ EntityTag eTag = new EntityTag(etagGenerator.computeHashForChampObject(updated));
+ response = Response.status(Status.OK).entity(mapper.writeValueAsString(updated)).tag(eTag).build();
} catch (IOException e) {
response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
} catch (ChampServiceException ce) {
response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
+ } catch (IllegalArgumentException e) {
+ response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
} catch (Exception e) {
response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
LoggingUtil.logInternalError(logger, e);
LoggingUtil.initMdcContext(req, headers);
long startTimeInMs = System.currentTimeMillis();
List<ChampRelationship> retrieved;
- Optional<ChampObject> rObject;
Response response = null;
ChampTransaction transaction = null;
try {
-
+ httpHeadersValidator.validateRequestHeaders(headers);
retrieved = champDataService.getRelationshipsByObject(oId, Optional.ofNullable(transaction));
- response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).build();
+ EntityTag eTag = new EntityTag(etagGenerator.computeHashForChampRelationships(retrieved));
+ response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).tag(eTag).build();
} catch (JsonProcessingException e) {
response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
} catch (ChampServiceException ce) {
LoggingUtil.initMdcContext(req, headers);
long startTimeInMs = System.currentTimeMillis();
String propertiesKey = ChampProperties.get(ChampServiceConstants.CHAMP_COLLECTION_PROPERTIES_KEY);
- List<ChampObject> objects;
+ List<ChampObject> champObjects;
Map<String, Object> filter = new HashMap<>();
for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
- if (!e.getKey().equals(propertiesKey)) {
+ if ((!e.getKey().equals(propertiesKey)) && !reservedKeyMatcher ( QUERY_OBJECT_ID_URL_MATCH, e.getKey () )) {
filter.put(e.getKey(), e.getValue().get(0));
}
}
Response response = null;
try {
- objects = champDataService.queryObjects(filter, properties);
- response = Response.status(Status.OK).type(MediaType.APPLICATION_JSON).entity(mapper.writeValueAsString(objects))
+ httpHeadersValidator.validateRequestHeaders(headers);
+ champObjects = champDataService.queryObjects(filter, properties);
+ EntityTag eTag = new EntityTag(etagGenerator.computeHashForChampObjects(champObjects));
+ response = Response.status(Status.OK).type(MediaType.APPLICATION_JSON).tag(eTag).entity(mapper.writeValueAsString(champObjects))
.build();
} catch (JsonProcessingException e) {
- e.printStackTrace();
response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
} catch (ChampServiceException e1) {
response = Response.status(e1.getHttpStatus()).entity(e1.getMessage()).build();
ChampRelationship retrieved;
Response response = null;
try {
+ httpHeadersValidator.validateRequestHeaders(headers);
ChampTransaction transaction = champDataService.getTransaction(tId);
if (tId != null && transaction == null) {
response = Response.status(Status.NOT_FOUND).entity(rId + " not found").build();
return response;
}
- response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).build();
+ EntityTag eTag = new EntityTag(etagGenerator.computeHashForChampRelationship(retrieved));
+ response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).tag(eTag).build();
} catch (IOException e) {
response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
logger.info(ChampMsgs.INCOMING_REQUEST, tId, relationship);
Response response = null;
try {
+ httpHeadersValidator.validateRequestHeaders(headers);
ChampTransaction transaction = champDataService.getTransaction(tId);
if (tId != null && transaction == null) {
throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
ChampRelationship r = mapper.readValue(relationship, ChampRelationship.class);
ChampRelationship created = champDataService.storeRelationship(r, Optional.ofNullable(transaction));
-
- response = Response.status(Status.CREATED).entity(mapper.writeValueAsString(created)).build();
+ EntityTag eTag = new EntityTag(etagGenerator.computeHashForChampRelationship(created));
+ response = Response.status(Status.CREATED).entity(mapper.writeValueAsString(created)).tag(eTag).build();
} catch (IOException e) {
response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
} catch (ChampServiceException ce) {
response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
+ } catch (IllegalArgumentException e) {
+ response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
} catch (Exception e) {
response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
LoggingUtil.logInternalError(logger, e);
Response response = null;
try {
+ httpHeadersValidator.validateRequestHeaders(headers);
ChampTransaction transaction = champDataService.getTransaction(tId);
if (tId != null && transaction == null) {
throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
}
ChampRelationship r = mapper.readValue(relationship, ChampRelationship.class);
ChampRelationship updated = champDataService.updateRelationship(r, rId, Optional.ofNullable(transaction));
-
- response = Response.status(Status.OK).entity(mapper.writeValueAsString(updated)).build();
+ EntityTag eTag = new EntityTag(etagGenerator.computeHashForChampRelationship(updated));
+ response = Response.status(Status.OK).entity(mapper.writeValueAsString(updated)).tag(eTag).build();
} catch (IOException e) {
response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
} catch (ChampServiceException ce) {
response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
+ } catch (IllegalArgumentException e) {
+ response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
} catch (Exception e) {
response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
LoggingUtil.logInternalError(logger, e);
Response response = null;
try {
+ httpHeadersValidator.validateRequestHeaders(headers);
ChampTransaction transaction = champDataService.getTransaction(tId);
if (tId != null && transaction == null) {
throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
@Context HttpServletRequest req) {
LoggingUtil.initMdcContext(req, headers);
long startTimeInMs = System.currentTimeMillis();
- List<ChampRelationship> list;
+ List<ChampRelationship> champRelationshipList;
Map<String, Object> filter = new HashMap<>();
for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
- filter.put(e.getKey(), e.getValue().get(0));
+ if (!reservedKeyMatcher ( QUERY_OBJECT_ID_URL_MATCH, e.getKey () )) {
+ filter.put ( e.getKey (), e.getValue ().get ( 0 ) );
+ }
}
Response response = null;
try {
- list = champDataService.queryRelationships(filter);
- response = Response.status(Status.OK).type(MediaType.APPLICATION_JSON).entity(mapper.writeValueAsString(list))
+ httpHeadersValidator.validateRequestHeaders(headers);
+ champRelationshipList = champDataService.queryRelationships(filter);
+ EntityTag eTag = new EntityTag(etagGenerator.computeHashForChampRelationships(champRelationshipList));
+ response = Response.status(Status.OK).type(MediaType.APPLICATION_JSON).tag(eTag).entity(mapper.writeValueAsString(champRelationshipList))
.build();
} catch (JsonProcessingException e) {
- e.printStackTrace();
response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
} catch (ChampServiceException e1) {
response = Response.status(e1.getHttpStatus()).entity(e1.getMessage()).build();
@Context HttpServletRequest req) {
LoggingUtil.initMdcContext(req, headers);
long startTimeInMs = System.currentTimeMillis();
- Status s;
- String transaction = champDataService.openTransaction();
-
- s = Status.OK;
- Response response = Response.status(s).entity(transaction).build();
- logger.info(ChampMsgs.PROCESS_EVENT, "Opened Transaction with ID: " + transaction, s.toString());
- LoggingUtil.logRestRequest(logger, auditLogger, req, response);
- metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "POST", Long.toString(System.currentTimeMillis() - startTimeInMs));
+ Response response = null;
+ try {
+ httpHeadersValidator.validateRequestHeaders(headers);
+ String transaction = champDataService.openTransaction();
+ Status s = Status.OK;
+ response = Response.status(s).entity(transaction).build();
+ logger.info(ChampMsgs.PROCESS_EVENT, "Opened Transaction with ID: " + transaction, s.toString());
+ } catch (ChampServiceException e) {
+ response = Response.status(e.getHttpStatus()).entity(e.getMessage()).build();
+ } finally {
+ LoggingUtil.logRestRequest(logger, auditLogger, req, response);
+ metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "POST", Long.toString(System.currentTimeMillis() - startTimeInMs));
+ }
return response;
}
}
try {
+ httpHeadersValidator.validateRequestHeaders(headers);
response = Response.status(Status.OK).entity(mapper.writeValueAsString(tId + " is OPEN")).build();
} catch (JsonProcessingException e) {
response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
+ } catch (ChampServiceException e) {
+ response = Response.status(e.getHttpStatus()).entity(e.getMessage()).build();
} catch (Exception e) {
response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
LoggingUtil.logInternalError(logger, e);
Response response = null;
try {
+ httpHeadersValidator.validateRequestHeaders(headers);
JSONObject jsonObj = new JSONObject(t);
String method = jsonObj.getString(this.TRANSACTION_METHOD);
return response;
}
+ private boolean reservedKeyMatcher(Pattern p, String key) {
+ Matcher m = p.matcher ( key );
+ if (m.matches()) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
}