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: FSScoping.java,v 1.2 2008/06/25 05:46:45 qcheng Exp $ 026 * Portions Copyrighted 2014 ForgeRock AS 027 */ 028 029package com.sun.identity.federation.message; 030 031import static org.forgerock.http.util.Uris.urlEncodeQueryParameterNameOrValue; 032 033import com.sun.identity.federation.common.FSUtils; 034import com.sun.identity.federation.common.IFSConstants; 035import com.sun.identity.federation.message.common.FSMsgException; 036import com.sun.identity.federation.message.common.IDPEntries; 037import com.sun.identity.federation.message.common.IDPEntry; 038import com.sun.identity.shared.xml.XMLUtils; 039import java.util.ArrayList; 040import java.util.Iterator; 041import java.util.List; 042import javax.servlet.http.HttpServletRequest; 043import org.w3c.dom.Element; 044import org.w3c.dom.Node; 045import org.w3c.dom.NodeList; 046 047 048/** 049 * This class <code>FSScoping</code> creates scoping element for the 050 * authentication request. 051 * 052 * @supported.all.api 053 * @deprecated since 12.0.0 054 */ 055@Deprecated 056public class FSScoping { 057 058 private int proxyCount = -1; 059 private FSIDPList idpList = null; 060 061 /** 062 * Default constructor 063 */ 064 public FSScoping() {} 065 066 /** 067 * Constructor creates <code>FSScoping</code> object. 068 * 069 * @param idpList the <code>FSIDPList</code> object. 070 * @param proxyCount the number of proxies 071 */ 072 public FSScoping(FSIDPList idpList, int proxyCount) { 073 this.idpList = idpList; 074 this.proxyCount = proxyCount; 075 } 076 077 /** 078 * Constructor creates <code>FSScoping</code> object from 079 * the Document Element. 080 * 081 * @param root the Document Element . 082 * @throws FSMsgException if there is a failure creating this object. 083 */ 084 public FSScoping(Element root) throws FSMsgException { 085 if(root == null) { 086 FSUtils.debug.error("FSScoping(Element): null input"); 087 throw new FSMsgException("nullInput", null); 088 } 089 String tagName = root.getLocalName(); 090 if(tagName == null || !tagName.equals("Scoping")) { 091 FSUtils.debug.error("FSScoping(Element): wrong input"); 092 throw new FSMsgException("wrongInput", null); 093 } 094 NodeList childNodes = root.getChildNodes(); 095 int length = childNodes.getLength(); 096 for (int i=0; i < length; i++) { 097 Node child = childNodes.item(i); 098 String nodeName = child.getLocalName(); 099 if(nodeName == null) { 100 continue; 101 } 102 if(nodeName.equals("ProxyCount")) { 103 String count = XMLUtils.getElementValue((Element)child); 104 try { 105 proxyCount = Integer.parseInt(count); 106 } catch (NumberFormatException ne) { 107 FSUtils.debug.error("FSScoping(Element): invalid proxy" + 108 "Count", ne); 109 throw new FSMsgException("wrongInput", null); 110 } 111 } else if(nodeName.equals("IDPList")) { 112 idpList = new FSIDPList((Element)child); 113 } 114 } 115 } 116 117 /** 118 * Sets the proxy count. 119 * 120 * @param count number of proxies 121 */ 122 public void setProxyCount(int count) { 123 proxyCount = count; 124 } 125 126 /** 127 * Returns the proxy count. 128 * 129 * @return number of proxies. 130 */ 131 public int getProxyCount() { 132 return proxyCount; 133 } 134 135 /** 136 * Sets preferred ordered List of IDPs that is known to SP for proxying. 137 * 138 * @param idpList the <code>FSIDPList</code> object. 139 */ 140 public void setIDPList(FSIDPList idpList) { 141 this.idpList = idpList; 142 } 143 144 /** 145 * Returns the preferred IDPs list in an authentication request. 146 * 147 * @return the <code>FSIDPList</code> object. 148 */ 149 public FSIDPList getIDPList() { 150 return idpList; 151 } 152 153 /** 154 * Returns a <code>XML</code> string representation of this object. 155 * 156 * @return XML String representing this object. 157 * @throws FSMsgException if there is an error creating 158 * the XML string or if the required elements to create 159 * the string do not conform to the schema. 160 */ 161 162 public String toXMLString() throws FSMsgException { 163 return toXMLString(true, true); 164 } 165 166 /** 167 * Creates a String representation of this object. 168 * 169 * @param includeNS : Determines whether or not the namespace qualifier 170 * is prepended to the Element when converted 171 * @param declareNS : Determines whether or not the namespace is declared 172 * within the Element. 173 * @return string containing the valid XML for this element. 174 * @throws FSMsgException if there is an error. 175 */ 176 public String toXMLString(boolean includeNS, boolean declareNS) 177 throws FSMsgException { 178 StringBuffer xml = new StringBuffer(300); 179 String prefix = ""; 180 String uri = ""; 181 if(includeNS) { 182 prefix = IFSConstants.LIB_PREFIX; 183 } 184 if(declareNS) { 185 uri = IFSConstants.LIB_12_NAMESPACE_STRING; 186 } 187 xml.append("<").append(prefix).append("Scoping") 188 .append(uri).append(">\n"); 189 if(proxyCount >= 0) { 190 xml.append("<").append(prefix).append("ProxyCount").append(">") 191 .append(proxyCount).append("</").append(prefix) 192 .append("ProxyCount").append(">\n"); 193 } 194 if(idpList != null) { 195 xml.append(idpList.toXMLString(true, false)); 196 } 197 xml.append("</").append(prefix).append("Scoping").append(">\n"); 198 return xml.toString(); 199 } 200 201 /** 202 * Returns an URL Encoded String. 203 * 204 * @return a url encoded query string. 205 * @throws FSMsgException if there is an error. 206 */ 207 public String toURLEncodedQueryString() throws FSMsgException { 208 209 if(proxyCount == -1) { 210 FSUtils.debug.error("FSScoping.toURLEncodedQueryString: " + 211 "proxyCount is not defined."); 212 throw new FSMsgException("proxyCountNotDefined",null); 213 } 214 215 StringBuffer sb = new StringBuffer(100); 216 sb.append("ProxyCount=").append(proxyCount).append("&"); 217 if (idpList != null) { 218 IDPEntries entries = idpList.getIDPEntries(); 219 if(entries != null) { 220 List idps = entries.getIDPEntryList(); 221 if(idps != null && idps.size() != 0) { 222 Iterator iter = idps.iterator(); 223 StringBuffer strProviders = new StringBuffer(100); 224 String space = ""; 225 while(iter.hasNext()) { 226 IDPEntry entry = (IDPEntry)iter.next(); 227 String providerID = entry.getProviderID(); 228 strProviders.append(space).append(providerID); 229 space = " "; 230 } 231 sb.append("IDPEntries=").append( 232 urlEncodeQueryParameterNameOrValue(strProviders.toString())); 233 } 234 } 235 } 236 sb.append(IFSConstants.AMPERSAND); 237 return sb.toString(); 238 239 } 240 241 /** 242 * Returns <code>FSScoping</code> object. The 243 * object is creating by parsing the <code>HttpServletRequest</code> 244 * object. 245 * 246 * @param request the <code>HttpServletRequest</code> object. 247 * @throws FSMsgException if there is an error creating this object. 248 */ 249 public static FSScoping parseURLEncodedRequest(HttpServletRequest request) { 250 251 if (request == null) { 252 return null; 253 } 254 String count = request.getParameter("ProxyCount"); 255 if(count == null) { 256 return null; 257 } 258 int proxyCount = -1; 259 try { 260 proxyCount = Integer.parseInt(count); 261 } catch (NumberFormatException ne) { 262 FSUtils.debug.error("FSScoping.parseURLEncodedRequest:" + 263 "proxyCount can not be parsed."); 264 return null; 265 } 266 267 FSScoping scoping = new FSScoping(); 268 scoping.setProxyCount(proxyCount); 269 270 String[] idps = request.getParameterValues("IDPEntries"); 271 if (idps == null || idps.length == 0) { 272 return scoping; 273 } 274 275 List list = new ArrayList(); 276 for (int i=0; i < idps.length; i++) { 277 String providerID = idps[i]; 278 IDPEntry entry = new IDPEntry(providerID, null, null); 279 list.add(entry); 280 } 281 IDPEntries entries = new IDPEntries(list); 282 FSIDPList idpsList = new FSIDPList(entries, null); 283 scoping.setIDPList(idpsList); 284 285 return scoping; 286 } 287}