001/*
002 * Copyright 2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.revetsec.jose;
018
019import com.revetsec.ErrorCategory;
020import org.jspecify.annotations.NonNull;
021
022import javax.annotation.concurrent.NotThreadSafe;
023
024import static java.util.Objects.requireNonNull;
025
026/**
027 * Thrown when a token uses a JOSE feature that Revetsec does not support: encryption, the JSON serialization, a
028 * critical header parameter, an unencoded or compressed payload, or a nested token.
029 * <p>
030 * Its category is {@link ErrorCategory#UNSUPPORTED}, and its {@link JoseException.Reason} is one of
031 * {@link JoseException.Reason#ENCRYPTED_TOKEN ENCRYPTED_TOKEN},
032 * {@link JoseException.Reason#JSON_SERIALIZATION JSON_SERIALIZATION},
033 * {@link JoseException.Reason#CRITICAL_HEADER CRITICAL_HEADER},
034 * {@link JoseException.Reason#UNENCODED_PAYLOAD UNENCODED_PAYLOAD},
035 * {@link JoseException.Reason#COMPRESSED_PAYLOAD COMPRESSED_PAYLOAD} and
036 * {@link JoseException.Reason#NESTED_TOKEN NESTED_TOKEN}. It has no cause and is never transient.
037 *
038 * @author <a href="https://www.revetkn.com">Mark Allen</a>
039 * @since 1.0.0
040 */
041@NotThreadSafe
042public final class UnsupportedJoseFeatureException extends JoseException {
043        /**
044         * The serialized form's version.
045         */
046        private static final long serialVersionUID = 1L;
047
048        private UnsupportedJoseFeatureException(@NonNull Reason reason) {
049                super(reason);
050        }
051
052        /**
053         * Returns a new instance for {@code reason}. Only Revetsec calls this.
054         *
055         * @param reason a reason in category {@link ErrorCategory#UNSUPPORTED}
056         * @return a new instance
057         * @throws IllegalArgumentException if {@code reason} belongs to another category
058         */
059        @NonNull
060        static UnsupportedJoseFeatureException fromReason(@NonNull Reason reason) {
061                if (requireNonNull(reason).category() != ErrorCategory.UNSUPPORTED)
062                        throw new IllegalArgumentException("An unsupported-feature reason must be in category UNSUPPORTED.");
063
064                return new UnsupportedJoseFeatureException(reason);
065        }
066}