下面列出了javax.ws.rs.core.MediaType#isCompatible() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void writeTo(Example data,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> headers,
OutputStream out) throws IOException {
if (mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE)) {
if (prettyPrint) {
out.write(Json.pretty().writeValueAsString(data).getBytes("utf-8"));
} else {
out.write(Json.mapper().writeValueAsString(data).getBytes("utf-8"));
}
}
}
/**
* Given a list of acceptable media types, get an RDF syntax.
*
* @param ioService the I/O service
* @param acceptableTypes the types from HTTP headers
* @param mimeType an additional "default" mimeType to match
* @return an RDFSyntax or, in the case of binaries, null
* @throws NotAcceptableException if no acceptable syntax is available
*/
public static RDFSyntax getSyntax(final IOService ioService, final List<MediaType> acceptableTypes,
final String mimeType) {
if (acceptableTypes.isEmpty()) {
return mimeType != null ? null : TURTLE;
}
final MediaType mt = mimeType != null ? MediaType.valueOf(mimeType) : null;
for (final MediaType type : acceptableTypes) {
if (type.isCompatible(mt)) {
return null;
}
final RDFSyntax syntax = ioService.supportedReadSyntaxes().stream()
.filter(s -> MediaType.valueOf(s.mediaType()).isCompatible(type))
.findFirst().orElse(null);
if (syntax != null) {
return syntax;
}
}
LOGGER.debug("Valid syntax not found among {} or {}", acceptableTypes, mimeType);
throw new NotAcceptableException();
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
{
return List.class.isAssignableFrom(type) &&
TypeToken.of(genericType).resolveType(LIST_GENERIC_TOKEN).getRawType().equals(SerializedPage.class) &&
mediaType.isCompatible(PRESTO_PAGES_TYPE);
}
private void addXslProcessingInstruction(Marshaller ms, MediaType mt, XSLTTransform ann)
throws Exception {
if (ann.type() == XSLTTransform.TransformType.CLIENT
|| ann.type() == XSLTTransform.TransformType.BOTH && ann.mediaTypes().length > 0) {
for (String s : ann.mediaTypes()) {
if (mt.isCompatible(JAXRSUtils.toMediaType(s))) {
return;
}
}
String absRef = resolveXMLResourceURI(ann.value());
String xslPi = "<?xml-stylesheet type=\"text/xsl\" href=\"" + absRef + "\"?>";
setXmlPiProperty(ms, xslPi);
}
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType) {
if (Collection.class.isAssignableFrom(type) && mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE)) {
if (genericType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType)genericType;
if (Arrays.asList(parameterizedType.getActualTypeArguments()).contains(IndexerDefinition.class)) {
return true;
}
}
}
return false;
}
@Override
public void writeTo(Example data,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> headers,
OutputStream out) throws IOException {
if (mediaType.isCompatible(MediaType.TEXT_PLAIN_TYPE)) {
out.write(data.asString().getBytes("utf-8"));
}
}
/**
* Retrieve map of parameters from the passed in message
*/
protected void populateMap(MultivaluedMap<String, String> params,
Annotation[] anns,
InputStream is,
MediaType mt,
boolean decode) {
if (mt.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE)) {
MultipartBody body =
AttachmentUtils.getMultipartBody(mc, attachmentDir, attachmentThreshold, attachmentMaxSize);
FormUtils.populateMapFromMultipart(params, body, PhaseInterceptorChain.getCurrentMessage(),
decode);
} else {
String enc = HttpUtils.getEncoding(mt, StandardCharsets.UTF_8.name());
Object servletRequest = mc != null ? mc.getHttpServletRequest() : null;
if (servletRequest == null) {
FormUtils.populateMapFromString(params,
PhaseInterceptorChain.getCurrentMessage(),
FormUtils.readBody(is, enc),
enc,
decode);
} else {
FormUtils.populateMapFromString(params,
PhaseInterceptorChain.getCurrentMessage(),
FormUtils.readBody(is, enc),
enc,
decode,
(javax.servlet.http.HttpServletRequest)servletRequest);
}
}
}
@Override
public boolean isWriteable(
final Class<?> type, final Type genericType, final Annotation[] annotations,
final MediaType mediaType
) {
return mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
}
private static RDFSyntax getRdfSyntax(final String contentType, final List<RDFSyntax> supported) {
if (contentType != null) {
final MediaType type = MediaType.valueOf(contentType);
for (final RDFSyntax s : supported) {
if (type.isCompatible(MediaType.valueOf(s.mediaType()))) {
return s;
}
}
}
return null;
}
@Override
public boolean supports(MediaType mediaType) {
for (MediaType item : SUPPORTED_TYPES) {
if (item.isCompatible(mediaType) && !mediaType.isWildcardType()) {
return true;
}
}
return false;
}
public static boolean isValid(List<MediaType> consumes, MediaType contentType) {
for (MediaType allowed : consumes) {
if (contentType.isCompatible(allowed)) {
return true;
}
}
return false;
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
// TODO Auto-generated method stub
return mediaType.isCompatible(MediaType.APPLICATION_XML_TYPE);
}
@Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType) {
return mediaType.isCompatible(APPLICATION_JSON_TYPE);
}
@Override public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType) {
return mediaType.isCompatible(APPLICATION_JSON_TYPE);
}
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
}
private static Object processRequestBodyParameter(Class<?> parameterClass,
Type parameterType,
Annotation[] parameterAnns,
Message message,
OperationResourceInfo ori)
throws IOException, WebApplicationException {
if (parameterClass == AsyncResponse.class) {
return new AsyncResponseImpl(message);
}
String contentType = (String)message.get(Message.CONTENT_TYPE);
if (contentType == null) {
String defaultCt = (String)message.getContextualProperty(DEFAULT_CONTENT_TYPE);
contentType = defaultCt == null ? MediaType.APPLICATION_OCTET_STREAM : defaultCt;
}
MessageContext mc = new MessageContextImpl(message);
MediaType mt = mc.getHttpHeaders().getMediaType();
InputStream is;
if (mt == null || mt.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
is = copyAndGetEntityStream(message);
} else {
is = message.getContent(InputStream.class);
}
if (is == null) {
Reader reader = message.getContent(Reader.class);
if (reader != null) {
is = new ReaderInputStream(reader);
}
}
return readFromMessageBody(parameterClass,
parameterType,
parameterAnns,
is,
toMediaType(contentType),
ori,
message);
}
private static void checkMediaTypes(MediaType mt1, String mt2) {
if (!mt1.isCompatible(JAXRSUtils.toMediaType(mt2))) {
throw ExceptionUtils.toNotSupportedException(null, null);
}
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return Properties.class.isAssignableFrom(type) && mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
}
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
LOGGER.debug("Called with media type: {} and type: {}", mediaType.toString(), type);
boolean willReturn = (mediaType.isCompatible(MediaType.TEXT_PLAIN_TYPE) || mediaType.toString().equals(ExampleMediaTypes.PLAINTEXT_0_1_0)) && type == ExamplesIterable.class;
LOGGER.debug("Returning: {}", willReturn);
return willReturn;
}