All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.resteasy.util.MediaTypeMatcher Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package org.jboss.resteasy.util;

import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;

/**
 * Helper class that picks an object from a MediaType map from a list of sorted acceptable MediaTypes
 *
 * @author Bill Burke
 * @version $Revision: 1 $
 */
public class MediaTypeMatcher
{
   protected Map representations;

   public Map getRepresentations()
   {
      return representations;
   }

   public void setRepresentations(Map representations)
   {
      this.representations = representations;
   }

   public T match(List accepts)
   {
      List convertedAccepts = new ArrayList();
      for (MediaType accept : accepts) convertedAccepts.add(WeightedMediaType.parse(accept));

      IdentityHashMap consumesMap = new IdentityHashMap();

      for (Map.Entry entry : representations.entrySet())
      {
         for (WeightedMediaType accept : convertedAccepts)
         {
            if (entry.getKey().isCompatible(accept))
            {
               consumesMap.put(accept, entry.getValue());
               break;
            }
         }
      }
      if (consumesMap.size() == 0)
      {
         return null;
      }
      if (consumesMap.size() == 1) return consumesMap.values().iterator().next();

      ArrayList consumes = new ArrayList();
      consumes.addAll(consumesMap.keySet());
      Collections.sort(consumes);

      return consumesMap.get(consumes.get(0));
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy