if (invoker.doesProduce(accepts))
{
list.add(invoker);
if (invoker.getConsumes() == null)
{
WeightedMediaType defaultConsumes = WeightedMediaType.valueOf("*/*;q=0.0");
consumesMap.put(defaultConsumes, invoker);
}
else
{
for (WeightedMediaType consume : invoker.getPreferredConsumes())
{
consumesMap.put(consume, invoker);
}
}
}
}
}
}
if (list.size() == 0)
{
if (locator != null) return locator;
if (!methodMatch)
{
HashSet<String> allowed = new HashSet<String>();
for (ResourceMethod invoker : methods) allowed.addAll(invoker.getHttpMethods());
if (httpMethod.equalsIgnoreCase("HEAD") && allowed.contains("GET"))
{
return match("GET", contentType, oldaccepts);
}
if (allowed.contains("GET")) allowed.add("HEAD");
allowed.add("OPTIONS");
String allowHeaderValue = "";
boolean first = true;
for (String allow : allowed)
{
if (first) first = false;
else allowHeaderValue += ", ";
allowHeaderValue += allow;
}
if (httpMethod.equals("OPTIONS"))
{
Response res = Response.ok().header(HttpHeaderNames.ALLOW, allowHeaderValue).build();
throw new DefaultOptionsMethodException("No resource method found for options, return OK with Allow header", res);
}
else
{
Response res = Response.status(HttpResponseCodes.SC_METHOD_NOT_ALLOWED).header(HttpHeaderNames.ALLOW, allowHeaderValue).build();
throw new MethodNotAllowedException("No resource method found for " + httpMethod + ", return 405 with Allow header", res);
}
}
else if (!consumeMatch)
{
throw new UnsupportedMediaTypeException("Cannot consume content type");
}
throw new NotAcceptableException("No match for accept header");
}
if (list.size() == 1) return list.get(0);
list = new ArrayList<ResourceMethod>();
ArrayList<WeightedMediaType> consumes = new ArrayList<WeightedMediaType>();
consumes.addAll(consumesMap.keySet());
Collections.sort(consumes);
boolean first = true;
WeightedMediaType current = null;
// pull out top choices that have equal weighting and that are the same
for (WeightedMediaType type : consumes)
{
if (first)
{
list.add(consumesMap.get(type));
current = type;
first = false;
}
else
{
if (current.compareTo(type) == 0)
{
list.add(consumesMap.get(type));
}
else break;
}
}
if (list.size() == 1) return list.get(0);
// make an identiy map of produced media types
IdentityHashMap<WeightedMediaType, ResourceMethod> producesMap = new IdentityHashMap<WeightedMediaType, ResourceMethod>();
for (ResourceMethod invoker : list)
{
if (invoker.getProduces() == null)
{
WeightedMediaType defaultProduces = WeightedMediaType.valueOf("*/*;q=0.0");
producesMap.put(defaultProduces, invoker);
}
else
{
for (WeightedMediaType produce : invoker.getPreferredProduces())