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 com.noelios.restlet.ext.servlet;
020:
021: import java.io.InputStream;
022: import java.util.Iterator;
023: import java.util.Set;
024:
025: import javax.servlet.ServletContext;
026:
027: import org.restlet.Client;
028: import org.restlet.data.MediaType;
029: import org.restlet.data.Method;
030: import org.restlet.data.Reference;
031: import org.restlet.data.ReferenceList;
032: import org.restlet.data.Request;
033: import org.restlet.data.Response;
034: import org.restlet.data.Status;
035: import org.restlet.resource.InputRepresentation;
036: import org.restlet.resource.Representation;
037: import org.restlet.service.MetadataService;
038:
039: import com.noelios.restlet.local.WarClientHelper;
040:
041: /**
042: * Local client connector based on a Servlet context (JEE Web application
043: * context).
044: *
045: * @author Jerome Louvel (contact@noelios.com)
046: */
047: public class ServletWarClientHelper extends WarClientHelper {
048: /** The Servlet context to use. */
049: private ServletContext servletContext;
050:
051: /**
052: * Constructor.
053: *
054: * @param client
055: * The client to help.
056: * @param servletContext
057: * The Servlet context
058: */
059: public ServletWarClientHelper(Client client,
060: ServletContext servletContext) {
061: super (client);
062: this .servletContext = servletContext;
063: }
064:
065: /**
066: * Returns the Servlet context.
067: *
068: * @return The Servlet context.
069: */
070: public ServletContext getServletContext() {
071: return this .servletContext;
072: }
073:
074: @SuppressWarnings("unchecked")
075: @Override
076: protected void handleWar(Request request, Response response) {
077: if (request.getMethod().equals(Method.GET)
078: || request.getMethod().equals(Method.HEAD)) {
079: String basePath = request.getResourceRef().getPath();
080: int lastSlashIndex = basePath.lastIndexOf('/');
081: String entry = (lastSlashIndex == -1) ? basePath : basePath
082: .substring(lastSlashIndex + 1);
083: Representation output = null;
084:
085: if (basePath.endsWith("/")) {
086: // Return the directory listing
087: Set<String> entries = getServletContext()
088: .getResourcePaths(basePath);
089: // Directory listing may be null.
090: if (entries != null) {
091: ReferenceList rl = new ReferenceList(entries.size());
092: rl.setIdentifier(request.getResourceRef());
093:
094: for (Iterator<String> iter = entries.iterator(); iter
095: .hasNext();) {
096: entry = iter.next();
097: rl.add(new Reference(basePath
098: + entry.substring(basePath.length())));
099: }
100:
101: output = rl.getTextRepresentation();
102: }
103: } else {
104: // Return the entry content
105: MetadataService metadataService = getMetadataService(request);
106: InputStream ris = getServletContext()
107: .getResourceAsStream(basePath);
108: if (ris != null) {
109: output = new InputRepresentation(ris,
110: metadataService.getDefaultMediaType());
111: output.setIdentifier(request.getResourceRef());
112: updateMetadata(metadataService, entry, output);
113:
114: // See if the Servlet context specified a particular Mime
115: // Type
116: String mediaType = getServletContext().getMimeType(
117: basePath);
118:
119: if (mediaType != null) {
120: output.setMediaType(new MediaType(mediaType));
121: }
122: }
123: }
124:
125: response.setEntity(output);
126: response.setStatus(Status.SUCCESS_OK);
127: } else {
128: response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
129: response.getAllowedMethods().add(Method.GET);
130: response.getAllowedMethods().add(Method.HEAD);
131: }
132: }
133:
134: }
|