001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.oauth2.sdk.util;
019
020
021import com.nimbusds.common.contenttype.ContentType;
022import com.nimbusds.oauth2.sdk.ParseException;
023
024
025/**
026 * Content type matching.
027 */
028public final class ContentTypeUtils {
029
030
031        /**
032         * Ensures the content type of an HTTP header matches an expected 
033         * value. Note that this method compares only the primary type and 
034         * subtype; any content type parameters, such as {@code charset}, are 
035         * ignored.
036         *
037         * @param expected The expected content type. Must not be {@code null}.
038         * @param found    The found content type. May be {@code null}.
039         *
040         * @throws ParseException If the found content type is {@code null} or
041         *                        it primary and subtype and doesn't match the
042         *                        expected.
043         */
044        public static void ensureContentType(final ContentType expected, final ContentType found)
045                throws ParseException {
046        
047                if (found == null)
048                        throw new ParseException("Missing HTTP Content-Type header");
049                
050                if (! expected.matches(found))
051                        throw new ParseException("The HTTP Content-Type header must be " + expected);
052        }
053
054
055        /**
056         * @see #ensureContentType(ContentType, ContentType)
057         */
058        @Deprecated
059        public static void ensureContentType(final javax.mail.internet.ContentType expected, final javax.mail.internet.ContentType found)
060                throws ParseException {
061        
062                if (found == null)
063                        throw new ParseException("Missing HTTP Content-Type header");
064                
065                if (! expected.match(found))
066                        throw new ParseException("The HTTP Content-Type header must be " + expected);
067        }
068        
069
070        /**
071         * Prevents public instantiation.
072         */
073        private ContentTypeUtils() {}
074}