com.orange.ngsi.server.NgsiRestBaseController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ngsi-server Show documentation
Show all versions of ngsi-server Show documentation
NGSI v1 API server and client library
/*
* Copyright (C) 2015 Orange
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.orange.ngsi.server;
import com.orange.ngsi.ProtocolRegistry;
import com.orange.ngsi.exception.MismatchIdException;
import com.orange.ngsi.exception.MissingRequestParameterException;
import com.orange.ngsi.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
/**
* Controller for the NGSI 9/10 convenient REST requests
* Deviation from standard:
* - no support for attributeDomains requests
* - only NGSI 10 REST requests are supported
*/
public class NgsiRestBaseController {
private static Logger logger = LoggerFactory.getLogger(NgsiRestBaseController.class);
@Autowired
private NgsiValidation ngsiValidation;
@Autowired
private ProtocolRegistry protocolRegistry;
/* Context Entities */
@RequestMapping(method = RequestMethod.POST,
value = {"/contextEntities/{entityID}", "/contextEntities/{entityID}/attributes"},
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
final public ResponseEntity appendContextElement(
@PathVariable String entityID,
@RequestBody AppendContextElement appendContextElement,
HttpServletRequest httpServletRequest) throws Exception {
ngsiValidation.checkAppendContextElement(appendContextElement);
registerIntoDispatcher(httpServletRequest);
return new ResponseEntity<>(appendContextElement(entityID, appendContextElement), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.PUT,
value = {"/contextEntities/{entityID}", "/contextEntities/{entityID}/attributes"},
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
final public ResponseEntity updateContextEntity(@PathVariable String entityID,
@RequestBody UpdateContextElement updateContextElement,
HttpServletRequest httpServletRequest) throws Exception {
ngsiValidation.checkUpdateContextElement(updateContextElement);
registerIntoDispatcher(httpServletRequest);
return new ResponseEntity<>(updateContextElement(entityID, updateContextElement), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.GET,
value = {"/contextEntities/{entityID}", "/contextEntities/{entityID}/attributes"})
final public ResponseEntity getContextEntity(@PathVariable String entityID,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
return new ResponseEntity<>(getContextElement(entityID), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.DELETE,
value = {"/contextEntities/{entityID}", "/contextEntities/{entityID}/attributes"})
final public ResponseEntity deleteContextEntity(@PathVariable String entityID,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
return new ResponseEntity<>(deleteContextElement(entityID), HttpStatus.OK);
}
/* Context Attributes */
@RequestMapping(method = RequestMethod.POST,
value = "/contextEntities/{entityID}/attributes/{attributeName}",
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
final public ResponseEntity appendContextAttributeValue(@PathVariable String entityID,
@PathVariable String attributeName,
@RequestBody UpdateContextAttribute updateContextAttribute,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
ngsiValidation.checkUpdateContextAttribute(entityID, attributeName, null, updateContextAttribute);
return new ResponseEntity<>(appendContextAttribute(entityID, attributeName, updateContextAttribute), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.PUT,
value = "/contextEntities/{entityID}/attributes/{attributeName}",
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
final public ResponseEntity updateContextAttribute(@PathVariable String entityID,
@PathVariable String attributeName,
@RequestBody UpdateContextAttribute updateContextAttribute,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
ngsiValidation.checkUpdateContextAttribute(entityID, attributeName, null, updateContextAttribute);
return new ResponseEntity<>(updateContextAttribute(entityID, attributeName, updateContextAttribute), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.GET,
value = "/contextEntities/{entityID}/attributes/{attributeName}")
final public ResponseEntity getContextAttribute(@PathVariable String entityID,
@PathVariable String attributeName,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
return new ResponseEntity<>(getContextAttribute(entityID, attributeName), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.DELETE,
value = "/contextEntities/{entityID}/attributes/{attributeName}")
final public ResponseEntity deleteContextAttribute(@PathVariable String entityID,
@PathVariable String attributeName,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
return new ResponseEntity<>(deleteContextAttribute(entityID, attributeName), HttpStatus.OK);
}
/* Context Attributes Value instances */
@RequestMapping(method = RequestMethod.PUT,
value = "/contextEntities/{entityID}/attributes/{attributeName}/{valueID}",
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
final public ResponseEntity updateContextAttributeValue(@PathVariable String entityID,
@PathVariable String attributeName,
@PathVariable String valueID,
@RequestBody UpdateContextAttribute updateContextAttribute,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
ngsiValidation.checkUpdateContextAttribute(entityID, attributeName, valueID, updateContextAttribute);
return new ResponseEntity<>(updateContextAttributeValue(entityID, attributeName, valueID, updateContextAttribute), HttpStatus.OK);
}
@RequestMapping( method = RequestMethod.GET,
value = "/contextEntities/{entityID}/attributes/{attributeName}/{valueID}")
final public ResponseEntity getContextAttributeValue(@PathVariable String entityID,
@PathVariable String attributeName,
@PathVariable String valueID,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
return new ResponseEntity<>(getContextAttributeValue(entityID, attributeName, valueID), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.DELETE,
value = "/contextEntities/{entityID}/attributes/{attributeName}/{valueID}")
final public ResponseEntity deleteContextAttributeValue(@PathVariable String entityID,
@PathVariable String attributeName,
@PathVariable String valueID,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
return new ResponseEntity<>(deleteContextAttributeValue(entityID, attributeName, valueID), HttpStatus.OK);
}
/* Entity types */
@RequestMapping(method = RequestMethod.GET,
value = {"/contextEntityTypes/{typeName}",
"/contextEntityTypes/{typeName}/attributes"})
final public ResponseEntity getContextEntityTypes(
@PathVariable String typeName,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
return new ResponseEntity<>(getContextEntitiesType(typeName), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.GET,
value = "/contextEntityTypes/{typeName}/attributes/{attributeName}")
final public ResponseEntity getContextEntityTypes(
@PathVariable String typeName,
@PathVariable String attributeName,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
return new ResponseEntity<>(getContextEntitiesType(typeName, attributeName), HttpStatus.OK);
}
/* Subscriptions */
@RequestMapping(method = RequestMethod.POST,
value = "/contextSubscriptions",
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
final public ResponseEntity createSubscription(
@RequestBody SubscribeContext subscribeContext,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
ngsiValidation.checkSubscribeContext(subscribeContext);
return new ResponseEntity<>(createSubscription(subscribeContext), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.PUT,
value = "/contextSubscriptions/{subscriptionID}",
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
final public ResponseEntity updateSubscription(
@PathVariable String subscriptionID,
@RequestBody UpdateContextSubscription updateContextSubscription,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
ngsiValidation.checkUpdateSubscription(subscriptionID, updateContextSubscription);
return new ResponseEntity<>(updateSubscription(updateContextSubscription), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.DELETE,
value = "/contextSubscriptions/{subscriptionID}")
final public ResponseEntity deleteSubscription(
@PathVariable String subscriptionID,
HttpServletRequest httpServletRequest) throws Exception {
registerIntoDispatcher(httpServletRequest);
return new ResponseEntity<>(deleteSubscription(subscriptionID), HttpStatus.OK);
}
/*
* Exception handling
*/
@ExceptionHandler({MissingRequestParameterException.class})
public ResponseEntity