use of org.restlet.engine.io.WriterOutputStream in project qi4j-sdk by Qi4j.
the class ValueCompositeRequestWriter method writeRequest.
@Override
public boolean writeRequest(Object requestObject, Request request) throws ResourceException {
if (requestObject instanceof ValueComposite) {
// Value as parameter
final ValueComposite valueObject = (ValueComposite) requestObject;
if (request.getMethod().equals(Method.GET)) {
StateHolder holder = spi.stateOf(valueObject);
final ValueDescriptor descriptor = spi.valueDescriptorFor(valueObject);
final Reference ref = request.getResourceRef();
ref.setQuery(null);
try {
for (PropertyDescriptor propertyDescriptor : descriptor.state().properties()) {
Object value = holder.propertyFor(propertyDescriptor.accessor()).get();
String param;
if (value == null) {
param = null;
} else {
param = valueSerializer.serialize(value);
}
ref.addQueryParameter(propertyDescriptor.qualifiedName().name(), param);
}
} catch (ValueSerializationException e) {
throw new ResourceException(e);
}
} else {
request.setEntity(new WriterRepresentation(MediaType.APPLICATION_JSON) {
@Override
public void write(Writer writer) throws IOException {
setCharacterSet(CharacterSet.UTF_8);
valueSerializer.serialize(valueObject, new WriterOutputStream(writer));
}
});
}
return true;
}
return false;
}
Aggregations