001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.service;
020:
021: import java.util.Map;
022: import java.util.TreeMap;
023:
024: import org.restlet.data.Encoding;
025: import org.restlet.data.Language;
026: import org.restlet.data.MediaType;
027: import org.restlet.data.Metadata;
028:
029: /**
030: * Service providing access to metadata and their associated extension names.
031: *
032: * @author Jerome Louvel (contact@noelios.com)
033: */
034: public class MetadataService {
035: /** The default encoding for local representations. */
036: private Encoding defaultEncoding;
037:
038: /** The default language for local representations. */
039: private Language defaultLanguage;
040:
041: /** The default media type for local representations. */
042: private MediaType defaultMediaType;
043:
044: /** The mappings from extension names to metadata. */
045: private Map<String, Metadata> metadataMappings;
046:
047: /**
048: * Constructor.
049: */
050: public MetadataService() {
051: this .defaultEncoding = Encoding.IDENTITY;
052: this .defaultLanguage = Language.ENGLISH_US;
053: this .defaultMediaType = MediaType.APPLICATION_OCTET_STREAM;
054: this .metadataMappings = new TreeMap<String, Metadata>();
055: addCommonExtensions();
056: }
057:
058: /**
059: * Adds a common list of associations from extensions to metadata. The list
060: * of languages extensions:<br/>
061: * <ul>
062: * <li>en: English</li>
063: * <li>es: Spanish</li>
064: * <li>fr: French</li>
065: * </ul>
066: * <br/> The list of media type extensions:<br/>
067: * <ul>
068: * <li>css: CSS stylesheet</li>
069: * <li>doc: Microsoft Word document</li>
070: * <li>gif: GIF image</li>
071: * <li>html: HTML document</li>
072: * <li>ico: Windows icon (Favicon)</li>
073: * <li>jpeg, jpg: JPEG image</li>
074: * <li>js: JavaScript document</li>
075: * <li>json: JavaScript Object Notation document</li>
076: * <li>pdf: Adobe PDF document</li>
077: * <li>png: PNG image</li>
078: * <li>ppt: Microsoft Powerpoint document</li>
079: * <li>rdf: Description Framework document</li>
080: * <li>txt: Plain text</li>
081: * <li>swf: Shockwave Flash object</li>
082: * <li>xhtml: XHTML document</li>
083: * <li>xml: XML document</li>
084: * <li>zip: Zip archive</li>
085: * </ul>
086: */
087: public void addCommonExtensions() {
088: addExtension("en", Language.ENGLISH);
089: addExtension("es", Language.SPANISH);
090: addExtension("fr", Language.FRENCH);
091:
092: addExtension("css", MediaType.TEXT_CSS);
093: addExtension("doc", MediaType.APPLICATION_WORD);
094: addExtension("gif", MediaType.IMAGE_GIF);
095: addExtension("html", MediaType.TEXT_HTML);
096: addExtension("ico", MediaType.IMAGE_ICON);
097: addExtension("jpeg", MediaType.IMAGE_JPEG);
098: addExtension("jpg", MediaType.IMAGE_JPEG);
099: addExtension("js", MediaType.APPLICATION_JAVASCRIPT);
100: addExtension("json", MediaType.APPLICATION_JSON);
101: addExtension("pdf", MediaType.APPLICATION_PDF);
102: addExtension("png", MediaType.IMAGE_PNG);
103: addExtension("ppt", MediaType.APPLICATION_POWERPOINT);
104: addExtension("rdf", MediaType.APPLICATION_RDF_XML);
105: addExtension("txt", MediaType.TEXT_PLAIN);
106: addExtension("svg", MediaType.IMAGE_SVG);
107: addExtension("swf", MediaType.APPLICATION_FLASH);
108: addExtension("xhtml", MediaType.APPLICATION_XHTML_XML);
109: addExtension("xml", MediaType.TEXT_XML);
110: addExtension("zip", MediaType.APPLICATION_ZIP);
111: }
112:
113: /**
114: * Maps an extension to some metadata (media type, language or character
115: * set) to an extension.
116: *
117: * @param extension
118: * The extension name.
119: * @param metadata
120: * The metadata to map.
121: */
122: public void addExtension(String extension, Metadata metadata) {
123: this .metadataMappings.put(extension, metadata);
124: }
125:
126: /**
127: * Returns the default encoding for local representations.
128: *
129: * @return The default encoding for local representations.
130: */
131: public Encoding getDefaultEncoding() {
132: return this .defaultEncoding;
133: }
134:
135: /**
136: * Returns the default language for local representations.
137: *
138: * @return The default language for local representations.
139: */
140: public Language getDefaultLanguage() {
141: return this .defaultLanguage;
142: }
143:
144: /**
145: * Returns the default media type for local representations.
146: *
147: * @return The default media type for local representations.
148: */
149: public MediaType getDefaultMediaType() {
150: return this .defaultMediaType;
151: }
152:
153: /**
154: * Returns the first extension mapping to this metadata.
155: *
156: * @param metadata
157: * The metadata to find.
158: * @return The first extension mapping to this metadata.
159: */
160: public String getExtension(Metadata metadata) {
161: for (String extension : getMappings().keySet()) {
162: if (getMetadata(extension).equals(metadata)) {
163: return extension;
164: }
165: }
166:
167: return null;
168: }
169:
170: /**
171: * Returns the mappings from extension names to metadata.
172: *
173: * @return The mappings from extension names to metadata.
174: */
175: public Map<String, Metadata> getMappings() {
176: return this .metadataMappings;
177: }
178:
179: /**
180: * Returns the metadata associated to this extension. It returns null if the
181: * extension was not declared.
182: *
183: * @param extension
184: * The extension name without any delimiter.
185: * @return The metadata associated to this extension.
186: */
187: public Metadata getMetadata(String extension) {
188: return getMappings().get(extension);
189: }
190:
191: /**
192: * Sets the default encoding for local representations.
193: *
194: * @param defaultEncoding
195: * The default encoding for local representations.
196: */
197: public void setDefaultEncoding(Encoding defaultEncoding) {
198: this .defaultEncoding = defaultEncoding;
199: }
200:
201: /**
202: * Sets the default language for local representations.
203: *
204: * @param defaultLanguage
205: * The default language for local representations.
206: */
207: public void setDefaultLanguage(Language defaultLanguage) {
208: this .defaultLanguage = defaultLanguage;
209: }
210:
211: /**
212: * Sets the default media type for local representations.
213: *
214: * @param defaultMediaType
215: * The default media type for local representations.
216: */
217: public void setDefaultMediaType(MediaType defaultMediaType) {
218: this.defaultMediaType = defaultMediaType;
219: }
220:
221: }
|