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.runtimeinfo.rest;
23  
24  import javax.ws.rs.core.MediaType;
25  
26  /**
27   * Represents an output format for a REST endpoint.
28   */
29  public final class RestFormatData {
30    /**
31     * Default URL for the JSON format.
32     */
33    public static final String JSON_URL = "http://www.json.org/";
34  
35    /**
36     * Default URL for the XML format.
37     */
38    public static final String XML_URL = "http://www.w3.org/XML/";
39  
40    /**
41     * Name of the format, the value should be a constant from <a
42     * href="http://jackson.codehaus.org/javadoc/jax-rs/1.0/javax/ws/rs/core/MediaType.html"
43     * >javax.ws.rs.core.MediaType</a> or ExtendedMediaType (org.opencastproject.util.doc.rest.ExtendedMediaType).
44     */
45    private String name;
46  
47    /**
48     * URL to a page providing more information of the format. Currently only JSON and XML have a default URL.
49     */
50    private String url;
51  
52    /**
53     * Constructor that accepts a format name and finds the format's corresponding default URL and description. Currently
54     * only JSON and XML have a default URL.
55     *
56     * @param name
57     *          the format name, the value should be a constant from <a
58     *          href="http://jackson.codehaus.org/javadoc/jax-rs/1.0/javax/ws/rs/core/MediaType.html"
59     *          >javax.ws.rs.core.MediaType</a> or ExtendedMediaType
60     *          (org.opencastproject.util.doc.rest.ExtendedMediaType).
61     *
62     * @throws IllegalArgumentException
63     *           when name is null
64     */
65    public RestFormatData(String name) throws IllegalArgumentException {
66      if (name == null) {
67        throw new IllegalArgumentException("Name must not be null.");
68      }
69      this.name = name;
70      if ((name.equalsIgnoreCase(MediaType.TEXT_XML)) || (name.equalsIgnoreCase(MediaType.APPLICATION_XML))) {
71        url = XML_URL;
72      } else if (name.equalsIgnoreCase(MediaType.APPLICATION_JSON)) {
73        url = JSON_URL;
74      }
75    }
76  
77    /**
78     * Return a string representation of this object.
79     *
80     * @return a string representation of this object
81     */
82    @Override
83    public String toString() {
84      return name + ":(" + url + ")";
85    }
86  
87    /**
88     * Return the name of this format.
89     *
90     * @return the name of this format
91     */
92    public String getName() {
93      return name;
94    }
95  
96    /**
97     * Return the default URL of this format.
98     *
99     * @return the default URL of this format.
100    */
101   public String getUrl() {
102     return url;
103   }
104 
105 }