use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class MethodSelectingRouter method addAllConsumesProducesCombinations.
private void addAllConsumesProducesCombinations(final List<ConsumesProducesAcceptor> acceptors, final MethodRouting methodRouting) {
final ResourceMethod resourceMethod = methodRouting.method;
final Set<MediaType> effectiveInputTypes = new LinkedHashSet<>();
boolean consumesFromWorkers = fillMediaTypes(effectiveInputTypes, resourceMethod, resourceMethod.getConsumedTypes(), true);
final Set<MediaType> effectiveOutputTypes = new LinkedHashSet<>();
boolean producesFromWorkers = fillMediaTypes(effectiveOutputTypes, resourceMethod, resourceMethod.getProducedTypes(), false);
final Set<ConsumesProducesAcceptor> acceptorSet = new HashSet<>();
for (MediaType consumes : effectiveInputTypes) {
for (MediaType produces : effectiveOutputTypes) {
acceptorSet.add(new ConsumesProducesAcceptor(new CombinedMediaType.EffectiveMediaType(consumes, consumesFromWorkers), new CombinedMediaType.EffectiveMediaType(produces, producesFromWorkers), methodRouting));
}
}
acceptors.addAll(acceptorSet);
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class MethodSelectingRouter method selectMethod.
/**
* Select method to be invoked. Method is chosen among the given set of acceptors (if they are compatible with acceptable
* media types).
*
* @param acceptableMediaTypes media types acceptable by the client.
* @param satisfyingAcceptors pre-computed acceptors.
* @param effectiveContentType media type of incoming entity.
* @param singleInvokableMethod flag determining whether only one method to be invoked has been found among satisfying
* acceptors.
* @return method to be invoked.
*/
private MethodSelector selectMethod(final List<AcceptableMediaType> acceptableMediaTypes, final List<ConsumesProducesAcceptor> satisfyingAcceptors, final MediaType effectiveContentType, final boolean singleInvokableMethod) {
// Selected method we have a reader and writer for.
final MethodSelector method = new MethodSelector(null);
// If we cannot find a writer at this point use the best alternative.
final MethodSelector alternative = new MethodSelector(null);
for (final MediaType acceptableMediaType : acceptableMediaTypes) {
for (final ConsumesProducesAcceptor satisfiable : satisfyingAcceptors) {
final CombinedMediaType produces = CombinedMediaType.create(acceptableMediaType, satisfiable.produces);
if (produces != CombinedMediaType.NO_MATCH) {
final CombinedMediaType consumes = CombinedMediaType.create(effectiveContentType, satisfiable.consumes);
final RequestSpecificConsumesProducesAcceptor candidate = new RequestSpecificConsumesProducesAcceptor(consumes, produces, satisfiable.produces.isDerived(), satisfiable.methodRouting);
if (singleInvokableMethod) {
// Only one possible method and it's compatible.
return new MethodSelector(candidate);
} else if (candidate.compareTo(method.selected) < 0) {
// Candidate is better than the previous one.
if (method.selected == null || candidate.methodRouting.method != method.selected.methodRouting.method) {
// No candidate so far or better candidate.
if (isReadable(candidate) && isWriteable(candidate)) {
method.consider(candidate);
} else {
alternative.consider(candidate);
}
} else {
// Same resource method - better candidate, no need to compare anything else.
method.consider(candidate);
}
}
}
}
}
return method.selected != null ? method : alternative;
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class CombinedMediaType method create.
/**
* Create combined client/server media type.
*
* if the two types are not compatible, {@link #NO_MATCH} is returned.
*
* @param clientType client-side media type.
* @param serverType server-side media type.
* @return combined client/server media type.
*/
static CombinedMediaType create(MediaType clientType, EffectiveMediaType serverType) {
if (!clientType.isCompatible(serverType.getMediaType())) {
return NO_MATCH;
}
final MediaType strippedClientType = MediaTypes.stripQualityParams(clientType);
final MediaType strippedServerType = MediaTypes.stripQualityParams(serverType.getMediaType());
return new CombinedMediaType(MediaTypes.mostSpecific(strippedClientType, strippedServerType), MediaTypes.getQuality(clientType), QualitySourceMediaType.getQualitySource(serverType.getMediaType()), matchedWildcards(clientType, serverType));
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class MethodSelectingRouter method getMethodRouter.
private List<Router> getMethodRouter(final RequestProcessingContext context) {
final ContainerRequest request = context.request();
final List<ConsumesProducesAcceptor> acceptors = consumesProducesAcceptors.get(request.getMethod());
if (acceptors == null) {
throw new NotAllowedException(Response.status(Status.METHOD_NOT_ALLOWED).allow(consumesProducesAcceptors.keySet()).build());
}
final List<ConsumesProducesAcceptor> satisfyingAcceptors = new LinkedList<>();
final Set<ResourceMethod> differentInvokableMethods = Collections.newSetFromMap(new IdentityHashMap<>());
for (ConsumesProducesAcceptor cpi : acceptors) {
if (cpi.isConsumable(request)) {
satisfyingAcceptors.add(cpi);
differentInvokableMethods.add(cpi.methodRouting.method);
}
}
if (satisfyingAcceptors.isEmpty()) {
throw new NotSupportedException();
}
final List<AcceptableMediaType> acceptableMediaTypes = request.getQualifiedAcceptableMediaTypes();
final MediaType requestContentType = request.getMediaType();
final MediaType effectiveContentType = requestContentType == null ? MediaType.WILDCARD_TYPE : requestContentType;
final MethodSelector methodSelector = selectMethod(acceptableMediaTypes, satisfyingAcceptors, effectiveContentType, differentInvokableMethods.size() == 1);
if (methodSelector.selected != null) {
final RequestSpecificConsumesProducesAcceptor selected = methodSelector.selected;
if (methodSelector.sameFitnessAcceptors != null) {
reportMethodSelectionAmbiguity(acceptableMediaTypes, methodSelector.selected, methodSelector.sameFitnessAcceptors);
}
context.push(new Function<ContainerResponse, ContainerResponse>() {
@Override
public ContainerResponse apply(final ContainerResponse responseContext) {
// - either there is an entity, or we are responding to a HEAD request
if (responseContext.getMediaType() == null && ((responseContext.hasEntity() || HttpMethod.HEAD.equals(request.getMethod())))) {
MediaType effectiveResponseType = determineResponseMediaType(responseContext.getEntityClass(), responseContext.getEntityType(), methodSelector.selected, acceptableMediaTypes);
if (MediaTypes.isWildcard(effectiveResponseType)) {
if (effectiveResponseType.isWildcardType() || "application".equalsIgnoreCase(effectiveResponseType.getType())) {
effectiveResponseType = MediaType.APPLICATION_OCTET_STREAM_TYPE;
} else {
throw new NotAcceptableException();
}
}
responseContext.setMediaType(effectiveResponseType);
}
return responseContext;
}
});
return selected.methodRouting.routers;
}
throw new NotAcceptableException();
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class RequestUtil method getEntityParameters.
/**
* Returns the form parameters from a request entity as a multi-valued map.
* If the request does not have a POST method, or the media type is not
* x-www-form-urlencoded, then null is returned.
*
* @param request the client request containing the entity to extract parameters from.
* @return a {@link javax.ws.rs.core.MultivaluedMap} containing the entity form parameters.
*/
@SuppressWarnings("unchecked")
public static MultivaluedMap<String, String> getEntityParameters(ClientRequestContext request, MessageBodyWorkers messageBodyWorkers) {
Object entity = request.getEntity();
String method = request.getMethod();
MediaType mediaType = request.getMediaType();
// no entity, not a post or not x-www-form-urlencoded: return empty map
if (entity == null || method == null || !HttpMethod.POST.equalsIgnoreCase(method) || mediaType == null || !mediaType.equals(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
return new MultivaluedHashMap<String, String>();
}
// it's ready to go if already expressed as a multi-valued map
if (entity instanceof MultivaluedMap) {
return (MultivaluedMap<String, String>) entity;
}
Type entityType = entity.getClass();
// if the entity is generic, get specific type and class
if (entity instanceof GenericEntity) {
final GenericEntity generic = (GenericEntity) entity;
// overwrite
entityType = generic.getType();
entity = generic.getEntity();
}
final Class entityClass = entity.getClass();
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessageBodyWriter writer = messageBodyWorkers.getMessageBodyWriter(entityClass, entityType, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
try {
writer.writeTo(entity, entityClass, entityType, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, out);
} catch (WebApplicationException wae) {
throw new IllegalStateException(wae);
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
MessageBodyReader reader = messageBodyWorkers.getMessageBodyReader(MultivaluedMap.class, MultivaluedMap.class, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
try {
return (MultivaluedMap<String, String>) reader.readFrom(MultivaluedMap.class, MultivaluedMap.class, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, in);
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
Aggregations