001/* 002 * Copyright 2020-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2020-2024 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2020-2024 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.util; 037 038 039 040import java.io.Serializable; 041import java.util.LinkedHashMap; 042 043import com.unboundid.ldap.sdk.LDAPException; 044import com.unboundid.ldap.sdk.ResultCode; 045import com.unboundid.util.json.JSONObject; 046import com.unboundid.util.json.JSONString; 047import com.unboundid.util.json.JSONValue; 048 049import static com.unboundid.util.UtilityMessages.*; 050 051 052 053/** 054 * This class defines a data structure that represents an item in the OID 055 * registry. 056 * 057 * @see OIDRegistry 058 */ 059@NotMutable() 060@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 061public final class OIDRegistryItem 062 implements Serializable 063{ 064 /** 065 * The name of the JSON field that holds the name for the item. 066 */ 067 @NotNull private static final String FIELD_NAME = "name"; 068 069 070 071 /** 072 * The name of the JSON field that holds the OID for the item. 073 */ 074 @NotNull private static final String FIELD_OID = "oid"; 075 076 077 078 /** 079 * The name of the JSON field that holds the origin for the item. 080 */ 081 @NotNull private static final String FIELD_ORIGIN = "origin"; 082 083 084 085 /** 086 * The name of the JSON field that holds the type for the item. 087 */ 088 @NotNull private static final String FIELD_TYPE = "type"; 089 090 091 092 /** 093 * The name of the JSON field that holds the URL for the item. 094 */ 095 @NotNull private static final String FIELD_URL = "url"; 096 097 098 099 /** 100 * The serial version UID for this serializable class. 101 */ 102 private static final long serialVersionUID = 4342220623592884938L; 103 104 105 106 // A JSON object representation of this item. 107 @NotNull private final JSONObject jsonObject; 108 109 // The name for the item. 110 @NotNull private final String name; 111 112 // The OID for the item. 113 @NotNull private final String oid; 114 115 // The origin for the item. 116 @Nullable private final String origin; 117 118 // The type for the item. 119 @NotNull private final String type; 120 121 // A URL with more information about the item. 122 @Nullable private final String url; 123 124 125 126 /** 127 * Creates an OID registry item with the provided information. 128 * 129 * @param oid The OID for the item. 130 * @param name The name for the item. 131 * @param type The type for the item. 132 * @param origin The origin for the item, if any. 133 * @param url The URL for the item, if any. 134 */ 135 OIDRegistryItem(@NotNull final String oid, 136 @NotNull final String name, 137 @NotNull final String type, 138 @Nullable final String origin, 139 @Nullable final String url) 140 { 141 this.oid = oid; 142 this.name = name; 143 this.type = type; 144 this.origin = origin; 145 this.url = url; 146 147 final LinkedHashMap<String,JSONValue> jsonFields = new LinkedHashMap<>(); 148 jsonFields.put(FIELD_OID, new JSONString(oid)); 149 jsonFields.put(FIELD_NAME, new JSONString(name)); 150 jsonFields.put(FIELD_TYPE, new JSONString(type)); 151 152 if (origin != null) 153 { 154 jsonFields.put(FIELD_ORIGIN, new JSONString(origin)); 155 } 156 157 if (url != null) 158 { 159 jsonFields.put(FIELD_URL, new JSONString(url)); 160 } 161 162 jsonObject = new JSONObject(jsonFields); 163 } 164 165 166 167 /** 168 * Creates an OID registry item that is decoded from the provided JSON object. 169 * 170 * @param jsonObject The JSON object to decode as an OID registry item. 171 * 172 * @throws LDAPException If the provided JSON object cannot be parsed as a 173 * valid OID registry item. 174 */ 175 OIDRegistryItem(@NotNull final JSONObject jsonObject) 176 throws LDAPException 177 { 178 this.jsonObject = jsonObject; 179 180 oid = jsonObject.getFieldAsString(FIELD_OID); 181 if (oid == null) 182 { 183 throw new LDAPException(ResultCode.INVALID_ATTRIBUTE_SYNTAX, 184 ERR_OID_REGISTRY_ITEM_OBJECT_MISSING_FIELD.get( 185 jsonObject.toSingleLineString(), FIELD_OID)); 186 } 187 188 name = jsonObject.getFieldAsString(FIELD_NAME); 189 if (name == null) 190 { 191 throw new LDAPException(ResultCode.INVALID_ATTRIBUTE_SYNTAX, 192 ERR_OID_REGISTRY_ITEM_OBJECT_MISSING_FIELD.get( 193 jsonObject.toSingleLineString(), FIELD_NAME)); 194 } 195 196 type = jsonObject.getFieldAsString(FIELD_TYPE); 197 if (type == null) 198 { 199 throw new LDAPException(ResultCode.INVALID_ATTRIBUTE_SYNTAX, 200 ERR_OID_REGISTRY_ITEM_OBJECT_MISSING_FIELD.get( 201 jsonObject.toSingleLineString(), FIELD_TYPE)); 202 } 203 204 origin = jsonObject.getFieldAsString(FIELD_ORIGIN); 205 url = jsonObject.getFieldAsString(FIELD_URL); 206 } 207 208 209 210 /** 211 * Retrieves a string representation of the OID for this OID registry item. 212 * 213 * @return A string representation of the OID for this OID registry item. 214 */ 215 @NotNull() 216 public String getOID() 217 { 218 return oid; 219 } 220 221 222 223 /** 224 * Retrieves the name for this OID registry item. 225 * 226 * @return The name for this OID registry item. 227 */ 228 @NotNull() 229 public String getName() 230 { 231 return name; 232 } 233 234 235 236 /** 237 * Retrieves the type for this OID registry item. 238 * 239 * @return The type for this OID registry item. 240 */ 241 @NotNull() 242 public String getType() 243 { 244 return type; 245 } 246 247 248 249 /** 250 * Retrieves a string with information about the origin of this OID registry 251 * item, if available. 252 * 253 * @return A string with information about the origin of this OID registry 254 * item, or {@code null} if none is available. 255 */ 256 @Nullable() 257 public String getOrigin() 258 { 259 return origin; 260 } 261 262 263 264 /** 265 * Retrieves a URL with more information about this OID registry item, if 266 * available. 267 * 268 * @return A URL with more information about this OID registry item, or 269 * {@code null} if none is available. 270 */ 271 @Nullable() 272 public String getURL() 273 { 274 return url; 275 } 276 277 278 279 /** 280 * Retrieves a representation of this OID registry item as a JSON object. 281 * 282 * @return A representation of this OID registry item as a JSON object. 283 */ 284 @NotNull() 285 public JSONObject asJSONObject() 286 { 287 return jsonObject; 288 } 289 290 291 292 /** 293 * Retrieves a string representation of this OID registry item. 294 * 295 * @return A string representation of this OID registry item. 296 */ 297 @Override() 298 @NotNull() 299 public String toString() 300 { 301 return jsonObject.toSingleLineString(); 302 } 303}