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 java.io.Serializable; 022 023import net.jcip.annotations.Immutable; 024 025import net.minidev.json.JSONAware; 026import net.minidev.json.JSONObject; 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 2014-02-15 046 */ 047@Immutable 048public final class JOSEObjectType implements JSONAware, 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 if (type == null) { 086 throw new IllegalArgumentException("The object type must not be null"); 087 } 088 089 this.type = type; 090 } 091 092 093 /** 094 * Gets the JOSE object type. 095 * 096 * @return The JOSE object type. 097 */ 098 public String getType() { 099 100 return type; 101 } 102 103 104 /** 105 * Overrides {@code Object.hashCode()}. 106 * 107 * @return The object hash code. 108 */ 109 @Override 110 public int hashCode() { 111 112 return type.hashCode(); 113 } 114 115 116 /** 117 * Overrides {@code Object.equals()}. 118 * 119 * @param object The object to compare to. 120 * 121 * @return {@code true} if the objects have the same value, otherwise 122 * {@code false}. 123 */ 124 @Override 125 public boolean equals(final Object object) { 126 127 return object != null && 128 object instanceof JOSEObjectType && 129 this.toString().equals(object.toString()); 130 } 131 132 133 /** 134 * Returns the string representation of this JOSE object type. 135 * 136 * @see #getType 137 * 138 * @return The string representation. 139 */ 140 @Override 141 public String toString() { 142 143 return type; 144 } 145 146 147 /** 148 * Returns the JSON string representation of this JOSE object type. 149 * 150 * @return The JSON string representation. 151 */ 152 @Override 153 public String toJSONString() { 154 155 return "\"" + JSONObject.escape(type) + '"'; 156 } 157}