use of org.restlet.Uniform in project camel by apache.
the class RestletConsumer method createRestlet.
protected Restlet createRestlet() {
return new Restlet() {
@Override
public void handle(Request request, Response response) {
// must call super according to restlet documentation
super.handle(request, response);
LOG.debug("Consumer restlet handle request method: {}", request.getMethod());
Exchange exchange = null;
try {
// we want to handle the UoW
exchange = getEndpoint().createExchange();
createUoW(exchange);
RestletBinding binding = getEndpoint().getRestletBinding();
binding.populateExchangeFromRestletRequest(request, response, exchange);
try {
getProcessor().process(exchange);
} catch (Exception e) {
exchange.setException(e);
}
binding.populateRestletResponseFromExchange(exchange, response);
// resetlet will call the callback when its done sending where it would be safe
// to call doneUoW
Uniform callback = newResponseUniform(exchange);
response.setOnError(callback);
response.setOnSent(callback);
} catch (Throwable e) {
getExceptionHandler().handleException("Error processing request", exchange, e);
if (exchange != null) {
doneUoW(exchange);
}
}
}
};
}
use of org.restlet.Uniform in project camel by apache.
the class RestletProducer method process.
@Override
public boolean process(final Exchange exchange, final AsyncCallback callback) {
RestletEndpoint endpoint = (RestletEndpoint) getEndpoint();
// force processing synchronously using different api
if (endpoint.isSynchronous()) {
try {
process(exchange);
} catch (Throwable e) {
exchange.setException(e);
}
callback.done(true);
return true;
}
LOG.trace("Processing asynchronously");
final RestletBinding binding = endpoint.getRestletBinding();
Request request;
try {
String resourceUri = buildUri(endpoint, exchange);
URI uri = new URI(resourceUri);
request = new Request(endpoint.getRestletMethod(), resourceUri);
binding.populateRestletRequestFromExchange(request, exchange);
loadCookies(exchange, uri, request);
} catch (Throwable e) {
// break out in case of exception
exchange.setException(e);
callback.done(true);
return true;
}
// process the request asynchronously
LOG.debug("Sending request asynchronously: {} for exchangeId: {}", request, exchange.getExchangeId());
client.handle(request, new Uniform() {
@Override
public void handle(Request request, Response response) {
LOG.debug("Received response asynchronously: {} for exchangeId: {}", response, exchange.getExchangeId());
try {
if (response != null) {
String resourceUri = buildUri(endpoint, exchange);
URI uri = new URI(resourceUri);
Integer respCode = response.getStatus().getCode();
storeCookies(exchange, uri, response);
if (respCode > 207 && throwException) {
exchange.setException(populateRestletProducerException(exchange, response, respCode));
} else {
binding.populateExchangeFromRestletResponse(exchange, response);
}
}
} catch (Throwable e) {
exchange.setException(e);
} finally {
callback.done(false);
}
}
});
// we continue routing async
return false;
}
use of org.restlet.Uniform in project qi4j-sdk by Qi4j.
the class ContextRestlet method subResource.
// Callbacks used from resources
public void subResource(Class<? extends ContextResource> subResourceClass) {
Uniform subResource = subResources.get(subResourceClass);
if (subResource == null) {
// Instantiate and store subresource instance
subResource = module.newObject(subResourceClass, this);
subResources.put(subResourceClass, subResource);
}
subResource.handle(Request.getCurrent(), Response.getCurrent());
}
use of org.restlet.Uniform in project qi4j-sdk by Qi4j.
the class ContextRestlet method handle.
@Override
public void handle(Request request, Response response) {
super.handle(request, response);
MDC.put("url", request.getResourceRef().toString());
try {
int tries = 0;
// TODO Make this number configurable
while (tries < 10) {
tries++;
// Root of the call
Reference ref = request.getResourceRef();
List<String> segments = ref.getScheme().equals("riap") ? ref.getRelativeRef(new Reference("riap://application/")).getSegments() : ref.getRelativeRef().getSegments();
// Handle conversion of verbs into standard interactions
if (segments.get(segments.size() - 1).equals("")) {
if (request.getMethod().equals(Method.DELETE)) {
// Translate DELETE into command "delete"
segments.set(segments.size() - 1, "delete");
} else if (request.getMethod().equals(Method.PUT)) {
// Translate PUT into command "update"
segments.set(segments.size() - 1, "update");
}
}
request.getAttributes().put("segments", segments);
request.getAttributes().put("template", new StringBuilder("/rest/"));
Usecase usecase = UsecaseBuilder.buildUsecase(getUsecaseName(request)).withMetaInfo(request.getMethod().isSafe() ? CacheOptions.ALWAYS : CacheOptions.NEVER).newUsecase();
UnitOfWork uow = module.newUnitOfWork(usecase);
ObjectSelection.newSelection();
try {
// Start handling the build-up for the context
Uniform resource = createRoot(request, response);
resource.handle(request, response);
if (response.getEntity() != null) {
if (response.getEntity().getModificationDate() == null) {
ResourceValidity validity = (ResourceValidity) Request.getCurrent().getAttributes().get(ContextResource.RESOURCE_VALIDITY);
if (validity != null) {
validity.updateResponse(response);
}
}
// Check if characterset is set
if (response.getEntity().getCharacterSet() == null) {
response.getEntity().setCharacterSet(CharacterSet.UTF_8);
}
// Check if language is set
if (response.getEntity().getLanguages().isEmpty()) {
response.getEntity().getLanguages().add(Language.ENGLISH);
}
uow.discard();
} else {
// Check if last modified and tag is set
ResourceValidity validity = null;
try {
validity = ObjectSelection.type(ResourceValidity.class);
} catch (IllegalArgumentException e) {
// Ignore
}
uow.complete();
Object result = commandResult.getResult();
if (result != null) {
if (result instanceof Representation) {
response.setEntity((Representation) result);
} else {
if (!responseWriter.writeResponse(result, response)) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Could not write result of type " + result.getClass().getName());
}
}
if (response.getEntity() != null) {
// Check if characterset is set
if (response.getEntity().getCharacterSet() == null) {
response.getEntity().setCharacterSet(CharacterSet.UTF_8);
}
// Check if language is set
if (response.getEntity().getLanguages().isEmpty()) {
response.getEntity().getLanguages().add(Language.ENGLISH);
}
// Check if last modified and tag should be set
if (validity != null) {
UnitOfWork lastModifiedUoW = module.newUnitOfWork();
try {
validity.updateEntity(lastModifiedUoW);
validity.updateResponse(response);
} finally {
lastModifiedUoW.discard();
}
}
}
}
return;
}
return;
} catch (ConcurrentEntityModificationException ex) {
uow.discard();
// Try again
ObjectSelection.newSelection();
} catch (Throwable e) {
uow.discard();
handleException(response, e);
return;
}
}
// Try again
} finally {
MDC.clear();
}
}
Aggregations