View Javadoc

1   /*
2    * The MIT License
3    *
4    * Original work sponsored and donated by National Board of e-Health (NSI), Denmark (http://www.nsi.dk)
5    *
6    * Copyright (C) 2011 National Board of e-Health (NSI), Denmark (http://www.nsi.dk)
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   *
26   * $HeadURL: https://svn.softwareborsen.dk/sosi/trunk/modules/seal/src/main/java/dk/sosi/seal/model/Reply.java $
27   * $Id: Reply.java 8697 2011-09-02 10:33:55Z chg@lakeside.dk $
28   */
29  package dk.sosi.seal.model;
30  
31  import dk.sosi.seal.SOSIFactory;
32  import dk.sosi.seal.model.dombuilders.SAMLReplyDOMBuilder;
33  import dk.sosi.seal.vault.CredentialVault;
34  import org.w3c.dom.Document;
35  
36  /**
37   * Interface for SOSI/DGWS SOAP replies.
38   *
39   * @author Jan Riis
40   * @author $LastChangedBy: chg@lakeside.dk $
41   * @since 1.0
42   */
43  public class Reply extends Message {
44  
45  	private String requestID;
46  	private String flowStatus;
47  	private String faultCode;
48  	private String faultString;
49  
50  	/**
51  	 * @param dgwsVersion
52  	 * 			  The DGWS version this message adheres to
53  	 * @param requestID
54  	 *            ID of the corresponding request message
55  	 * @param flowID
56  	 *            A unique flow ID for this reply message.
57  	 * @param flowStatus
58  	 *            Status for this reply message.
59  	 * @param factory
60  	 *            SOSIFactory to construct realizations of the SOSI abstractions
61  	 *            in the seal component.
62  	 */
63  	public Reply(String dgwsVersion, String requestID, String flowID, String flowStatus, SOSIFactory factory) {
64  
65  		super(dgwsVersion, flowID, factory);
66  		this.requestID = requestID;
67  		this.flowStatus = flowStatus;
68  		this.isFault = false;
69  	}
70  
71  	public Reply(String dgwsVersion, String requestID, String flowID, String faultCode, String faultString, SOSIFactory factory) {
72  
73  		super(dgwsVersion, flowID, factory);
74  		this.requestID = requestID;
75  		this.isFault = true;
76  		this.faultCode = faultCode;
77  		this.faultString = faultString;
78  	}
79  
80  	/**
81  	 * Returns the ID of the corresponding request message. This is a read-only
82  	 * attribute.
83  	 */
84  	public String getRequestID() {
85  
86  		return requestID;
87  	}
88  
89  	/**
90  	 * Returns the status for this reply message.
91  	 */
92  	public String getFlowStatus() {
93  
94  		if (isFault()) {
95  			// TODO: is this true (JRI)?
96  			throw new ModelException("The reply represents a Fault. No FlowStatus available");
97  		}
98  		return flowStatus;
99  	}
100 
101 	/**
102 	 * Sets the status for this reply message.
103 	 */
104 	public void setFlowStatus(String flowStatus) {
105 
106 		this.flowStatus = flowStatus;
107 	}
108 
109 	/**
110 	 * Sets the ID of the corresponding request message.
111 	 */
112 	public void setRequestID(String reqID) {
113 
114 		requestID = reqID;
115 	}
116 
117 	public boolean isFault() {
118 
119 		return isFault;
120 	}
121 
122 	public String getFaultCode() {
123 		if (!isFault()) {
124 			throw new ModelException("The reply is not a Fault. No FaultCode available");
125 		}
126 		return (faultCode==null)?"":faultCode;
127 	}
128 
129 	public String getFaultString() {
130 		if (!isFault()) {
131 			throw new ModelException("The reply is not a Fault. No FaultString available");
132 		}
133 		return (faultString==null)?"":faultString;
134 	}
135 
136 	public void setIDCard(IDCard idCard) {
137 		if(isFault())
138 			throw new ModelException("IDCards cannot be attached to error replies");
139 		super.setIDCard(idCard);
140 	}
141 
142 	/**
143 	 * Overrides Message.equals(). hashCode() is overwritten in superclass.
144 	 */
145 	public boolean equals(Object obj) { // NOPMD
146 
147 		if (!super.equals(obj) || obj.getClass() != getClass())
148 			return false; // NOPMD
149 		Reply reply = (Reply) obj;
150 		if (!getRequestID().equals(reply.getRequestID()))
151 			return false; // NOPMD
152 		if (isFault()) {
153 			if (!getFaultCode().equals(reply.getFaultCode()))
154 				return false; // NOPMD
155 			if (!getFaultString().equals(reply.getFaultString()))
156 				return false; // NOPMD
157 		} else {
158 			if (!getFlowStatus().equals(reply.getFlowStatus()))
159 				return false; // NOPMD
160 		}
161 		return true;
162 	}
163 
164 	/**
165 	 * Generates a new XML document using the given reply message.
166 	 */
167 	protected Document regenerateDOM(Document doc, CredentialVault vault) {
168 
169 		return new SAMLReplyDOMBuilder(doc, this, vault).buildDOMDocument();
170 	}
171 }