Search in sources :

Example 6 with Resource

use of org.qi4j.library.rest.common.Resource in project qi4j-sdk by Qi4j.

the class ResourceResponseWriter method writeResponse.

@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
    if (result instanceof Resource) {
        Resource resourceValue = (Resource) result;
        // Allowed methods
        response.getAllowedMethods().add(Method.GET);
        if (Iterables.matchesAny(LinksUtil.withRel("delete"), resourceValue.commands().get())) {
            response.getAllowedMethods().add(Method.DELETE);
        }
        if (Iterables.matchesAny(LinksUtil.withRel("update"), resourceValue.commands().get())) {
            response.getAllowedMethods().add(Method.PUT);
        }
        // Response according to what client accepts
        MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
        if (MediaType.APPLICATION_JSON.equals(type)) {
            response.setEntity(new StringRepresentation(resourceValue.toString(), MediaType.APPLICATION_JSON));
            return true;
        } else if (MediaType.TEXT_HTML.equals(type)) {
            Representation rep = new WriterRepresentation(MediaType.TEXT_HTML) {

                @Override
                public void write(Writer writer) throws IOException {
                    Map<String, Object> context = new HashMap<String, Object>();
                    context.put("request", response.getRequest());
                    context.put("response", response);
                    context.put("result", result);
                    try {
                        cfg.getTemplate("resource.htm").process(context, writer);
                    } catch (TemplateException e) {
                        throw new IOException(e);
                    }
                }
            };
            response.setEntity(rep);
            return true;
        }
    }
    return false;
}
Also used : TemplateException(freemarker.template.TemplateException) Resource(org.qi4j.library.rest.common.Resource) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) WriterRepresentation(org.restlet.representation.WriterRepresentation) IOException(java.io.IOException) StringRepresentation(org.restlet.representation.StringRepresentation) WriterRepresentation(org.restlet.representation.WriterRepresentation) MediaType(org.restlet.data.MediaType) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer)

Aggregations

Resource (org.qi4j.library.rest.common.Resource)6 Link (org.qi4j.library.rest.common.link.Link)5 Response (org.restlet.Response)5 Test (org.junit.Test)4 ContextResourceClient (org.qi4j.library.rest.client.api.ContextResourceClient)4 HandlerCommand (org.qi4j.library.rest.client.api.HandlerCommand)4 ResponseHandler (org.qi4j.library.rest.client.spi.ResponseHandler)4 ResultHandler (org.qi4j.library.rest.client.spi.ResultHandler)4 Links (org.qi4j.library.rest.common.link.Links)4 ContextResource (org.qi4j.library.rest.server.api.ContextResource)4 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)4 SubResource (org.qi4j.library.rest.server.api.SubResource)2 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ValueComposite (org.qi4j.api.value.ValueComposite)1 Request (org.restlet.Request)1