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: FSNameRegistrationResponse.java,v 1.3 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.saml.common.SAMLConstants;
035import com.sun.identity.saml.common.SAMLException;
036import com.sun.identity.saml.common.SAMLResponderException;
037import com.sun.identity.saml.common.SAMLVersionMismatchException;
038import com.sun.identity.saml.protocol.AbstractResponse;
039import com.sun.identity.saml.protocol.Status;
040import com.sun.identity.saml.protocol.StatusCode;
041import com.sun.identity.saml.xmlsig.XMLSignatureManager;
042import com.sun.identity.shared.DateUtils;
043import com.sun.identity.shared.encode.Base64;
044import com.sun.identity.shared.encode.URLEncDec;
045import com.sun.identity.shared.xml.XMLUtils;
046import java.text.ParseException;
047import java.util.Date;
048import java.util.List;
049import javax.servlet.http.HttpServletRequest;
050import org.w3c.dom.Document;
051import org.w3c.dom.Element;
052import org.w3c.dom.Node;
053import org.w3c.dom.NodeList;
054
055/**
056 * This class has methods to create <code>NameRegistrationResponse</code>
057 * object.
058 *
059 * @supported.all.api
060 * @deprecated since 12.0.0
061 */
062@Deprecated
063
064public class FSNameRegistrationResponse extends AbstractResponse {
065    private String providerId = null;
066    private String relayState = "";
067    private Status status = null;
068    protected String xmlString        = null;
069    protected String signatureString        = null;
070    protected String id = null;
071    protected int minorVersion = 0;
072    
073    /**
074     * Default Constructor.
075     */
076    public FSNameRegistrationResponse() {
077        try {
078            setIssueInstant(new Date());
079            StatusCode statusCode = new StatusCode(IFSConstants.SAML_SUCCESS);
080            status = new Status(statusCode);
081        } catch(Exception e){
082            FSUtils.debug.error("FSNameRegistrationResponse.Constructor", e);
083        }
084    }
085    
086    /**
087     * Returns the value of <code>RelayState</code> attribute.
088     *
089     * @return the value of <code>RelayState</code> attribute.
090     * @see #setRelayState(String)
091     */
092    public String getRelayState(){
093        return relayState;
094    }
095    
096    /**
097     * Set the value of <code>RelayState</code> attribute.
098     *
099     * @param relayState the value of <code>RelayState</code> attribute.
100     * @see #getRelayState()
101     */
102    public void setRelayState(String relayState){
103        this.relayState = relayState;
104    }
105    
106    /**
107     * Returns the value of <code>id</code> attribute.
108     *
109     * @return the value of <code>id</code> attribute.
110     * @see #setID(String)
111     */
112    
113    public String getID(){
114        return id;
115    }
116    
117    /**
118     * Sets the value of <code>id</code> attribute.
119     *
120     * @param id the value of <code>id</code> attribute.
121     * @see #getID()
122     */
123    public void setID(String id){
124        this.id = id;
125    }
126    
127    /**
128     * Returns the value of <code>ProviderID</code> attribute.
129     *
130     * @return the value of <code>ProviderID</code> attribute.
131     * @see #setProviderId(String).
132     */
133    public String getProviderId(){
134        return providerId;
135    }
136    
137    /**
138     * Sets the value of providerID attribute.
139     *
140     * @param providerId the value of providerID attribute.
141     * @see #getProviderId()
142     */
143    public void setProviderId(String providerId){
144        this.providerId = providerId;
145    }
146    
147    /**
148     * Returns signed <code>XML</code> representation of this
149     * object.
150     *
151     * @return xmlString signed <code>XML</code> representation of this
152     *         object.
153     */
154    public String getSignedXMLString(){
155        return xmlString;
156    }
157    
158    /**
159     * Returns the signed <code>NameRegistrationResponse</code> string.
160     *
161     * @return signatureString the signed <code>NameRegistrationResponse</code>
162     *         string.
163     */
164    public String getSignatureString(){
165        return signatureString;
166    }
167    
168    /**
169     * Constructor creates the <code>FSNameRegistrationResponse</code> object.
170     *
171     * @param responseID the value of <code>ResponseID</code> attribute.
172     * @param inResponseTo the value of <code>InResponseTo</code> attribute.
173     * @param status the <code>Status</code> object.
174     * @param providerId the value of <code>ProviderID</code> attribute.
175     * @param relayState the value of <code>RelayState</code> attribute.
176     * @throws FSMsgException if there is an error creating this object.
177     */
178    public FSNameRegistrationResponse(String responseID, String inResponseTo,
179            Status status, String providerId,
180            String relayState) throws FSMsgException {
181        if ((responseID == null) || (responseID.length() == 0)) {
182            this.responseID = FSUtils.generateID();
183            if (this.responseID == null) {
184                throw new FSMsgException("errorGenerateID",null);
185            }
186        } else {
187            this.responseID = responseID;
188        }
189        if (inResponseTo == null) {
190            FSUtils.debug.message("Response: inResponseTo is null.");
191            throw new FSMsgException("nullInput",null);
192        }
193        this.inResponseTo = inResponseTo;
194        if (status == null) {
195            FSUtils.debug.message("Response: missing <Status>.");
196            throw new FSMsgException("missingElement",null);
197        }
198        this.status = status;
199        this.providerId = providerId;
200        this.relayState = relayState;
201        setIssueInstant(new Date());
202    }
203    
204    /**
205     * Constructor creates the <code>FSNameRegistrationResponse</code> object
206     * from Document Element.
207     *
208     * @param root the Document Element objec.t
209     * @throws FSMsgException if there is an error creating this object.
210     * @throws SAMLException if there is an error creating this object.
211     */
212    public FSNameRegistrationResponse(Element root) throws
213            FSMsgException, SAMLException {
214        if (root == null) {
215            FSUtils.debug.message("FSNameRegistrationResponse.parseXML:" +
216                    " null input.");
217            throw new FSMsgException("nullInput",null);
218        }
219        String tag = null;
220        if (((tag = root.getLocalName()) == null) ||
221                (!tag.equals("RegisterNameIdentifierResponse"))) {
222            FSUtils.debug.error("FSNameRegistrationResponse.parseXML: wrong"+
223                    " input.");
224            throw new FSMsgException("wrongInput",null);
225        }
226        
227        id = root.getAttribute("id");
228        
229        // Attribute ResponseID
230        responseID = root.getAttribute("ResponseID");
231        if ((responseID == null) || (responseID.length() == 0)) {
232            FSUtils.debug.error("FSNameRegistrationResponse.parseXML: "
233                    + "Reponse doesn't have ResponseID.");
234            String[] args = { IFSConstants.RESPONSE_ID };
235            throw new FSMsgException("missingAttribute",args);
236        }
237        
238        parseMajorVersion(root.getAttribute("MajorVersion"));
239        parseMinorVersion(root.getAttribute("MinorVersion"));
240        
241        // Attribute InResponseTo
242        inResponseTo = root.getAttribute("InResponseTo");
243        if (inResponseTo == null) {
244            FSUtils.debug.error("FSNameRegistrationResponse.parseXML: "
245                    + "Response doesn't have InResponseTo.");
246            String[] args = { IFSConstants.IN_RESPONSE_TO };
247            throw new FSMsgException("missingAttribute",args);
248        }
249        // Attribute IssueInstant
250        String instantString = root.getAttribute("IssueInstant");
251        if ((instantString == null) || (instantString.length() == 0)) {
252            FSUtils.debug.error("FSNameRegistrationResponse(Element): " +
253                    "missing IssueInstant");
254            String[] args = { IFSConstants.ISSUE_INSTANT };
255            throw new FSMsgException("missingAttribute",args);
256        } else {
257            try {
258                issueInstant = DateUtils.stringToDate(instantString);
259            } catch (ParseException e) {
260                FSUtils.debug.error(
261                        "FSNameRegistrationResponse(Element):" +
262                        " could not parse IssueInstant:" , e);
263                throw new FSMsgException("wrongInput",null);
264            }
265        }
266        
267        NodeList nl = root.getChildNodes();
268        Node child;
269        String childName;
270        int length = nl.getLength();
271        for (int i = 0; i < length; i++) {
272            child = nl.item(i);
273            if ((childName = child.getLocalName()) != null) {
274                if (childName.equals("Status")) {
275                    if (status != null) {
276                        FSUtils.debug.error("FSNameRegistrationResponse: " +
277                                "included more than one <Status>");
278                        throw new FSMsgException("moreElement",null);
279                    }
280                    status = new Status((Element) child);
281                } else if (childName.equals(IFSConstants.SIGNATURE)) {
282                } else if (childName.equals("ProviderID")) {
283                    if (providerId != null) {
284                        FSUtils.debug.error("FSNameRegistrationResponse:" +
285                                " included more than one providerId");
286                        throw new FSMsgException("moreElement",null);
287                    }
288                    providerId = XMLUtils.getElementValue((Element) child);
289                } else if (childName.equals("RelayState")) {
290                    relayState = XMLUtils.getElementValue((Element) child);
291                }else {
292                    FSUtils.debug.error("FSNameRegistrationResponse: " +
293                            "included wrong element:" + childName);
294                    throw new FSMsgException("wrongInput",null);
295                }
296            } // end if childName != null
297        } // end for loop
298        
299        if (status == null) {
300            FSUtils.debug.message(
301                    "FSNameRegistrationResponse: missing element <Status>.");
302            throw new FSMsgException("oneElement",null);
303        }
304        
305        if (providerId == null) {
306            FSUtils.debug.message(
307                    "FSNameRegistrationResponse: missing element providerId.");
308            throw new FSMsgException("oneElement",null);
309        }
310        
311        //check for signature
312        List signs = XMLUtils.getElementsByTagNameNS1(root,
313                SAMLConstants.XMLSIG_NAMESPACE_URI,
314                SAMLConstants.XMLSIG_ELEMENT_NAME);
315        int signsSize = signs.size();
316        if (signsSize == 1) {
317            Element elem = (Element)signs.get(0);
318            setSignature(elem);
319            xmlString = XMLUtils.print(root);
320            signed = true;
321        } else if (signsSize != 0) {
322            FSUtils.debug.error("FSNameRegistrationResponse(Element): " +
323                    "included more than one Signature element.");
324            throw new FSMsgException("moreElement",null);
325        }
326    }
327    
328    /**
329     * Returns the <code>MinorVersion</code>.
330     *
331     * @return the <code>MinorVersion</code>.
332     * @see #setMinorVersion(int)
333     */
334    public int getMinorVersion() {
335        return minorVersion;
336    }
337    
338    /**
339     * Sets the <code>MinorVersion</code>.
340     *
341     * @param version the <code>MinorVersion</code>.
342     * @see #getMinorVersion()
343     */
344    
345    public void setMinorVersion(int version) {
346        minorVersion = version;
347    }
348    
349    /**
350     * Returns the Response <code>Status</code>.
351     *
352     * @return the Response <code>Status</code>.
353     * @see #setStatus(Status)
354     */
355    public Status getStatus() {
356        return status;
357    }
358    
359    
360    /**
361     * Sets the Response <code>Status</code>.
362     *
363     * @param status the Response <code>Status</code object.
364     * @see #getStatus
365     */
366    public void setStatus(Status status) {
367        this.status=status;
368    }
369    
370    /**
371     * Sets the <code>MajorVersion</code> by parsing the version string.
372     *
373     * @param majorVer a String representing the <code>MajorVersion</code> to
374     *        be set.
375     * @throws FSMsgException on error.
376     * @throws SAMLException when the version mismatchs.
377     */
378    private void parseMajorVersion(
379            String majorVer
380            ) throws FSMsgException, SAMLException {
381        try {
382            majorVersion = Integer.parseInt(majorVer);
383        } catch (NumberFormatException e) {
384            FSUtils.debug.error("Response(Element): invalid MajorVersion", e);
385            throw new FSMsgException("wrongInput",null);
386        }
387        
388        if (majorVersion != SAMLConstants.PROTOCOL_MAJOR_VERSION) {
389            if (majorVersion > SAMLConstants.PROTOCOL_MAJOR_VERSION) {
390                if (FSUtils.debug.messageEnabled()) {
391                    FSUtils.debug.message("Response(Element):MajorVersion of"
392                            + " the Response is too high.");
393                }
394                throw new SAMLVersionMismatchException(FSUtils.BUNDLE_NAME,
395                        "responseVersionTooHigh",null);
396            } else {
397                if (FSUtils.debug.messageEnabled()) {
398                    FSUtils.debug.message("Response(Element):MajorVersion of"
399                            + " the Response is too low.");
400                }
401                throw new SAMLVersionMismatchException(FSUtils.BUNDLE_NAME,
402                        "responseVersionTooLow",null);
403            }
404        }
405    }
406    
407    /**
408     * Sets the <code>MinorVersion</code> by parsing the version string.
409     *
410     * @param minorVer a String representing the <code>MinorVersion</code> to
411     *        be set.
412     * @throws SAMLException when the version mismatchs.
413     */
414    private void parseMinorVersion(String minorVer) throws FSMsgException {
415        try {
416            minorVersion = Integer.parseInt(minorVer);
417        } catch (NumberFormatException e) {
418            if (FSUtils.debug.messageEnabled()) {
419                FSUtils.debug.message(
420                        "FSRegisResp(Element): "
421                        + "invalid MinorVersion", e);
422            }
423            throw new FSMsgException("wrongInput",null);
424        }
425        
426        if (minorVersion > IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) {
427            FSUtils.debug.error("FSRegisResp(Element):MinorVersion of"
428                    + " the Response is too high.");
429            throw new FSMsgException("responseVersionTooHigh",null);
430        } else if (minorVersion < IFSConstants.FF_11_PROTOCOL_MINOR_VERSION) {
431            FSUtils.debug.error("FSRegisResp(Element):MinorVersion of"
432                    + " the Response is too low.");
433            throw new FSMsgException("responseVersionTooLow",null);
434        }
435        
436    }
437    
438    /**
439     * Returns the <code>FSNameRegistrationResponse</code> object.
440     *
441     * @param xml the XML string to be parsed.
442     * @return <code>FSNameRegistrationResponsee</code> object created from
443     *         the XML string.
444     * @throws FSMsgException if there is error creating the object.
445     */
446    public static FSNameRegistrationResponse parseXML(String xml)
447    throws FSMsgException {
448        try{
449            Document doc = XMLUtils.toDOMDocument(xml, FSUtils.debug);
450            Element root = doc.getDocumentElement();
451            return new FSNameRegistrationResponse(root);
452        }catch(SAMLException ex){
453            if (FSUtils.debug.messageEnabled()) {
454                FSUtils.debug.message("FSNameRegistrationResponse.parseXML: "
455                        + "Error while parsing input xml string");
456            }
457            throw new FSMsgException("parseError", null, ex);
458        }
459        
460    }
461    
462    /**
463     * Returns the string representation of this object.
464     * This method translates the response to an XML string.
465     *
466     * @return An XML String representing the Response.
467     * @throws FSMsgException on error.
468     */
469    public String toXMLString()  throws FSMsgException {
470        return this.toXMLString(true, true);
471    }
472    
473    /**
474     * Returns a String representation of the Logout Response.
475     *
476     * @param includeNS : Determines whether or not the namespace qualifier
477     *        is prepended to the Element when converted
478     * @param declareNS : Determines whether or not the namespace is declared
479     *        within the Element.
480     * @return a string containing the valid XML for this element
481     * @throws FSMsgException if there is an error converting
482     *         this object to a string.
483     */
484    public String toXMLString(boolean includeNS, boolean declareNS)
485    throws FSMsgException {
486        return toXMLString(includeNS, declareNS, false);
487    }
488    
489    /**
490     * Returns a String representation of the Logout Response.
491     *
492     * @param includeNS Determines whether or not the namespace qualifier
493     *        is prepended to the Element when converted
494     * @param declareNS Determines whether or not the namespace is declared
495     *        within the Element.
496     * @param includeHeader Determines whether the output include the xml
497     *        declaration header.
498     * @return a string containing the valid XML for this element
499     * @throws FSMsgException if there is an error converting
500     *         this object to a string.
501     */
502    public String toXMLString(boolean includeNS, boolean declareNS,
503            boolean includeHeader) throws FSMsgException {
504        StringBuffer xml = new StringBuffer(300);
505        if (includeHeader) {
506            xml.append("<?xml version=\"1.0\" encoding=\"").
507                    append(SAMLConstants.DEFAULT_ENCODING).append("\" ?>\n");
508        }
509        String prefixLIB = "";
510        String uriLIB = "";
511        if (includeNS) {
512            prefixLIB = IFSConstants.LIB_PREFIX;
513        }
514        
515        if (declareNS) {
516            if (minorVersion == IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) {
517                uriLIB = IFSConstants.LIB_12_NAMESPACE_STRING;
518            } else {
519                uriLIB = IFSConstants.LIB_NAMESPACE_STRING;
520            }
521        }
522        
523        String instantString = DateUtils.toUTCDateFormat(issueInstant);
524        
525        if ((providerId == null) || (providerId.length() == 0)){
526            FSUtils.debug.error("FSNameRegistrationResponse.toXMLString: "
527                    + "providerId is null in the response with responseId:"
528                    + responseID);
529            throw new FSMsgException("nullProviderID",null);
530        }
531        
532        if ((responseID != null) && (inResponseTo != null)){
533            xml.append("<").append(prefixLIB).
534                    append("RegisterNameIdentifierResponse").append(uriLIB);
535            if (minorVersion == IFSConstants.FF_11_PROTOCOL_MINOR_VERSION &&
536                    id != null && !(id.length() == 0)) {
537                xml.append(" id=\"").append(id).append("\" ");
538            }
539            xml.append(" ResponseID=\"").append(responseID).append("\" ").
540                    append(" InResponseTo=\"").append(inResponseTo).
541                    append("\" ").
542                    append(" MajorVersion=\"").append(majorVersion).
543                    append("\" ").
544                    append(" MinorVersion=\"").append(minorVersion).
545                    append("\" ").
546                    append(" IssueInstant=\"").append(instantString).
547                    append("\" ").
548                    append(">");
549        }
550        
551        if (signed) {
552            if (signatureString != null) {
553                xml.append(signatureString);
554            } else if (signature != null) {
555                signatureString = XMLUtils.print(signature);
556                xml.append(signatureString);
557            }
558        }
559        
560        if (providerId != null) {
561            xml.append("<").append(prefixLIB).append("ProviderID").append(">").
562                    append(providerId).
563                    append("</").append(prefixLIB).append("ProviderID").
564                    append(">");
565        }
566        
567        if (status != null) {
568            xml.append(status.toString(includeNS, true));
569        }
570        
571        if (relayState != null) {
572            xml.append("<").append(prefixLIB).append("RelayState").
573                    append(">").append(relayState).
574                    append("</").append(prefixLIB).
575                    append("RelayState").append(">");
576        }
577        
578        xml.append("</").append(prefixLIB).
579                append("RegisterNameIdentifierResponse>");
580        return xml.toString();
581    }
582    
583    /**
584     * Returns <code>FSNameRegistrationResponse</code> object. The object
585     * is created by parsing an Base64 encode Name Registration Response
586     * string.
587     *
588     * @param encodedRes the encoded response string
589     * @throws FSMsgException if there is an error
590     *         creating this object.
591     * @throws SAMLException if there is an error
592     *         creating this object.
593     */
594    public static FSNameRegistrationResponse parseBASE64EncodedString(
595            String encodedRes) throws FSMsgException, SAMLException {
596        if (encodedRes != null){
597            String decodedNameRegRes = new String(Base64.decode(encodedRes));
598            if (FSUtils.debug.messageEnabled()) {
599                FSUtils.debug.message("FSNameRegistrationResponse."
600                        + "parseBASE64EncodedString: decoded input string: "
601                        + decodedNameRegRes);
602            }
603            return parseXML(decodedNameRegRes);
604        } else{
605            if (FSUtils.debug.messageEnabled()) {
606                FSUtils.debug.message(
607                        "FSNameRegistrationResponse.parseBASE64EncodedString"
608                        + ": null String passed in as argument.");
609            }
610            throw new FSMsgException("nullInput",null);
611        }
612    }
613    
614    /**
615     * Returns a Base64 Encoded String.
616     *
617     * @return a Base64 Encoded String.
618     * @throws FSMsgException if there is an error encoding
619     *         the string.
620     */
621    public String toBASE64EncodedString() throws FSMsgException {
622        if ((responseID == null) || (responseID.length() == 0)){
623            responseID = FSUtils.generateID();
624            if (responseID == null) {
625                FSUtils.debug.error(
626                        "FSNameRegistrationResponse.toBASE64EncodedString: "
627                        + "couldn't generate ResponseID.");
628                throw new FSMsgException("errorGenerateID",null);
629            }
630        }
631        return Base64.encode(this.toXMLString().getBytes());
632    }
633    
634    /**
635     * Signs the Name Registration Response.
636     *
637     * @param certAlias the Certificate Alias.
638     * @throws SAMLException if this object cannot be signed.
639     */
640    public void signXML(String certAlias) throws SAMLException {
641        FSUtils.debug.message("FSNameRegistrationResponse.signXML: Called");
642        if (signed) {
643            if (FSUtils.debug.messageEnabled()) {
644                FSUtils.debug.message("FSNameRegistrationResponse.signXML: "
645                        + "the assertion is already signed.");
646            }
647            throw new SAMLResponderException(FSUtils.BUNDLE_NAME,
648                    "alreadySigned",null);
649        }
650        if (certAlias == null || certAlias.length() == 0) {
651            throw new SAMLResponderException(FSUtils.BUNDLE_NAME,
652                    "cannotFindCertAlias",null);
653        }
654        try{
655            XMLSignatureManager manager = XMLSignatureManager.getInstance();
656            if (minorVersion == IFSConstants.FF_11_PROTOCOL_MINOR_VERSION) {
657                signatureString = manager.signXML(this.toXMLString(true, true),
658                        certAlias, IFSConstants.DEF_SIG_ALGO, IFSConstants.ID,
659                        this.id, false);
660            } else if (minorVersion ==
661                    IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) {
662                signatureString = manager.signXML(this.toXMLString(true, true),
663                        certAlias, IFSConstants.DEF_SIG_ALGO,
664                        IFSConstants.RESPONSE_ID,
665                        this.getResponseID(), false);
666            } else {
667                if (FSUtils.debug.messageEnabled()) {
668                    FSUtils.debug.message("invalid minor version.");
669                }
670            }
671            signature =
672                    XMLUtils.toDOMDocument(signatureString, FSUtils.debug)
673                    .getDocumentElement();
674            
675            signed = true;
676            xmlString = this.toXMLString(true, true);
677        }catch(Exception e){
678            throw new SAMLResponderException(FSUtils.BUNDLE_NAME,
679                                             "signError",null);
680        }
681    }
682    
683    /**
684     * Unsupported operation.
685     */
686    public void signXML() throws SAMLException {
687        throw new SAMLException(FSUtils.BUNDLE_NAME,
688                               "unsupportedOperation",null);
689    }
690    
691    /**
692     * Sets the Signature.
693     *
694     * @param elem the Document Element.
695     * @return true if success otherwise false.
696     */
697    public boolean setSignature(Element elem) {
698        signatureString = XMLUtils.print(elem);
699        return super.setSignature(elem);
700    }
701    
702    /**
703     * Returns an URL Encoded String.
704     *
705     * @return a url encoded query string.
706     * @throws FSMsgException if there is an error.
707     */
708    public String toURLEncodedQueryString() throws FSMsgException {
709        if ((providerId == null) || (providerId.length() == 0)){
710            FSUtils.debug.error("FSNameRegistrationResponse."
711                    + "toURLEncodedQueryString: providerId is null in "
712                    + "the response ");
713            throw new FSMsgException("nullProviderIdInRequest",null);
714        }
715        if ((responseID == null) || (responseID.length() == 0)){
716            responseID = FSUtils.generateID();
717            if (responseID == null) {
718                FSUtils.debug.error("FSNameRegistrationRequest."
719                        + "toURLEncodedQueryString: couldn't generate "
720                        + "responseID.");
721                throw new FSMsgException("errorGenerateID",null);
722            }
723        }
724        StringBuffer urlEncodedAuthnReq = new StringBuffer(300);
725        urlEncodedAuthnReq.append("ResponseID=").
726                append(URLEncDec.encode(responseID)).
727                append(IFSConstants.AMPERSAND);
728        urlEncodedAuthnReq.append("MajorVersion=").
729                append(majorVersion).append(IFSConstants.AMPERSAND);
730        urlEncodedAuthnReq.append("MinorVersion=").
731                append(minorVersion).append(IFSConstants.AMPERSAND);
732        urlEncodedAuthnReq.append("InResponseTo=").
733                append(URLEncDec.encode(inResponseTo)).
734                append(IFSConstants.AMPERSAND);
735        
736        if (issueInstant != null){
737            urlEncodedAuthnReq.append("IssueInstant=")
738            .append(URLEncDec.encode(
739                    DateUtils.toUTCDateFormat(issueInstant)))
740                    .append(IFSConstants.AMPERSAND);
741        } else {
742            FSUtils.debug.error("FSNameRegistrationRequest."
743                    + "toURLEncodedQueryString: issueInstant missing");
744            String[] args = { IFSConstants.ISSUE_INSTANT };
745            throw new FSMsgException("missingAttribute",args);
746        }
747        if (providerId != null && !providerId.equals("")) {
748            urlEncodedAuthnReq.append("ProviderID=").
749                    append(URLEncDec.encode(providerId)).
750                    append(IFSConstants.AMPERSAND);
751        }
752        
753        if (relayState != null && relayState.length() > 0) {
754            urlEncodedAuthnReq.append("RelayState=").
755                    append(URLEncDec.encode(relayState)).
756                    append(IFSConstants.AMPERSAND);
757        }
758        
759        if (status != null) {
760            urlEncodedAuthnReq.append("Value=");
761            urlEncodedAuthnReq.append(
762                    URLEncDec.encode(status.getStatusCode().getValue())).
763                    append(IFSConstants.AMPERSAND);
764        }
765        
766        return urlEncodedAuthnReq.toString();
767    }
768    
769    
770    /**
771     * Returns <code>FSNameRegistrationLogoutResponse</code> object. The
772     * object is creating by parsing the <code>HttpServletRequest</code>
773     * object.
774     *
775     * @param request the <code>HttpServletRequest</code> object.
776     * @throws FSMsgException if there is an error
777     *         creating this object.
778     */
779    public static FSNameRegistrationResponse parseURLEncodedRequest(
780            HttpServletRequest request) throws FSMsgException, SAMLException {
781        FSNameRegistrationResponse retNameRegistrationResponse =
782                new FSNameRegistrationResponse();
783        try {
784            FSUtils.debug.message("checking minor version");
785            retNameRegistrationResponse.majorVersion =
786                    Integer.parseInt(request.getParameter("MajorVersion"));
787            retNameRegistrationResponse.minorVersion =
788                    Integer.parseInt(request.getParameter("MinorVersion"));
789        } catch(NumberFormatException ex){
790            FSUtils.debug.error("FSNameRegistrationResponse.parseURL" +
791                    "EncodedRequest: version parsing error:" + ex);
792            throw new FSMsgException("invalidNumber",null);
793        }
794        
795        if (request.getParameter("ResponseID")!= null) {
796            retNameRegistrationResponse.responseID =
797                    request.getParameter("ResponseID");
798        } else {
799            FSUtils.debug.error("FSNameRegistrationResponse.parseURL" +
800                    "EncodedRequest: Response ID is null" );
801            String[] args = { IFSConstants.RESPONSE_ID };
802            throw new FSMsgException("missingAttribute",args);
803        }
804        
805        String instantString = request.getParameter("IssueInstant");
806        if (instantString == null || instantString.length() == 0) {
807            FSUtils.debug.error("FSNameRegistrationResponse.parseURL" +
808                    "EncodedRequest: Issue Instant is null" );
809            String[] args = { IFSConstants.ISSUE_INSTANT };
810            throw new FSMsgException("missingAttribute",args);
811        }
812        try{
813            retNameRegistrationResponse.issueInstant =
814                    DateUtils.stringToDate(instantString);
815        } catch (ParseException e){
816            FSUtils.debug.error("FSNameRegistrationResponse.parseURL" +
817                    "EncodedRequest: Can not parse Issue Instant", e);
818            throw new FSMsgException("parseError",null);
819        }
820        if (request.getParameter("ProviderID")!= null){
821            retNameRegistrationResponse.providerId =
822                    request.getParameter("ProviderID");
823        } else {
824            FSUtils.debug.error("FSNameRegistrationResponse.parseURL" +
825                    "EncodedRequest: Provider ID is null " );
826            throw new FSMsgException("missingElement",null);
827        }
828        
829        if (request.getParameter("RelayState")!= null){
830            retNameRegistrationResponse.relayState =
831                    request.getParameter("RelayState");
832        }
833        if (request.getParameter("InResponseTo")!= null){
834            retNameRegistrationResponse.inResponseTo =
835                    request.getParameter("InResponseTo");
836        }
837        
838        if (request.getParameter("Value") != null){
839            FSUtils.debug.message("Status : " + request.getParameter("Value"));
840            StatusCode statusCode =
841                    new StatusCode(request.getParameter("Value"));
842            retNameRegistrationResponse.status = new Status(statusCode);
843        } else {
844            FSUtils.debug.error("FSNameRegistrationResponse.parseURL" +
845                    "EncodedRequest: Status Value is  null " );
846            throw new FSMsgException("missingElement",null);
847        }
848        
849        FSUtils.debug.message("Returning registration response Object");
850        return retNameRegistrationResponse;
851    }
852}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.