1 package com.ibm.webdav; 2 3 17 import org.w3c.dom.*; 18 import java.util.*; 19 20 21 import java.text.ParseException ; 22 23 37 public class PropertyResponse extends Response implements java.io.Serializable { 38 39 private Hashtable pnproperties = new Hashtable(); 41 42 private Hashtable strxproperties = new Hashtable(); 50 51 private Hashtable propertydefs = new Hashtable(); 53 54 62 public PropertyResponse(Document document, Element response) throws ServerException { 63 super(document); 64 try { 65 Element href = (Element) response.getElementsByTagNameNS("DAV:","href").item(0); 67 setResource(((Text) href.getFirstChild()).getData()); 68 69 NodeList propstats = response.getElementsByTagNameNS("DAV:","propstat"); 71 Element propstat = null; 72 for (int i = 0; i < propstats.getLength(); i++) { 73 propstat = (Element) propstats.item(i); 74 Element prop = (Element) propstat.getElementsByTagNameNS("DAV:","prop").item(0); 77 78 Element status = (Element) propstat.getElementsByTagNameNS("DAV:","status").item(0); 80 String statusMessage = ((Text) status.getFirstChild()).getData(); 81 StringTokenizer statusFields = new StringTokenizer(statusMessage, " "); 82 statusFields.nextToken(); int statusCode = Integer.parseInt(statusFields.nextToken()); 84 NodeList properties = prop.getChildNodes(); 85 Node property = null; 86 for (int j = 0; j < properties.getLength(); j++) { 87 property = (Node) properties.item(j); 88 if (property.getNodeType() == Node.ELEMENT_NODE) { 90 Element el = (Element)property; 91 PropertyName propname = new PropertyName( el ); 93 addProperty(propname, (Element) property, statusCode); 94 } 95 } 96 } 97 Element responseDescription = (Element) response.getElementsByTagNameNS("DAV:","responsedescription").item(0); 98 if (responseDescription != null) { 99 setDescription(((Text) responseDescription.getFirstChild()).getData()); 100 } 101 } catch (Exception exc) { 102 exc.printStackTrace(); 103 throw new ServerException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "Invalid PropertyResponse"); 104 } 105 } 106 111 public PropertyResponse(String url) { 112 super(url); 113 } 114 122 public void addProperty( PropertyName propertyName, Element propertyElement, int status) throws ServerException { 123 if (pnproperties.contains(propertyName)) { 124 throw new ServerException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "Duplicate property in a Response"); 125 } else { 126 pnproperties.put(propertyName, new PropertyValue(propertyElement, status)); 127 strxproperties.put(propertyName.asExpandedString(), propertyName); 128 } 129 } 130 131 134 public Element asXML() { 135 136 Element response = document.createElementNS("DAV:","D:response"); 137 138 Element href = document.createElementNS("DAV:","D:href"); 139 140 href.appendChild(document.createTextNode(getResource())); 141 response.appendChild(href); 142 143 Hashtable byStatus = new Hashtable(); 145 Enumeration propertyNames = getPropertyNamesPN(); 146 while (propertyNames.hasMoreElements()) { 147 PropertyName propertyName = (PropertyName) propertyNames.nextElement(); 148 PropertyValue propertyValue = getProperty(propertyName); 149 Integer status = new Integer (propertyValue.status); 150 151 Vector props = null; 153 if (byStatus.containsKey(status)) { 154 props = (Vector) byStatus.get(status); 155 } else { 156 props = new Vector(); 157 byStatus.put(status, props); 158 } 159 props.addElement(propertyName); 160 } 161 Enumeration statuses = byStatus.keys(); 163 164 if (!statuses.hasMoreElements()) { 167 Element propstat = document.createElementNS("DAV:","D:propstat"); 168 169 Element prop = document.createElementNS("DAV:","D:prop"); 170 171 propstat.appendChild(prop); 172 Element statusElement = document.createElementNS("DAV:","D:status"); 173 174 String statusText = HTTPVersion + " " + WebDAVStatus.SC_OK + " " + WebDAVStatus.getStatusMessage(WebDAVStatus.SC_OK); 175 statusElement.appendChild(document.createTextNode(statusText)); 176 propstat.appendChild(statusElement); 177 response.appendChild(propstat); 178 } 179 180 while (statuses.hasMoreElements()) { 182 Integer status = (Integer ) statuses.nextElement(); 183 Enumeration propertiesHavingThisStatus = ((Vector) byStatus.get(status)).elements(); 184 Element propstat = document.createElementNS("DAV:","D:propstat"); 185 186 Element prop = document.createElementNS("DAV:","D:prop"); 187 188 189 while (propertiesHavingThisStatus.hasMoreElements()) { 191 PropertyName propertyName = (PropertyName) propertiesHavingThisStatus.nextElement(); 192 PropertyValue propertyValue = getProperty(propertyName); 193 195 try { 196 prop.appendChild(document.importNode(propertyValue.value,true)); 197 } catch(NullPointerException e) { 198 System.err.println("Null pointer for property - " + propertyName); 199 throw e; 200 } 201 202 } 203 propstat.appendChild(prop); 204 String statusText = HTTPVersion + " " + status + " " + WebDAVStatus.getStatusMessage(status.intValue()); 205 Element statusElement = document.createElementNS("DAV:","D:status"); 206 207 statusElement.appendChild(document.createTextNode(statusText)); 208 propstat.appendChild(statusElement); 209 response.appendChild(propstat); 210 } 211 212 Enumeration defKeys = propertydefs.keys(); 213 214 if(defKeys.hasMoreElements()) { 215 Element propdefn = document.createElementNS("DAV:","D:propdefn"); 216 response.appendChild(propdefn); 217 218 while(defKeys.hasMoreElements()) { 219 Object key = defKeys.nextElement(); 220 PropertyDefinition propdef = (PropertyDefinition)propertydefs.get(key); 221 222 propdefn.appendChild(document.importNode(propdef.asXML(),true)); 223 } 224 225 } 226 227 if (getDescription() != null) { 228 Element description = document.createElementNS("DAV:","D:responsedescription"); 229 230 description.appendChild(document.createTextNode(getDescription())); 231 response.appendChild(description); 232 } 233 return response; 234 } 235 242 public Vector getActiveLocks() throws WebDAVException { 243 Vector allLocks = new Vector(); 244 PropertyValue prop = getProp("DAV:lockdiscovery"); 245 if (prop != null) { 246 NodeList activeLocks = ((Element) prop.value).getElementsByTagNameNS("DAV:","activelock"); 247 Element activeLock = null; 248 for (int i = 0; i < activeLocks.getLength(); i++) { 249 activeLock = (Element) activeLocks.item(i); 250 allLocks.addElement(new ActiveLock(activeLock)); 251 } 252 } 253 return allLocks; 254 } 255 259 public String getAuthor() { 260 String author = null; 261 PropertyValue prop = getProp("DAV:author"); 262 if (prop != null) { 263 Text pcdata = (Text) prop.value.getFirstChild(); 264 if (pcdata != null) { 265 author = pcdata.getData(); 266 } 267 } 268 return author; 269 } 270 274 public Date getCheckinDate() { 275 Date date = null; 276 PropertyValue prop = getProp("DAV:checkin-date"); 277 if (prop != null) { 278 Text pcdata = (Text) prop.value.getFirstChild(); 279 if (pcdata != null) { 280 String dateString = pcdata.getData(); 281 try { 282 date = (new SimpleISO8601DateFormat()).parse(dateString); 283 } catch (ParseException exc) { 284 System.err.println("Invalid date format for creationdate in " + getResource()); 285 } 286 } 287 } 288 return date; 289 } 290 293 public String getComment() { 294 String comment = null; 295 PropertyValue prop = getProp("DAV:comment"); 296 if (prop != null) { 297 Text pcdata = (Text) prop.value.getFirstChild(); 298 if (pcdata != null) { 299 comment = pcdata.getData(); 300 } 301 } 302 return comment; 303 } 304 307 public String getContentLanguage() { 308 String contentLanguage = null; 309 PropertyValue prop = getProp("DAV:getcontentlanguage"); 310 if (prop != null) { 311 Text pcdata = (Text) prop.value.getFirstChild(); 312 if (pcdata != null) { 313 contentLanguage = pcdata.getData(); 314 } 315 } 316 return contentLanguage; 317 } 318 321 public int getContentLength() { 322 int length = -1; 323 PropertyValue prop = getProp("DAV:getcontentlength"); 324 if (prop != null) { 325 Text pcdata = (Text) prop.value.getFirstChild(); 326 if (pcdata != null) { 327 try { 328 length = Integer.parseInt(pcdata.getData()); 329 } catch (NumberFormatException exc) { 330 System.err.println("Bad contentlength property in " + getResource()); 331 } 332 } 333 } 334 return length; 335 } 336 339 public String getContentType() { 340 String contentType = null; 341 PropertyValue prop = getProp("DAV:getcontenttype"); 342 if (prop != null) { 343 Text pcdata = (Text) prop.value.getFirstChild(); 344 if (pcdata != null) { 345 contentType = pcdata.getData(); 346 } 347 } 348 return contentType; 349 } 350 353 public Date getCreationDate() { 354 Date date = null; 355 PropertyValue prop = getProp("DAV:creationdate"); 356 if (prop != null) { 357 Text pcdata = (Text) prop.value.getFirstChild(); 358 if (pcdata != null) { 359 String dateString = pcdata.getData(); 360 try { 361 date = (new SimpleISO8601DateFormat()).parse(dateString); 362 } catch (ParseException exc) { 363 System.err.println("Invalid date format for creationdate in " + getResource()); 364 } 365 } 366 } 367 return date; 368 } 369 372 public String getDisplayName() { 373 String displayName = null; 374 PropertyValue prop = getProp("DAV:displayname"); 375 if (prop != null) { 376 Text pcdata = (Text) prop.value.getFirstChild(); 377 if (pcdata != null) { 378 displayName = pcdata.getData(); 379 } 380 } 381 return displayName; 382 } 383 386 public String getETag() { 387 String eTag = null; 388 PropertyValue prop = getProp("DAV:getetag"); 389 if (prop != null) { 390 Text pcdata = (Text) prop.value.getFirstChild(); 391 if (pcdata != null) { 392 eTag = pcdata.getData(); 393 } 394 } 395 return eTag; 396 } 397 400 public Date getLastModifiedDate() { 401 Date date = null; 402 PropertyValue prop = getProp("DAV:getlastmodified"); 403 if (prop != null) { 404 Text pcdata = (Text) prop.value.getFirstChild(); 405 if (pcdata != null) { 406 String dateString = pcdata.getData(); 407 try { 408 date = (new SimpleRFC1123DateFormat()).parse(dateString); 409 } catch (ParseException exc) { 410 System.err.println("Invalid date format for getlastmodified in " + getResource()); 411 } 412 } 413 } 414 return date; 415 } 416 435 private PropertyValue getProp( String pnstr ) { 436 437 PropertyName pn; 438 try { 439 pn = new PropertyName( pnstr ); 440 } catch (InvalidPropertyNameException excc) { 441 throw new RuntimeException ( "internal error: bad property: "+pnstr ); 442 } 443 PropertyValue prop = getProperty( pn ); 444 return prop; 445 } 446 451 public Dictionary getPropertiesByPropName() { 452 return pnproperties; 453 } 454 455 public Dictionary getPropertyDefinitionsByPropName() { 456 return this.propertydefs; 457 } 458 465 public PropertyValue getProperty( PropertyName name) { 466 return (PropertyValue) pnproperties.get(name); 467 } 468 473 public Enumeration getPropertyNamesPN() { 474 return pnproperties.keys(); 475 } 476 480 public String getResourceType() { 481 String resourceType = null; 482 PropertyValue prop = getProp("DAV:resourcetype"); 483 if (prop != null) { 484 Element type = (Element) prop.value.getFirstChild(); 485 if (type != null) { 486 resourceType = type.getTagName(); 487 } 488 } 489 return resourceType; 490 } 491 495 public boolean isOK() { 496 boolean isOk = true; 497 Enumeration propertyValues = getPropertiesByPropName().elements(); 498 while (isOk && propertyValues.hasMoreElements()) { 499 PropertyValue propertyValue = (PropertyValue) propertyValues.nextElement(); 500 isOk = isOk && propertyValue.status < 300; 501 } 502 return isOk; 503 } 504 509 public boolean isOnACollection() { 510 PropertyValue resourcetype = getProp("DAV:resourcetype"); 511 boolean isACollection = false; 512 if (resourcetype != null) { 513 Element value = (Element) resourcetype.value; 514 isACollection = value.getElementsByTagNameNS("DAV:","collection").getLength()>0; 515 } 516 return isACollection; 517 } 518 524 public void removeProperty( PropertyName propertyName) throws ServerException { 525 if (pnproperties.contains(propertyName)) { 526 strxproperties.remove( propertyName.asExpandedString() ); 527 pnproperties.remove( propertyName ); 528 } 529 } 530 535 public void setProperty( PropertyName name, PropertyValue value) { 536 strxproperties.put( name.asExpandedString(), name); 537 pnproperties.put(name, value); 538 } 539 544 public PropertyResponse toPropertyResponse() { 545 return this; 546 } 547 } 548 | Popular Tags |