KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > vfs > webdav > DAVProcessor


1 /* ========================================================================== *
2  * Copyright (C) 2004-2005 Pier Fumagalli <http://www.betaversion.org/~pier/> *
3  * All rights reserved. *
4  * ========================================================================== *
5  * *
6  * Licensed under the Apache License, Version 2.0 (the "License"). You may *
7  * not use this file except in compliance with the License. You may obtain a *
8  * copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>. *
9  * *
10  * Unless required by applicable law or agreed to in writing, software *
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT *
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *
13  * License for the specific language governing permissions and limitations *
14  * under the License. *
15  * *
16  * ========================================================================== */

17 package com.sslexplorer.vfs.webdav;
18
19 import java.io.IOException JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import com.sslexplorer.boot.Util;
28 import com.sslexplorer.policyframework.LaunchSession;
29 import com.sslexplorer.policyframework.LaunchSessionFactory;
30 import com.sslexplorer.security.SessionInfo;
31 import com.sslexplorer.vfs.VFSRepository;
32 import com.sslexplorer.vfs.VFSResource;
33 import com.sslexplorer.vfs.webdav.methods.COPY;
34 import com.sslexplorer.vfs.webdav.methods.DELETE;
35 import com.sslexplorer.vfs.webdav.methods.GET;
36 import com.sslexplorer.vfs.webdav.methods.HEAD;
37 import com.sslexplorer.vfs.webdav.methods.MKCOL;
38 import com.sslexplorer.vfs.webdav.methods.MOVE;
39 import com.sslexplorer.vfs.webdav.methods.OPTIONS;
40 import com.sslexplorer.vfs.webdav.methods.PROPFIND;
41 import com.sslexplorer.vfs.webdav.methods.PROPPATCH;
42 import com.sslexplorer.vfs.webdav.methods.PUT;
43
44 /**
45  * <p>The <a HREF="http://www.rfc-editor.org/rfc/rfc2518.txt">WebDAV</a>
46  * transactions processor.</p>
47  *
48  * @author <a HREF="http://www.betaversion.org/~pier/">Pier Fumagalli</a>
49  */

50 public class DAVProcessor {
51     final static Log log = LogFactory.getLog(DAVProcessor.class);
52
53     /** <p>All the implemented methods, comma separated.</p> */
54     public static final String JavaDoc METHODS = "COPY,DELETE,GET,HEAD,MKCOL,MOVE,OPTIONS,PROPFIND,PROPPATCH,PUT";
55
56     private VFSRepository repository = null;
57     private Map JavaDoc methods = null;
58     private SessionInfo session;
59
60     private static Map JavaDoc methodImpls = new HashMap JavaDoc();
61     
62     
63     public static void addDAVMethod(Class JavaDoc cls) {
64         
65             methodImpls.put(Util.getSimpleClassName(cls), cls);
66     }
67     
68     public static void removeDAVMethod(Class JavaDoc cls) {
69             methodImpls.remove(Util.getSimpleClassName(cls));
70     }
71     
72     /**
73      * <p>Create a new {@link DAVProcessor} instance.</p>
74      * @param repository
75      * @param session
76      */

77     public DAVProcessor(VFSRepository repository, SessionInfo session) {
78         this.repository = repository;
79         this.session = session;
80         initialiseProcessor();
81     }
82     
83     /**
84      *
85      */

86     public void initialiseProcessor(){
87         this.methods = new HashMap JavaDoc();
88         this.methods.put("DELETE", new DELETE());
89         this.methods.put("GET", new GET());
90         this.methods.put("HEAD", new HEAD());
91         this.methods.put("MKCOL", new MKCOL());
92         this.methods.put("MOVE", new MOVE());
93         this.methods.put("OPTIONS", new OPTIONS());
94         this.methods.put("PROPFIND", new PROPFIND());
95         this.methods.put("PROPPATCH", new PROPPATCH());
96         this.methods.put("PUT", new PUT());
97         this.methods.put("COPY", new COPY());
98         
99         Map.Entry JavaDoc entry;
100         for(Iterator JavaDoc it = methodImpls.entrySet().iterator(); it.hasNext();) {
101                try {
102                 entry = (Map.Entry JavaDoc) it.next();
103                 this.methods.put(entry.getKey(), ((Class JavaDoc) entry.getValue()).newInstance());
104             } catch (InstantiationException JavaDoc e) {
105                 log.error("Could not create DAVMethod implementation", e);
106             } catch (IllegalAccessException JavaDoc e) {
107                 log.error("Could not create DAVMethod implementation", e);
108             }
109         }
110     }
111     
112     /**
113      * Get the session this processor is associated with
114      *
115      * @return session
116      */

117     public SessionInfo getSession() {
118         return session;
119     }
120
121     /**
122      * <p>Process the specified {@link DAVTransaction} fully.</p>
123      * @throws IOException
124      */

125     public void process(DAVTransaction transaction)
126     throws Exception JavaDoc {
127
128             String JavaDoc method = transaction.getMethod();
129             if (this.methods.containsKey(method)) {
130                 String JavaDoc path = transaction.getPath();
131                 if(log.isDebugEnabled())
132                     log.debug("Looking for resource for '" + path + "'");
133                 
134                 // Get the best launch session possible
135
LaunchSession launchSession = null;
136                 String JavaDoc launchId = transaction.getRequest().getParameter(LaunchSession.LAUNCH_ID);
137                 if(launchId != null) {
138                     launchSession = LaunchSessionFactory.getInstance().getLaunchSession(launchId);
139                 }
140                 if(launchSession == null) {
141                     launchSession = new LaunchSession(transaction.getSessionInfo());
142                 }
143                 
144                 VFSResource resource = repository.getResource(launchSession, path, transaction.getCredentials());
145
146                 /* This is to verify that we have access. We process the cause of the
147                  * exception to determine whether an authentication exception should be thrown
148                  */

149                 resource.verifyAccess();
150                 
151                 /*
152                  * Now verify the launch session allows access
153                  */

154                 
155                 DAVMethod instance = ((DAVMethod)this.methods.get(method));
156                 instance.process(transaction, resource);
157             } else {
158                 String JavaDoc message = "Method \"" + method + "\" not implemented";
159                 throw new DAVException(501, message);
160             }
161     }
162
163     /**
164      * @return
165      */

166     public VFSRepository getRepository() {
167         return repository;
168     }
169 }
170
Popular Tags