use of org.restlet.representation.WriterRepresentation 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;
}
use of org.restlet.representation.WriterRepresentation 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;
}
use of org.restlet.representation.WriterRepresentation 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.WriterRepresentation in project qi4j-sdk by Qi4j.
the class EntitiesResource method representHtml.
private Representation representHtml() throws ResourceException {
try {
final Iterable<EntityReference> query = entityFinder.findEntities(EntityComposite.class, null, null, null, null, Collections.<String, Object>emptyMap());
Representation representation = new WriterRepresentation(MediaType.TEXT_HTML) {
@Override
public void write(Writer buf) throws IOException {
PrintWriter out = new PrintWriter(buf);
out.println("<html><head><title>All entities</title></head><body><h1>All entities</h1><ul>");
for (EntityReference entity : query) {
out.println("<li><a href=\"" + getRequest().getResourceRef().clone().addSegment(entity.identity() + ".html") + "\">" + entity.identity() + "</a></li>");
}
out.println("</ul></body></html>");
}
};
representation.setCharacterSet(CharacterSet.UTF_8);
return representation;
} catch (EntityFinderException e) {
throw new ResourceException(e);
}
}
use of org.restlet.representation.WriterRepresentation in project qi4j-sdk by Qi4j.
the class EntitiesResource method representRdf.
private Representation representRdf() throws ResourceException {
try {
final Iterable<EntityReference> query = entityFinder.findEntities(EntityComposite.class, null, null, null, null, Collections.<String, Object>emptyMap());
WriterRepresentation representation = new WriterRepresentation(MediaType.APPLICATION_RDF_XML) {
@Override
public void write(Writer writer) throws IOException {
PrintWriter out = new PrintWriter(writer);
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<rdf:RDF\n" + "\txmlns=\"urn:qi4j:\"\n" + "\txmlns:qi4j=\"http://www.qi4j.org/rdf/model/1.0/\"\n" + "\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" + "\txmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">");
for (EntityReference qualifiedIdentity : query) {
out.println("<qi4j:entity rdf:about=\"" + getRequest().getResourceRef().getPath() + "/" + qualifiedIdentity.identity() + ".rdf\"/>");
}
out.println("</rdf:RDF>");
}
};
representation.setCharacterSet(CharacterSet.UTF_8);
return representation;
} catch (EntityFinderException e) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
}
}
Aggregations