/** * Indicate whether this {@code MediaType} is compatible with the given media type. * <p>For instance, {@code text/*} is compatible with {@code text/plain}, * {@code text/html}, and vice versa. In effect, this method is similar to * {@link #includes}, except that it <b>is</b> symmetric. * <p>Simply calls {@link #isCompatibleWith(MimeType)} but declared with a * {@code MediaType} parameter for binary backwards compatibility. * @param other the reference media type with which to compare * @return {@code true} if this media type is compatible with the given media type; * {@code false} otherwise */ public boolean isCompatibleWith(@Nullable MediaType other) { return super.isCompatibleWith(other); }
@Override public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) { if (mimeType == null) { return true; } return this.decodableMimeTypes.stream().anyMatch(candidate -> candidate.isCompatibleWith(mimeType)); }
@Override public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) { if (mimeType == null) { return true; } return this.encodableMimeTypes.stream().anyMatch(candidate -> candidate.isCompatibleWith(mimeType)); }
protected boolean supportsMimeType(@Nullable MimeType mimeType) { return (mimeType == null || this.mimeTypes.stream().anyMatch(m -> m.isCompatibleWith(mimeType))); }
protected boolean supportsMimeType(@Nullable MimeType mimeType) { return (mimeType == null || MIME_TYPES.stream().anyMatch(m -> m.isCompatibleWith(mimeType))); }
/** * Indicate whether this {@code MediaType} is compatible with the given media type. * <p>For instance, {@code text/*} is compatible with {@code text/plain}, * {@code text/html}, and vice versa. In effect, this method is similar to * {@link #includes}, except that it <b>is</b> symmetric. * <p>Simply calls {@link #isCompatibleWith(MimeType)} but declared with a * {@code MediaType} parameter for binary backwards compatibility. * @param other the reference media type with which to compare * @return {@code true} if this media type is compatible with the given media type; * {@code false} otherwise */ public boolean isCompatibleWith(@Nullable MediaType other) { return super.isCompatibleWith(other); }
@Override public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) { if (mimeType == null) { return true; } return this.decodableMimeTypes.stream().anyMatch(candidate -> candidate.isCompatibleWith(mimeType)); }
@Override public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) { if (mimeType == null) { return true; } return this.encodableMimeTypes.stream().anyMatch(candidate -> candidate.isCompatibleWith(mimeType)); }
protected boolean supportsMimeType(@Nullable MimeType mimeType) { return (mimeType == null || this.mimeTypes.stream().anyMatch(m -> m.isCompatibleWith(mimeType))); }
protected boolean supportsMimeType(@Nullable MimeType mimeType) { return (mimeType == null || MIME_TYPES.stream().anyMatch(m -> m.isCompatibleWith(mimeType))); }
public WebSocketMessage<?> encode(Message<byte[]> message, Class<? extends WebSocketSession> sessionType) { StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); Assert.notNull(accessor, "No StompHeaderAccessor available"); byte[] payload = message.getPayload(); byte[] bytes = ENCODER.encode(accessor.getMessageHeaders(), payload); boolean useBinary = (payload.length > 0 && !(SockJsSession.class.isAssignableFrom(sessionType)) && MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(accessor.getContentType())); return (useBinary ? new BinaryMessage(bytes) : new TextMessage(bytes)); } }
private CompressionPredicate getMimeTypesPredicate(String[] mimeTypes) { if (ObjectUtils.isEmpty(mimeTypes)) { return ALWAYS_COMPRESS; } return (request, response) -> { String contentType = response.responseHeaders() .get(HttpHeaderNames.CONTENT_TYPE); if (StringUtils.isEmpty(contentType)) { return false; } MimeType contentMimeType = MimeTypeUtils.parseMimeType(contentType); return Arrays.stream(mimeTypes).map(MimeTypeUtils::parseMimeType) .anyMatch((candidate) -> candidate.isCompatibleWith(contentMimeType)); }; }
@Test public void isCompatible() { MimeType textPlain = MimeTypeUtils.TEXT_PLAIN; assertTrue("Equal types is not compatible", textPlain.isCompatibleWith(textPlain)); MimeType allText = new MimeType("text"); assertTrue("All subtypes is not compatible", allText.isCompatibleWith(textPlain)); assertTrue("All subtypes is not compatible", textPlain.isCompatibleWith(allText)); assertTrue("All types is not compatible", MimeTypeUtils.ALL.isCompatibleWith(textPlain)); assertTrue("All types is not compatible", textPlain.isCompatibleWith(MimeTypeUtils.ALL)); assertTrue("All types is not compatible", MimeTypeUtils.ALL.isCompatibleWith(textPlain)); assertTrue("All types is compatible", textPlain.isCompatibleWith(MimeTypeUtils.ALL)); MimeType applicationSoapXml = new MimeType("application", "soap+xml"); MimeType applicationWildcardXml = new MimeType("application", "*+xml"); MimeType suffixXml = new MimeType("application", "x.y+z+xml"); // SPR-15795 assertTrue(applicationSoapXml.isCompatibleWith(applicationSoapXml)); assertTrue(applicationWildcardXml.isCompatibleWith(applicationWildcardXml)); assertTrue(applicationWildcardXml.isCompatibleWith(suffixXml)); assertTrue(applicationWildcardXml.isCompatibleWith(applicationSoapXml)); assertTrue(applicationSoapXml.isCompatibleWith(applicationWildcardXml)); assertTrue(suffixXml.isCompatibleWith(applicationWildcardXml)); assertFalse(applicationWildcardXml.isCompatibleWith(MimeTypeUtils.APPLICATION_JSON)); }
public SELF hasContentType(String mimeType) { String contentType = actual.getHeader("content-type"); try { if (!(isNotBlank(contentType) && MimeType.valueOf(contentType).isCompatibleWith(MimeType.valueOf(mimeType)))) { failWithMessage("Expected content type <%s> but was <%s>", mimeType, contentType); } } catch (InvalidMimeTypeException e) { failWithMessage("Actual content type <%s> could not be parsed", contentType); } return myself; }
@Override public boolean resolve(HttpServerExchange value) { String contentType = value.getResponseHeaders() .getFirst(HttpHeaders.CONTENT_TYPE); if (contentType != null) { for (MimeType mimeType : this.mimeTypes) { if (mimeType .isCompatibleWith(MimeTypeUtils.parseMimeType(contentType))) { return true; } } } return false; }
byte[] bytes = this.stompEncoder.encode(stompAccessor.getMessageHeaders(), payload); boolean useBinary = (payload.length > 0 && !(session instanceof SockJsSession) && MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(stompAccessor.getContentType())); if (useBinary) { session.sendMessage(new BinaryMessage(bytes));
@Override public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) { if (mimeType == null) { return true; } return this.decodableMimeTypes.stream().anyMatch(candidate -> candidate.isCompatibleWith(mimeType)); }
@Override public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) { if (mimeType == null) { return true; } return this.decodableMimeTypes.stream().anyMatch(candidate -> candidate.isCompatibleWith(mimeType)); }
public WebSocketMessage<?> encode(Message<byte[]> message, Class<? extends WebSocketSession> sessionType) { StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); Assert.notNull(accessor, "No StompHeaderAccessor available"); byte[] payload = message.getPayload(); byte[] bytes = ENCODER.encode(accessor.getMessageHeaders(), payload); boolean useBinary = (payload.length > 0 && !(SockJsSession.class.isAssignableFrom(sessionType)) && MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(accessor.getContentType())); return (useBinary ? new BinaryMessage(bytes) : new TextMessage(bytes)); } }
public WebSocketMessage<?> encode(Message<byte[]> message, Class<? extends WebSocketSession> sessionType) { StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); Assert.notNull(accessor, "No StompHeaderAccessor available"); byte[] payload = message.getPayload(); byte[] bytes = ENCODER.encode(accessor.getMessageHeaders(), payload); boolean useBinary = (payload.length > 0 && !(SockJsSession.class.isAssignableFrom(sessionType)) && MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(accessor.getContentType())); return (useBinary ? new BinaryMessage(bytes) : new TextMessage(bytes)); } }