View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.security.api;
23  
24  import org.opencastproject.util.EqualsUtil;
25  
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.Map.Entry;
31  
32  import javax.xml.bind.annotation.XmlAccessType;
33  import javax.xml.bind.annotation.XmlAccessorType;
34  import javax.xml.bind.annotation.XmlAttribute;
35  import javax.xml.bind.annotation.XmlElement;
36  import javax.xml.bind.annotation.XmlElementWrapper;
37  import javax.xml.bind.annotation.XmlID;
38  import javax.xml.bind.annotation.XmlRootElement;
39  import javax.xml.bind.annotation.XmlType;
40  import javax.xml.bind.annotation.XmlValue;
41  
42  /**
43   * An organization that is hosted on this Opencast instance.
44   */
45  @XmlAccessorType(XmlAccessType.FIELD)
46  @XmlType(name = "organization", namespace = "http://org.opencastproject.security")
47  @XmlRootElement(name = "organization", namespace = "http://org.opencastproject.security")
48  public class JaxbOrganization implements Organization {
49  
50    /** The organizational identifier */
51    @XmlID
52    @XmlAttribute
53    protected String id = null;
54  
55    /** The friendly name of the organization */
56    @XmlElement(name = "name")
57    protected String name = null;
58  
59    /** Server and port mapping */
60    @XmlElement(name = "server")
61    @XmlElementWrapper(name = "servers")
62    protected List<OrgServer> servers = null;
63  
64    /** The local admin role name */
65    @XmlElement(name = "adminRole")
66    protected String adminRole = null;
67  
68    /** The local anonymous role name */
69    @XmlElement(name = "anonymousRole")
70    protected String anonymousRole = null;
71  
72    /** Arbitrary string properties associated with this organization */
73    @XmlElement(name = "property")
74    @XmlElementWrapper(name = "properties")
75    protected List<OrgProperty> properties = null;
76  
77    /**
78     * No-arg constructor needed by JAXB
79     */
80    public JaxbOrganization() {
81    }
82  
83    public JaxbOrganization(String orgId) {
84      this.id = orgId;
85    }
86  
87    /**
88     * Constructs an organization with its attributes.
89     *
90     * @param id
91     *          the unique identifier
92     * @param name
93     *          the friendly name
94     * @param servers
95     *          the hosts names and ports
96     * @param adminRole
97     *          name of the local admin role
98     * @param anonymousRole
99     *          name of the local anonymous role
100    * @param properties
101    *          arbitrary properties defined for this organization, which might include branding, etc.
102    */
103   public JaxbOrganization(String id, String name, Map<String, Integer> servers, String adminRole, String anonymousRole,
104           Map<String, String> properties) {
105     this();
106     this.id = id;
107     this.name = name;
108     this.servers = new ArrayList<JaxbOrganization.OrgServer>();
109     if (servers != null && !servers.isEmpty()) {
110       for (Entry<String, Integer> entry : servers.entrySet()) {
111         this.servers.add(new OrgServer(entry.getKey(), entry.getValue()));
112       }
113     }
114     this.adminRole = adminRole;
115     this.anonymousRole = anonymousRole;
116     this.properties = new ArrayList<JaxbOrganization.OrgProperty>();
117     if (properties != null && !properties.isEmpty()) {
118       for (Entry<String, String> entry : properties.entrySet()) {
119         this.properties.add(new OrgProperty(entry.getKey(), entry.getValue()));
120       }
121     }
122   }
123 
124   /**
125    * Constructs an organization from an organization
126    *
127    * @param org
128    *          the organization
129    */
130   public static JaxbOrganization fromOrganization(Organization org) {
131     if (org instanceof JaxbOrganization)
132       return (JaxbOrganization) org;
133     return new JaxbOrganization(org.getId(), org.getName(), org.getServers(), org.getAdminRole(),
134             org.getAnonymousRole(), org.getProperties());
135   }
136 
137   /**
138    * @see org.opencastproject.security.api.Organization#getId()
139    */
140   @Override
141   public String getId() {
142     return id;
143   }
144 
145   /**
146    * @see org.opencastproject.security.api.Organization#getName()
147    */
148   @Override
149   public String getName() {
150     return name;
151   }
152 
153   /**
154    * @see org.opencastproject.security.api.Organization#getServers()
155    */
156   @Override
157   public Map<String, Integer> getServers() {
158     Map<String, Integer> map = new HashMap<String, Integer>();
159     if (servers != null) {
160       for (OrgServer server : servers) {
161         map.put(server.getName(), server.getPort());
162       }
163     }
164     return map;
165   }
166 
167   /**
168    * @see org.opencastproject.security.api.Organization#getId()
169    */
170   @Override
171   public String getAdminRole() {
172     return adminRole;
173   }
174 
175   /**
176    * @see org.opencastproject.security.api.Organization#getId()
177    */
178   @Override
179   public String getAnonymousRole() {
180     return anonymousRole;
181   }
182 
183   /**
184    * @see org.opencastproject.security.api.Organization#getProperties()
185    */
186   @Override
187   public Map<String, String> getProperties() {
188     Map<String, String> map = new HashMap<String, String>();
189     for (OrgProperty prop : properties) {
190       map.put(prop.getKey(), prop.getValue());
191     }
192     return map;
193   }
194 
195   /**
196    * {@inheritDoc}
197    *
198    * @see java.lang.Object#toString()
199    */
200   @Override
201   public String toString() {
202     return id;
203   }
204 
205   /**
206    * {@inheritDoc}
207    *
208    * @see java.lang.Object#equals(java.lang.Object)
209    */
210   @Override
211   public boolean equals(Object obj) {
212     if (!(obj instanceof Organization))
213       return false;
214     return ((Organization) obj).getId().equals(id);
215   }
216 
217   /**
218    * {@inheritDoc}
219    *
220    * @see java.lang.Object#hashCode()
221    */
222   @Override
223   public int hashCode() {
224     return EqualsUtil.hash(id);
225   }
226 
227   /**
228    * An organization property. To read about why this class is necessary, see http://java.net/jira/browse/JAXB-223
229    */
230   @XmlAccessorType(XmlAccessType.FIELD)
231   @XmlType(name = "server", namespace = "http://org.opencastproject.security")
232   public static class OrgServer {
233 
234     /** The server name */
235     @XmlAttribute
236     protected String name;
237 
238     /** The server port */
239     @XmlAttribute
240     protected int port;
241 
242     /**
243      * No-arg constructor needed by JAXB
244      */
245     public OrgServer() {
246     }
247 
248     /**
249      * Constructs an organization server mapping with a server name and a port.
250      *
251      * @param name
252      *          the name
253      * @param port
254      *          the port
255      */
256     public OrgServer(String name, int port) {
257       this.name = name;
258       this.port = port;
259     }
260 
261     /**
262      * @return the server name
263      */
264     public String getName() {
265       return name;
266     }
267 
268     /**
269      * @return the port
270      */
271     public int getPort() {
272       return port;
273     }
274   }
275 
276   /**
277    * An organization property. To read about why this class is necessary, see http://java.net/jira/browse/JAXB-223
278    */
279   @XmlAccessorType(XmlAccessType.FIELD)
280   @XmlType(name = "property", namespace = "http://org.opencastproject.security")
281   public static class OrgProperty {
282 
283     /** The property key */
284     @XmlAttribute
285     protected String key;
286 
287     /** The property value */
288     @XmlValue
289     protected String value;
290 
291     /**
292      * No-arg constructor needed by JAXB
293      */
294     public OrgProperty() {
295     }
296 
297     /**
298      * Constructs an organization property with a key and a value.
299      *
300      * @param key
301      *          the key
302      * @param value
303      *          the value
304      */
305     public OrgProperty(String key, String value) {
306       this.key = key;
307       this.value = value;
308     }
309 
310     /**
311      * @return the key
312      */
313     public String getKey() {
314       return key;
315     }
316 
317     /**
318      * @return the value
319      */
320     public String getValue() {
321       return value;
322     }
323   }
324 
325 }