001/** 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved 005 * 006 * The contents of this file are subject to the terms 007 * of the Common Development and Distribution License 008 * (the License). You may not use this file except in 009 * compliance with the License. 010 * 011 * You can obtain a copy of the License at 012 * https://opensso.dev.java.net/public/CDDLv1.0.html or 013 * opensso/legal/CDDLv1.0.txt 014 * See the License for the specific language governing 015 * permission and limitations under the License. 016 * 017 * When distributing Covered Code, include this CDDL 018 * Header Notice in each file and include the License file 019 * at opensso/legal/CDDLv1.0.txt. 020 * If applicable, add the following below the CDDL Header, 021 * with the fields enclosed by brackets [] replaced by 022 * your own identifying information: 023 * "Portions Copyrighted [year] [name of copyright owner]" 024 * 025 * $Id: EncryptableNameIdentifier.java,v 1.4 2008/06/25 05:46:46 qcheng Exp $ 026 * 027 */ 028 029package com.sun.identity.federation.message.common; 030 031import org.w3c.dom.Element; 032import java.util.Date; 033 034import com.sun.identity.federation.common.FSException; 035import com.sun.identity.federation.common.IFSConstants; 036import com.sun.identity.federation.common.FSUtils; 037 038import com.sun.identity.saml.assertion.NameIdentifier; 039import com.sun.identity.shared.DateUtils; 040import com.sun.identity.shared.xml.XMLUtils; 041 042/** 043 * This class contains methods for encrypting the <code>NameIdentifier</code> 044 * object. 045 * 046 * @supported.all.api 047 */ 048public class EncryptableNameIdentifier { 049 050 private java.lang.String _nameQualifier = ""; 051 private java.lang.String _name = ""; 052 private java.lang.String _nonce = ""; 053 private java.lang.String _format = ""; 054 private java.util.Date _issueInstant = null; 055 056 057 /** 058 * Default Constructor. 059 */ 060 protected EncryptableNameIdentifier() {} 061 062 /** 063 * Constructor creates <code>EncryptableNameIdentifier</code> object. 064 * 065 * @param ni the <code>NameIdentifier</code> object to be encrypted. 066 * @throws FSException if there is an error. 067 */ 068 public EncryptableNameIdentifier(NameIdentifier ni) throws FSException { 069 if(ni == null) { 070 throw new FSException("nullInput", null) ; 071 } 072 _nameQualifier = ni.getNameQualifier(); 073 _name = ni.getName(); 074 if(_nameQualifier == null || _name == null) { 075 throw new FSException("nullInput", null) ; 076 } 077 _format = ni.getFormat(); 078 if(_format == null) { 079 throw new FSException("notValidFormat", null) ; 080 } 081 _nonce = FSUtils.generateID(); 082 _issueInstant = new Date(); 083 } 084 085 /** 086 * Consturctor creates <code>EncryptableNameIdentifier</code> object. 087 * 088 * @param name 089 * @param nameQualifier 090 * @param format 091 * @param issueInstant the Issue Instant 092 * @param nonce 093 * @throws FSException if there is an error. 094 */ 095 public EncryptableNameIdentifier(String name,String nameQualifier, 096 String format,Date issueInstant, 097 String nonce ) throws FSException { 098 099 if(name == null || nameQualifier == null || issueInstant == null || 100 format == null || nonce == null) { 101 throw new FSException("nullInput", null) ; 102 } 103 _name = name; 104 _nameQualifier = nameQualifier; 105 _format = format; 106 _nonce = nonce; 107 _issueInstant = issueInstant; 108 } 109 110 111 112 /** 113 * Constructs a <code>EncryptedNameIdentifer</code> element from 114 * the Document Element. 115 * 116 * @param nameIdentifier a <code>org.w3c.dom.Element</code> 117 * representing DOM tree for <code>EncryptableNameIdentifier</code> 118 * object 119 * @throws FSException if it could not process the 120 * <code>org.w3c.dom.Element</code> properly, implying that there 121 * is an error in the sender or in the element definition. 122 */ 123 public EncryptableNameIdentifier(org.w3c.dom.Element nameIdentifier) 124 throws FSException { 125 Element elt = (Element) nameIdentifier; 126 String eltName = elt.getLocalName(); 127 if (eltName == null) { 128 if (FSUtils.debug.messageEnabled()) { 129 FSUtils.debug.message("EncryptableNameIdentifier: local" + 130 " name missing"); 131 } 132 throw new FSException("nullInput", null) ; 133 } 134 if (!(eltName.equals("EncryptableNameIdentifier"))) { 135 if (FSUtils.debug.messageEnabled()) { 136 FSUtils.debug.message("EncryptableNameIdentifier: invalid"+ 137 " root element"); 138 } 139 throw new FSException("invalidElement", null) ; 140 } 141 String read = elt.getAttribute("NameQualifier"); 142 if (read != null) { 143 _nameQualifier = read; 144 } 145 read = elt.getAttribute("Format"); 146 if (read != null) { 147 _format = read; 148 } 149 150 read = elt.getAttribute("Nonce"); 151 if (read != null) { 152 _nonce = read; 153 } 154 155 read = elt.getAttribute("IssueInstant"); 156 if(read != null) { 157 try { 158 _issueInstant = DateUtils.stringToDate(read); 159 } catch (java.text.ParseException pe) { 160 if (FSUtils.debug.messageEnabled()) { 161 FSUtils.debug.message("EncryptableNameIdentifier: "+ 162 "Could not parse issue instant", pe); 163 } 164 throw new FSException("wrongInput", null) ; 165 } 166 } 167 read = XMLUtils.getElementValue(elt); 168 if ((read == null) || (read.length() == 0)) { 169 if (FSUtils.debug.messageEnabled()) { 170 FSUtils.debug.message("EncryptableNameIdentifier: null"+ 171 " input specified"); 172 } 173 throw new FSException("nullInput", null) ; 174 } else { 175 _name = read; 176 } 177 } 178 179 /** 180 * Returns value of the <code>Format</code> attribute. 181 * 182 * @return value of the <code>Format</code> attribute. 183 */ 184 public java.lang.String getFormat() { 185 return _format; 186 } 187 188 189 /** 190 * Sets the <code>Format</code> attribute. 191 * 192 * @param format the value of the <code>Format</code> attribute. 193 * @return true if the operation succeeds. 194 */ 195 public boolean setFormat(java.lang.String format ) { 196 // TODO do I need to restrict the format to those defined 197 // by SAML specification ? 198 if ((format == null) || (format.length() == 0)) { 199 return false; 200 } 201 _format = format; 202 return true; 203 } 204 205 /** 206 * Returns the <code>NameQualifier</code> attribute. 207 * 208 * @return the <code>nameQualifier</code>. 209 */ 210 public java.lang.String getNameQualifier() { 211 return _nameQualifier; 212 } 213 214 215 /** 216 * Sets <code>nameQualifier</code> attribute. 217 * 218 * @param nameQualifier the <code>nameQualifier</code> attribute. 219 * @return true if operation succeeds. 220 */ 221 public boolean setNameQualifier(java.lang.String nameQualifier ) { 222 if ((nameQualifier == null) || (nameQualifier.length() == 0)) { 223 return false; 224 } 225 _nameQualifier=nameQualifier; 226 return true; 227 } 228 229 /** 230 * Sets the name attribute. 231 * 232 * @param name name of the <code>nameQualifier</code>. 233 * @return true if operation succeeds. 234 */ 235 protected boolean setName(java.lang.String name ) { 236 if ((name == null) || (name.length() == 0)) { 237 return false; 238 } 239 _name = name; 240 return true; 241 } 242 243 /** 244 * Returns the name from <code>NameQualifier</code>. 245 * 246 * @return the name from <code>NameQualifier</code>. 247 */ 248 public java.lang.String getName() { 249 return _name; 250 } 251 252 /** 253 * Retunrs the nounce. 254 * 255 * @return the nounce. 256 */ 257 public java.lang.String getNonce() { 258 return _nonce; 259 } 260 261 /** 262 * Returns the Issue Instant. 263 * 264 * @return the Issue Instant. 265 */ 266 public java.util.Date getIssueInstant() { 267 return _issueInstant; 268 } 269 270 /** 271 * Returns a String representation of the element. 272 * 273 * @return A string containing the valid XML for this element 274 * By default name space name is prepended to the element name 275 * example <code><saml:EncryptableNameIdentifier></code>. 276 */ 277 public java.lang.String toString() { 278 // call toString() with includeNS true by default and declareNS false 279 String xml = this.toString(true, false); 280 return xml; 281 } 282 283 /** 284 * Returns String representation of the 285 * <code><EncryptableNameIdentifier></code> element. 286 * 287 * @param includeNS Determines whether or not the namespace qualifier is 288 * prepended to the Element when converted. 289 * @param declareNS Determines whether or not the namespace is declared 290 * within the Element. 291 * @return A string containing the valid XML for this element 292 */ 293 public java.lang.String toString(boolean includeNS, boolean declareNS) { 294 StringBuffer xml = new StringBuffer(3000); 295 String NS=""; 296 String appendNS=""; 297 if (declareNS) { 298 NS=IFSConstants.LIB_12_NAMESPACE_STRING; 299 } 300 if (includeNS) { 301 appendNS=IFSConstants.LIB_PREFIX; 302 } 303 304 String dateStr = null; 305 if(_issueInstant != null) { 306 dateStr = DateUtils.toUTCDateFormat(_issueInstant); 307 } 308 309 xml.append("<").append(appendNS).append("EncryptableNameIdentifier"). 310 append(NS); 311 if ((_nameQualifier != null) && (!(_nameQualifier.length() == 0))) { 312 xml.append(" ").append("NameQualifier").append("=\""). 313 append(_nameQualifier).append("\""); 314 } 315 if ((_format != null) && (!(_format.length() == 0))) { 316 xml.append(" ").append("Format").append("=\"").append(_format). 317 append("\""); 318 } 319 if ((_nonce != null) && (!(_nonce.length() == 0))) { 320 xml.append(" ").append("Nonce").append("=\"").append(_nonce). 321 append("\""); 322 } 323 if ((_issueInstant != null) && (dateStr.length() != 0)) { 324 xml.append(" ").append("IssueInstant").append("=\""). 325 append(dateStr).append("\""); 326 } 327 xml.append(">").append(_name); 328 xml.append("</").append(appendNS).append("EncryptableNameIdentifier"). 329 append(">"); 330 return xml.toString(); 331 } 332}