Search in sources :

Example 46 with Reference

use of org.restlet.data.Reference 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();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Reference(org.restlet.data.Reference) Uniform(org.restlet.Uniform) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) ResourceException(org.restlet.resource.ResourceException) Usecase(org.qi4j.api.usecase.Usecase)

Aggregations

Reference (org.restlet.data.Reference)46 Request (org.restlet.Request)26 Response (org.restlet.Response)26 Representation (org.restlet.representation.Representation)19 StringWriter (java.io.StringWriter)15 HashMap (java.util.HashMap)11 ZNRecord (org.apache.helix.ZNRecord)11 StringReader (java.io.StringReader)10 TypeReference (org.codehaus.jackson.type.TypeReference)10 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)9 ResourceException (org.restlet.resource.ResourceException)8 Form (org.restlet.data.Form)5 IOException (java.io.IOException)3 Date (java.util.Date)3 Test (org.junit.Test)3 ContextResourceClient (org.qi4j.library.rest.client.api.ContextResourceClient)3 ContextResourceClientFactory (org.qi4j.library.rest.client.api.ContextResourceClientFactory)3 ErrorHandler (org.qi4j.library.rest.client.api.ErrorHandler)3 HandlerCommand (org.qi4j.library.rest.client.api.HandlerCommand)3 ResponseHandler (org.qi4j.library.rest.client.spi.ResponseHandler)3