1    /*
2     * Copyright 2006 :torweg free software group
3     * 
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.accesscontrol.authentication;
19   
20   import java.util.HashMap;
21   import java.util.HashSet;
22   import java.util.Map;
23   import java.util.Set;
24   
25   import org.jdom.Element;
26   import org.torweg.pulse.bundle.Result;
27   
28   /**
29    * The {@code Result} of the {@code Authentication}.
30    * 
31    * @author Christian Schatt
32    * @version $Revision: 1380 $
33    */
34   public class AuthenticationResult implements Result {
35   
36       /**
37        * The serialVersionUID of the {@code AuthenticationResult}.
38        */
39       private static final long serialVersionUID = -4190087654409506672L;
40   
41       /**
42        * The login-status of this {@code AuthenticationResult}.
43        */
44       private boolean loginStatus = false;
45   
46       /**
47        * The name-mappings for the httpParameters of this
48        * {@code AuthenticationResult}.
49        */
50       private Map<String, String> parameterMappings = new HashMap<String, String>();
51   
52       /**
53        * The login-modes of this {@code AuthenticationResult}.
54        */
55       private Set<String> loginModes = new HashSet<String>();
56   
57       /**
58        * The error-code of this {@code AuthenticationResult}.
59        */
60       private Integer errorCode = null;
61   
62       /**
63        * The error-description of this {@code AuthenticationResult}.
64        */
65       private String errorDescription = null;
66   
67       /**
68        * desired redirect URI.
69        */
70       private String redirectURI = null;
71   
72       /**
73        * Sets the login-status of this {@code AuthenticationResult}.
74        * 
75        * @param newLoginStatus
76        *            the new login-status of this {@code AuthenticationResult}
77        */
78       protected final void setLoginStatus(final boolean newLoginStatus) {
79           this.loginStatus = newLoginStatus;
80       }
81   
82       /**
83        * Sets the name-mappings for the httpParameters of this
84        * {@code AuthenticationResult}.
85        * 
86        * @param newParameterMappings
87        *            the new name-mappings for the httpParameters of the
88        *            {@code AuthenticationResult}
89        */
90       protected final void setParameterMappings(
91               final Map<String, String> newParameterMappings) {
92           if (newParameterMappings == null) {
93               throw new NullPointerException(
94                       "AuthenticationResult.parameterMappings"
95                               + " must not be set to null.");
96           }
97           this.parameterMappings = newParameterMappings;
98       }
99   
100      /**
101       * Sets the login-modes of this {@code AuthenticationResult}.
102       * 
103       * @param newLoginModes
104       *            the new login-modes of this {@code AuthenticationResult}
105       */
106      protected final void setLoginModes(final Set<String> newLoginModes) {
107          if (newLoginModes == null) {
108              throw new NullPointerException(
109                      "AuthenticationResult.loginModes must not be set to null.");
110          }
111          this.loginModes = newLoginModes;
112      }
113  
114      /**
115       * Sets the error-code and the error-description of this
116       * {@code AuthenticationResult}. If the parameter newErrorCode is
117       * {@code null}, the error-description will be set to {@code null}
118       * , too, regardless of the parameter newErrorDescription.
119       * 
120       * @param newErrorCode
121       *            the new error-code of this {@code AuthenticationResult}
122       * @param newErrorDescription
123       *            the new error-description of this
124       *            {@code AuthenticationResult}
125       */
126      protected final void setError(final Integer newErrorCode,
127              final String newErrorDescription) {
128          if (newErrorCode != null) {
129              this.errorCode = newErrorCode;
130              this.errorDescription = newErrorDescription;
131          }
132      }
133  
134      /**
135       * Sets the desired redirect URI.
136       * 
137       * @param redirect
138       *            the desired redirect URI
139       */
140      protected void setRedirectURI(final String redirect) {
141          this.redirectURI = redirect;
142      }
143  
144      /**
145       * Serializes the state of this {@code AuthenticationResult} as a JDOM
146       * {@code Element}.
147       * 
148       * @return the state of this {@code AuthenticationResult} as a JDOM
149       *         {@code Element}.
150       * 
151       * @see org.torweg.pulse.bundle.JDOMable#deserializeToJDOM()
152       */
153      public final Element deserializeToJDOM() {
154          Element result = new Element("result").setAttribute("class", this
155                  .getClass().getCanonicalName());
156          if (this.redirectURI != null) {
157              result
158                      .addContent(new Element("redirect")
159                              .setText(this.redirectURI));
160          }
161          result.addContent(new Element("login-status").setAttribute("value",
162                  Boolean.toString(this.loginStatus)));
163          Element mappings = new Element("parameter-mappings");
164          for (Map.Entry<String, String> entry : this.parameterMappings
165                  .entrySet()) {
166              Element mapping = new Element(entry.getKey());
167              mapping.setAttribute("name", entry.getValue());
168              mappings.addContent(mapping);
169          }
170          result.addContent(mappings);
171          Element modes = new Element("login-modes");
172          for (String mode : this.loginModes) {
173              modes.addContent(new Element("login-mode").setAttribute("name",
174                      mode));
175          }
176          result.addContent(modes);
177          if (this.errorCode != null) {
178              result.addContent(new Element("error-code").setAttribute("value",
179                      this.errorCode.toString()).setAttribute("description",
180                      this.errorDescription));
181          }
182          return result;
183      }
184  
185  }
186