Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1.     @Context
  2.     private HttpHeaders headers;
  3.     @Inject
  4.     private ResourceInfo resourceInfo; 
  5.  
  6.     private final static Set<MediaType> MEDIA_SUPPORTED = new HashSet<>(Arrays.asList(
  7.             MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_HTML_TYPE));
  8.  
  9.     private MediaType getMediaType() {
  10.         Optional<MediaType> mt = getMediaTypeFromHeaders(headers);
  11.         if (!mt.isPresent()) {
  12.             mt = getMediaTypeFromMethodAnnotation(resourceInfo);
  13.         }
  14.         if (!mt.isPresent()) {
  15.             mt = Optional.of(MediaType.TEXT_HTML_TYPE);
  16.         }
  17.         return mt.get();
  18.     }
  19.  
  20.     private Optional<MediaType> getMediaTypeFromMethodAnnotation(final ResourceInfo resourceInfo) {
  21.        
  22.         final Optional<Method> method = Optional.fromNullable(
  23.                 resourceInfo.getResourceMethod());
  24.         if (!method.isPresent()) {
  25.             return Optional.absent();
  26.         }
  27.         Optional<Produces> produces = Optional.fromNullable(
  28.                 method.get().getAnnotation(Produces.class));
  29.         if (!produces.isPresent()) {
  30.             final Class<?> cls = resourceInfo.getResourceClass();
  31.             produces = Optional.fromNullable((Produces) cls.getAnnotation(Produces.class));
  32.         }
  33.         if (!produces.isPresent()) {
  34.             return Optional.absent();
  35.         }
  36.         final List<String> mediaTypes = new LinkedList<>();
  37.         for (final String mtlist: produces.get().value()) { // comma separated list
  38.             for (final String mt: mtlist.split(",")) {
  39.                 mediaTypes.add(mt.trim());
  40.             }
  41.         }
  42.         for (final String mtype: mediaTypes) {
  43.             final MediaType putativeType;
  44.             try {
  45.                 putativeType = MediaType.valueOf(mtype);
  46.             } catch (IllegalArgumentException e) {
  47.                 // this might be impossible if JAX-RS validates the method annotations...
  48.                 LoggerFactory.getLogger(getClass()).error(String.format(
  49.                         "Invalid @Produces annotation on method %s: %s",
  50.                         method.get().toGenericString(), mtype));
  51.                 continue; // an else block like python would be nice here
  52.             }
  53.             if (MEDIA_SUPPORTED.contains(putativeType)) {
  54.                 return Optional.of(putativeType);
  55.             } else {
  56.                 LoggerFactory.getLogger(getClass()).error(String.format(
  57.                         "Unsupported @Produces annotation on method %s: %s",
  58.                         method.get().toGenericString(), mtype));
  59.             }
  60.         }
  61.         return Optional.absent();
  62.     }
  63.  
  64.     /* JAX-RS will cause an error to be thrown if the Accept header does not contain a supported
  65.      * media type, so we can ignore any unsupported types here
  66.      */
  67.     private Optional<MediaType> getMediaTypeFromHeaders(final HttpHeaders headers) {
  68.         //sorted by q-value
  69.         final Optional<List<MediaType>> mtypes = Optional.fromNullable(
  70.                 headers.getAcceptableMediaTypes());
  71.         if (mtypes.isPresent()) {
  72.             for (final MediaType m: mtypes.get()) {
  73.                 if (MEDIA_SUPPORTED.contains(m)) {
  74.                     return Optional.of(m);
  75.                 }
  76.             }
  77.         }
  78.         return Optional.absent();
  79.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement