001/* 002 * nimbus-jose-jwt 003 * 004 * Copyright 2012-2016, Connect2id Ltd. 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; 019 020 021import com.nimbusds.jose.util.JSONStringUtils; 022import net.jcip.annotations.Immutable; 023 024import java.io.Serializable; 025import java.util.Objects; 026 027 028 029/** 030 * JOSE object type, represents the {@code typ} header parameter in unsecured, 031 * JSON Web Signature (JWS) and JSON Web Encryption (JWE) objects. This class 032 * is immutable. 033 * 034 * <p>Includes constants for the following standard types: 035 * 036 * <ul> 037 * <li>{@link #JOSE} 038 * <li>{@link #JOSE_JSON JOSE+JSON} 039 * <li>{@link #JWT} 040 * </ul> 041 * 042 * <p>Additional types can be defined using the constructor. 043 * 044 * @author Vladimir Dzhuvinov 045 * @version 2024-04-20 046 */ 047@Immutable 048public final class JOSEObjectType implements Serializable { 049 050 051 private static final long serialVersionUID = 1L; 052 053 054 /** 055 * Compact encoded JOSE object type. 056 */ 057 public static final JOSEObjectType JOSE = new JOSEObjectType("JOSE"); 058 059 060 /** 061 * JSON-encoded JOSE object type. 062 */ 063 public static final JOSEObjectType JOSE_JSON = new JOSEObjectType("JOSE+JSON"); 064 065 066 /** 067 * JSON Web Token (JWT) object type. 068 */ 069 public static final JOSEObjectType JWT = new JOSEObjectType("JWT"); 070 071 072 /** 073 * The object type. 074 */ 075 private final String type; 076 077 078 /** 079 * Creates a new JOSE object type. 080 * 081 * @param type The object type. Must not be {@code null}. 082 */ 083 public JOSEObjectType(final String type) { 084 085 this.type = Objects.requireNonNull(type); 086 } 087 088 089 /** 090 * Gets the JOSE object type. 091 * 092 * @return The JOSE object type. 093 */ 094 public String getType() { 095 096 return type; 097 } 098 099 100 /** 101 * Overrides {@code Object.hashCode()}. 102 * 103 * @return The object hash code. 104 */ 105 @Override 106 public int hashCode() { 107 108 return type.toLowerCase().hashCode(); 109 } 110 111 112 /** 113 * Overrides {@code Object.equals()}. 114 * 115 * @param object The object to compare to. 116 * 117 * @return {@code true} if the objects have the same value, otherwise 118 * {@code false}. 119 */ 120 @Override 121 public boolean equals(final Object object) { 122 123 return object instanceof JOSEObjectType && 124 this.type.equalsIgnoreCase(((JOSEObjectType) object).type); 125 } 126 127 128 /** 129 * Returns the string representation of this JOSE object type. 130 * 131 * @see #getType 132 * 133 * @return The string representation. 134 */ 135 @Override 136 public String toString() { 137 138 return type; 139 } 140 141 142 /** 143 * Returns the JSON string representation of this JOSE object type. 144 * 145 * @return The JSON string representation. 146 */ 147 public String toJSONString() { 148 return JSONStringUtils.toJSONString(type); 149 } 150}