    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/framework/util/MimeTypeMapper.java $
    002    /*----------------------------------------------------------------------------
    003     This file is part of deegree, http://deegree.org/
    004     Copyright (C) 2001-2009 by:
    005       Department of Geography, University of Bonn
    006     and
    007       lat/lon GmbH
    008
    009     This library is free software; you can redistribute it and/or modify it under
    010     the terms of the GNU Lesser General Public License as published by the Free
    011     Software Foundation; either version 2.1 of the License, or (at your option)
    012     any later version.
    013     This library is distributed in the hope that it will be useful, but WITHOUT
    014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    016     details.
    017     You should have received a copy of the GNU Lesser General Public License
    018     along with this library; if not, write to the Free Software Foundation, Inc.,
    019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    020
    021     Contact information:
    022
    023     lat/lon GmbH
    024     Aennchenstr. 19, 53177 Bonn
    025     Germany
    026     http://lat-lon.de/
    027
    028     Department of Geography, University of Bonn
    029     Prof. Dr. Klaus Greve
    030     Postfach 1147, 53001 Bonn
    031     Germany
    032     http://www.geographie.uni-bonn.de/deegree/
    033
    034     e-mail: info@deegree.org
    035    ----------------------------------------------------------------------------*/
    036    package org.deegree.framework.util;
    037
    038    /**
    039     * Helper class to check for supported mime types.
    040     *
    041     * @author <a href="mailto:schaefer@lat-lon.de">Axel Schaefer</a>
    042     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
    043     * @author last edited by: $Author: mschneider $
    044     *
    045     * @version $Revision: 18195 $
    046     */
    047    public class MimeTypeMapper {
    048
    049        /**
    050         * Returns true if the submitted content type is defined by the OGC.
    051         *
    052         * @param contentType
    053         *            mime type to check
    054         * @return true if the contentType starts with "application/vnd.ogc", false otherwise
    055         */
    056        public static boolean isOGCType( String contentType ) {
    057            return contentType.startsWith( "application/vnd.ogc" );
    058        }
    059
    060        /**
    061         * Returns true if the submitted content type is an image type.
    062         *
    063         * @param contentType
    064         *            mime type to check
    065         * @return true if the contenttype starts with 'image', false otherwise
    066         */
    067        public static boolean isImageType( String contentType ) {
    068            return contentType.startsWith( "image" );
    069        }
    070
    071        /**
    072         * Returns true if the submitted image content type is known by deegree.
    073         *
    074         * @param contentType
    075         *            mime type to check
    076         * @return true if the content type denotes an image mime-type known by deegree, false otherwise
    077         */
    078        public static boolean isKnownImageType( String contentType ) {
    079            return contentType.equalsIgnoreCase( "image/jpeg" ) || contentType.equalsIgnoreCase( "image/jpg" )
    080                   || contentType.equalsIgnoreCase( "image/gif" ) || contentType.equalsIgnoreCase( "image/tif" )
    081                   || contentType.equalsIgnoreCase( "image/tiff" ) || contentType.equalsIgnoreCase( "image/bmp" )
    082                   || contentType.equalsIgnoreCase( "image/svg+xml" ) || contentType.equalsIgnoreCase( "image/png" )
    083                   || contentType.equalsIgnoreCase( "image/png; mode=8bit" )
    084                   || contentType.equalsIgnoreCase( "image/png; mode=24bit" );
    085        }
    086
    087        /**
    088         * Returns true if the submitted content type is known by the deegree OWS implementations.
    089         *
    090         * @param contentType
    091         *            mime type to check
    092         * @return true if the mime type is known by deegree, false otherwise
    093         */
    094        public static boolean isKnownMimeType( String contentType ) {
    095            int paramIndex = contentType.indexOf( ";" );
    096            if ( paramIndex != -1 ) {
    097                contentType = contentType.substring( 0, paramIndex );
    098            }
    099            return contentType.equalsIgnoreCase( "image/jpeg" ) || contentType.equalsIgnoreCase( "image/jpg" )
    100                   || contentType.equalsIgnoreCase( "image/gif" ) || contentType.equalsIgnoreCase( "image/tif" )
    101                   || contentType.equalsIgnoreCase( "image/tiff" ) || contentType.equalsIgnoreCase( "image/png" )
    102                   || contentType.equalsIgnoreCase( "text/html" ) || contentType.equalsIgnoreCase( "text/text" )
    103                   || contentType.equalsIgnoreCase( "text/plain" ) || contentType.equalsIgnoreCase( "text/xml" )
    104                   || contentType.equalsIgnoreCase( "image/bmp" ) || contentType.equalsIgnoreCase( "application/xml" )
    105                   || contentType.equalsIgnoreCase( "application/vnd.ogc.se_xml" )
    106                   || contentType.equalsIgnoreCase( "application/vnd.ogc.se_inimage" )
    107                   || contentType.equalsIgnoreCase( "application/vnd.ogc.se_blank" )
    108                   || contentType.equalsIgnoreCase( "application/vnd.ogc.wms_xml" )
    109                   || contentType.equalsIgnoreCase( "application/vnd.ogc.gml" )
    110                   || contentType.equalsIgnoreCase( "application/vnd.gdinrw.session_xml" )
    111                   || contentType.equalsIgnoreCase( "application/vnd.gdinrw.secure_xml" )
    112                   || contentType.equalsIgnoreCase( "image/svg+xml" );
    113        }
    114
    115        /**
    116         *
    117         * @param contentType
    118         *
    119         * @return true if the submitted content type is a mime type defined by a OGC specification
    120         */
    121        public static boolean isKnownOGCType( String contentType ) {
    122            return contentType.equalsIgnoreCase( "application/xml" )
    123                   || contentType.equalsIgnoreCase( "application/vnd.ogc.se_xml" )
    124                   || contentType.equalsIgnoreCase( "application/vnd.ogc.se_inimage" )
    125                   || contentType.equalsIgnoreCase( "application/vnd.ogc.se_blank" )
    126                   || contentType.equalsIgnoreCase( "application/vnd.ogc.wms_xml" )
    127                   || contentType.equalsIgnoreCase( "application/vnd.gdinrw.session_xml" )
    128                   || contentType.equalsIgnoreCase( "application/vnd.gdinrw.secure_xml" )
    129                   || contentType.equalsIgnoreCase( "application/vnd.ogc.gml" );
    130        }
    131
    132        /**
    133         * maps a 'simple' format name like gif, jpg or text to the corresponding mime type --> e.g. image/gif, image/jpeg
    134         * or text/plain
    135         *
    136         * @param contentType
    137         *
    138         * @return the mapped content type.
    139         */
    140        public static String toMimeType( String contentType ) {
    141
    142            String mimetype = "";
    143
    144            if ( isKnownMimeType( contentType ) ) {
    145                mimetype = contentType;
    146            } else {
    147                if ( contentType.equalsIgnoreCase( "jpeg" ) ) {
    148                    mimetype = "image/jpeg";
    149                } else if ( contentType.equalsIgnoreCase( "jpg" ) ) {
    150                    mimetype = "image/jpeg";
    151                } else if ( contentType.equalsIgnoreCase( "gif" ) ) {
    152                    mimetype = "image/gif";
    153                } else if ( contentType.equalsIgnoreCase( "png" ) ) {
    154                    mimetype = "image/png";
    155                } else if ( contentType.equalsIgnoreCase( "bmp" ) ) {
    156                    mimetype = "image/bmp";
    157                } else if ( contentType.equalsIgnoreCase( "tif" ) ) {
    158                    mimetype = "image/tiff";
    159                } else if ( contentType.equalsIgnoreCase( "tiff" ) ) {
    160                    mimetype = "image/tiff";
    161                } else if ( contentType.equalsIgnoreCase( "svg" ) ) {
    162                    mimetype = "image/svg+xml";
    163                } else if ( contentType.equalsIgnoreCase( "xml" ) ) {
    164                    mimetype = "text/xml";
    165                } else if ( contentType.equalsIgnoreCase( "gml" ) ) {
    166                    mimetype = "text/gml";
    167                } else if ( contentType.equalsIgnoreCase( "text" ) ) {
    168                    mimetype = "text/plain";
    169                } else if ( contentType.equalsIgnoreCase( "inimage" ) ) {
    170                    mimetype = "application/vnd.ogc.se_inimage";
    171                } else if ( contentType.equalsIgnoreCase( "blank" ) ) {
    172                    mimetype = "application/vnd.ogc.se_blank";
    173                } else if ( contentType.equalsIgnoreCase( "gml" ) ) {
    174                    mimetype = "application/vnd.ogc.gml";
    175                } else {
    176                    // unknown mimetype
    177                    mimetype = "unknown/unknown";
    178                }
    179            }
    180            return mimetype;
    181        }
    182    }