/** * Converts a regular Java object into a Representation. The converter will * use the preferred variant of the selected converter. * * @param source * The source object to convert. * @return The converted representation. * @throws IOException */ public Representation toRepresentation(Object source) throws IOException { return toRepresentation(source, null, null); }
/** * Converts a regular Java object into a Representation. * * @param source * The source object to convert. * @param target * The target representation variant. * @return The converted representation. * @throws IOException */ public Representation toRepresentation(Object source, Variant target) throws IOException { return toRepresentation(source, target, null); }
/** * Converts a regular Java object into a Representation. * * @param source * The source object to convert. * @param target * The target representation media type. * @return The converted representation. * @throws IOException */ public Representation toRepresentation(Object source, MediaType target) throws IOException { return toRepresentation(source, new Variant(target)); }
/** * Converts an object into a representation based on client preferences. * * @param source * The object to convert. * @param target * The target representation variant. * @return The wrapper representation. * @throws IOException */ public Representation toRepresentation(Object source, Variant target) throws IOException { Representation result = null; if (source != null) { org.restlet.service.ConverterService cs = getConverterService(); result = cs.toRepresentation(source, target, this); } return result; }
private String getRepresentationAsText(Object value) { Class<? extends Object> clazz = value.getClass(); boolean isPrimitiveOrWrapped = clazz.isPrimitive() || ClassUtils.wrapperToPrimitive(clazz) != null; if (isPrimitiveOrWrapped || clazz == String.class) { return String.valueOf(value); } String representationAsText = null; try { Representation representation = clientResource.getApplication() .getConverterService().toRepresentation(value); representationAsText = representation.getText(); } catch (IOException e) { throw new WebApplicationException(e); } return representationAsText; }
/** * Sets the entity from a higher-level object. This object is converted to a * representation using the Application's converter service. If you want to * use this method to facilitate the setting of entities, you need to * provide a custom implementation of the ConverterService class, overriding * the toRepresentation(Object) method. * * @param object * The higher-level object. * @see org.restlet.service.ConverterService * @deprecated Since 1.1, the ConverterService is deprecated, with no * replacement as it doesn't fit well with content negotiation. * Most users prefer to handle those conversion in Resource * subclasses. */ @Deprecated public void setEntity(Object object) { if (object instanceof Representation) { setEntity((Representation) object); } else { setEntity(getConverterService().toRepresentation(object)); } }
@Override public long getSize(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { Representation representation = null; try { // Convert the object into a representation Variant targetVariant = new Variant(toRestletMediaType(mediaType)); representation = getConverterService().toRepresentation(object, targetVariant, null); } catch (IOException e) { Context.getCurrentLogger().log(Level.FINE, "Unable to get the size", e); } return (representation == null) ? -1 : representation.getSize(); }
@Override public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { // Convert the object into a representation Variant targetVariant = new Variant(toRestletMediaType(mediaType)); Representation representation = getConverterService().toRepresentation( object, targetVariant, null); // Copy entity headers (NOT SUPPORTED) // Series<Parameter> entityHeaders = new Form(); // HttpServerAdapter.addEntityHeaders(representation, entityHeaders); // // for (Parameter header : entityHeaders) { // httpHeaders.add(header.getName(), header.getValue()); // } // Write the representation if (representation != null) { representation.write(entityStream); } } }
result = converterService.toRepresentation( representationObject, variant); } catch (Exception e) {