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     }
134     return new JaxbOrganization(org.getId(), org.getName(), org.getServers(), org.getAdminRole(),
135             org.getAnonymousRole(), org.getProperties());
136   }
137 
138   /**
139    * @see org.opencastproject.security.api.Organization#getId()
140    */
141   @Override
142   public String getId() {
143     return id;
144   }
145 
146   /**
147    * @see org.opencastproject.security.api.Organization#getName()
148    */
149   @Override
150   public String getName() {
151     return name;
152   }
153 
154   /**
155    * @see org.opencastproject.security.api.Organization#getServers()
156    */
157   @Override
158   public Map<String, Integer> getServers() {
159     Map<String, Integer> map = new HashMap<String, Integer>();
160     if (servers != null) {
161       for (OrgServer server : servers) {
162         map.put(server.getName(), server.getPort());
163       }
164     }
165     return map;
166   }
167 
168   /**
169    * @see org.opencastproject.security.api.Organization#getId()
170    */
171   @Override
172   public String getAdminRole() {
173     return adminRole;
174   }
175 
176   /**
177    * @see org.opencastproject.security.api.Organization#getId()
178    */
179   @Override
180   public String getAnonymousRole() {
181     return anonymousRole;
182   }
183 
184   /**
185    * @see org.opencastproject.security.api.Organization#getProperties()
186    */
187   @Override
188   public Map<String, String> getProperties() {
189     Map<String, String> map = new HashMap<String, String>();
190     for (OrgProperty prop : properties) {
191       map.put(prop.getKey(), prop.getValue());
192     }
193     return map;
194   }
195 
196   /**
197    * {@inheritDoc}
198    *
199    * @see java.lang.Object#toString()
200    */
201   @Override
202   public String toString() {
203     return id;
204   }
205 
206   /**
207    * {@inheritDoc}
208    *
209    * @see java.lang.Object#equals(java.lang.Object)
210    */
211   @Override
212   public boolean equals(Object obj) {
213     if (!(obj instanceof Organization)) {
214       return false;
215     }
216     return ((Organization) obj).getId().equals(id);
217   }
218 
219   /**
220    * {@inheritDoc}
221    *
222    * @see java.lang.Object#hashCode()
223    */
224   @Override
225   public int hashCode() {
226     return EqualsUtil.hash(id);
227   }
228 
229   /**
230    * An organization property. To read about why this class is necessary, see http://java.net/jira/browse/JAXB-223
231    */
232   @XmlAccessorType(XmlAccessType.FIELD)
233   @XmlType(name = "server", namespace = "http://org.opencastproject.security")
234   public static class OrgServer {
235 
236     /** The server name */
237     @XmlAttribute
238     protected String name;
239 
240     /** The server port */
241     @XmlAttribute
242     protected int port;
243 
244     /**
245      * No-arg constructor needed by JAXB
246      */
247     public OrgServer() {
248     }
249 
250     /**
251      * Constructs an organization server mapping with a server name and a port.
252      *
253      * @param name
254      *          the name
255      * @param port
256      *          the port
257      */
258     public OrgServer(String name, int port) {
259       this.name = name;
260       this.port = port;
261     }
262 
263     /**
264      * @return the server name
265      */
266     public String getName() {
267       return name;
268     }
269 
270     /**
271      * @return the port
272      */
273     public int getPort() {
274       return port;
275     }
276   }
277 
278   /**
279    * An organization property. To read about why this class is necessary, see http://java.net/jira/browse/JAXB-223
280    */
281   @XmlAccessorType(XmlAccessType.FIELD)
282   @XmlType(name = "property", namespace = "http://org.opencastproject.security")
283   public static class OrgProperty {
284 
285     /** The property key */
286     @XmlAttribute
287     protected String key;
288 
289     /** The property value */
290     @XmlValue
291     protected String value;
292 
293     /**
294      * No-arg constructor needed by JAXB
295      */
296     public OrgProperty() {
297     }
298 
299     /**
300      * Constructs an organization property with a key and a value.
301      *
302      * @param key
303      *          the key
304      * @param value
305      *          the value
306      */
307     public OrgProperty(String key, String value) {
308       this.key = key;
309       this.value = value;
310     }
311 
312     /**
313      * @return the key
314      */
315     public String getKey() {
316       return key;
317     }
318 
319     /**
320      * @return the value
321      */
322     public String getValue() {
323       return value;
324     }
325   }
326 
327 }