KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > method > OptionsMethod


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/OptionsMethod.java,v 1.44.2.2 2004/08/12 14:06:52 luetzkendorf Exp $
3  * $Revision: 1.44.2.2 $
4  * $Date: 2004/08/12 14:06:52 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.webdav.method;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.apache.slide.common.NamespaceAccessToken;
30 import org.apache.slide.common.SlideException;
31 import org.apache.slide.content.NodeRevisionDescriptor;
32 import org.apache.slide.content.NodeRevisionDescriptors;
33 import org.apache.slide.event.EventDispatcher;
34 import org.apache.slide.search.SearchLanguage;
35 import org.apache.slide.util.Configuration;
36 import org.apache.slide.webdav.WebdavException;
37 import org.apache.slide.webdav.WebdavServletConfig;
38 import org.apache.slide.webdav.event.WebdavEvent;
39 import org.apache.slide.webdav.util.AclConstants;
40 import org.apache.slide.webdav.util.BindConstants;
41 import org.apache.slide.webdav.util.DeltavConstants;
42 import org.apache.slide.webdav.util.HistoryPathHandler;
43 import org.apache.slide.webdav.util.UriHandler;
44 import org.apache.slide.webdav.util.WebdavStatus;
45 import org.apache.slide.webdav.util.WorkspacePathHandler;
46 import org.apache.slide.webdav.util.resourcekind.AbstractResourceKind;
47 import org.apache.slide.webdav.util.resourcekind.ResourceKind;
48 import org.jdom.Document;
49 import org.jdom.Element;
50 import org.jdom.JDOMException;
51 import org.jdom.output.XMLOutputter;
52
53
54 /**
55  * OPTIONS Method.
56  *
57  */

58 public class OptionsMethod extends AbstractWebdavMethod
59     implements DeltavConstants, AclConstants, BindConstants, ReadMethod {
60     
61     // An XML outputter
62
private XMLOutputter xmlOut;
63     // private Namespace xmlNs = Namespace.getNamespace(DEFAULT_NAMESPACE);
64

65     // the collections which may contain VH or workspace resources are requested
66
private boolean versionHistoryCollectionSetRequested = false;
67     private boolean workspaceCollectionSetRequested = false;
68     private boolean responseBodyNeeded = false;
69     
70     // ----------------------------------------------------------- Constructors
71

72     
73     /**
74      * Constructor.
75      *
76      * @param token the token for accessing the namespace
77      * @param config configuration of the WebDAV servlet
78      */

79     public OptionsMethod(NamespaceAccessToken token,
80                          WebdavServletConfig config) {
81         super(token, config);
82         org.jdom.output.Format format = org.jdom.output.Format.getPrettyFormat();
83         format.setIndent(XML_RESPONSE_INDENT);
84         xmlOut = new XMLOutputter(format);
85     }
86     
87     protected void parseRequest() throws WebdavException {
88         
89         if( req.getContentLength() > 0 || isRequestChunked()) {
90             try {
91                 Iterator JavaDoc i = parseRequestContent(E_OPTIONS).getChildren().iterator();
92                 while( i.hasNext() ) {
93                     Element e = (Element)i.next();
94                     if( e.getName().equals(E_VERSION_HISTORY_COLLECTION_SET) )
95                         versionHistoryCollectionSetRequested = true;
96                     if( e.getName().equals(E_WORKSPACE_COLLECTION_SET) )
97                         workspaceCollectionSetRequested = true;
98                 }
99                 if( versionHistoryCollectionSetRequested || workspaceCollectionSetRequested )
100                     responseBodyNeeded = true;
101             }
102             catch(JDOMException e ){
103                 int statusCode = WebdavStatus.SC_BAD_REQUEST;
104                 sendError( statusCode, e );
105                 throw new WebdavException( statusCode );
106             }
107             catch( IOException JavaDoc e ){
108                 int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR;
109                 sendError( statusCode, e );
110                 throw new WebdavException( statusCode );
111             }
112         }
113     }
114     
115     protected void executeRequest() throws WebdavException {
116         
117         /*
118          We have been experiencing a strange behaviour with the *creation* of a
119          Microsoft webfolder. When the DAV: header of an OPTIONS response
120          becomes too long, an <The folder you entered does not appear to be
121          valid. Please choose another.> error occurs.
122          
123          As a workaround we break-up the DAV: header into multiple DAV: header
124          lines.
125          */

126         
127         StringBuffer JavaDoc davHeader = new StringBuffer JavaDoc();
128         davHeader.append( F_WEBDAV );
129         
130         if( Configuration.useIntegratedLocking() )
131             davHeader.append( ", " ).append( F_LOCKING );
132         
133         davHeader.append( ", " ).append( F_SLIDE );
134         
135         if( Configuration.useIntegratedSecurity() )
136             davHeader.append( ", " ).append( F_ACCESS_CONTROL );
137         
138         if( Configuration.useGlobalBinding() )
139             davHeader.append( ", " ).append( F_BINDING );
140         
141         resp.addHeader( "DAV", davHeader.toString() );
142         
143         if( Configuration.useVersionControl() ) {
144             davHeader = new StringBuffer JavaDoc();
145             davHeader.append( F_VERSION_CONTROL );
146             davHeader.append( ", " ).append( F_VERSION_HISTORY );
147             davHeader.append( ", " ).append( F_CHECKOUT_IN_PLACE );
148             resp.addHeader( "DAV", davHeader.toString() );
149             
150             davHeader = new StringBuffer JavaDoc();
151             davHeader.append( F_WORKSPACE );
152             davHeader.append( ", " ).append( F_WORKING_RESOURCE );
153             davHeader.append( ", " ).append( F_UPDATE );
154             davHeader.append( ", " ).append( F_LABEL );
155             resp.addHeader( "DAV", davHeader.toString() );
156         }
157         
158         String JavaDoc resourceUri = requestUri;
159         if (resourceUri == null) {
160             resourceUri = "/";
161         }
162         
163         // prepare for allow-header
164
boolean resourceExists = false;
165         //boolean resourceIsCollection = false;
166
SearchLanguage[] languages =
167             token.getSearchHelper().getSupportedLanguages ();
168         
169         ResourceKind resourceKind = null;
170         try {
171             if ( WebdavEvent.OPTIONS.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.OPTIONS, new WebdavEvent(this));
172
173             NodeRevisionDescriptors revisionDescriptors =
174                 content.retrieve(slideToken, resourceUri);
175             NodeRevisionDescriptor revisionDescriptor =
176                 content.retrieve(slideToken, revisionDescriptors);
177             resourceKind = AbstractResourceKind.determineResourceKind(token, revisionDescriptors, revisionDescriptor);
178             resourceExists = true;
179             //if( WebdavUtils.isCollection(revisionDescriptor) )
180
//resourceIsCollection = true;
181
} catch (SlideException e) {
182             if (resourceUri.equals("/")) {
183                 resourceExists = true;
184                 //resourceIsCollection = true;
185
} else {
186                 resourceExists = false;
187             }
188         }
189         
190         // build allow-header
191
StringBuffer JavaDoc methodsAllowed = new StringBuffer JavaDoc();
192         if( resourceKind != null ) {
193             Iterator JavaDoc methodIterator = resourceKind.getSupportedMethods().iterator();
194             while (methodIterator.hasNext()) {
195                 methodsAllowed.append((String JavaDoc)methodIterator.next());
196                 methodsAllowed.append(", ");
197             }
198             if (methodsAllowed.length() > 1) {
199                 methodsAllowed.setLength(methodsAllowed.length()-2);
200             }
201         }
202         else {
203             methodsAllowed.append( "OPTIONS, MKCOL, PUT" );
204             if( Configuration.useIntegratedLocking() )
205                 methodsAllowed.append( ", LOCK" );
206         }
207         
208         resp.addHeader("Allow", methodsAllowed.toString());
209         
210         if (Configuration.useSearch () && languages != null && resourceExists)
211             for (int i = 0; i < languages.length; i++)
212                 resp.addHeader ("DASL", "<" + languages [i].getGrammarUri() +
213                                     languages [i].getName () + ">");
214         
215         resp.addHeader("MS-Author-Via", "DAV");
216         
217         // build response body if needed
218
if( responseBodyNeeded ) {
219             resp.setContentType( TEXT_XML_UTF_8 );
220             UriHandler uh = UriHandler.getUriHandler( resourceUri );
221             String JavaDoc storeName = uh.getAssociatedBaseStoreName(token.getName());
222             UriHandler hpath = HistoryPathHandler.getHistoryPathHandler();
223             UriHandler wspath = WorkspacePathHandler.getWorkspacePathHandler();
224             Element ore =
225                 new Element( E_OPTIONS_RESPONSE, DNSP );
226             if( versionHistoryCollectionSetRequested ) {
227                 Element vhcse =
228                     new Element( E_VERSION_HISTORY_COLLECTION_SET, DNSP );
229                 Iterator JavaDoc i = ((HistoryPathHandler)hpath).getResolvedHistoryPaths(storeName).iterator();
230                 while( i.hasNext() ) {
231                     Element hre =
232                         new Element( E_HREF,DNSP );
233                     hre.addContent( getFullPath(String.valueOf(i.next())) );
234                     vhcse.addContent( hre );
235                 }
236                 ore.addContent( vhcse );
237             }
238             if( workspaceCollectionSetRequested ) {
239                 Element wscse =
240                     new Element( E_WORKSPACE_COLLECTION_SET, DNSP );
241                 Iterator JavaDoc i = ((WorkspacePathHandler)wspath).getResolvedWorkspacePaths(storeName).iterator();
242                 while( i.hasNext() ) {
243                     Element hre =
244                         new Element( E_HREF, DNSP );
245                     hre.addContent( getFullPath(String.valueOf(i.next())) );
246                     wscse.addContent( hre );
247                 }
248                 ore.addContent( wscse );
249             }
250             try {
251                 xmlOut.output(new Document(ore), resp.getWriter());
252             }
253             catch( IOException JavaDoc e ) {
254                 int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR;
255                 sendError( statusCode, e );
256                 throw new WebdavException( statusCode );
257             }
258         }
259     }
260 }
261
Popular Tags