use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class MessageBodyFactory method addWriters.
private static void addWriters(final List<WriterModel> models, final Set<MessageBodyWriter> writers, final boolean custom) {
for (final MessageBodyWriter provider : writers) {
final List<MediaType> values = MediaTypes.createFrom(provider.getClass().getAnnotation(Produces.class));
models.add(new WriterModel(provider, values, custom));
}
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class TestRuntimeDelegate method testMediaType.
public void testMediaType() {
MediaType m = new MediaType("text", "plain");
Assert.assertNotNull(m);
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class UriConnegFilter method filter.
@Override
public void filter(final ContainerRequestContext rc) throws IOException {
final UriInfo uriInfo = rc.getUriInfo();
// Quick check for a '.' character
String path = uriInfo.getRequestUri().getRawPath();
if (path.indexOf('.') == -1) {
return;
}
final List<PathSegment> l = uriInfo.getPathSegments(false);
if (l.isEmpty()) {
return;
}
// Get the last non-empty path segment
PathSegment segment = null;
for (int i = l.size() - 1; i >= 0; i--) {
segment = l.get(i);
if (segment.getPath().length() > 0) {
break;
}
}
if (segment == null) {
return;
}
final int length = path.length();
// Get the suffixes
final String[] suffixes = segment.getPath().split("\\.");
for (int i = suffixes.length - 1; i >= 1; i--) {
final String suffix = suffixes[i];
if (suffix.length() == 0) {
continue;
}
final MediaType accept = mediaTypeMappings.get(suffix);
if (accept != null) {
rc.getHeaders().putSingle(HttpHeaders.ACCEPT, accept.toString());
final int index = path.lastIndexOf('.' + suffix);
path = new StringBuilder(path).delete(index, index + suffix.length() + 1).toString();
suffixes[i] = "";
break;
}
}
for (int i = suffixes.length - 1; i >= 1; i--) {
final String suffix = suffixes[i];
if (suffix.length() == 0) {
continue;
}
final String acceptLanguage = languageMappings.get(suffix);
if (acceptLanguage != null) {
rc.getHeaders().putSingle(HttpHeaders.ACCEPT_LANGUAGE, acceptLanguage);
final int index = path.lastIndexOf('.' + suffix);
path = new StringBuilder(path).delete(index, index + suffix.length() + 1).toString();
suffixes[i] = "";
break;
}
}
if (length != path.length()) {
rc.setRequestUri(uriInfo.getRequestUriBuilder().replacePath(path).build());
}
}
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;
}
Aggregations