Search in sources :

Example 26 with MediaType

use of org.restlet.data.MediaType in project qi4j-sdk by Qi4j.

the class ValueDescriptorResponseWriter method writeResponse.

@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
    if (result instanceof ValueDescriptor) {
        MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
        if (MediaType.APPLICATION_JSON.equals(type)) {
            JSONObject json = new JSONObject();
            ValueDescriptor vd = (ValueDescriptor) result;
            try {
                for (PropertyDescriptor propertyDescriptor : vd.state().properties()) {
                    Object o = propertyDescriptor.initialValue(module);
                    if (o == null) {
                        json.put(propertyDescriptor.qualifiedName().name(), JSONObject.NULL);
                    } else {
                        json.put(propertyDescriptor.qualifiedName().name(), o.toString());
                    }
                }
            } catch (JSONException e) {
                throw new ResourceException(e);
            }
            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> context = new HashMap<String, Object>();
                    context.put("request", response.getRequest());
                    context.put("response", response);
                    context.put("result", result);
                    try {
                        cfg.getTemplate("form.htm").process(context, writer);
                    } catch (TemplateException e) {
                        throw new IOException(e);
                    }
                }
            };
            response.setEntity(rep);
            return true;
        }
    }
    return false;
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) TemplateException(freemarker.template.TemplateException) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) JSONException(org.json.JSONException) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) WriterRepresentation(org.restlet.representation.WriterRepresentation) IOException(java.io.IOException) JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) WriterRepresentation(org.restlet.representation.WriterRepresentation) MediaType(org.restlet.data.MediaType) JSONObject(org.json.JSONObject) ResourceException(org.restlet.resource.ResourceException) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer)

Example 27 with MediaType

use of org.restlet.data.MediaType in project qi4j-sdk by Qi4j.

the class ExtensionMediaTypeFilter method beforeHandle.

@Override
protected int beforeHandle(Request request, Response response) {
    List<String> segments = request.getResourceRef().getSegments();
    if (segments.get(segments.size() - 1).equals("")) {
        return Filter.CONTINUE;
    }
    String extensions = request.getResourceRef().getExtensions();
    if (extensions != null) {
        int idx = extensions.lastIndexOf(".");
        if (idx != -1) {
            extensions = extensions.substring(idx + 1);
        }
        MetadataService metadataService = getApplication().getMetadataService();
        Metadata metadata = metadataService.getMetadata(extensions);
        if (metadata != null && metadata instanceof MediaType) {
            request.getClientInfo().setAcceptedMediaTypes(Collections.singletonList(new Preference<MediaType>((MediaType) metadata)));
            String path = request.getResourceRef().getPath();
            path = path.substring(0, path.length() - extensions.length() - 1);
            request.getResourceRef().setPath(path);
        }
    }
    return Filter.CONTINUE;
}
Also used : Preference(org.restlet.data.Preference) Metadata(org.restlet.data.Metadata) MediaType(org.restlet.data.MediaType) MetadataService(org.restlet.service.MetadataService)

Example 28 with MediaType

use of org.restlet.data.MediaType 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 29 with MediaType

use of org.restlet.data.MediaType in project qi4j-sdk by Qi4j.

the class ResourceTemplateResponseWriter method writeResponse.

@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
    MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
    if (type != null) {
        // Try to find template for this specific resource
        StringBuilder templateBuilder = (StringBuilder) response.getRequest().getAttributes().get("template");
        String templateName = templateBuilder.toString();
        if (result instanceof ValueDescriptor) {
            templateName += "_form";
        }
        final String extension = metadataService.getExtension(type);
        templateName += "." + extension;
        // Have we failed on this one before, then don't try again
        if (skip.contains(templateName)) {
            return false;
        }
        try {
            final Template template = cfg.getTemplate(templateName);
            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 {
                        template.process(context, writer);
                    } catch (TemplateException e) {
                        throw new IOException(e);
                    }
                }
            };
            response.setEntity(rep);
            return true;
        } catch (Exception e) {
            skip.add(templateName);
        // Ignore
        }
    }
    return false;
}
Also used : HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) Representation(org.restlet.representation.Representation) WriterRepresentation(org.restlet.representation.WriterRepresentation) IOException(java.io.IOException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) ResourceException(org.restlet.resource.ResourceException) Template(freemarker.template.Template) WriterRepresentation(org.restlet.representation.WriterRepresentation) MediaType(org.restlet.data.MediaType) Writer(java.io.Writer)

Aggregations

MediaType (org.restlet.data.MediaType)29 StringRepresentation (org.restlet.representation.StringRepresentation)11 Representation (org.restlet.representation.Representation)10 IOException (java.io.IOException)9 Map (java.util.Map)7 WriterRepresentation (org.restlet.representation.WriterRepresentation)7 Writer (java.io.Writer)6 TemplateException (freemarker.template.TemplateException)5 HashMap (java.util.HashMap)5 Form (org.restlet.data.Form)5 ArrayList (java.util.ArrayList)4 Preference (org.restlet.data.Preference)4 EmptyRepresentation (org.restlet.representation.EmptyRepresentation)4 ResourceException (org.restlet.resource.ResourceException)4 Template (freemarker.template.Template)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 ParseException (java.text.ParseException)3 InputRepresentation (org.restlet.representation.InputRepresentation)3 Representation (org.restlet.resource.Representation)3