001: package com.ibm.webdav.protocol.http;
002:
003: /*
004: * (C) Copyright IBM Corp. 2000 All rights reserved.
005: *
006: * The program is provided "AS IS" without any warranty express or
007: * implied, including the warranty of non-infringement and the implied
008: * warranties of merchantibility and fitness for a particular purpose.
009: * IBM will not be liable for any damages suffered by you as a result
010: * of using the Program. In no event will IBM be liable for any
011: * special, indirect or consequential damages or lost profits even if
012: * IBM has been advised of the possibility of their occurrence. IBM
013: * will not be liable for any third party claims against you.
014: *
015: * Portions Copyright (C) Simulacra Media Ltd, 2004.
016: */
017: import java.io.*;
018: import java.util.*;
019:
020: import javax.servlet.*;
021: import javax.servlet.http.*;
022:
023: import com.ibm.webdav.*;
024: import com.ibm.webdav.impl.*;
025:
026: /** This implementation of WebDAV uses a distributed object model. Each WebDAV method
027: * correspondes to some method on a Resource object. ResourceHTTPStub and ResourceHTTPSkel are the
028: * client proxy, and server listener classes that enable remote invocation of Resource
029: * methods to a ResourceImpl instance over HTTP. ResourceHTTPSkel implements the server side of
030: * ResourceHTTPStub. It marshals arguments (HTTP headers and entity request bodies in
031: * this case), dispatches the remote method, and marshals the results (HTTP response
032: * headers, response entity bodies, and statuses) back to the client.
033: * <p>
034: * ResourceHTTPStub is a servlet that is intended to replace the file servlet in the
035: * JavaWebServer or IBM WebSphere AppServer. In conjunction with the file servlet,
036: * it provides a WebDAV service for the JavaWebServer.</p>
037: * @author Jim Amsden <jamsden@us.ibm.com>
038: * @see ResourceHTTPStub
039: * @see WebDAVMethod
040: */
041: public class ResourceHTTPSkel extends HttpServlet {
042: /**
043: * Initialize global variables
044: */
045: public void init(ServletConfig config) throws ServletException {
046: super .init(config);
047:
048: }
049:
050: /** Service all HTTP requests including WebDAV extensions. This is the servlet
051: * entry point for all method dispatching for the HTTP protocol.
052: *
053: * @param request contains information about the client request: the method,
054: * request headers, and request entity body.
055: * @param response provides a means for the server to send a response back to
056: * the client including response headers and a response entity body.
057: */
058: protected void service(HttpServletRequest request,
059: HttpServletResponse response) throws ServletException,
060: IOException {
061: try {
062: // create an instance of a WebDAV request method
063: WebDAVMethod method = WebDAVMethod
064: .create(request, response);
065: response.setHeader("dav4j-server-version",
066: Resource.DAV4JVersion);
067:
068: ResourceImpl resource = method.getResource();
069:
070: if (this .allowUser(resource) == false) {
071: // Not allowed, so report he's unauthorized
072: response.setHeader("WWW-Authenticate",
073: "BASIC realm=\"OHRM\"");
074: response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
075: } else {
076: if (ResourceImpl.debug) {
077: System.err.println(method.getMethodName() + " "
078: + resource.getURL().getFile());
079:
080: Enumeration propertyNames = resource
081: .getRequestContext().keys();
082:
083: while (propertyNames.hasMoreElements()) {
084: String name = (String) propertyNames
085: .nextElement();
086: String value = (String) resource
087: .getRequestContext().get(name);
088: System.err.println(name + ": " + value);
089: }
090:
091: System.err.println();
092: }
093:
094: // dispatch the request method. Each method handles the request entity,
095: // response headers, and response entity differently.
096: WebDAVStatus statusCode = method.execute();
097:
098: if (ResourceImpl.debug) {
099: System.err.println("server statusCode = "
100: + statusCode);
101: }
102: }
103: } catch (Exception exc) {
104: // all exceptions should have been caught, but just in case...
105: System.err.println("ResourctHTTPSkel internal error: "
106: + exc);
107: exc.printStackTrace();
108: }
109: }
110:
111: protected boolean allowUser(ResourceImpl resource)
112: throws IOException {
113: String user = resource.getContext().getRequestContext()
114: .getAuthorizationId();
115: String pwd = resource.getContext().getRequestContext()
116: .getPassword();
117:
118: if ((user != null) && (user.length() > 0) && (pwd != null)
119: && (pwd.length() > 0)) {
120: return resource.authenticateUser(user, pwd);
121: } else {
122: return false;
123: }
124: }
125: }
|