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 javax.mail.internet.ContentType;
022
023import com.nimbusds.oauth2.sdk.ParseException;
024
025
026/**
027 * Content type matching.
028 */
029public class ContentTypeUtils {
030
031
032        /**
033         * Ensures the content type of an HTTP header matches an expected 
034         * value. Note that this method compares only the primary type and 
035         * subtype; any content type parameters, such as {@code charset}, are 
036         * ignored.
037         *
038         * @param expected The expected content type. Must not be {@code null}.
039         * @param found    The found content type. May be {@code null}.
040         *
041         * @throws ParseException If the found content type is {@code null} or
042         *                        it primary and subtype and doesn't match the
043         *                        expected.
044         */
045        public static void ensureContentType(final ContentType expected, final ContentType found)
046                throws ParseException {
047        
048                if (found == null)
049                        throw new ParseException("Missing HTTP Content-Type header");
050                
051                if (! expected.match(found))
052                        throw new ParseException("The HTTP Content-Type header must be " + expected);
053        }
054        
055
056        /**
057         * Prevents instantiation.
058         */
059        private ContentTypeUtils() {
060        
061                // do nothing
062        }
063}