use of freemarker.template.TemplateException 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 freemarker.template.TemplateException 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 freemarker.template.TemplateException 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;
}
use of freemarker.template.TemplateException 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);
}
}
};
}
use of freemarker.template.TemplateException 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);
}
}
};
}
Aggregations