下面列出了怎么用org.springframework.util.InvalidMimeTypeException的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
}
}
@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);
}
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;
}
}
}
@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);
}
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;
}
}
}
@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);
}
/**
* 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;
}
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;
}
}
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;
}
@Test(expected = InvalidMimeTypeException.class)
public void decodeFrameWithInvalidContentType() {
assertIncompleteDecode("SEND\ncontent-type:text/plain;charset=U\n\nThe body\0");
}
/**
* Constructor that allows wrapping {@link InvalidMimeTypeException}.
*/
InvalidMediaTypeException(InvalidMimeTypeException ex) {
super(ex.getMessage(), ex);
this.mediaType = ex.getMimeType();
}
@Test(expected = InvalidMimeTypeException.class)
public void decodeFrameWithInvalidContentType() {
assertIncompleteDecode("SEND\ncontent-type:text/plain;charset=U\n\nThe body\0");
}
/**
* Constructor that allows wrapping {@link InvalidMimeTypeException}.
*/
InvalidMediaTypeException(InvalidMimeTypeException ex) {
super(ex.getMessage(), ex);
this.mediaType = ex.getMimeType();
}
/**
* Constructor that allows wrapping {@link InvalidMimeTypeException}.
*/
InvalidMediaTypeException(InvalidMimeTypeException ex) {
super(ex.getMessage(), ex);
this.mediaType = ex.getMimeType();
}
@Test(expected = InvalidMimeTypeException.class)
public void decodeFrameWithInvalidContentType() {
assertIncompleteDecode("SEND\ncontent-type:text/plain;charset=U\n\nThe body\0");
}
/**
* Constructor that allows wrapping {@link InvalidMimeTypeException}.
*/
InvalidMediaTypeException(InvalidMimeTypeException ex) {
super(ex.getMessage(), ex);
this.mediaType = ex.getMimeType();
}
/**
* 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;
/**
* 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;