Search in sources :

Example 1 with MimeTypeParserException

use of com.helger.commons.mime.MimeTypeParserException in project ph-web by phax.

the class MockHttpServletResponse method setContentType.

public void setContentType(@Nullable final String sContentType) {
    m_sContentType = sContentType;
    if (sContentType != null) {
        try {
            final IMimeType aContentType = MimeTypeParser.parseMimeType(sContentType);
            final String sEncoding = MimeTypeHelper.getCharsetNameFromMimeType(aContentType);
            if (sEncoding != null)
                setCharacterEncoding(sEncoding);
        } catch (final MimeTypeParserException ex) {
            LOGGER.warn("Passed content type '" + sContentType + "' cannot be parsed as a MIME type");
        }
    }
}
Also used : IMimeType(com.helger.commons.mime.IMimeType) MimeTypeParserException(com.helger.commons.mime.MimeTypeParserException)

Example 2 with MimeTypeParserException

use of com.helger.commons.mime.MimeTypeParserException in project phase4 by phax.

the class PModePayloadProfileJsonConverter method convertToNative.

@Nonnull
public static PModePayloadProfile convertToNative(final IJsonObject aElement) {
    final String sName = aElement.getAsString(NAME);
    final String sMimeType = aElement.getAsString(MIME_TYPE);
    final IMimeType aMimeType;
    try {
        aMimeType = MimeTypeParser.parseMimeType(sMimeType);
    } catch (final MimeTypeParserException ex) {
        throw new IllegalArgumentException("Failed to parse MIME Type '" + sMimeType + "'", ex);
    }
    final String sXSDFilename = aElement.getAsString(XSD_FILENAME);
    final Integer aMaxSizeKB = aElement.getAsIntObj(MAX_SIZE_KB);
    final EMandatory eMandatory = EMandatory.valueOf(aElement.getAsBoolean(MANDATORY, PModePayloadProfile.DEFAULT_MANDATORY));
    return new PModePayloadProfile(sName, aMimeType, sXSDFilename, aMaxSizeKB, eMandatory);
}
Also used : IMimeType(com.helger.commons.mime.IMimeType) EMandatory(com.helger.commons.state.EMandatory) MimeTypeParserException(com.helger.commons.mime.MimeTypeParserException) Nonnull(javax.annotation.Nonnull)

Example 3 with MimeTypeParserException

use of com.helger.commons.mime.MimeTypeParserException in project phase4 by phax.

the class PModePayloadProfileMicroTypeConverter method convertToNative.

@Nonnull
public PModePayloadProfile convertToNative(final IMicroElement aElement) {
    final String sName = aElement.getAttributeValue(ATTR_NAME);
    final String sMimeType = aElement.getAttributeValue(ATTR_MIME_TYPE);
    final IMimeType aMimeType;
    try {
        aMimeType = MimeTypeParser.parseMimeType(sMimeType);
    } catch (final MimeTypeParserException ex) {
        throw new IllegalArgumentException("Failed to parse MIME Type '" + sMimeType + "'", ex);
    }
    final String sXSDFilename = aElement.getAttributeValue(ATTR_XSD_FILENAME);
    final Integer aMaxSizeKB = aElement.getAttributeValueWithConversion(ATTR_MAX_SIZE_KB, Integer.class);
    final EMandatory eMandatory = EMandatory.valueOf(aElement.getAttributeValueAsBool(ATTR_MANDATORY, PModePayloadProfile.DEFAULT_MANDATORY));
    return new PModePayloadProfile(sName, aMimeType, sXSDFilename, aMaxSizeKB, eMandatory);
}
Also used : IMimeType(com.helger.commons.mime.IMimeType) EMandatory(com.helger.commons.state.EMandatory) MimeTypeParserException(com.helger.commons.mime.MimeTypeParserException) Nonnull(javax.annotation.Nonnull)

Example 4 with MimeTypeParserException

use of com.helger.commons.mime.MimeTypeParserException in project ph-commons by phax.

the class MimeTypeInfoMicroTypeConverter method convertToNative.

@Nullable
public MimeTypeInfo convertToNative(@Nonnull final IMicroElement aElement) {
    final ICommonsOrderedSet<MimeTypeWithSource> aMimeTypes = new CommonsLinkedHashSet<>();
    for (final IMicroElement eMimeType : aElement.getAllChildElements(ELEMENT_MIMETYPE)) {
        try {
            final MimeType aMimeType = MimeTypeParser.parseMimeType(eMimeType.getTextContentTrimmed());
            final String sSource = eMimeType.getAttributeValue(ATTR_SOURCE);
            aMimeTypes.add(new MimeTypeWithSource(aMimeType, sSource));
        } catch (final MimeTypeParserException ex) {
            throw new IllegalStateException("Failed to parse MIME type", ex);
        }
    }
    final String sComment = MicroHelper.getChildTextContent(aElement, ELEMENT_COMMENT);
    final ICommonsOrderedSet<String> aParentTypes = new CommonsLinkedHashSet<>();
    for (final IMicroElement eParentType : aElement.getAllChildElements(ELEMENT_PARENT_TYPE)) aParentTypes.add(eParentType.getTextContentTrimmed());
    final ICommonsOrderedSet<String> aGlobs = new CommonsLinkedHashSet<>();
    for (final IMicroElement eGlob : aElement.getAllChildElements(ELEMENT_GLOB)) aGlobs.add(eGlob.getTextContentTrimmed());
    final ICommonsOrderedSet<ExtensionWithSource> aExtensions = new CommonsLinkedHashSet<>();
    for (final IMicroElement eExtension : aElement.getAllChildElements(ELEMENT_EXTENSION)) {
        // May be null if the empty extension ("") is used
        final String sExtension = StringHelper.getNotNull(eExtension.getTextContentTrimmed());
        final String sSource = eExtension.getAttributeValue(ATTR_SOURCE);
        aExtensions.add(new ExtensionWithSource(sExtension, sSource));
    }
    final String sSource = aElement.getAttributeValue(ATTR_SOURCE);
    return new MimeTypeInfo(aMimeTypes, sComment, aParentTypes, aGlobs, aExtensions, sSource);
}
Also used : MimeTypeWithSource(com.helger.xml.util.mime.MimeTypeInfo.MimeTypeWithSource) ExtensionWithSource(com.helger.xml.util.mime.MimeTypeInfo.ExtensionWithSource) IMicroElement(com.helger.xml.microdom.IMicroElement) MimeTypeParserException(com.helger.commons.mime.MimeTypeParserException) CommonsLinkedHashSet(com.helger.commons.collection.impl.CommonsLinkedHashSet) MimeType(com.helger.commons.mime.MimeType) Nullable(javax.annotation.Nullable)

Aggregations

MimeTypeParserException (com.helger.commons.mime.MimeTypeParserException)4 IMimeType (com.helger.commons.mime.IMimeType)3 EMandatory (com.helger.commons.state.EMandatory)2 Nonnull (javax.annotation.Nonnull)2 CommonsLinkedHashSet (com.helger.commons.collection.impl.CommonsLinkedHashSet)1 MimeType (com.helger.commons.mime.MimeType)1 IMicroElement (com.helger.xml.microdom.IMicroElement)1 ExtensionWithSource (com.helger.xml.util.mime.MimeTypeInfo.ExtensionWithSource)1 MimeTypeWithSource (com.helger.xml.util.mime.MimeTypeInfo.MimeTypeWithSource)1 Nullable (javax.annotation.Nullable)1