use of org.qi4j.api.value.ValueDescriptor 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.qi4j.api.value.ValueDescriptor in project qi4j-sdk by Qi4j.
the class ValueEqualityTest method givenValuesOfTheSameTypeWhenTestingValueDescriptorEqualityExpectEquals.
//
// -------------------------------:: ValueDescriptor equality tests ::----------------------------------------------
//
@Test
public void givenValuesOfTheSameTypeWhenTestingValueDescriptorEqualityExpectEquals() {
Some some = buildSomeValue(module);
ValueDescriptor someDescriptor = qi4j.api().valueDescriptorFor(some);
Some other = buildSomeValue(module);
ValueDescriptor otherDescriptor = qi4j.api().valueDescriptorFor(other);
assertThat("ValueDescriptors equal", someDescriptor, equalTo(otherDescriptor));
assertThat("ValueDescriptors hashcode equal", someDescriptor.hashCode(), equalTo(otherDescriptor.hashCode()));
}
Aggregations