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: SessionContextStatement.java,v 1.2 2008/06/25 05:47:22 qcheng Exp $ 026 * 027 */ 028 029 030package com.sun.identity.liberty.ws.security; 031 032import com.sun.identity.federation.common.IFSConstants; 033import com.sun.identity.liberty.ws.common.wsse.WSSEConstants; 034 035import com.sun.identity.saml.assertion.Subject; 036import com.sun.identity.saml.assertion.SubjectStatement; 037 038import com.sun.identity.saml.common.SAMLConstants; 039import com.sun.identity.saml.common.SAMLException; 040import com.sun.identity.saml.common.SAMLRequesterException; 041import com.sun.identity.saml.common.SAMLUtils; 042 043import org.w3c.dom.Element; 044import org.w3c.dom.Node; 045import org.w3c.dom.NodeList; 046 047/** 048 * The <code>SessionContextStatement</code> element conveys session status 049 * of an entity to another system entity within the body of an 050 * <code><saml:assertion></code> element. 051 * 052 * @supported.all.api 053 */ 054public class SessionContextStatement extends SubjectStatement { 055 /** 056 * The Statement is an Session Context Statement. 057 */ 058 public final static int SESSIONCONTEXT_STATEMENT = 5; 059 protected ProxySubject _proxySubject = null; 060 protected SessionContext _sessionContext = null; 061 062 /** 063 * Constructs a <code>SessionContextStatement</code> object from a 064 * <code>SessionContext</code> object and a <code>Subject</code> object. 065 * 066 * @param sessionContext <code>SessionContext</code> object. 067 * @param subject <code>Subject</code> object. 068 * @throws SAMLException if <code>sessionContext</code> is null or subject 069 * is null. 070 */ 071 public SessionContextStatement(SessionContext sessionContext, 072 Subject subject) 073 throws SAMLException { 074 if ((sessionContext == null) || (subject ==null)) { 075 SAMLUtils.debug.message("SessionContextStatement: " + 076 "SessionContext is null!"); 077 throw new SAMLRequesterException( 078 SAMLUtils.bundle.getString("nullInput")); 079 } 080 _sessionContext = sessionContext; 081 this._subject = subject; 082 } 083 084 /** 085 * Constructs a <code>SessionContextStatement</code> object from a 086 * <code>SessionContext</code> object, a <code>proxySubject</code> and 087 * a <code>Subject</code> object. 088 * 089 * @param sessionContext <code>SessionContext</code> object. 090 * @param proxySubject <code>ProxySubject</code> object. 091 * @param subject <code>Subject</code> object. 092 * @throws SAMLException if <code>sessionContext</code> is null or 093 * subject is null. 094 */ 095 public SessionContextStatement(SessionContext sessionContext, 096 ProxySubject proxySubject, 097 Subject subject) 098 throws SAMLException { 099 if ((sessionContext == null) || (subject ==null)) { 100 SAMLUtils.debug.message("SessionContextStatement: " + 101 "SessionContext is null!"); 102 throw new SAMLRequesterException( 103 SAMLUtils.bundle.getString("nullInput")); 104 } 105 _sessionContext = sessionContext; 106 _proxySubject = proxySubject; 107 this._subject = subject; 108 } 109 110 /** 111 * Constructs a <code>SessionContextStatement</code> object from a DOM 112 * element. 113 * 114 * @param element the Document Element 115 * @throws SAMLException if there is an error in the sender or in 116 * the element definition. 117 */ 118 public SessionContextStatement(Element element)throws SAMLException { 119 // make sure input is not null 120 if (element == null) { 121 SAMLUtils.debug.message("AttributeStatement: null input."); 122 throw new SAMLRequesterException( 123 SAMLUtils.bundle.getString("nullInput")); 124 } 125 // check if it's an SessionContextStatement 126 boolean valid = SAMLUtils.checkStatement(element, 127 "SessionContextStatement"); 128 if (!valid) { 129 SAMLUtils.debug.message("SessionContextStatement: Wrong input."); 130 throw new SAMLRequesterException( 131 SAMLUtils.bundle.getString("wrongInput")); 132 } 133 134 //Handle the children elements of SessionContextStatement 135 NodeList nodes = element.getChildNodes(); 136 int nodeCount = nodes.getLength(); 137 if (nodeCount > 0) { 138 for (int i = 0; i < nodeCount; i++) { 139 Node currentNode = nodes.item(i); 140 if (currentNode.getNodeType() == Node.ELEMENT_NODE) { 141 String tagName = currentNode.getLocalName(); 142 String tagNS = currentNode.getNamespaceURI(); 143 if ((tagName == null) || tagName.length() == 0 || 144 tagNS == null || tagNS.length() == 0) { 145 if (SAMLUtils.debug.messageEnabled()) { 146 SAMLUtils.debug.message("SessionContextStatement:" 147 + " The tag name or tag namespace of child" 148 + " element is either null or empty."); 149 } 150 throw new SAMLRequesterException( 151 SAMLUtils.bundle.getString("nullInput")); 152 } 153 if (tagName.equals("Subject") && 154 tagNS.equals(SAMLConstants.assertionSAMLNameSpaceURI)) { 155 if (this._subject != null) { 156 if (SAMLUtils.debug.messageEnabled()) { 157 SAMLUtils.debug.message("SessionContext" + 158 "Statement should only contain " + 159 "one subject"); 160 } 161 throw new SAMLRequesterException( 162 SAMLUtils.bundle.getString("oneElement")); 163 164 } else { 165 this._subject = new Subject((Element) currentNode); 166 } 167 } else if (tagName.equals("ProxySubject") && 168 tagNS.equals(WSSEConstants.NS_SEC)) { 169 if (_proxySubject != null) { 170 if (SAMLUtils.debug.messageEnabled()) { 171 SAMLUtils.debug.message("SessionContext" + 172 "Statement should only contain " + 173 "one ProxySubject"); 174 } 175 throw new SAMLRequesterException( 176 SAMLUtils.bundle.getString("oneElement")); 177 } else { 178 _proxySubject = 179 new ProxySubject((Element) currentNode); 180 } 181 } else if (tagName.equals("SessionContext") && 182 tagNS.equals(WSSEConstants.NS_SEC)) { 183 if (_sessionContext != null) { 184 if (SAMLUtils.debug.messageEnabled()) { 185 SAMLUtils.debug.message("SessionContext" + 186 "Statement should only contain " + 187 "one SessionContext"); 188 } 189 throw new SAMLRequesterException( 190 SAMLUtils.bundle.getString("oneElement")); 191 } else { 192 _sessionContext = 193 new SessionContext((Element) currentNode); 194 } 195 } else { 196 if (SAMLUtils.debug.messageEnabled()) { 197 SAMLUtils.debug.message("SessionContextStatement:" 198 + "Wrong element " + tagName + " included."); 199 } 200 throw new SAMLRequesterException( 201 SAMLUtils.bundle.getString("wrongInput")); 202 } 203 } // end of if (currentNode.getNodeType() == Node.ELEMENT_NODE) 204 } // end of for loop 205 } // end of if (nodeCount > 0) 206 } 207 208 /** 209 * Return the <code>ProxySubject</code> in the 210 * <code>SessionContextStatement</code>. 211 * 212 * @return <code>ProxySubject</code>. 213 */ 214 public ProxySubject getProxySubject() { 215 return _proxySubject; 216 } 217 218 /** 219 * Returns the <code>SessionContext</code> in the 220 * <code>SessionContextStatement</code>. 221 * 222 * @return <code>SessionContext</code> 223 */ 224 public SessionContext getSessionContext() { 225 return _sessionContext; 226 } 227 228 /** 229 * Sets the <code>ProxySubject</code> for 230 * <code>SessionContextStatement</code>. 231 * 232 * @param proxySubject the object to be set. 233 * @return true if the operation is successful. 234 */ 235 public boolean setProxySubject(ProxySubject proxySubject) { 236 if (proxySubject == null) { 237 if (SAMLUtils.debug.messageEnabled()) { 238 SAMLUtils.debug.message("ResourceAccessStatement: " + 239 "setResourceID:Input is null."); 240 } 241 return false; 242 } 243 _proxySubject = proxySubject; 244 return true; 245 } 246 247 248 /** 249 * Returns the real type of the Statement. 250 * 251 * @return An integer which represents <code>SessionContextStatement</code> 252 * internally. 253 */ 254 public int getStatementType() { 255 return SESSIONCONTEXT_STATEMENT; 256 } 257 258 /** 259 * Returns a String representation of <code>SessionContextStatement</code>. 260 * 261 * @return String representation of the <code>SessionContextStatement</code>. 262 * object. 263 */ 264 public String toString() { 265 return toString(true, false); 266 } 267 268 /** 269 * Returns a String representation of the 270 * <code>ResourceAccessStatement</code>. 271 * 272 * @param includeNS Determines whether or not the namespace qualifier is 273 * prepended to the Element when converted. 274 * @param declareNS Determines whether or not the namespace is declared 275 * within the Element. 276 * @return String representation of the 277 * <code><saml:ResourceAccessStatement></code> element. 278 */ 279 public String toString(boolean includeNS, boolean declareNS) { 280 StringBuffer xml = new StringBuffer(1000); 281 String prefix = ""; 282 String secprefix = ""; 283 String libprefix = ""; 284 String uri = ""; 285 String securi = ""; 286 287 if (includeNS) { 288 prefix = SAMLConstants.ASSERTION_PREFIX; 289 libprefix = IFSConstants.LIB_PREFIX; 290 secprefix = WSSEConstants.TAG_SEC + ":"; 291 } 292 if (declareNS) { 293 uri = SAMLConstants.assertionDeclareStr; 294 securi = " " + WSSEConstants.TAG_XMLNS + ":" + 295 WSSEConstants.TAG_SEC + "=" + "\"" + 296 WSSEConstants.NS_SEC + "\""; 297 } 298 299 try { 300 xml.append("<").append(secprefix). 301 append(WSSEConstants.TAG_SESSIONCONTEXTSTATEMENT). 302 append(securi).append(">\n"); 303 xml.append(this._subject.toString(includeNS, true)); 304 if (_proxySubject != null) { 305 xml.append(_proxySubject.toString(includeNS, declareNS)); 306 } 307 xml.append(_sessionContext.toXMLString(includeNS, declareNS)); 308 xml.append("</").append(secprefix). 309 append("SessionContextStatement>"); 310 } catch (Exception e) { 311 return null; 312 } 313 314 return(xml.toString()); 315 } 316} 317