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.jwk;
018
019import org.forgerock.json.JsonException;
020
021/**
022 * Enum representing the possible KeyTypes.
023 */
024public enum KeyType {
025    /**
026     * RSA key.
027     */
028    RSA("RSA"),
029
030    /**
031     * Elliptical Curve Key.
032     */
033    EC("EC"),
034
035    /**
036     * Octet Key.
037     */
038    OCT("oct");
039
040    /**
041     * The value of the KeyType.
042     */
043    private String value = null;
044
045    /**
046     * Construct a KeyType.
047     * @param value value to give that keytype
048     */
049    private KeyType(String value) {
050        this.value = value;
051    }
052
053    /**
054     * Get the value of the KeyType.
055     * @return the value of the KeyType
056     */
057    public String value() {
058        return toString();
059    }
060
061    /**
062     * Get the KeyType given a string.
063     * @param keyType string representing the KeyType
064     * @return a KeyType or null if given null KeyType
065     */
066    public static KeyType getKeyType(String keyType) {
067        if (keyType == null || keyType.isEmpty()) {
068            return null;
069        }
070        try {
071            return KeyType.valueOf(keyType.toUpperCase());
072        } catch (IllegalArgumentException e) {
073            throw new JsonException("Invalid key type");
074        }
075    }
076
077    /**
078     * Gets the value of the KeyType.
079     * @return value of the KeyType
080     */
081    @Override
082    public String toString() {
083        return value;
084    }
085}