Java Code Examples for org.restlet.data.Status#CLIENT_ERROR_BAD_REQUEST

The following examples show how to use org.restlet.data.Status#CLIENT_ERROR_BAD_REQUEST . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AgentsAgidObjectsDelete.java    From vicinity-gateway-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Deletes - unregisters the IoT object(s).
 * 
 * @param entity Representation of the incoming JSON. List of IoT thing descriptions that are to be removed 
 * (taken from request).
 * @return Notification of success or failure.
 * 
 */
@Post("json")
public Representation accept(Representation entity) {
	
	String attrAgid = getAttribute(ATTR_AGID);
	
	Logger logger = (Logger) getContext().getAttributes().get(Api.CONTEXT_LOGGER);
	XMLConfiguration config = (XMLConfiguration) getContext().getAttributes().get(Api.CONTEXT_CONFIG);
	
	if (attrAgid == null){
		logger.info("AGID: " + attrAgid + " Invalid Agent ID.");
		throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, 
				"Invalid Agent ID.");
	}
	
	if (!entity.getMediaType().equals(MediaType.APPLICATION_JSON)){
		logger.info("AGID: " + attrAgid + " Invalid object descriptions.");
		
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid object descriptions");
	}
	
	return deleteObjects(entity, logger, config);
}
 
Example 2
Source File: ObjectsOidPropertiesPid.java    From vicinity-gateway-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Sets the property value of an available IoT object.
 * 
 * @param entity Representation of the incoming JSON.
 * @param object Model.
 */
@Put("json")
public Representation store(Representation entity) {
	String attrOid = getAttribute(ATTR_OID);
	String attrPid = getAttribute(ATTR_PID);
	String callerOid = getRequest().getChallengeResponse().getIdentifier();
	Map<String, String> queryParams = getQuery().getValuesMap();
	
	Logger logger = (Logger) getContext().getAttributes().get(Api.CONTEXT_LOGGER);
	
	if (attrOid == null || attrPid == null){
		logger.info("OID: " + attrOid + " PID: " + attrPid 
								+ " Invalid identifier.");
		
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid identifier.");
	}
	
	String body = getRequestBody(entity, logger);
	
	return updateProperty(callerOid, attrOid, attrPid, body, queryParams);
}
 
Example 3
Source File: ObjectsOidPropertiesPid.java    From vicinity-gateway-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Gets the property value of an available IoT object.
 * 
 * @return Latest property value.
 */
@Get
public Representation represent(Representation entity) {
	String attrOid = getAttribute(ATTR_OID);
	String attrPid = getAttribute(ATTR_PID);
	String callerOid = getRequest().getChallengeResponse().getIdentifier();
	Map<String, String> queryParams = getQuery().getValuesMap();
	
	Logger logger = (Logger) getContext().getAttributes().get(Api.CONTEXT_LOGGER);
	
	if (attrOid == null || attrPid == null){
		logger.info("OID: " + attrOid + " PID: " + attrPid + " Invalid identifier.");
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid identifier.");
	}
	
	String body = getRequestBody(entity, logger);
	
	return getObjectProperty(callerOid, attrOid, attrPid, body, queryParams);
	
}
 
Example 4
Source File: ObjectsOidActionsAidTasksTid.java    From vicinity-gateway-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Gets a specific task status to perform an action of an available IoT object.
 * 
 * @return Task status.
 */
@Get
public Representation represent(Representation entity) {
	String attrOid = getAttribute(ATTR_OID);
	String attrAid = getAttribute(ATTR_AID);
	String attrTid = getAttribute(ATTR_TID);
	String callerOid = getRequest().getChallengeResponse().getIdentifier();
	Map<String, String> queryParams = getQuery().getValuesMap();
	
	Logger logger = (Logger) getContext().getAttributes().get(Api.CONTEXT_LOGGER);
	
	if (attrOid == null || attrAid == null || attrTid == null){
		logger.info("OID: " + attrOid + " AID: " + attrAid + " TID: " + attrTid 
				+ " Given identifier does not exist.");
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Given identifier does not exist.");
	}
	
	String body = getRequestBody(entity, logger);
	
	return getActionTaskStatus(callerOid, attrOid, attrAid, attrTid, queryParams, body);
}
 
Example 5
Source File: ObjectsOidEventsEid.java    From vicinity-gateway-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Cancel subscription to the channel.
 * 
 * @return statusMessage {@link StatusMessage StatusMessage} with the result of the operation.
 */
@Delete
public Representation remove(Representation entity) {
	String attrOid = getAttribute(ATTR_OID);
	String attrEid = getAttribute(ATTR_EID);
	String callerOid = getRequest().getChallengeResponse().getIdentifier();
	Map<String, String> queryParams = getQuery().getValuesMap();
	
	Logger logger = (Logger) getContext().getAttributes().get(Api.CONTEXT_LOGGER);
	
	if (attrEid == null){
		logger.info("EID: " + attrEid + " Given identifier does not exist.");
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid identifier.");
	}
	
	String body = getRequestBody(entity, logger);
	
	CommunicationManager communicationManager 
					= (CommunicationManager) getContext().getAttributes().get(Api.CONTEXT_COMMMANAGER);
	
	return new JsonRepresentation(communicationManager.unsubscribeFromEventChannel(callerOid, attrOid, attrEid, 
			queryParams, body).buildMessage().toString());
}
 
Example 6
Source File: ObjectsOidEventsEid.java    From vicinity-gateway-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Subscribes to the channel.
 * 
 * @param entity Representation of the incoming JSON.
 * @param object Model (from request).
 * @return A task to perform the action was submitted.
 */
@Post("json")
public Representation accept(Representation entity) {
	String attrOid = getAttribute(ATTR_OID);
	String attrEid = getAttribute(ATTR_EID);
	String callerOid = getRequest().getChallengeResponse().getIdentifier();
	Map<String, String> queryParams = getQuery().getValuesMap();
	
	Logger logger = (Logger) getContext().getAttributes().get(Api.CONTEXT_LOGGER);
	
	if (attrOid == null || attrEid == null){
		logger.info("OID: " + attrOid + " EID: " + attrEid + " Invalid identifier.");
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid identifier.");
	}
	
	String body = getRequestBody(entity, logger);

	CommunicationManager communicationManager 
				= (CommunicationManager) getContext().getAttributes().get(Api.CONTEXT_COMMMANAGER);


	return new JsonRepresentation(communicationManager.subscribeToEventChannel(callerOid, attrOid, attrEid, 
			queryParams, body).buildMessage().toString());
}
 
Example 7
Source File: EventsEid.java    From vicinity-gateway-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Used by an Agent/Adapter, that is capable of generating events and is willing to send these events to subscribed 
 * objects. A call to this end point activates the channel – from that moment, other objects in the network are able
 * to subscribe for receiving those messages.
 *  
 * @param entity Optional JSON with parameters.
 * @return statusMessage {@link StatusMessage StatusMessage} with the result of the operation.
 */
@Post("json")
public Representation accept(Representation entity) {
	String attrEid = getAttribute(ATTR_EID);
	String callerOid = getRequest().getChallengeResponse().getIdentifier();

	Map<String, String> queryParams = getQuery().getValuesMap();
	
	Logger logger = (Logger) getContext().getAttributes().get(Api.CONTEXT_LOGGER);
	
	if (attrEid == null){
		logger.info("EID: " + attrEid + " Invalid identifier.");
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid identifier.");
	}
	
	String body = getRequestBody(entity, logger);
	
	CommunicationManager communicationManager 
					= (CommunicationManager) getContext().getAttributes().get(Api.CONTEXT_COMMMANAGER);
	
	StatusMessage statusMessage = communicationManager.activateEventChannel(callerOid, attrEid, queryParams, body);
	
	return new JsonRepresentation(statusMessage.buildMessage().toString());
}
 
Example 8
Source File: RestManager.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new ManagedResource in the RestManager.
 */
@SuppressWarnings("unchecked")
@Override
public synchronized void doPut(BaseSolrResource endpoint, Representation entity, Object json) {      
  if (json instanceof Map) {
    String resourceId = ManagedEndpoint.resolveResourceId(endpoint.getRequest());
    Map<String,String> info = (Map<String,String>)json;
    info.put("resourceId", resourceId);
    storeManagedData(applyUpdatesToManagedData(json));
  } else {
    throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
        "Expected Map to create a new ManagedResource but received a "+json.getClass().getName());
  }          
  // PUT just returns success status code with an empty body
}
 
Example 9
Source File: ManagedSynonymGraphFilterFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected Object applyUpdatesToManagedData(Object updates) {
  boolean ignoreCase = getIgnoreCase();
  boolean madeChanges = false;
  if (updates instanceof List) {
    madeChanges = applyListUpdates((List<String>)updates, ignoreCase);
  } else if (updates instanceof Map) {
    madeChanges = applyMapUpdates((Map<String,Object>)updates, ignoreCase);
  } else {
    throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
        "Unsupported data format (" + updates.getClass().getName() + "); expected a JSON object (Map or List)!");
  }
  return madeChanges ? getStoredView() : null;
}
 
Example 10
Source File: EventsEid.java    From vicinity-gateway-api with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Used by an Agent/Adapter that is capable of generating events, to send an event to all subscribed objects on 
 * the network.
 * 
 * @param entity JSON with the event.
 * @return statusMessage {@link StatusMessage StatusMessage} with the result of the operation.
 */
@Put("json")
public Representation store(Representation entity) {
	String attrEid = getAttribute(ATTR_EID);
	String callerOid = getRequest().getChallengeResponse().getIdentifier();
	Map<String, String> queryParams = getQuery().getValuesMap();
	
	Logger logger = (Logger) getContext().getAttributes().get(Api.CONTEXT_LOGGER);
	
	// check the mandatory attributes
	if (attrEid == null){
		logger.info("EID: " + attrEid + " Invalid identifier.");
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid identifier.");
	}
	
	String body = getRequestBody(entity, logger);
	
	CommunicationManager communicationManager 
					= (CommunicationManager) getContext().getAttributes().get(Api.CONTEXT_COMMMANAGER);
	
	StatusMessage statusMessage 
					= communicationManager.sendEventToSubscribedObjects(callerOid, attrEid, body, 
							queryParams);
	
	return new JsonRepresentation(statusMessage.buildMessage().toString());
}
 
Example 11
Source File: SearchSemantic.java    From vicinity-gateway-api with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Retrieves a request body.
 * 
 * @param entity Entity to extract the body from.
 * @param logger Logger.
 * @return Text representation of the body.
 */
private String getRequestBody(Representation entity, Logger logger) {
	
	if (entity == null) {
		return null;
	}
	
	// check the body of the event to be sent
	if (!entity.getMediaType().equals(MediaType.APPLICATION_JSON)){
		logger.warning("Invalid request body - must be a valid JSON.");
		
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid request body - must be a valid JSON.");
	}
	
	// get the json
	String eventJsonString = null;
	try {
		eventJsonString = entity.getText();
	} catch (IOException e) {
		logger.info(e.getMessage());
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid request body");
	}
	
	return eventJsonString;
}
 
Example 12
Source File: ObjectsOidProperties.java    From vicinity-gateway-api with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Retrieves a request body.
 * 
 * @param entity Entity to extract the body from.
 * @param logger Logger.
 * @return Text representation of the body.
 */
private String getRequestBody(Representation entity, Logger logger) {
	
	if (entity == null) {
		return null;
	}
	
	// check the body of the event to be sent
	if (!entity.getMediaType().equals(MediaType.APPLICATION_JSON)){
		logger.warning("Invalid request body - must be a valid JSON.");
		
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid request body - must be a valid JSON.");
	}
	
	// get the json
	String eventJsonString = null;
	try {
		eventJsonString = entity.getText();
	} catch (IOException e) {
		logger.info(e.getMessage());
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid request body");
	}
	
	return eventJsonString;
}
 
Example 13
Source File: ObjectsOidActionsAidTasksTid.java    From vicinity-gateway-api with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Retrieves a request body.
 * 
 * @param entity Entity to extract the body from.
 * @param logger Logger.
 * @return Text representation of the body.
 */
private String getRequestBody(Representation entity, Logger logger) {
	
	if (entity == null) {
		return null;
	}
	
	// check the body of the event to be sent
	if (!entity.getMediaType().equals(MediaType.APPLICATION_JSON)){
		logger.warning("Invalid request body - must be a valid JSON.");
		
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid request body - must be a valid JSON.");
	}
	
	// get the json
	String eventJsonString = null;
	try {
		eventJsonString = entity.getText();
	} catch (IOException e) {
		logger.info(e.getMessage());
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid request body");
	}
	
	return eventJsonString;
}
 
Example 14
Source File: ObjectsOidActionsAid.java    From vicinity-gateway-api with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Retrieves a request body.
 * 
 * @param entity Entity to extract the body from.
 * @param logger Logger.
 * @return Text representation of the body.
 */
private String getRequestBody(Representation entity, Logger logger) {
	
	if (entity == null) {
		return null;
	}
	
	// check the body of the event to be sent
	if (!entity.getMediaType().equals(MediaType.APPLICATION_JSON)){
		logger.warning("Invalid request body - must be a valid JSON.");
		
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid request body - must be a valid JSON.");
	}
	
	// get the json
	String eventJsonString = null;
	try {
		eventJsonString = entity.getText();
	} catch (IOException e) {
		logger.info(e.getMessage());
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid request body");
	}
	
	return eventJsonString;
}
 
Example 15
Source File: ObjectsOid.java    From vicinity-gateway-api with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Retrieves a request body.
 * 
 * @param entity Entity to extract the body from.
 * @param logger Logger.
 * @return Text representation of the body.
 */
private String getRequestBody(Representation entity, Logger logger) {
	
	if (entity == null) {
		return null;
	}
	
	// check the body of the event to be sent
	if (!entity.getMediaType().equals(MediaType.APPLICATION_JSON)){
		logger.warning("Invalid request body - must be a valid JSON.");
		
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid request body - must be a valid JSON.");
	}
	
	// get the json
	String eventJsonString = null;
	try {
		eventJsonString = entity.getText();
	} catch (IOException e) {
		logger.info(e.getMessage());
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Invalid request body");
	}
	
	return eventJsonString;
}
 
Example 16
Source File: ObjectsOidProperties.java    From vicinity-gateway-api with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Answers the GET call
 * 
 * @return A {@link StatusMessage StatusMessage} 
 */
@Get
public Representation represent(Representation entity) {
	
	String attrOid = getAttribute(ATTR_OID);
	String callerOid = getRequest().getChallengeResponse().getIdentifier();
	Map<String, String> queryParams = getQuery().getValuesMap();
	
	Logger logger = (Logger) getContext().getAttributes().get(Api.CONTEXT_LOGGER);
	
	if (attrOid == null){
		
		logger.info("OID: " + attrOid + " Given identifier does not exist.");
		
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Given identifier does not exist.");
	}
	

	String body = getRequestBody(entity, logger);
	
	return getObjectProperties(callerOid, attrOid, body, queryParams);
	
}
 
Example 17
Source File: ObjectsOidEvents.java    From vicinity-gateway-api with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Answers the GET call
 * 
 * @return A {@link StatusMessage StatusMessage} 
 */
@Get
public Representation represent(Representation entity) {
	
	String attrOid = getAttribute(ATTR_OID);
	String callerOid = getRequest().getChallengeResponse().getIdentifier();
	Map<String, String> queryParams = getQuery().getValuesMap();
	
	Logger logger = (Logger) getContext().getAttributes().get(Api.CONTEXT_LOGGER);
	
	if (attrOid == null){
		
		logger.info("OID: " + attrOid + " Given identifier does not exist.");
		
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Given identifier does not exist.");
	}
	

	String body = getRequestBody(entity, logger);
	
	return getObjectEvents(callerOid, attrOid, body, queryParams);
	
}
 
Example 18
Source File: DefaultParameterResolver.java    From ontopia with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public <C> C resolveAttribute(TopicMapIF topicmap, Request request, String attribute, Class<C> klass, boolean allowNull) {
	Object resolved = null;
	
	Object id = request.getAttributes().get(attribute);
	if (id instanceof String) {
		String stringId = (String) id;
		
		if (String.class.isAssignableFrom(klass)) {
			return (C) stringId;
		} else if (LocatorIF.class.isAssignableFrom(klass)) {
			try {
				resolved = new URILocator(stringId);
			} catch (MalformedURLException me) {
				throw new OntopiaClientException(Status.CLIENT_ERROR_BAD_REQUEST, "Malformed URI: " + stringId, me);
			}
		} else if (TMObjectIF.class.isAssignableFrom(klass)) {
			resolved = resolve(stringId, topicmap, allowNull);
		} else {
			throw new OntopiaServerException("Don't know how to resolve '" + stringId + "' to " + klass.getName());
		}
	}
	
	// filters may pass locators
	if (id instanceof LocatorIF) {
		LocatorIF locator = (LocatorIF) id;
		
		if (LocatorIF.class.isAssignableFrom(klass)) {
			return (C) locator;
		} else if (String.class.isAssignableFrom(klass)) {
			return (C) locator.getAddress();
		} else if (TMObjectIF.class.isAssignableFrom(klass)) {
			resolved = resolveAsLocator(topicmap, locator);
		} else {
			throw new OntopiaServerException("Don't know how to resolve '" + locator + "' to " + klass.getName());
		}
	}
	
	if ((resolved == null) && (!allowNull)) {
		throw OntopiaRestErrors.MANDATORY_ATTRIBUTE_IS_NULL.build(attribute, klass.getName());
	}

	if (resolved != null) {
		if (!klass.isAssignableFrom(resolved.getClass())) {
			throw OntopiaRestErrors.MANDATORY_ATTRIBUTE_IS_WRONG_TYPE.build(attribute, klass.getName(), resolved);
		} else {
			return (C) resolved;
		}
	}
	
	return null;
	
}
 
Example 19
Source File: ManagedSynonymGraphFilterFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected boolean applyMapUpdates(Map<String,Object> jsonMap, boolean ignoreCase) {
  boolean madeChanges = false;

  for (String term : jsonMap.keySet()) {

    String origTerm = term;
    term = applyCaseSetting(ignoreCase, term);

    // find the mappings using the case aware key
    CasePreservedSynonymMappings cpsm = synonymMappings.get(term);
    if (cpsm == null)
      cpsm = new CasePreservedSynonymMappings();

    Set<String> output = cpsm.mappings.get(origTerm);

    Object val = jsonMap.get(origTerm); // IMPORTANT: use the original
    if (val instanceof String) {
      String strVal = (String)val;

      if (output == null) {
        output = new TreeSet<>();
        cpsm.mappings.put(origTerm, output);
      }

      if (output.add(strVal)) {
        madeChanges = true;
      }
    } else if (val instanceof List) {
      @SuppressWarnings({"unchecked"})
      List<String> vals = (List<String>)val;

      if (output == null) {
        output = new TreeSet<>();
        cpsm.mappings.put(origTerm, output);
      }

      for (String nextVal : vals) {
        if (output.add(nextVal)) {
          madeChanges = true;
        }
      }

    } else {
      throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Unsupported value "+val+
          " for "+term+"; expected single value or a JSON array!");
    }

    // only add the cpsm to the synonymMappings if it has valid data
    if (!synonymMappings.containsKey(term) && cpsm.mappings.get(origTerm) != null) {
      synonymMappings.put(term, cpsm);
    }
  }

  return madeChanges;
}
 
Example 20
Source File: ObjectsOid.java    From vicinity-gateway-api with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Answers the GET call
 * 
 * @return A {@link StatusMessage StatusMessage} 
 */
@Get
public Representation represent(Representation entity) {
	
	String attrOid = getAttribute(ATTR_OID);
	String callerOid = getRequest().getChallengeResponse().getIdentifier();
	Map<String, String> queryParams = getQuery().getValuesMap();
	
	Logger logger = (Logger) getContext().getAttributes().get(Api.CONTEXT_LOGGER);
	
	if (attrOid == null){
		
		logger.info("OID: " + attrOid + " Given identifier does not exist.");
		
		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
				"Given identifier does not exist.");
	}
	

	String body = getRequestBody(entity, logger);
	
	return getObjectThingDescription(callerOid, attrOid, body, queryParams);
	
}