1 23 24 package org.apache.webdav.lib.methods; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.util.ArrayList ; 30 import java.util.Enumeration ; 31 import java.util.List ; 32 import java.util.StringTokenizer ; 33 import java.util.Vector ; 34 import javax.xml.parsers.DocumentBuilder ; 35 import javax.xml.parsers.DocumentBuilderFactory ; 36 import javax.xml.parsers.ParserConfigurationException ; 37 import org.apache.commons.httpclient.Header; 38 import org.apache.commons.httpclient.HttpConnection; 39 import org.apache.commons.httpclient.HttpException; 40 import org.apache.commons.httpclient.HttpState; 41 import org.apache.webdav.lib.util.WebdavStatus; 42 import org.apache.webdav.lib.util.XMLPrinter; 43 import org.w3c.dom.Document ; 44 import org.xml.sax.InputSource ; 45 import org.xml.sax.SAXException ; 46 47 48 52 public class OptionsMethod 53 extends XMLResponseMethodBase { 54 55 56 58 59 62 public static final String DAV_LEVEL1 = "1"; 63 64 65 68 public static final String DAV_LEVEL2 = "2"; 69 70 71 74 public static final String ADVANCED_COLLECTIONS = "3"; 75 76 77 80 public static final String DELTAV = "4"; 81 82 83 86 public static final String ACL = "5"; 87 88 89 92 public static final String DASL = "6"; 93 94 97 public static final int OPTIONS_WORKSPACE = 8; 98 99 102 public static final int OPTIONS_VERSION_HISTORY = 9; 103 104 105 106 108 109 112 public OptionsMethod() { 113 } 114 115 116 119 public OptionsMethod(String path) { 120 super(path); 121 } 122 123 126 public OptionsMethod(String path, int type) { 127 super(path); 128 this.type = type; 129 } 130 131 135 private Vector davCapabilities = new Vector (); 136 137 138 141 private Vector methodsAllowed = new Vector (); 142 143 private int type = 0; 144 145 private boolean hasXMLBody = false; 146 147 148 150 151 154 public boolean isAllowed(String method) { 155 checkUsed(); 156 return methodsAllowed.contains(method); 157 } 158 159 160 163 public Enumeration getAllowedMethods() { 164 checkUsed(); 165 return methodsAllowed.elements(); 166 } 167 168 169 172 public boolean isSupported(String capability) { 173 checkUsed(); 174 return davCapabilities.contains(capability); 175 } 176 177 178 181 public Enumeration getDavCapabilities() { 182 checkUsed(); 183 return davCapabilities.elements(); 184 } 185 186 191 public void parseResponse(InputStream input, HttpState state, HttpConnection conn) 192 throws IOException , HttpException { 193 try 194 { 195 if (getStatusLine().getStatusCode() == WebdavStatus.SC_OK && hasXMLBody) { 196 parseXMLResponse(input); 197 } 198 } 199 catch (IOException e) { 200 } 202 } 203 204 205 207 208 209 210 211 218 public void processResponseHeaders(HttpState state, 219 HttpConnection conn) { 220 221 Header davHeader = getResponseHeader("dav"); 222 if (davHeader != null) { 223 String davHeaderValue = davHeader.getValue(); 224 StringTokenizer tokenizer = 225 new StringTokenizer (davHeaderValue, ","); 226 while (tokenizer.hasMoreElements()) { 227 String davCapability = tokenizer.nextToken().trim(); 228 davCapabilities.addElement(davCapability); 229 } 230 } 231 232 Header allowHeader = getResponseHeader("allow"); 233 if (allowHeader != null) { 234 String allowHeaderValue = allowHeader.getValue(); 235 StringTokenizer tokenizer = 236 new StringTokenizer (allowHeaderValue, ","); 237 while (tokenizer.hasMoreElements()) { 238 String methodAllowed = 239 tokenizer.nextToken().trim().toUpperCase(); 240 methodsAllowed.addElement(methodAllowed); 241 } 242 } 243 244 Header lengthHeader = getResponseHeader("content-length"); 245 Header typeHeader = getResponseHeader("content-type"); 246 if( 247 (lengthHeader != null && 248 Integer.parseInt(lengthHeader.getValue()) > 0) || 249 (typeHeader != null && 250 typeHeader.getValue().startsWith("text/xml"))) 251 hasXMLBody = true; 252 253 super.processResponseHeaders(state, conn); 254 } 255 256 262 protected String generateRequestBody() { 263 264 if (type != 0){ 265 XMLPrinter printer = new XMLPrinter(); 266 267 printer.writeXMLHeader(); 268 printer.writeElement("D", "DAV:", "options", 270 XMLPrinter.OPENING); 271 272 if (type == OPTIONS_VERSION_HISTORY) 273 printer.writeElement("D", "version-history-collection-set", XMLPrinter.NO_CONTENT); 274 if (type == OPTIONS_WORKSPACE) 275 printer.writeElement("D", "workspace-collection-set", XMLPrinter.NO_CONTENT); 276 277 printer.writeElement("D", "options", XMLPrinter.CLOSING); 278 279 return printer.toString(); 280 } 281 282 return null; 283 } 284 285 public String getName() { 286 return "OPTIONS"; 287 } 288 289 public void addRequestHeaders(HttpState state, HttpConnection conn) 291 throws IOException , HttpException { 292 293 if (type!= 0){ 294 if (getRequestHeader("Content-Type") == null ) super.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 296 } 297 298 super.addRequestHeaders(state, conn); 299 } 300 301 309 public Enumeration getAllResponseURLs() { 310 checkUsed(); 311 return getResponseURLs().elements(); 312 } 313 314 public Enumeration getResponseProperties(){ 315 Vector result = new Vector (); 316 return (Enumeration ) result; 317 } 318 319 320 protected Document parseResponseContent(InputStream is) 321 throws ParserConfigurationException , SAXException , IOException { 322 323 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 324 factory.setNamespaceAware(true); 325 326 DocumentBuilder builder = factory.newDocumentBuilder(); 327 328 byte[] chunk; 329 byte[] all; 330 int chunkLen; 331 int allLen; 332 List chunks; 333 int i; 334 int max; 335 int ofs; 336 337 allLen = 0; 338 chunk = new byte[1024*4]; 339 chunkLen = is.read(chunk); 340 chunks = new ArrayList (); 341 while (chunkLen != -1) { 342 chunks.add(new Integer (chunkLen)); 343 chunks.add(chunk); 344 allLen += chunkLen; 345 chunk = new byte[1024*4]; 346 chunkLen = is.read(chunk); 347 } 348 349 all = new byte[allLen]; 350 ofs = 0; 351 max = chunks.size(); 352 for (i = 0; i < max; i += 2) { 353 chunkLen = ((Integer ) chunks.get(i)).intValue(); 354 chunk = (byte[]) chunks.get(i + 1); 355 System.arraycopy(chunk, 0, all, ofs, chunkLen); 356 ofs += chunkLen; 357 } 358 359 if (all.length == 0) return null; 360 return builder.parse(new InputSource (new ByteArrayInputStream (all))); 361 362 } 363 364 } 365 366 367 368 369 | Popular Tags |