Search in sources :

Example 11 with WriterRepresentation

use of org.restlet.representation.WriterRepresentation in project qi4j-sdk by Qi4j.

the class FormResponseWriter method writeResponse.

@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
    if (result instanceof Form) {
        MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
        if (MediaType.APPLICATION_JSON.equals(type)) {
            JSONObject json = new JSONObject();
            Form form = (Form) result;
            try {
                for (Parameter parameter : form) {
                    String value = parameter.getValue();
                    if (value == null) {
                        json.put(parameter.getName(), JSONObject.NULL);
                    } else {
                        json.put(parameter.getName(), value);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            StringRepresentation representation = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON);
            response.setEntity(representation);
            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> root = new HashMap<String, Object>();
                    root.put("request", response.getRequest());
                    root.put("response", response);
                    root.put("result", result);
                    try {
                        Template formHtmlTemplate = cfg.getTemplate("form.htm");
                        formHtmlTemplate.process(root, writer);
                    } catch (TemplateException e) {
                        throw new IOException(e);
                    }
                }
            };
            response.setEntity(rep);
            return true;
        }
    }
    return false;
}
Also used : Form(org.restlet.data.Form) TemplateException(freemarker.template.TemplateException) JSONException(org.json.JSONException) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) WriterRepresentation(org.restlet.representation.WriterRepresentation) IOException(java.io.IOException) Template(freemarker.template.Template) JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) WriterRepresentation(org.restlet.representation.WriterRepresentation) MediaType(org.restlet.data.MediaType) Parameter(org.restlet.data.Parameter) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer)

Example 12 with WriterRepresentation

use of org.restlet.representation.WriterRepresentation in project qi4j-sdk by Qi4j.

the class LinksResponseWriter method createAtomRepresentation.

private Representation createAtomRepresentation(final Object result, final Response response) {
    return new WriterRepresentation(MediaType.APPLICATION_ATOM) {

        @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("links.atom").process(context, writer);
            } catch (TemplateException e) {
                throw new IOException(e);
            }
        }
    };
}
Also used : HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) WriterRepresentation(org.restlet.representation.WriterRepresentation) IOException(java.io.IOException) Writer(java.io.Writer)

Example 13 with WriterRepresentation

use of org.restlet.representation.WriterRepresentation in project qi4j-sdk by Qi4j.

the class LinksResponseWriter method createTextHtmlRepresentation.

private Representation createTextHtmlRepresentation(final Object result, final Response response) {
    return 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("links.htm").process(context, writer);
            } catch (TemplateException e) {
                throw new IOException(e);
            }
        }
    };
}
Also used : HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) WriterRepresentation(org.restlet.representation.WriterRepresentation) IOException(java.io.IOException) Writer(java.io.Writer)

Aggregations

Writer (java.io.Writer)13 WriterRepresentation (org.restlet.representation.WriterRepresentation)13 IOException (java.io.IOException)9 TemplateException (freemarker.template.TemplateException)7 HashMap (java.util.HashMap)7 Representation (org.restlet.representation.Representation)7 ResourceException (org.restlet.resource.ResourceException)7 MediaType (org.restlet.data.MediaType)6 StringRepresentation (org.restlet.representation.StringRepresentation)6 PrintWriter (java.io.PrintWriter)4 Map (java.util.Map)4 Template (freemarker.template.Template)3 EntityReference (org.qi4j.api.entity.EntityReference)3 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)3 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)3 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 ValueComposite (org.qi4j.api.value.ValueComposite)2 EntityFinderException (org.qi4j.spi.query.EntityFinderException)2 Reference (org.restlet.data.Reference)2