Example usage for org.springframework.util MimeType getType

List of usage examples for org.springframework.util MimeType getType

Introduction

In this page you can find the example usage for org.springframework.util MimeType getType.

Prototype

public String getType() 

Source Link

Document

Return the primary type.

Usage

From source file:net.acesinc.convergentui.BaseFilter.java

protected String getContentType(RequestContext context) {
    String contentType = "unknown";
    MimeType type = getMimeType(context);
    if (type != null) {
        contentType = type.getType() + "/" + type.getSubtype();
    }//from w ww.java  2 s.  c  o  m

    return contentType;
}

From source file:com.thoughtworks.go.api.ApiController.java

protected boolean isJsonContentType(Request request) {
    String mime = request.headers("Content-Type");
    if (isBlank(mime)) {
        return false;
    }//w  w w  . ja v a2 s  .c  o  m
    try {
        MimeType mimeType = MimeType.valueOf(mime);
        return "application".equals(mimeType.getType()) && "json".equals(mimeType.getSubtype());
    } catch (InvalidMimeTypeException e) {
        return false;
    }
}

From source file:org.springframework.cloud.stream.binder.AbstractBinder.java

private Object deserializePayload(byte[] bytes, MimeType contentType) {
    if ("text".equalsIgnoreCase(contentType.getType()) || MimeTypeUtils.APPLICATION_JSON.equals(contentType)) {
        try {/*from  www.  j a v a  2 s  .  co  m*/
            return new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            String errorMessage = "unable to deserialize [java.lang.String]. Encoding not supported. "
                    + e.getMessage();
            logger.error(errorMessage);
            throw new SerializationFailedException(errorMessage, e);
        }
    } else {
        String className = JavaClassMimeTypeConversion.classNameFromMimeType(contentType);
        try {
            // Cache types to avoid unnecessary ClassUtils.forName calls.
            Class<?> targetType = this.payloadTypeCache.get(className);
            if (targetType == null) {
                targetType = ClassUtils.forName(className, null);
                this.payloadTypeCache.put(className, targetType);
            }
            return this.codec.decode(bytes, targetType);
        } // catch all exceptions that could occur during de-serialization
        catch (Exception e) {
            String errorMessage = "Unable to deserialize [" + className + "] using the contentType ["
                    + contentType + "] " + e.getMessage();
            logger.error(errorMessage);
            throw new SerializationFailedException(errorMessage, e);
        }
    }
}

From source file:org.springframework.integration.x.bus.converter.AbstractFromMessageConverter.java

public boolean supportsTargetMimeType(MimeType mimeType) {
    for (MimeType targetMimeType : targetMimeTypes) {
        if (mimeType.getType().equals(targetMimeType.getType())
                && mimeType.getSubtype().equals(targetMimeType.getSubtype())) {
            return true;
        }//  ww w . j a v  a 2 s  .  com
    }
    return false;
}

From source file:org.springframework.messaging.converter.AbstractMessageConverter.java

protected boolean supportsMimeType(@Nullable MessageHeaders headers) {
    if (getSupportedMimeTypes().isEmpty()) {
        return true;
    }//w  ww.  j  av a2 s. c o m
    MimeType mimeType = getMimeType(headers);
    if (mimeType == null) {
        return !isStrictContentTypeMatch();
    }
    for (MimeType current : getSupportedMimeTypes()) {
        if (current.getType().equals(mimeType.getType())
                && current.getSubtype().equals(mimeType.getSubtype())) {
            return true;
        }
    }
    return false;
}

From source file:org.springframework.messaging.support.converter.AbstractMessageConverter.java

protected boolean supportsMimeType(MessageHeaders headers) {
    MimeType mimeType = getMimeType(headers);
    if (mimeType == null) {
        return true;
    }//  ww  w .  ja va  2  s  . c  om
    if (getSupportedMimeTypes().isEmpty()) {
        return true;
    }
    for (MimeType supported : getSupportedMimeTypes()) {
        if (supported.getType().equals(mimeType.getType())
                && supported.getSubtype().equals(mimeType.getSubtype())) {
            return true;
        }
    }
    return false;
}