Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - package net.peierls.util.restlet;
 - import java.io.IOException;
 - import java.io.Serializable;
 - import java.util.List;
 - import static java.util.Collections.singletonList;
 - import javax.inject.Inject;
 - import org.restlet.data.Form;
 - import org.restlet.data.MediaType;
 - import org.restlet.engine.converter.ConverterHelper;
 - import org.restlet.engine.resource.VariantInfo;
 - import org.restlet.representation.Representation;
 - import org.restlet.representation.Variant;
 - import org.restlet.resource.Resource;
 - /**
 - * Converts form representations to Java POJOs that have been
 - * marked with the {@link FormDeserializable} annotation,
 - * using a {@link FormDeserializer}.
 - * Only handles one direction, form to object.
 - */
 - public class FormConverter extends ConverterHelper {
 - @Inject FormConverter(FormDeserializer formDeserializer) {
 - this.formDeserializer = formDeserializer;
 - }
 - @Override public List<VariantInfo> getVariants(Class<?> source) {
 - if (source.isAnnotationPresent(FormDeserializable.class)) {
 - return singletonList(VARIANT_FORM);
 - } else {
 - return null;
 - }
 - }
 - @Override public <T> float score(Representation source, Class<T> target, Resource resource) {
 - MediaType mediaType = source.getMediaType();
 - if (target.isAnnotationPresent(FormDeserializable.class)
 - && MediaType.APPLICATION_WWW_FORM.isCompatible(mediaType)) {
 - return 2.0f;
 - } else {
 - return -1.0f;
 - }
 - }
 - @SuppressWarnings("unchecked")
 - @Override public <T> T toObject(Representation source,
 - Class<T> target, Resource resource) throws IOException {
 - if (target.isAnnotationPresent(FormDeserializable.class)) {
 - Form form = new Form(source);
 - return formDeserializer.deserialize(form, target);
 - } else {
 - return null;
 - }
 - }
 - //
 - // Don't need these because we're only going in one direction,
 - // HTML form representation to Object.
 - //
 - @Override public List<Class<?>> getObjectClasses(Variant source) {
 - return null;
 - }
 - @Override public float score(Object source, Variant target, Resource resource) {
 - return -1.0f;
 - }
 - @Override public Representation toRepresentation(Object source,
 - Variant target, Resource resource) throws IOException {
 - return null;
 - }
 - private final FormDeserializer formDeserializer;
 - private static final VariantInfo VARIANT_FORM = new VariantInfo(MediaType.APPLICATION_WWW_FORM);
 - }
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment