use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class OutboundMessageContext method getAcceptableMediaTypes.
/**
* Get a list of media types that are acceptable for the message.
*
* @return a read-only list of requested message media types sorted according
* to their q-value, with highest preference first.
*/
@SuppressWarnings("unchecked")
public List<MediaType> getAcceptableMediaTypes() {
final List<Object> values = headers.get(HttpHeaders.ACCEPT);
if (values == null || values.isEmpty()) {
return WILDCARD_ACCEPTABLE_TYPE_SINGLETON_LIST;
}
final List<MediaType> result = new ArrayList<>(values.size());
final RuntimeDelegate rd = RuntimeDelegate.getInstance();
boolean conversionApplied = false;
for (final Object value : values) {
try {
if (value instanceof MediaType) {
final AcceptableMediaType _value = AcceptableMediaType.valueOf((MediaType) value);
// true if value was not an instance of AcceptableMediaType already
conversionApplied = _value != value;
result.add(_value);
} else {
conversionApplied = true;
result.addAll(HttpHeaderReader.readAcceptMediaType(HeaderUtils.asString(value, rd)));
}
} catch (java.text.ParseException e) {
throw exception(HttpHeaders.ACCEPT, value, e);
}
}
if (conversionApplied) {
// cache converted
headers.put(HttpHeaders.ACCEPT, result.stream().map((Function<MediaType, Object>) mediaType -> mediaType).collect(Collectors.toList()));
}
return Collections.unmodifiableList(result);
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class MessageBodyFactory method getMessageBodyWriterMediaTypes.
@Override
@SuppressWarnings("unchecked")
public List<MediaType> getMessageBodyWriterMediaTypes(final Class<?> c, final Type t, final Annotation[] as) {
final Set<MediaType> writeableMediaTypes = new LinkedHashSet<>();
for (final WriterModel model : writers) {
boolean writeableWorker = false;
for (final MediaType mt : model.declaredTypes()) {
if (model.isWriteable(c, t, as, mt)) {
writeableMediaTypes.add(mt);
writeableWorker = true;
}
if (!writeableMediaTypes.contains(MediaType.WILDCARD_TYPE) && writeableWorker && model.declaredTypes().contains(MediaType.WILDCARD_TYPE)) {
writeableMediaTypes.add(MediaType.WILDCARD_TYPE);
}
}
}
final List<MediaType> mtl = new ArrayList<>(writeableMediaTypes);
Collections.sort(mtl, MediaTypes.PARTIAL_ORDER_COMPARATOR);
return mtl;
}
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());
}
}
Aggregations