1 23 24 package org.apache.slide.webdav.method; 25 26 import java.io.IOException ; 27 import java.util.Iterator ; 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 58 public class OptionsMethod extends AbstractWebdavMethod 59 implements DeltavConstants, AclConstants, BindConstants, ReadMethod { 60 61 private XMLOutputter xmlOut; 63 65 private boolean versionHistoryCollectionSetRequested = false; 67 private boolean workspaceCollectionSetRequested = false; 68 private boolean responseBodyNeeded = false; 69 70 72 73 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 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 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 126 127 StringBuffer davHeader = new StringBuffer (); 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 (); 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 (); 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 resourceUri = requestUri; 159 if (resourceUri == null) { 160 resourceUri = "/"; 161 } 162 163 boolean resourceExists = false; 165 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 } catch (SlideException e) { 182 if (resourceUri.equals("/")) { 183 resourceExists = true; 184 } else { 186 resourceExists = false; 187 } 188 } 189 190 StringBuffer methodsAllowed = new StringBuffer (); 192 if( resourceKind != null ) { 193 Iterator methodIterator = resourceKind.getSupportedMethods().iterator(); 194 while (methodIterator.hasNext()) { 195 methodsAllowed.append((String )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 if( responseBodyNeeded ) { 219 resp.setContentType( TEXT_XML_UTF_8 ); 220 UriHandler uh = UriHandler.getUriHandler( resourceUri ); 221 String 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 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 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 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 |