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 {@code Content-Type} of an HTTP header matches an
033         * expected value. Note that this method compares only the primary type
034         * and subtype; any content type parameters, such as {@code charset},
035         * are 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         *                        doesn't match the expected.
042         */
043        public static void ensureContentType(final ContentType expected, final ContentType found)
044                throws ParseException {
045        
046                ensureContentType(expected, null, found);
047        }
048
049
050        /**
051         * Ensures the {@code Content-Type} of an HTTP header matches an
052         * expected value. Note that this method compares only the primary type
053         * and subtype; any content type parameters, such as {@code charset},
054         * are ignored.
055         *
056         * @param expected      The expected content type. Must not be {@code null}.
057         * @param subTypeSuffix Acceptable suffix if the sub type doesn't
058         *                      match exactly, {@code null} if not specified.
059         * @param found         The found content type. May be {@code null}.
060         *
061         * @throws ParseException If the found content type is {@code null} or
062         *                        doesn't match the expected.
063         */
064        public static void ensureContentType(final ContentType expected,
065                                             final String subTypeSuffix,
066                                             final ContentType found)
067                throws ParseException {
068
069                if (found == null) {
070                        throw new ParseException("Missing HTTP Content-Type header");
071                }
072
073                if (expected.matches(found)) {
074                        // Exact match
075                        return;
076                }
077
078                if (expected.getBaseType().equals(found.getBaseType()) && found.hasSubTypeSuffix(subTypeSuffix)) {
079                        // Base + suffix match
080                        return;
081                }
082
083                if (subTypeSuffix == null) {
084
085                        throw new ParseException("The HTTP Content-Type header must be " +
086                                expected.getType() +
087                                ", received " + found.getType());
088
089                } else {
090
091                        throw new ParseException("The HTTP Content-Type header must be " +
092                                expected.getType() +
093                                " or have the +" + subTypeSuffix + " suffix, " +
094                                "received " + found.getType());
095                }
096        }
097        
098
099        /**
100         * Prevents public instantiation.
101         */
102        private ContentTypeUtils() {}
103}