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 * Represents the Possible KeyUse values.
023 */
024public enum KeyUse {
025    /**
026     * Use Key as a signature key.
027     */
028    SIG,
029
030    /**
031     * Use Key as a encryption key.
032     */
033    ENC;
034
035    /**
036     * Construct a KeyUse.
037     */
038    private KeyUse() {
039
040    }
041
042    /**
043     * Get the Value of the KeyUse.
044     * @return the KeyUse value.
045     */
046    public String value() {
047        return toString();
048    }
049
050    /**
051     * Get the KeyUse.
052     * @param keyUse the string representing the KeyUse to get
053     * @return a KeyUse, or null if keyUse is null or empty
054     */
055    public static KeyUse getKeyUse(String keyUse) {
056        if (keyUse == null || keyUse.isEmpty()) {
057            return null;
058        }
059        try {
060            return KeyUse.valueOf(keyUse.toUpperCase());
061        } catch (IllegalArgumentException e) {
062            throw new JsonException("Invalid key use");
063        }
064    }
065
066    /**
067     * Prints the KeyUse value.
068     * @return the KeyUse in lowercase
069     */
070    @Override
071    public String toString() {
072        return super.toString().toLowerCase();
073    }
074}