001/*
002 * nimbus-jose-jwt
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.jose.crypto.impl;
019
020
021import java.util.Collection;
022
023import com.nimbusds.jose.EncryptionMethod;
024import com.nimbusds.jose.JWEAlgorithm;
025import com.nimbusds.jose.JWSAlgorithm;
026import com.nimbusds.jose.jwk.Curve;
027
028
029/**
030 * Algorithm support messages, intended for JOSE exceptions.
031 *
032 * @author Vladimir Dzhuvinov
033 * @version 2015-05-20
034 */
035public class AlgorithmSupportMessage {
036
037
038        /**
039         * Itemises the specified collection to human readable string.
040         *
041         * @param collection The collection, with valid {@code toString}
042         *                   methods. Must not be {@code null}.
043         *
044         * @return The string.
045         */
046        private static String itemize(final Collection collection) {
047
048                StringBuilder sb = new StringBuilder();
049
050                Object[] items = collection.toArray();
051
052                for (int i=0; i < items.length; i++) {
053
054                        if (i == 0) {
055                                // no delimiter
056                        } else if (i < items.length - 1) {
057                                sb.append(", ");
058                        } else if (i == items.length - 1) {
059                                sb.append(" or ");
060                        }
061
062                        sb.append(items[i].toString());
063                }
064
065                return sb.toString();
066        }
067
068
069        /**
070         * Returns a message that the specified JWS algorithm is not supported.
071         *
072         * @param unsupported The unsupported JWS algorithm. Must not be
073         *                    {@code null}.
074         * @param supported   The supported JWS algorithms. Must not be
075         *                    {@code null}.
076         *
077         * @return The message.
078         */
079        public static String unsupportedJWSAlgorithm(final JWSAlgorithm unsupported,
080                                                     final Collection<JWSAlgorithm> supported) {
081
082                return "Unsupported JWS algorithm " + unsupported + ", must be " + itemize(supported);
083        }
084
085
086        /**
087         * Returns a message that the specified JWE algorithm is not supported.
088         *
089         * @param unsupported The unsupported JWE algorithm. Must not be
090         *                    {@code null}.
091         * @param supported   The supported JWE algorithms. Must not be
092         *                    {@code null}.
093         *
094         * @return The message.
095         */
096        public static String unsupportedJWEAlgorithm(final JWEAlgorithm unsupported,
097                                                     final Collection<JWEAlgorithm> supported) {
098
099                return "Unsupported JWE algorithm " + unsupported + ", must be " + itemize(supported);
100        }
101
102
103        /**
104         * Returns a message that the specified JWE encryption method is not
105         * supported.
106         *
107         * @param unsupported The unsupported JWE encryption method. Must not
108         *                    be {@code null}.
109         * @param supported   The supported JWE encryption methods. Must not be
110         *                    {@code null}.
111         *
112         * @return The message.
113         */
114        public static String unsupportedEncryptionMethod(final EncryptionMethod unsupported,
115                                                         final Collection<EncryptionMethod> supported) {
116
117                return "Unsupported JWE encryption method " + unsupported + ", must be " + itemize(supported);
118        }
119
120
121        /**
122         * Returns a message that the specified elliptic curve is not
123         * supported.
124         *
125         * @param unsupported The unsupported elliptic curve. Must not be
126         *                    {@code null}.
127         * @param supported   The supported elliptic curves. Must not be
128         *                    {@code null}.
129         *
130         * @return The message.
131         */
132        public static String unsupportedEllipticCurve(final Curve unsupported,
133                                                      final Collection<Curve> supported) {
134
135                return "Unsupported elliptic curve " + unsupported + ", must be " + itemize(supported);
136        }
137
138
139        /**
140         * Prevents public instantiation.
141         */
142        private AlgorithmSupportMessage() {
143
144        }
145}