All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.restlet.service.MetadataService Maven / Gradle / Ivy

Go to download

This OSGi bundle wraps org.restlet, and com.noelios.restlet ${pkgVersion} jar files.

The newest version!
/**
 * Copyright 2005-2008 Noelios Technologies.
 * 
 * The contents of this file are subject to the terms of the following open
 * source licenses: LGPL 3.0 or LGPL 2.1 or CDDL 1.0 (the "Licenses"). You can
 * select the license that you prefer but you may not use this file except in
 * compliance with one of these Licenses.
 * 
 * You can obtain a copy of the LGPL 3.0 license at
 * http://www.gnu.org/licenses/lgpl-3.0.html
 * 
 * You can obtain a copy of the LGPL 2.1 license at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 * 
 * You can obtain a copy of the CDDL 1.0 license at
 * http://www.sun.com/cddl/cddl.html
 * 
 * See the Licenses for the specific language governing permissions and
 * limitations under the Licenses.
 * 
 * Alternatively, you can obtain a royaltee free commercial license with less
 * limitations, transferable or non-transferable, directly at
 * http://www.noelios.com/products/restlet-engine
 * 
 * Restlet is a registered trademark of Noelios Technologies.
 */

package org.restlet.service;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.restlet.data.Encoding;
import org.restlet.data.Language;
import org.restlet.data.MediaType;
import org.restlet.data.Metadata;

/**
 * Service providing access to metadata and their associated extension names.
 * 
 * @author Jerome Louvel
 */
public class MetadataService extends Service {
    /**
     * Associates an extension name and a metadata.
     * 
     * @author Alex Milowski ([email protected])
     * @author Thierry Boileau
     */
    private class MetadataExtension {
        /** The mapped metadata. */
        private final Metadata metadata;

        /** The name of the extension. */
        private final String name;

        /**
         * Constructor.
         * 
         * @param name
         *            The extension name.
         * @param metadata
         *            The metadata.
         */
        public MetadataExtension(String name, Metadata metadata) {
            this.name = name;
            this.metadata = metadata;
        }

        /**
         * Returns the metadata.
         * 
         * @return the metadata.
         */
        public Metadata getMetadata() {
            return this.metadata;
        }

        /**
         * Returns the extension name.
         * 
         * @return The extension name.
         */
        public String getName() {
            return this.name;
        }

    }

    /** The default encoding for local representations. */
    private volatile Encoding defaultEncoding;

    /** The default language for local representations. */
    private volatile Language defaultLanguage;

    /** The default media type for local representations. */
    private volatile MediaType defaultMediaType;

    /** The list of mappings between extension names and metadata. */
    private final List mappings;

    /**
     * Constructor. Sets the default language to {@link Language#ENGLISH_US},
     * the default encoding to {@link Encoding#IDENTITY} (no encoding) and the
     * default media type to {@link MediaType#APPLICATION_OCTET_STREAM}. It also
     * calls the {@link #addCommonExtensions()} method.
     */
    public MetadataService() {
        this.defaultEncoding = Encoding.IDENTITY;
        this.defaultLanguage = Language.ENGLISH_US;
        this.defaultMediaType = MediaType.APPLICATION_OCTET_STREAM;
        this.mappings = new CopyOnWriteArrayList();
        addCommonExtensions();
    }

/**
     * Adds a common list of associations from extensions to metadata. The list
     * of languages extensions:
*
    *
  • en: English
  • *
  • es: Spanish
  • *
  • fr: French
  • *
*
* The list of media type extensions:
*
    *
  • ai: PostScript document
  • *
  • atom: Atom syndication document
  • *
  • au: AU audio file
  • *
  • bin: Binary file
  • *
  • bmp: Bitmap graphics
  • *
  • class: Java bytecode
  • *
  • css: CSS stylesheet
  • *
  • dib: Device-Independent Bitmap Graphics
  • *
  • doc: Microsoft Word document
  • *
  • dtd: XML Document Type Definition
  • *
  • eps: Encapsulated PostScript
  • *
  • exe: Executable File (Microsoft Corporation)
  • *
  • fmt: FreeMarker encoding
  • *
  • gif: GIF image
  • *
  • hqx: BinHex 4 Compressed Archive (Macintosh)
  • *
  • htm, html: HTML document
  • *
  • ico: Windows icon (Favicon)
  • *
  • jad: Java Application Descriptor file
  • *
  • jar: Java Archive
  • *
  • java: Java source code
  • *
  • jnlp: Java Web start launch file
  • *
  • jpe, jpeg, jpg: JPEG image
  • *
  • js: JavaScript document
  • *
  • jsf: Java Server Faces file
  • *
  • json: JavaScript Object Notation document
  • *
  • kar: Karaoke MIDI file
  • *
  • latex: LaTeX document
  • *
  • man: Manual file
  • *
  • mathml: Mathml XML document
  • *
  • mid, midi: MIDI Audio
  • *
  • mov, qt: QuickTime video clip (Apple Computer, Inc.)
  • *
  • mp2, mp3: MPEG Audio Stream file
  • *
  • mp4: MPEG-4 video file
  • *
  • mpe, mpeg, mpg: MPEG video clip
  • *
  • pdf: Adobe PDF document
  • *
  • png: PNG image
  • *
  • pps, ppt: Microsoft Powerpoint document
  • *
  • ps: PostScript document
  • *
  • rdf: Description Framework document
  • *
  • rnc: Relax NG Schema document, Compact syntax
  • *
  • rng: Relax NG Schema document, XML syntax
  • *
  • rss: RSS file
  • *
  • rtf: Rich Text Format document
  • *
  • sit: StuffIt compressed archive file
  • *
  • snd: Amiga sound
  • *
  • svg: Scalable Vector Graphics file
  • *
  • swf: Adobe Flash file
  • *
  • tar: Tape Archive file
  • *
  • tex: Tex file
  • *
  • tif, tiff: Tagged Image Format File
  • *
  • txt: Plain text
  • *
  • ulw: MU-LAW (US telephony format)
  • *
  • vm: Velocity encoding
  • *
  • vrml: Virtual Reality Modeling Language file
  • *
  • vxml: VoiceXML source file
  • *
  • wadl: Web Application Description Language document
  • *
  • wav: Waveform audio
  • *
  • wrl: Plain text VRML file
  • *
  • xht, xhtml: XHTML document
  • *
  • xls: Microsoft Excel document
  • *
  • xml: XML document
  • *
  • xsd: W3C XML Schema document
  • *
  • xslt: XSL Transform file
  • *
  • xul: XML User Interface Language file
  • *
  • z: UNIX compressed archive file
  • *
  • zip: Zip archive
  • *
*/ public void addCommonExtensions() { addExtension("en", Language.ENGLISH); addExtension("es", Language.SPANISH); addExtension("fr", Language.FRENCH); addExtension("ai", MediaType.APPLICATION_POSTSCRIPT); addExtension("atom", MediaType.APPLICATION_ATOM_XML); addExtension("au", MediaType.AUDIO_BASIC); addExtension("bin", MediaType.APPLICATION_OCTET_STREAM); addExtension("bmp", MediaType.IMAGE_BMP); addExtension("class", MediaType.APPLICATION_JAVA); addExtension("css", MediaType.TEXT_CSS); addExtension("dib", MediaType.IMAGE_BMP); addExtension("doc", MediaType.APPLICATION_WORD); addExtension("dtd", MediaType.APPLICATION_XML_DTD); addExtension("eps", MediaType.APPLICATION_POSTSCRIPT); addExtension("exe", MediaType.APPLICATION_OCTET_STREAM); addExtension("fmt", Encoding.FREEMARKER); addExtension("gif", MediaType.IMAGE_GIF); addExtension("hqx", MediaType.APPLICATION_MAC_BINHEX40); addExtension("htm", MediaType.TEXT_HTML); addExtension("html", MediaType.TEXT_HTML); addExtension("ico", MediaType.IMAGE_ICON); addExtension("jad", MediaType.TEXT_J2ME_APP_DESCRIPTOR); addExtension("jar", MediaType.APPLICATION_JAVA_ARCHIVE); addExtension("java", MediaType.TEXT_PLAIN); addExtension("jnlp", MediaType.APPLICATION_JNLP); addExtension("jpe", MediaType.IMAGE_JPEG); addExtension("jpeg", MediaType.IMAGE_JPEG); addExtension("jpg", MediaType.IMAGE_JPEG); addExtension("js", MediaType.APPLICATION_JAVASCRIPT); addExtension("jsf", MediaType.TEXT_PLAIN); addExtension("json", MediaType.APPLICATION_JSON); addExtension("kar", MediaType.AUDIO_MIDI); addExtension("latex", MediaType.APPLICATION_LATEX); addExtension("man", MediaType.APPLICATION_TROFF_MAN); addExtension("mathml", MediaType.APPLICATION_MATHML_XML); addExtension("mid", MediaType.AUDIO_MIDI); addExtension("midi", MediaType.AUDIO_MIDI); addExtension("mov", MediaType.VIDEO_QUICKTIME); addExtension("mp2", MediaType.AUDIO_MPEG); addExtension("mp3", MediaType.AUDIO_MPEG); addExtension("mp4", MediaType.VIDEO_MP4); addExtension("mpe", MediaType.VIDEO_MPEG); addExtension("mpeg", MediaType.VIDEO_MPEG); addExtension("mpg", MediaType.VIDEO_MPEG); addExtension("pdf", MediaType.APPLICATION_PDF); addExtension("png", MediaType.IMAGE_PNG); addExtension("pps", MediaType.APPLICATION_POWERPOINT); addExtension("ppt", MediaType.APPLICATION_POWERPOINT); addExtension("ps", MediaType.APPLICATION_POSTSCRIPT); addExtension("qt", MediaType.VIDEO_QUICKTIME); addExtension("rdf", MediaType.APPLICATION_RDF_XML); addExtension("rnc", MediaType.APPLICATION_RELAXNG_COMPACT); addExtension("rng", MediaType.APPLICATION_RELAXNG_XML); addExtension("rss", MediaType.APPLICATION_RSS_XML); addExtension("rtf", MediaType.APPLICATION_RTF); addExtension("sit", MediaType.APPLICATION_STUFFIT); addExtension("snd", MediaType.AUDIO_BASIC); addExtension("svg", MediaType.IMAGE_SVG); addExtension("swf", MediaType.APPLICATION_FLASH); addExtension("tar", MediaType.APPLICATION_TAR); addExtension("tex", MediaType.APPLICATION_TEX); addExtension("tif", MediaType.IMAGE_TIFF); addExtension("tiff", MediaType.IMAGE_TIFF); addExtension("txt", MediaType.TEXT_PLAIN, true); addExtension("ulw", MediaType.AUDIO_BASIC); addExtension("vm", Encoding.VELOCITY); addExtension("vrml", MediaType.MODEL_VRML); addExtension("vxml", MediaType.APPLICATION_VOICEXML); addExtension("wadl", MediaType.APPLICATION_WADL_XML); addExtension("wav", MediaType.AUDIO_WAV); addExtension("wrl", MediaType.MODEL_VRML); addExtension("xht", MediaType.APPLICATION_XHTML_XML); addExtension("xhtml", MediaType.APPLICATION_XHTML_XML); addExtension("xls", MediaType.APPLICATION_EXCEL); addExtension("xml", MediaType.TEXT_XML); addExtension("xml", MediaType.APPLICATION_XML); addExtension("xsd", MediaType.APPLICATION_W3C_SCHEMA_XML); addExtension("xslt", MediaType.APPLICATION_W3C_XSLT); addExtension("xul", MediaType.APPLICATION_XUL); addExtension("z", MediaType.APPLICATION_COMPRESS); addExtension("zip", MediaType.APPLICATION_ZIP); } /** * Maps an extension to some metadata (media type, language or character * set) to an extension. * * @param extension * The extension name. * @param metadata * The metadata to map. */ public void addExtension(String extension, Metadata metadata) { addExtension(extension, metadata, false); } /** * Maps an extension to some metadata (media type, language or character * set) to an extension. * * @param extension * The extension name. * @param metadata * The metadata to map. * @param preferred * indicates if this mapping is the preferred one. */ public void addExtension(String extension, Metadata metadata, boolean preferred) { if (preferred) { // Add the mapping at the beginning of the list this.mappings.add(0, new MetadataExtension(extension, metadata)); } else { // Add the mapping at the end of the list this.mappings.add(new MetadataExtension(extension, metadata)); } } /** * clears the mappings for all extensions. */ public void clearExtensions() { this.mappings.clear(); } /** * Returns the default encoding for local representations. * * @return The default encoding for local representations. */ public Encoding getDefaultEncoding() { return this.defaultEncoding; } /** * Returns the default language for local representations. * * @return The default language for local representations. */ public Language getDefaultLanguage() { return this.defaultLanguage; } /** * Returns the default media type for local representations. * * @return The default media type for local representations. */ public MediaType getDefaultMediaType() { return this.defaultMediaType; } /** * Returns the first extension mapping to this metadata. * * @param metadata * The metadata to find. * @return The first extension mapping to this metadata. */ public String getExtension(Metadata metadata) { if (metadata != null) { // Look for the first registered convenient mapping. for (final MetadataExtension metadataExtension : this.mappings) { if (metadata.equals(metadataExtension.getMetadata())) { return metadataExtension.getName(); } } } return null; } /** * Returns the mappings from extension names to metadata. Creates a new * instance if no one has been set. Note that this map is only a snapshot of * the list of mappings. * * @return The mappings from extension names to metadata. * @deprecated . */ @Deprecated public Map getMappings() { final Map result = new TreeMap(); for (final MetadataExtension metadataExtension : this.mappings) { result.put(metadataExtension.getName(), metadataExtension .getMetadata()); } return result; } /** * Returns the metadata associated to this extension. It returns null if the * extension was not declared. * * @param extension * The extension name without any delimiter. * @return The metadata associated to this extension. */ public Metadata getMetadata(String extension) { if (extension != null) { // Look for the first registered convenient mapping. for (final MetadataExtension metadataExtension : this.mappings) { if (extension.equals(metadataExtension.getName())) { return metadataExtension.getMetadata(); } } } return null; } /** * Sets the default encoding for local representations. * * @param defaultEncoding * The default encoding for local representations. */ public void setDefaultEncoding(Encoding defaultEncoding) { this.defaultEncoding = defaultEncoding; } /** * Sets the default language for local representations. * * @param defaultLanguage * The default language for local representations. */ public void setDefaultLanguage(Language defaultLanguage) { this.defaultLanguage = defaultLanguage; } /** * Sets the default media type for local representations. * * @param defaultMediaType * The default media type for local representations. */ public void setDefaultMediaType(MediaType defaultMediaType) { this.defaultMediaType = defaultMediaType; } /** * Sets the mappings from extension names to metadata. * * @param mappings * The mappings from extension names to metadata. * @deprecated */ @Deprecated public void setMappings(Map mappings) { this.mappings.clear(); for (final String extension : mappings.keySet()) { addExtension(extension, mappings.get(extension), false); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy