Java Code Examples for javax.ws.rs.core.MediaType#isCompatible()

The following examples show how to use javax.ws.rs.core.MediaType#isCompatible() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonExampleProvider.java    From swagger-inflector with Apache License 2.0 6 votes vote down vote up
@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"));
    }
  }
}
 
Example 2
Source File: HttpUtils.java    From trellis with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 3
Source File: MediaTypeUtils.java    From cougar with Apache License 2.0 5 votes vote down vote up
public static boolean isValid(List<MediaType> consumes, MediaType contentType) {
    for (MediaType allowed : consumes) {
        if (contentType.isCompatible(allowed)) {
            return true;
        }
    }
    return false;
}
 
Example 4
Source File: PagesResponseWriter.java    From presto with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 5
Source File: JacksonProcessor.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supports(MediaType mediaType) {
    for (MediaType item : SUPPORTED_TYPES) {
        if (item.isCompatible(mediaType) && !mediaType.isWildcardType()) {
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: PostHandler.java    From trellis with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: JacksonMessageBodyWriter.java    From heroic with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isWriteable(
    final Class<?> type, final Type genericType, final Annotation[] annotations,
    final MediaType mediaType
) {
    return mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
}
 
Example 8
Source File: FormEncodingProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 9
Source File: PlainExampleProvider.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@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"));
    }
}
 
Example 10
Source File: IndexerDefinitionsMessageBodyWriter.java    From hbase-indexer with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 11
Source File: JAXBElementProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 12
Source File: JsonMessageReaderWriter.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
    return mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
}
 
Example 13
Source File: MoshiMessageBodyReader.java    From jax-rs-moshi with Apache License 2.0 4 votes vote down vote up
@Override public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations,
    MediaType mediaType) {
  return mediaType.isCompatible(APPLICATION_JSON_TYPE);
}
 
Example 14
Source File: JsonMessageBodyWriter.java    From sling-whiteboard with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
    return mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
}
 
Example 15
Source File: JAXRSUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 16
Source File: MoshiMessageBodyWriter.java    From jax-rs-moshi with Apache License 2.0 4 votes vote down vote up
@Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations,
    MediaType mediaType) {
  return mediaType.isCompatible(APPLICATION_JSON_TYPE);
}
 
Example 17
Source File: CustomMessageBodyWriterExtension.java    From JaxRSProviders with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
	// TODO Auto-generated method stub
	return mediaType.isCompatible(MediaType.APPLICATION_XML_TYPE);
}
 
Example 18
Source File: AttachmentUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static void checkMediaTypes(MediaType mt1, String mt2) {
    if (!mt1.isCompatible(JAXRSUtils.toMediaType(mt2))) {
        throw ExceptionUtils.toNotSupportedException(null, null);
    }
}
 
Example 19
Source File: MCRPropertiesToJSONTransformer.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
    return Properties.class.isAssignableFrom(type) && mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
}
 
Example 20
Source File: PlainTextExamplesMessageBodyReader.java    From vw-webservice with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
@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;
}