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




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.