@Nullable public MimeType getContentType() { Object value = getHeader(MessageHeaders.CONTENT_TYPE); if (value == null) { return null; } return (value instanceof MimeType ? (MimeType) value : MimeType.valueOf(value.toString())); }
/** * Set the content-type header. * Applies to the SEND, MESSAGE, and ERROR frames. */ public void setContentType(@Nullable MimeType mimeType) { if (mimeType != null) { Assert.isTrue(!mimeType.isWildcardType(), "'Content-Type' cannot contain wildcard type '*'"); Assert.isTrue(!mimeType.isWildcardSubtype(), "'Content-Type' cannot contain wildcard subtype '*'"); set(CONTENT_TYPE, mimeType.toString()); } else { set(CONTENT_TYPE, null); } }
/** * Copy-constructor that copies the type, subtype, parameters of the given {@code MimeType}, * and allows to set the specified character set. * @param other the other MimeType * @param charset the character set * @throws IllegalArgumentException if any of the parameters contains illegal characters * @since 4.3 */ public MimeType(MimeType other, Charset charset) { this(other.getType(), other.getSubtype(), addCharsetParameter(charset, other.getParameters())); }
/** * Copy-constructor that copies the type and subtype of the given {@code MimeType}, * and allows for different parameter. * @param other the other MimeType * @param parameters the parameters (may be {@code null}) * @throws IllegalArgumentException if any of the parameters contains illegal characters */ public MimeType(MimeType other, @Nullable Map<String, String> parameters) { this(other.getType(), other.getSubtype(), parameters); }
/** * Re-create the given mime type as a media type. * @since 5.0 */ public static MediaType asMediaType(MimeType mimeType) { if (mimeType instanceof MediaType) { return (MediaType) mimeType; } return new MediaType(mimeType.getType(), mimeType.getSubtype(), mimeType.getParameters()); }
@Test public void parseCharset() { String s = "text/html; charset=iso-8859-1"; MimeType mimeType = MimeType.valueOf(s); assertEquals("Invalid type", "text", mimeType.getType()); assertEquals("Invalid subtype", "html", mimeType.getSubtype()); assertEquals("Invalid charset", StandardCharsets.ISO_8859_1, mimeType.getCharset()); }
protected boolean isJsonContentType(Request request) { String mime = request.headers("Content-Type"); if (isBlank(mime)) { return false; } try { MimeType mimeType = MimeType.valueOf(mime); return "application".equals(mimeType.getType()) && "json".equals(mimeType.getSubtype()); } catch (InvalidMimeTypeException e) { return false; } }
@Test public void testToMessageWithContentType() throws Exception { TcpMessageMapper mapper = new TcpMessageMapper(); mapper.setAddContentTypeHeader(true); TcpConnection connection = mock(TcpConnection.class); Socket socket = mock(Socket.class); InetAddress local = mock(InetAddress.class); SocketInfo info = new SocketInfo(socket); when(socket.getLocalAddress()).thenReturn(local); when(connection.getPayload()).thenReturn(TEST_PAYLOAD.getBytes()); when(connection.getHostName()).thenReturn("MyHost"); when(connection.getHostAddress()).thenReturn("1.1.1.1"); when(connection.getPort()).thenReturn(1234); when(connection.getSocketInfo()).thenReturn(info); Message<?> message = mapper.toMessage(connection); assertEquals(TEST_PAYLOAD, new String((byte[]) message.getPayload())); assertEquals("MyHost", message.getHeaders().get(IpHeaders.HOSTNAME)); assertEquals("1.1.1.1", message.getHeaders().get(IpHeaders.IP_ADDRESS)); assertEquals(1234, message.getHeaders().get(IpHeaders.REMOTE_PORT)); assertSame(local, message.getHeaders().get(IpHeaders.LOCAL_ADDRESS)); assertEquals("application/octet-stream;charset=UTF-8", message.getHeaders().get(MessageHeaders.CONTENT_TYPE)); MimeType parseOk = MimeType.valueOf((String) message.getHeaders().get(MessageHeaders.CONTENT_TYPE)); assertEquals(message.getHeaders().get(MessageHeaders.CONTENT_TYPE), parseOk.toString()); }
@Override public void setContentType(MimeType contentType) { super.setContentType(contentType); setNativeHeader(STOMP_CONTENT_TYPE_HEADER, contentType.toString()); }
@Override public int compare(T mimeType1, T mimeType2) { if (mimeType1.isWildcardType() && !mimeType2.isWildcardType()) { // */* < audio/* return 1; } else if (mimeType2.isWildcardType() && !mimeType1.isWildcardType()) { // audio/* > */* return -1; } else if (!mimeType1.getType().equals(mimeType2.getType())) { // audio/basic == text/html return 0; } else { // mediaType1.getType().equals(mediaType2.getType()) if (mimeType1.isWildcardSubtype() && !mimeType2.isWildcardSubtype()) { // audio/* < audio/basic return 1; } else if (mimeType2.isWildcardSubtype() && !mimeType1.isWildcardSubtype()) { // audio/basic > audio/* return -1; } else if (!mimeType1.getSubtype().equals(mimeType2.getSubtype())) { // audio/basic == audio/wave return 0; } else { // mediaType2.getSubtype().equals(mediaType2.getSubtype()) return compareParameters(mimeType1, mimeType2); } } }
int comp = getType().compareToIgnoreCase(other.getType()); if (comp != 0) { return comp; comp = getSubtype().compareToIgnoreCase(other.getSubtype()); if (comp != 0) { return comp; comp = getParameters().size() - other.getParameters().size(); if (comp != 0) { return comp; thisAttributes.addAll(getParameters().keySet()); TreeSet<String> otherAttributes = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); otherAttributes.addAll(other.getParameters().keySet()); Iterator<String> thisAttributesIterator = thisAttributes.iterator(); Iterator<String> otherAttributesIterator = otherAttributes.iterator(); Charset thisCharset = getCharset(); Charset otherCharset = other.getCharset(); if (thisCharset != otherCharset) { if (thisCharset == null) { String thisValue = getParameters().get(thisAttribute); String otherValue = other.getParameters().get(otherAttribute); if (otherValue == null) { otherValue = "";
@Override public boolean canConvert(String fromMimeType, String toMimeType) { for (RenditionProvider provider : providers) { if (MimeType.valueOf(fromMimeType) .includes(MimeType.valueOf(provider.consumes()))) { for (String produce : provider.produces()) { if (MimeType.valueOf(toMimeType) .includes(MimeType.valueOf(produce))) { return true; } } } } return false; }
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; }
/** * 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); }
private String appendPayload(Object payload) { if (payload.getClass() != byte[].class) { throw new IllegalStateException( "Expected byte array payload but got: " + ClassUtils.getQualifiedName(payload.getClass())); } byte[] bytes = (byte[]) payload; MimeType mimeType = getContentType(); String contentType = (mimeType != null ? " " + mimeType.toString() : ""); if (bytes.length == 0 || mimeType == null || !isReadableContentType()) { return contentType; } Charset charset = mimeType.getCharset(); charset = (charset != null ? charset : StandardCharsets.UTF_8); return (bytes.length < 80) ? contentType + " payload=" + new String(bytes, charset) : contentType + " payload=" + new String(Arrays.copyOf(bytes, 80), charset) + "...(truncated)"; }
/** * Get the java Object type for the MimeType X_JAVA_OBJECT * * @return the class for the content type */ public static Class<?> getJavaTypeForJavaObjectContentType(MimeType contentType) { Assert.isTrue(X_JAVA_OBJECT.includes(contentType), "Content type must be " + X_JAVA_OBJECT.toString() + ", or " + "included in it"); if (contentType.getParameter("type") != null) { try { return ClassUtils.forName(contentType.getParameter("type"), null); } catch (Exception e) { throw new ConversionException(e.getMessage(), e); } } return Object.class; }
/** * Indicate whether this {@code MediaType} includes the given media type. * <p>For instance, {@code text/*} includes {@code text/plain} and {@code text/html}, * and {@code application/*+xml} includes {@code application/soap+xml}, etc. * This method is <b>not</b> symmetric. * <p>Simply calls {@link #includes(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 includes the given media type; * {@code false} otherwise */ public boolean includes(@Nullable MediaType other) { return super.includes(other); }
@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)); }