@Test public void testRepresentationToObject() throws IOException, JAXBException { ConverterService cs = new ConverterService(); ProtobufRepresentation<org.restlet.ext.protobuf.Sample.PersonDto> protobufRep = new ProtobufRepresentation<org.restlet.ext.protobuf.Sample.PersonDto>( org.restlet.ext.protobuf.Sample.PersonDto.newBuilder().build(), MediaType.APPLICATION_XML); Object rep = cs.toObject(protobufRep, org.restlet.ext.protobuf.Sample.PersonDto.class, null); assertTrue(rep instanceof org.restlet.ext.protobuf.Sample.PersonDto); }
@Test public void testObjectionToRepresentation() { ConverterService cs = new ConverterService(); Representation rep = cs.toRepresentation( org.restlet.ext.protobuf.Sample.PersonDto.newBuilder().build(), new Variant(MediaType.APPLICATION_XML), null); assertTrue(rep instanceof ProtobufRepresentation<?>); }
/** * Handles an object entity. Automatically serializes the object using the {@link * org.restlet.service.ConverterService}. * * @param method The request method to use. * @param entity The object entity to post. * @param resultClass The class of the response entity. * @return The response object entity. * @throws ResourceException */ private <T> T handle(Method method, Object entity, Class<T> resultClass) throws ResourceException { T result = null; org.restlet.service.ConverterService cs = getConverterService(); ClientInfo clientInfo = getClientInfo(); if (clientInfo.getAcceptedMediaTypes().isEmpty()) { cs.updatePreferences(clientInfo.getAcceptedMediaTypes(), resultClass); } Representation requestEntity = null; if (entity != null) { List<? extends Variant> variants = cs.getVariants(entity.getClass(), null); requestEntity = toRepresentation(entity, clientInfo.getPreferredVariant(variants, getMetadataService())); } result = toObject(handle(method, requestEntity, clientInfo), resultClass); return result; }
/** * Returns a list of response variants based on the annotation value. * * @param metadataService The metadata service to use. * @param converterService The converter service to use. * @return A list of response variants. */ @SuppressWarnings("unchecked") public List<Variant> getResponseVariants( MetadataService metadataService, org.restlet.service.ConverterService converterService) { List<Variant> result = null; if ((getJavaOutputType() != null) && (getJavaOutputType() != void.class) && (getJavaOutputType() != Void.class)) { result = getVariants(metadataService, getOutput()); if (result == null) { result = (List<Variant>) converterService.getVariants(getJavaOutputType(), null); } } return result; }
/** * Returns a list of request variants based on the annotation value. * * @param metadataService The metadata service to use. * @return A list of request variants. */ @SuppressWarnings("unchecked") public List<Variant> getRequestVariants( MetadataService metadataService, org.restlet.service.ConverterService converterService) { List<Variant> result = null; Class<?>[] classes = getJavaInputTypes(); if (classes != null && classes.length >= 1) { result = getVariants(metadataService, getInput()); if (result == null) { Class<?> inputClass = classes[0]; result = (List<Variant>) converterService.getVariants(inputClass, null); } } return result; }