SHARE
TWEET

FormConverter

tpeierls Mar 16th, 2012 374 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package net.peierls.util.restlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.Serializable;
  5. import java.util.List;
  6. import static java.util.Collections.singletonList;
  7.  
  8. import javax.inject.Inject;
  9.  
  10. import org.restlet.data.Form;
  11. import org.restlet.data.MediaType;
  12. import org.restlet.engine.converter.ConverterHelper;
  13. import org.restlet.engine.resource.VariantInfo;
  14. import org.restlet.representation.Representation;
  15. import org.restlet.representation.Variant;
  16. import org.restlet.resource.Resource;
  17.  
  18.  
  19. /**
  20.  * Converts form representations to Java POJOs that have been
  21.  * marked with the {@link FormDeserializable} annotation,
  22.  * using a {@link FormDeserializer}.
  23.  * Only handles one direction, form to object.
  24.  */
  25. public class FormConverter extends ConverterHelper {
  26.  
  27.     @Inject FormConverter(FormDeserializer formDeserializer) {
  28.         this.formDeserializer = formDeserializer;
  29.     }
  30.  
  31.     @Override public List<VariantInfo> getVariants(Class<?> source) {
  32.         if (source.isAnnotationPresent(FormDeserializable.class)) {
  33.             return singletonList(VARIANT_FORM);
  34.         } else {
  35.             return null;
  36.         }
  37.     }
  38.  
  39.     @Override public <T> float score(Representation source, Class<T> target, Resource resource) {
  40.         MediaType mediaType = source.getMediaType();
  41.         if (target.isAnnotationPresent(FormDeserializable.class)
  42.                 && MediaType.APPLICATION_WWW_FORM.isCompatible(mediaType)) {
  43.             return 2.0f;
  44.         } else {
  45.             return -1.0f;
  46.         }
  47.     }
  48.  
  49.     @SuppressWarnings("unchecked")
  50.     @Override public <T> T toObject(Representation source,
  51.             Class<T> target, Resource resource) throws IOException {
  52.  
  53.         if (target.isAnnotationPresent(FormDeserializable.class)) {
  54.             Form form = new Form(source);
  55.             return formDeserializer.deserialize(form, target);
  56.         } else {
  57.             return null;
  58.         }
  59.     }
  60.  
  61.     //
  62.     // Don't need these because we're only going in one direction,
  63.     // HTML form representation to Object.
  64.     //
  65.  
  66.     @Override public List<Class<?>> getObjectClasses(Variant source) {
  67.         return null;
  68.     }
  69.  
  70.     @Override public float score(Object source, Variant target, Resource resource) {
  71.         return -1.0f;
  72.     }
  73.  
  74.     @Override public Representation toRepresentation(Object source,
  75.             Variant target, Resource resource) throws IOException {
  76.  
  77.         return null;
  78.     }
  79.  
  80.  
  81.     private final FormDeserializer formDeserializer;
  82.  
  83.  
  84.     private static final VariantInfo VARIANT_FORM = new VariantInfo(MediaType.APPLICATION_WWW_FORM);
  85. }
RAW Paste Data
Top