KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > protocol > http > ResourceHTTPSkel


1 package com.ibm.webdav.protocol.http;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17 import java.io.*;
18 import java.util.*;
19
20 import javax.servlet.*;
21 import javax.servlet.http.*;
22
23 import com.ibm.webdav.*;
24 import com.ibm.webdav.impl.*;
25
26
27 /** This implementation of WebDAV uses a distributed object model. Each WebDAV method
28  * correspondes to some method on a Resource object. ResourceHTTPStub and ResourceHTTPSkel are the
29  * client proxy, and server listener classes that enable remote invocation of Resource
30  * methods to a ResourceImpl instance over HTTP. ResourceHTTPSkel implements the server side of
31  * ResourceHTTPStub. It marshals arguments (HTTP headers and entity request bodies in
32  * this case), dispatches the remote method, and marshals the results (HTTP response
33  * headers, response entity bodies, and statuses) back to the client.
34  * <p>
35  * ResourceHTTPStub is a servlet that is intended to replace the file servlet in the
36  * JavaWebServer or IBM WebSphere AppServer. In conjunction with the file servlet,
37  * it provides a WebDAV service for the JavaWebServer.</p>
38  * @author Jim Amsden &lt;jamsden@us.ibm.com&gt;
39  * @see ResourceHTTPStub
40  * @see WebDAVMethod
41  */

42 public class ResourceHTTPSkel extends HttpServlet {
43     /**
44          * Initialize global variables
45          */

46     public void init(ServletConfig config) throws ServletException {
47         super.init(config);
48
49     }
50
51     /** Service all HTTP requests including WebDAV extensions. This is the servlet
52      * entry point for all method dispatching for the HTTP protocol.
53      *
54      * @param request contains information about the client request: the method,
55      * request headers, and request entity body.
56      * @param response provides a means for the server to send a response back to
57      * the client including response headers and a response entity body.
58      */

59     protected void service(HttpServletRequest request,
60                            HttpServletResponse response)
61                     throws ServletException, IOException {
62         try {
63             // create an instance of a WebDAV request method
64
WebDAVMethod method = WebDAVMethod.create(request, response);
65             response.setHeader("dav4j-server-version", Resource.DAV4JVersion);
66
67             ResourceImpl resource = method.getResource();
68
69             if (this.allowUser(resource) == false) {
70                 // Not allowed, so report he's unauthorized
71
response.setHeader("WWW-Authenticate", "BASIC realm=\"OHRM\"");
72                 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
73             } else {
74                 if (ResourceImpl.debug) {
75                     System.err.println(method.getMethodName() + " " +
76                                        resource.getURL().getFile());
77
78                     Enumeration propertyNames = resource.getRequestContext()
79                                                         .keys();
80
81                     while (propertyNames.hasMoreElements()) {
82                         String JavaDoc name = (String JavaDoc) propertyNames.nextElement();
83                         String JavaDoc value = (String JavaDoc) resource.getRequestContext()
84                                                         .get(name);
85                         System.err.println(name + ": " + value);
86                     }
87
88                     System.err.println();
89                 }
90
91                 // dispatch the request method. Each method handles the request entity,
92
// response headers, and response entity differently.
93
WebDAVStatus statusCode = method.execute();
94
95                 if (ResourceImpl.debug) {
96                     System.err.println("server statusCode = " + statusCode);
97                 }
98             }
99         } catch (Exception JavaDoc exc) {
100             // all exceptions should have been caught, but just in case...
101
System.err.println("ResourctHTTPSkel internal error: " + exc);
102             exc.printStackTrace();
103         }
104     }
105
106     protected boolean allowUser(ResourceImpl resource)
107                          throws IOException {
108         String JavaDoc user = resource.getContext().getRequestContext()
109                               .getAuthorizationId();
110         String JavaDoc pwd = resource.getContext().getRequestContext().getPassword();
111
112         if ((user != null) && (user.length() > 0) && (pwd != null) &&
113                 (pwd.length() > 0)) {
114             return resource.authenticateUser(user, pwd);
115         } else {
116             return false;
117         }
118     }
119 }
Popular Tags