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;
}
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);
}
}
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;
}
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;
}
Aggregations