Search in sources :

Example 56 with StringRepresentation

use of org.restlet.representation.StringRepresentation 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 57 with StringRepresentation

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

the class ContextResource method handleException.

private void handleException(Response response, Throwable ex) {
    while (ex instanceof InvocationTargetException) {
        ex = ex.getCause();
    }
    try {
        throw ex;
    } catch (ResourceException e) {
        // IAE (or subclasses) are considered client faults
        response.setEntity(new StringRepresentation(e.getMessage()));
        response.setStatus(e.getStatus());
    } catch (ConstraintViolationException e) {
        try {
            ConstraintViolationMessages cvm = new ConstraintViolationMessages();
            // CVE are considered client faults
            String messages = "";
            Locale locale = ObjectSelection.type(Locale.class);
            for (ConstraintViolation constraintViolation : e.constraintViolations()) {
                if (!messages.isEmpty()) {
                    messages += "\n";
                }
                messages += cvm.getMessage(constraintViolation, locale);
            }
            response.setEntity(new StringRepresentation(messages));
            response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
        } catch (Exception e1) {
            response.setEntity(new StringRepresentation(e.getMessage()));
            response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
        }
    } catch (IllegalArgumentException e) {
        // IAE (or subclasses) are considered client faults
        response.setEntity(new StringRepresentation(e.getMessage()));
        response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
    } catch (RuntimeException e) {
        // RuntimeExceptions are considered server faults
        LoggerFactory.getLogger(getClass()).warn("Exception thrown during processing", e);
        response.setEntity(new StringRepresentation(e.getMessage()));
        response.setStatus(Status.SERVER_ERROR_INTERNAL);
    } catch (Exception e) {
        // Checked exceptions are considered client faults
        String s = e.getMessage();
        if (s == null) {
            s = e.getClass().getSimpleName();
        }
        response.setEntity(new StringRepresentation(s));
        response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
    } catch (Throwable e) {
        // Anything else are considered server faults
        LoggerFactory.getLogger(getClass()).error("Exception thrown during processing", e);
        response.setEntity(new StringRepresentation(e.getMessage()));
        response.setStatus(Status.SERVER_ERROR_INTERNAL);
    }
}
Also used : Locale(java.util.Locale) StringRepresentation(org.restlet.representation.StringRepresentation) ConstraintViolation(org.qi4j.api.constraint.ConstraintViolation) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) ConstraintViolationMessages(org.qi4j.library.rest.server.restlet.ConstraintViolationMessages) ResourceException(org.restlet.resource.ResourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) ResourceException(org.restlet.resource.ResourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException)

Example 58 with StringRepresentation

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

the class DefaultResponseWriter method writeResponse.

@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
    MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
    if (MediaType.APPLICATION_JSON.equals(type)) {
        if (result instanceof String || result instanceof Number || result instanceof Boolean) {
            StringRepresentation representation = new StringRepresentation(result.toString(), MediaType.APPLICATION_JSON);
            response.setEntity(representation);
            return true;
        }
    }
    return false;
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) MediaType(org.restlet.data.MediaType)

Example 59 with StringRepresentation

use of org.restlet.representation.StringRepresentation 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)

Aggregations

StringRepresentation (org.restlet.representation.StringRepresentation)59 HttpVerb (com.linkedin.pinot.common.restlet.swagger.HttpVerb)30 Paths (com.linkedin.pinot.common.restlet.swagger.Paths)30 Summary (com.linkedin.pinot.common.restlet.swagger.Summary)30 Tags (com.linkedin.pinot.common.restlet.swagger.Tags)29 JSONException (org.json.JSONException)16 JSONObject (org.json.JSONObject)15 Responses (com.linkedin.pinot.common.restlet.swagger.Responses)14 IOException (java.io.IOException)12 JSONArray (org.json.JSONArray)12 Representation (org.restlet.representation.Representation)9 MediaType (org.restlet.data.MediaType)8 AbstractTableConfig (com.linkedin.pinot.common.config.AbstractTableConfig)7 File (java.io.File)7 PinotResourceManagerResponse (com.linkedin.pinot.controller.helix.core.PinotResourceManagerResponse)6 WriterRepresentation (org.restlet.representation.WriterRepresentation)6 Schema (com.linkedin.pinot.common.data.Schema)5 Writer (java.io.Writer)5 Get (org.restlet.resource.Get)5 TemplateException (freemarker.template.TemplateException)4