Java String Sanitize sanitizeMimeType(final String mimeType)

Here you can find the source of sanitizeMimeType(final String mimeType)

Description

Mimetypes can be a tricky thing as browsers and/or environments tend to alter them without good reason.

License

Apache License

Parameter

Parameter Description
mimeType The mimetype that needs to be sanitized.

Return

A standard compliant mimetype in lowercase

Declaration

public static String sanitizeMimeType(final String mimeType) 

Method Source Code

//package com.java2s;
/*/*w  ww . ja  va  2s  . co  m*/
 * Copyright 2015 Hippo B.V. (http://www.onehippo.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    public static final String MIME_TYPE_JPEG = "image/jpeg";
    public static final String MIME_TYPE_PJPEG = "image/pjpeg";
    public static final String MIME_TYPE_CITRIX_JPEG = "image/x-citrix-pjpeg";
    public static final String MIME_TYPE_GIF = "image/gif";
    public static final String MIME_TYPE_CITRIX_GIF = "image/x-citrix-gif";
    public static final String MIME_TYPE_PNG = "image/png";
    public static final String MIME_TYPE_X_PNG = "image/x-png";

    /**
     * Mimetypes can be a tricky thing as browsers and/or environments tend to alter them without good reason. This
     * method will try to fix any of the quirks concerning mimetypes that are found out there.
     *
     * @param mimeType  The mimetype that needs to be sanitized.
     * @return A standard compliant mimetype in lowercase
     */
    public static String sanitizeMimeType(final String mimeType) {
        // IE uploads JPEG files with the non-standard MIME type image/pjpeg for which ImageIO
        // doesn't have an ImageReader. Simply replacing the MIME type with image/jpeg solves this.
        // For more info see http://www.iana.org/assignments/media-types/image/ and
        // http://groups.google.com/group/comp.infosystems.www.authoring.images/msg/7706603e4bd1d9d4?hl=en
        if (MIME_TYPE_PJPEG.equalsIgnoreCase(mimeType)) {
            return MIME_TYPE_JPEG;
        }

        // Citrix environments change the mimetype of jpeg and gif files.
        if (MIME_TYPE_CITRIX_JPEG.equalsIgnoreCase(mimeType)) {
            return MIME_TYPE_JPEG;
        } else if (MIME_TYPE_CITRIX_GIF.equalsIgnoreCase(mimeType)) {
            return MIME_TYPE_GIF;
        }

        // Before it was accepted as a standard mimetype, PNG images where marked as image/x-png which is still the
        // case for IE8
        if (MIME_TYPE_X_PNG.equalsIgnoreCase(mimeType)) {
            return MIME_TYPE_PNG;
        }

        return mimeType.toLowerCase();
    }
}

Related

  1. sanitizeJavascript(String originalString)
  2. sanitizeLine(String line)
  3. sanitizeLineSpaces(String line)
  4. sanitizeLoggerNamePart(String name)
  5. sanitizeMethodName(String methodName)
  6. sanitizeName(String input)
  7. SanitizeName(String Name)
  8. sanitizeName(String name)
  9. sanitizeName(String name)