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.data;
020:
021: /**
022: * Protocol used by client and server connectors. Connectors enable the
023: * communication between components by implementing standard protocols.
024: *
025: * @author Jerome Louvel (contact@noelios.com)
026: */
027: public final class Protocol extends Metadata {
028:
029: /** Indicates that the port number is undefined. */
030: public static final int UNKNOWN_PORT = -1;
031:
032: /** All protocols wildcard. */
033: public static final Protocol ALL = new Protocol("all", "ALL",
034: "Wildcard for all protocols", UNKNOWN_PORT);
035:
036: /**
037: * AJP 1.3 protocol to communicate with Apache HTTP server or Microsoft IIS.
038: */
039: public static final Protocol AJP = new Protocol("ajp", "AJP",
040: "Apache Jakarta Protocol", 8009);
041:
042: /**
043: * CLAP (ClassLoader Access Protocol) is a custom scheme to access to
044: * representations via classloaders. Example URI:
045: * "clap://thread/org/restlet/Restlet.class".<br>
046: * <br>
047: * In order to work, CLAP requires a client connector provided by the core
048: * Restlet engine.
049: *
050: * @see org.restlet.data.LocalReference
051: */
052: public static final Protocol CLAP = new Protocol("clap", "CLAP",
053: "Class Loader Access Protocol", UNKNOWN_PORT);
054:
055: /** Local Web Archive access protocol. */
056: public static final Protocol WAR = new Protocol("war", "WAR",
057: "Web Archive Access Protocol", UNKNOWN_PORT);
058:
059: /** Local file system access protocol. */
060: public static final Protocol FILE = new Protocol("file", "FILE",
061: "Local File System Protocol", UNKNOWN_PORT);
062:
063: /**
064: * FILE is a standard scheme to access to representations stored in the file
065: * system (locally most of the time). Example URI:
066: * "file:///D/root/index.html".<br>
067: * <br>
068: * In order to work, FILE requires a client connector provided by the core
069: * Restlet engine.
070: *
071: * @see org.restlet.data.LocalReference
072: */
073: public static final Protocol FTP = new Protocol("ftp", "FTP",
074: "File Transfer Protocol", 21);
075:
076: /** HTTP protocol. */
077: public static final Protocol HTTP = new Protocol("http", "HTTP",
078: "HyperText Transport Protocol", 80);
079:
080: /** HTTPS protocol (via SSL socket). */
081: public static final Protocol HTTPS = new Protocol("https", "HTTPS",
082: "HyperText Transport Protocol (Secure)", 443);
083:
084: /**
085: * JAR (Java ARchive) is a common scheme to access to representations inside
086: * archive files. Example URI:
087: * "jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class".
088: *
089: * @see org.restlet.data.LocalReference
090: */
091: public static final Protocol JAR = new Protocol("jar", "JAR",
092: "Java ARchive", UNKNOWN_PORT);
093:
094: /** JDBC protocol. */
095: public static final Protocol JDBC = new Protocol("jdbc", "JDBC",
096: "Java DataBase Connectivity", UNKNOWN_PORT);
097:
098: /** SMTP protocol. */
099: public static final Protocol SMTP = new Protocol("smtp", "SMTP",
100: "Simple Mail Transfer Protocol", 25);
101:
102: /** SMTP with STARTTLS protocol (started with a plain socket). */
103: public static final Protocol SMTP_STARTTLS = new Protocol(
104: "smtp",
105: "SMTP_STARTTLS",
106: "Simple Mail Transfer Protocol (starting a TLS encryption)",
107: 25);
108:
109: /** SMTPS protocol (via SSL/TLS socket). */
110: public static final Protocol SMTPS = new Protocol("smtps", "SMTPS",
111: "Simple Mail Transfer Protocol (Secure)", 465);
112:
113: /**
114: * Creates the protocol associated to a URI scheme name. If an existing
115: * constant exists then it is returned, otherwise a new instance is created.
116: *
117: * @param schemeName
118: * The scheme name.
119: * @return The associated protocol.
120: */
121: public static Protocol valueOf(final String schemeName) {
122: Protocol result = null;
123:
124: if (schemeName != null) {
125: if (schemeName.equalsIgnoreCase(AJP.getSchemeName()))
126: result = AJP;
127: else if (schemeName.equalsIgnoreCase(WAR.getSchemeName()))
128: result = WAR;
129: else if (schemeName.equalsIgnoreCase(FILE.getSchemeName()))
130: result = FILE;
131: else if (schemeName.equalsIgnoreCase(HTTP.getSchemeName()))
132: result = HTTP;
133: else if (schemeName.equalsIgnoreCase(HTTPS.getSchemeName()))
134: result = HTTPS;
135: else if (schemeName.equalsIgnoreCase(JDBC.getSchemeName()))
136: result = JDBC;
137: else if (schemeName.equalsIgnoreCase(SMTP.getSchemeName()))
138: result = SMTP;
139: else if (schemeName.equalsIgnoreCase(SMTPS.getSchemeName()))
140: result = SMTPS;
141: else
142: result = new Protocol(schemeName);
143: }
144:
145: return result;
146: }
147:
148: /** The scheme name. */
149: private String schemeName;
150:
151: /** The default port if known or -1. */
152: private int defaultPort;
153:
154: /**
155: * Constructor.
156: *
157: * @param schemeName
158: * The scheme name.
159: */
160: public Protocol(final String schemeName) {
161: this (schemeName, schemeName.toUpperCase(), schemeName
162: .toUpperCase()
163: + " Protocol", UNKNOWN_PORT);
164: }
165:
166: /**
167: * Constructor.
168: *
169: * @param schemeName
170: * The scheme name.
171: * @param name
172: * The unique name.
173: * @param description
174: * The description.
175: * @param defaultPort
176: * The default port.
177: */
178: public Protocol(final String schemeName, final String name,
179: final String description, int defaultPort) {
180: super (name, description);
181: this .schemeName = schemeName;
182: this .defaultPort = defaultPort;
183: }
184:
185: /** {@inheritDoc} */
186: @Override
187: public boolean equals(final Object object) {
188: return (object instanceof Protocol)
189: && getName().equalsIgnoreCase(
190: ((Protocol) object).getName());
191: }
192:
193: /**
194: * Returns the default port number.
195: *
196: * @return The default port number.
197: */
198: public int getDefaultPort() {
199: return this .defaultPort;
200: }
201:
202: /**
203: * Returns the URI scheme name.
204: *
205: * @return The URI scheme name.
206: */
207: public String getSchemeName() {
208: return this .schemeName;
209: }
210:
211: /** {@inheritDoc} */
212: @Override
213: public int hashCode() {
214: return (getName() == null) ? 0 : getName().toLowerCase()
215: .hashCode();
216: }
217: }
|