public static Charset parseContentTypeForTextualCharset()

in qpid-jms-client/src/main/java/org/apache/qpid/jms/util/ContentTypeSupport.java [44:94]


    public static Charset parseContentTypeForTextualCharset(final String contentType) throws InvalidContentTypeException {
        if (contentType == null || contentType.trim().isEmpty()) {
            throw new InvalidContentTypeException("Content type can't be null or empty");
        }

        int subTypeSeparator = contentType.indexOf("/");
        if(subTypeSeparator == -1) {
            throw new InvalidContentTypeException("Content type has no '/' separator: " + contentType);
        }

        final String type = contentType.substring(0, subTypeSeparator).toLowerCase().trim();

        String subTypePart = contentType.substring(subTypeSeparator +1).toLowerCase().trim();

        String parameterPart = null;
        int parameterSeparator = subTypePart.indexOf(";");
        if(parameterSeparator != -1)
        {
            if(parameterSeparator < subTypePart.length() - 1) {
                parameterPart = contentType.substring(subTypeSeparator + 1).toLowerCase().trim();
            }
            subTypePart = subTypePart.substring(0, parameterSeparator).trim();
        }

        if(subTypePart.isEmpty()) {
            throw new InvalidContentTypeException("Content type has no subtype after '/'" + contentType);
        }

        final String subType = subTypePart;

        if(isTextual(type, subType)) {
            String charset = findCharset(parameterPart);
            if(charset == null) {
                charset = UTF_8;
            }

            if (UTF_8.equals(charset)) {
                return StandardCharsets.UTF_8;
            } else {
                try {
                    return Charset.forName(charset);
                } catch (IllegalCharsetNameException icne) {
                    throw new InvalidContentTypeException("Illegal charset: " + charset);
                } catch (UnsupportedCharsetException uce) {
                    throw new InvalidContentTypeException("Unsupported charset: " + charset);
                }
            }
        }

        return null;
    }