Java Code Examples for org.springframework.util.InvalidMimeTypeException

The following examples show how to use org.springframework.util.InvalidMimeTypeException. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: StompDecoder.java    License: MIT License 5 votes vote down vote up
private void readHeaders(ByteBuffer byteBuffer, StompHeaderAccessor headerAccessor) {
	while (true) {
		ByteArrayOutputStream headerStream = new ByteArrayOutputStream(256);
		boolean headerComplete = false;
		while (byteBuffer.hasRemaining()) {
			if (tryConsumeEndOfLine(byteBuffer)) {
				headerComplete = true;
				break;
			}
			headerStream.write(byteBuffer.get());
		}
		if (headerStream.size() > 0 && headerComplete) {
			String header = new String(headerStream.toByteArray(), StandardCharsets.UTF_8);
			int colonIndex = header.indexOf(':');
			if (colonIndex <= 0) {
				if (byteBuffer.remaining() > 0) {
					throw new StompConversionException("Illegal header: '" + header +
							"'. A header must be of the form <name>:[<value>].");
				}
			}
			else {
				String headerName = unescape(header.substring(0, colonIndex));
				String headerValue = unescape(header.substring(colonIndex + 1));
				try {
					headerAccessor.addNativeHeader(headerName, headerValue);
				}
				catch (InvalidMimeTypeException ex) {
					if (byteBuffer.remaining() > 0) {
						throw ex;
					}
				}
			}
		}
		else {
			break;
		}
	}
}
 
Example 2
@Test(expected = InvalidMimeTypeException.class)
public void resolveInvalidStringContentType() {
	Map<String, Object> map = new HashMap<>();
	map.put(MessageHeaders.CONTENT_TYPE, "invalidContentType");
	MessageHeaders headers = new MessageHeaders(map);
	this.resolver.resolve(headers);
}
 
Example 3
Source Project: java-technology-stack   Source File: StompDecoder.java    License: MIT License 5 votes vote down vote up
private void readHeaders(ByteBuffer byteBuffer, StompHeaderAccessor headerAccessor) {
	while (true) {
		ByteArrayOutputStream headerStream = new ByteArrayOutputStream(256);
		boolean headerComplete = false;
		while (byteBuffer.hasRemaining()) {
			if (tryConsumeEndOfLine(byteBuffer)) {
				headerComplete = true;
				break;
			}
			headerStream.write(byteBuffer.get());
		}
		if (headerStream.size() > 0 && headerComplete) {
			String header = new String(headerStream.toByteArray(), StandardCharsets.UTF_8);
			int colonIndex = header.indexOf(':');
			if (colonIndex <= 0) {
				if (byteBuffer.remaining() > 0) {
					throw new StompConversionException("Illegal header: '" + header +
							"'. A header must be of the form <name>:[<value>].");
				}
			}
			else {
				String headerName = unescape(header.substring(0, colonIndex));
				String headerValue = unescape(header.substring(colonIndex + 1));
				try {
					headerAccessor.addNativeHeader(headerName, headerValue);
				}
				catch (InvalidMimeTypeException ex) {
					if (byteBuffer.remaining() > 0) {
						throw ex;
					}
				}
			}
		}
		else {
			break;
		}
	}
}
 
Example 4
@Test(expected = InvalidMimeTypeException.class)
public void resolveInvalidStringContentType() {
	Map<String, Object> map = new HashMap<>();
	map.put(MessageHeaders.CONTENT_TYPE, "invalidContentType");
	MessageHeaders headers = new MessageHeaders(map);
	this.resolver.resolve(headers);
}
 
Example 5
Source Project: spring4-understanding   Source File: StompDecoder.java    License: Apache License 2.0 5 votes vote down vote up
private void readHeaders(ByteBuffer buffer, StompHeaderAccessor headerAccessor) {
	while (true) {
		ByteArrayOutputStream headerStream = new ByteArrayOutputStream(256);
		boolean headerComplete = false;
		while (buffer.hasRemaining()) {
			if (tryConsumeEndOfLine(buffer)) {
				headerComplete = true;
				break;
			}
			headerStream.write(buffer.get());
		}
		if (headerStream.size() > 0 && headerComplete) {
			String header = new String(headerStream.toByteArray(), UTF8_CHARSET);
			int colonIndex = header.indexOf(':');
			if (colonIndex <= 0) {
				if (buffer.remaining() > 0) {
					throw new StompConversionException("Illegal header: '" + header +
							"'. A header must be of the form <name>:[<value>].");
				}
			}
			else {
				String headerName = unescape(header.substring(0, colonIndex));
				String headerValue = unescape(header.substring(colonIndex + 1));
				try {
					headerAccessor.addNativeHeader(headerName, headerValue);
				}
				catch (InvalidMimeTypeException ex) {
					if (buffer.remaining() > 0) {
						throw ex;
					}
				}
			}
		}
		else {
			break;
		}
	}
}
 
Example 6
@Test(expected = InvalidMimeTypeException.class)
public void resolveInvalidStringContentType() {
	Map<String, Object> map = new HashMap<String, Object>();
	map.put(MessageHeaders.CONTENT_TYPE, "invalidContentType");
	MessageHeaders headers = new MessageHeaders(map);
	this.resolver.resolve(headers);
}
 
Example 7
/**
 * Indicates whether the content type represented by the given string is a
 * valid, known content type.
 *
 * @param contentType the content type string.
 * @return true if the content is valid, false if not.
 */
public static boolean isValidContentType( String contentType )
{
    try
    {
        MimeTypeUtils.parseMimeType( contentType );
    }
    catch ( InvalidMimeTypeException ignored )
    {
        return false;
    }

    return true;
}
 
Example 8
Source Project: gocd   Source File: ApiController.java    License: Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 9
Source Project: gocd   Source File: MockHttpServletResponseAssert.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source Project: spring-analysis-note   Source File: StompDecoderTests.java    License: MIT License 4 votes vote down vote up
@Test(expected = InvalidMimeTypeException.class)
public void decodeFrameWithInvalidContentType() {
	assertIncompleteDecode("SEND\ncontent-type:text/plain;charset=U\n\nThe body\0");
}
 
Example 11
Source Project: spring-analysis-note   Source File: InvalidMediaTypeException.java    License: MIT License 4 votes vote down vote up
/**
 * Constructor that allows wrapping {@link InvalidMimeTypeException}.
 */
InvalidMediaTypeException(InvalidMimeTypeException ex) {
	super(ex.getMessage(), ex);
	this.mediaType = ex.getMimeType();
}
 
Example 12
Source Project: java-technology-stack   Source File: StompDecoderTests.java    License: MIT License 4 votes vote down vote up
@Test(expected = InvalidMimeTypeException.class)
public void decodeFrameWithInvalidContentType() {
	assertIncompleteDecode("SEND\ncontent-type:text/plain;charset=U\n\nThe body\0");
}
 
Example 13
Source Project: java-technology-stack   Source File: InvalidMediaTypeException.java    License: MIT License 4 votes vote down vote up
/**
 * Constructor that allows wrapping {@link InvalidMimeTypeException}.
 */
InvalidMediaTypeException(InvalidMimeTypeException ex) {
	super(ex.getMessage(), ex);
	this.mediaType = ex.getMimeType();
}
 
Example 14
/**
 * Constructor that allows wrapping {@link InvalidMimeTypeException}.
 */
InvalidMediaTypeException(InvalidMimeTypeException ex) {
	super(ex.getMessage(), ex);
	this.mediaType = ex.getMimeType();
}
 
Example 15
Source Project: spring4-understanding   Source File: StompCodecTests.java    License: Apache License 2.0 4 votes vote down vote up
@Test(expected = InvalidMimeTypeException.class)
public void decodeFrameWithInvalidContentType() {
	assertIncompleteDecode("SEND\ncontent-type:text/plain;charset=U\n\nThe body\0");
}
 
Example 16
/**
 * Constructor that allows wrapping {@link InvalidMimeTypeException}.
 */
InvalidMediaTypeException(InvalidMimeTypeException ex) {
	super(ex.getMessage(), ex);
	this.mediaType = ex.getMimeType();
}
 
Example 17
Source Project: spring-analysis-note   Source File: ContentTypeResolver.java    License: MIT License 2 votes vote down vote up
/**
 * Determine the {@link MimeType} of a message from the given MessageHeaders.
 * @param headers the headers to use for the resolution
 * @return the resolved {@code MimeType}, or {@code null} if none found
 * @throws InvalidMimeTypeException if the content type is a String that cannot be parsed
 * @throws IllegalArgumentException if there is a content type but its type is unknown
 */
@Nullable
MimeType resolve(@Nullable MessageHeaders headers) throws InvalidMimeTypeException;
 
Example 18
Source Project: java-technology-stack   Source File: ContentTypeResolver.java    License: MIT License 2 votes vote down vote up
/**
 * Determine the {@link MimeType} of a message from the given MessageHeaders.
 * @param headers the headers to use for the resolution
 * @return the resolved {@code MimeType}, or {@code null} if none found
 * @throws InvalidMimeTypeException if the content type is a String that cannot be parsed
 * @throws IllegalArgumentException if there is a content type but its type is unknown
 */
@Nullable
MimeType resolve(@Nullable MessageHeaders headers) throws InvalidMimeTypeException;