001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2013-2015 ForgeRock AS.
015 */
016
017package org.forgerock.json.jose.utils;
018
019import java.net.URI;
020import java.net.URISyntaxException;
021
022import org.forgerock.json.jose.exceptions.JwtRuntimeException;
023
024/**
025 * This class provides an utility method for validating that a String is either an arbitrary string without any ":"
026 * characters or if the String does contain a ":" character then the String is a valid URI.
027 *
028 * @see <a href="http://tools.ietf.org/html/draft-jones-json-web-token-10#section-2">StringOrURI</a>
029 *
030 * @since 2.0.0
031 */
032public final class StringOrURI {
033
034    /**
035     * Private constructor.
036     */
037    private StringOrURI() {
038    }
039
040    /**
041     * Validates that the given String is either an arbitrary string without any ":" characters, otherwise validates
042     * that the String is a valid URI.
043     *
044     * @param s The String to validate.
045     * @throws JwtRuntimeException if the given String contains a ":" character and is not a valid URI.
046     */
047    public static void validateStringOrURI(String s) {
048        if (s != null && s.contains(":")) {
049            try {
050                new URI(s);
051            } catch (URISyntaxException e) {
052                throw new JwtRuntimeException(e);
053            }
054        }
055    }
056}