1 19 20 package org.openharmonise.dav.server.property.domains; 21 22 23 import java.util.*; 24 import java.util.logging.*; 25 26 import org.openharmonise.commons.dsi.AbstractDataStoreInterface; 27 import org.openharmonise.commons.net.MimeTypeMapping; 28 import org.openharmonise.commons.xml.XMLUtils; 29 import org.openharmonise.commons.xml.namespace.NamespaceType; 30 import org.openharmonise.dav.server.managers.HarmonisePropertiesManager; 31 import org.openharmonise.dav.server.utils.*; 32 import org.openharmonise.rm.*; 33 import org.openharmonise.rm.metadata.Profile; 34 import org.openharmonise.rm.resources.*; 35 import org.openharmonise.rm.resources.content.*; 36 import org.openharmonise.rm.resources.metadata.properties.Property; 37 import org.openharmonise.rm.resources.metadata.properties.domains.Domain; 38 import org.openharmonise.rm.resources.metadata.values.Value; 39 import org.w3c.dom.*; 40 import org.w3c.dom.Document ; 41 42 import com.ibm.webdav.*; 43 44 52 public class DAVDomain { 53 54 public static final String TAG_DOMAIN = "domain"; 55 public static final String TAG_MIN_OCCURS = "minOccurs"; 56 public static final String TAG_MAX_OCCURS = "maxOccurs"; 57 public static final String TYPE_DOMAIN_DATA = "domainData"; 58 public static final String TAG_CONTENT_TYPE = "contenttype"; 59 60 private AbstractDataStoreInterface m_dsi = null; 61 62 65 private static final Logger m_logger = Logger.getLogger(DAVDomain.class.getName()); 66 67 71 private List m_domain_list = null; 72 73 78 public DAVDomain(AbstractDataStoreInterface dsi) { 79 m_dsi = dsi; 80 } 81 82 86 public DAVDomain(AbstractDataStoreInterface dsi, List domains) { 87 m_domain_list = domains; 88 m_dsi = dsi; 89 90 } 91 92 98 public Element asXML(Document doc) throws WebDAVException { 99 Element domainEl = 100 doc.createElementNS(NamespaceType.DAV.getURI(), TAG_DOMAIN); 101 domainEl.setPrefix(NamespaceType.DAV.getPrefix()); 102 103 if (m_domain_list.size() > 1) { 104 105 domainEl.setAttributeNS( 106 NamespaceType.SOAP_ENCODING.getURI(), 107 NamespaceType.SOAP_ENCODING.getPrefix() 108 + ":" 109 + HarmonisePropertiesManager.ATTRIB_ARRAY_SIZE, 110 String.valueOf(m_domain_list.size())); 111 112 domainEl.setAttributeNS( 113 NamespaceType.SOAP_ENCODING.getURI(), 114 NamespaceType.SOAP_ENCODING.getPrefix() 115 + ":" 116 + HarmonisePropertiesManager.ATTRIB_ITEM_TYPE, 117 TYPE_DOMAIN_DATA); 118 domainEl.setAttribute( 119 "xmlns:" + NamespaceType.SOAP_ENCODING.getPrefix(), 120 NamespaceType.SOAP_ENCODING.getURI()); 121 122 Iterator iter = m_domain_list.iterator(); 123 124 while (iter.hasNext()) { 125 Domain domain = (Domain) iter.next(); 126 127 Element domainDataEl = 128 doc.createElementNS( 129 NamespaceType.DAV.getURI(), 130 TYPE_DOMAIN_DATA); 131 132 domainDataEl.setPrefix(NamespaceType.DAV.getPrefix()); 133 addDomainDetails(domainDataEl, domain, doc); 134 135 domainEl.appendChild(domainDataEl); 136 } 137 138 } else if (m_domain_list.size() == 1) { 139 140 addDomainDetails(domainEl, (Domain) m_domain_list.get(0), doc); 141 } 142 143 return domainEl; 144 } 145 146 153 private void addDomainDetails( 154 Element domainEl, 155 Domain domain, 156 Document doc) 157 throws WebDAVException { 158 159 try { 160 String sDomainObj = domain.getDomainClass(); 161 162 Element resourceTypeEl = 163 doc.createElementNS( 164 NamespaceType.DAV.getURI(), 165 HarmonisePropertiesManager.TAG_RESOURCETYPE); 166 resourceTypeEl.setPrefix(NamespaceType.DAV.getPrefix()); 167 168 String sResourceType = 169 HarmoniseNameResolver.getResourceTypeFromClass(sDomainObj); 170 171 if(m_logger.isLoggable(Level.FINE)) { 172 m_logger.logp(Level.FINE, this.getClass().getName(), "addDomainDetails", "domain type - " + sResourceType + ", object - " + sDomainObj); 173 } 174 175 Element resourceEl = 176 doc.createElementNS(NamespaceType.DAV.getURI(), sResourceType); 177 resourceEl.setPrefix(NamespaceType.DAV.getPrefix()); 178 179 resourceTypeEl.appendChild(resourceEl); 180 181 domainEl.appendChild(resourceTypeEl); 182 183 int nMinOccurs = domain.getMinOccurs(); 184 int nMaxOccurs = domain.getMaxOccurs(); 185 186 if (nMinOccurs > 0) { 187 Element minOccEl = doc.createElementNS(NamespaceType.DAV.getURI(), TAG_MIN_OCCURS); 188 minOccEl.setPrefix(NamespaceType.DAV.getPrefix()); 189 minOccEl.appendChild( 190 doc.createTextNode(String.valueOf(nMinOccurs))); 191 domainEl.appendChild(minOccEl); 192 } 193 194 if (nMaxOccurs > 0) { 195 Element maxOccEl = doc.createElementNS(NamespaceType.DAV.getURI(), TAG_MAX_OCCURS); 196 maxOccEl.setPrefix(NamespaceType.DAV.getPrefix()); 197 maxOccEl.appendChild( 198 doc.createTextNode(String.valueOf(nMaxOccurs))); 199 domainEl.appendChild(maxOccEl); 200 } 201 202 List domainDetails = domain.getDetails(); 203 204 Class domainClass = Class.forName(sDomainObj); 205 206 if(Profile.class.isAssignableFrom(domainClass)) { 207 domainClass = Property.class; 208 } 209 210 if(domainDetails.size() > 0) { 211 Iterator detailIter = domainDetails.iterator(); 212 213 while (detailIter.hasNext()) { 214 String sDomainDetails = (String ) detailIter.next(); 215 216 String sDAVpath = 217 HarmoniseNameResolver.getDAVPath(domainClass, sDomainDetails); 218 219 Element hrefEl = 220 doc.createElementNS( 221 NamespaceType.DAV.getURI(), 222 HarmonisePropertiesManager.TAG_HREF); 223 hrefEl.setPrefix(NamespaceType.DAV.getPrefix()); 224 hrefEl.appendChild(doc.createTextNode(sDAVpath)); 225 domainEl.appendChild(hrefEl); 226 } 227 228 } 229 230 List contentRestrictions = domain.getContentRestrictions(); 231 232 Iterator contentIter = contentRestrictions.iterator(); 233 234 while (contentIter.hasNext()) { 235 String sContentType = (String ) contentIter.next(); 236 237 Element contentEl = 238 doc.createElementNS( 239 NamespaceType.DAV.getURI(), 240 TAG_CONTENT_TYPE); 241 contentEl.setPrefix(NamespaceType.DAV.getPrefix()); 242 contentEl.appendChild(doc.createTextNode(sContentType)); 243 domainEl.appendChild(contentEl); 244 } 245 246 } catch (DataAccessException e) { 247 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 248 throw new WebDAVException( 249 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 250 e.getLocalizedMessage()); 251 } catch (ClassNotFoundException e) { 252 throw new WebDAVException( 253 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 254 e.getLocalizedMessage()); 255 } catch (NameResolverException e) { 256 throw new WebDAVException( 257 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 258 e.getLocalizedMessage()); 259 } 260 261 } 262 263 266 public void populate(Element propEl) throws WebDAVException { 267 268 if(m_domain_list == null) { 269 m_domain_list = new ArrayList(); 270 } 271 272 NodeList nodes = propEl.getChildNodes(); 273 274 if(nodes.getLength() > 0) { 275 String sArraySize = 276 propEl.getAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),HarmonisePropertiesManager.ATTRIB_ARRAY_SIZE); 277 278 String sItemType = propEl.getAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), HarmonisePropertiesManager.ATTRIB_ITEM_TYPE); 279 280 if (sArraySize != null && sArraySize.trim().length() > 0) { 281 NodeList itemNodes = propEl.getElementsByTagName(sItemType); 282 283 for (int i = 0; i < itemNodes.getLength(); i++) { 284 if (itemNodes.item(i).getNodeType() != Node.ELEMENT_NODE) { 285 continue; 286 } 287 Element tmpEl = (Element) itemNodes.item(i); 288 289 Domain domain = instantiateDomain(tmpEl); 290 291 if(domain != null) { 292 m_domain_list.add(domain); 293 }else { 294 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Invalid domain spec"); 295 } 296 } 297 298 } else { 299 Domain domain = instantiateDomain(propEl); 300 301 if(domain != null) { 302 m_domain_list.add(domain); 303 } else { 304 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Invalid domain spec"); 305 } 306 307 } 308 } 309 } 310 311 314 public List getHarmoniseDomains() { 315 return m_domain_list; 316 } 317 318 325 private Domain instantiateDomain(Element dataEl) throws WebDAVException { 326 327 328 Domain domain = new Domain(m_dsi); 329 330 try { 331 332 Element minEl = XMLUtils.getFirstNamedChild(dataEl, TAG_MIN_OCCURS); 334 335 if (minEl != null) { 336 String sMin = minEl.getChildNodes().item(0).getNodeValue(); 337 338 domain.setMinOccurs(Integer.parseInt(sMin)); 339 } 340 341 Element maxEl = XMLUtils.getFirstNamedChild(dataEl, TAG_MAX_OCCURS); 342 343 if (maxEl != null) { 344 String sMax = maxEl.getChildNodes().item(0).getNodeValue(); 345 346 if(sMax.equalsIgnoreCase("unbounded") == true) { 347 domain.setMaxOccurs(Domain.UNBOUNDED); 348 } else { 349 domain.setMaxOccurs(Integer.parseInt(sMax)); 350 } 351 } 352 353 String sResourceType = null; 355 356 Element resourceEl = XMLUtils.getFirstNamedChild(dataEl, HarmonisePropertiesManager.TAG_RESOURCETYPE); 357 358 if(resourceEl != null) { 359 Element resourceTypeEl = XMLUtils.getFirstElementChild(resourceEl); 360 sResourceType = resourceTypeEl.getLocalName(); 361 } 362 363 NodeList hrefNodes = 365 dataEl.getElementsByTagNameNS( 366 NamespaceType.DAV.getURI(), 367 HarmonisePropertiesManager.TAG_HREF); 368 369 AbstractChildObject child = null; 370 371 for (int i = 0; i < hrefNodes.getLength(); i++) { 372 Element hrefEl = (Element) hrefNodes.item(i); 373 374 String hrefVal = hrefEl.getChildNodes().item(0).getNodeValue(); 375 376 377 378 if(m_logger.isLoggable(Level.FINE)) { 379 m_logger.logp(Level.FINE, this.getClass().getName(), "instantiateDomain","domain href - " + hrefVal); 380 } 381 382 child = 383 HarmoniseNameResolver.getObjectFromURL(m_dsi, hrefVal); 384 385 386 if(child != null) { 387 domain.addDetails(child.getFullPath()); 388 } else { 389 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,"No resource found matching domain"); 390 } 391 392 393 } 394 395 NodeList contentNodes = 397 dataEl.getElementsByTagNameNS( 398 NamespaceType.DAV.getURI(), 399 TAG_CONTENT_TYPE); 400 List contentTypes = new Vector(); 401 402 for (int i = 0; i < contentNodes.getLength(); i++) { 403 Element contentEl = (Element) contentNodes.item(i); 404 405 String contentVal = 406 contentEl.getChildNodes().item(0).getNodeValue(); 407 408 contentTypes.add(contentVal); 409 domain.addContentTypeRestriction(contentVal); 410 411 } 412 413 String sDomainObj = this.getObjectType((AbstractParentObject) child, sResourceType, contentTypes); 415 416 417 Class domainClass = Class.forName(sDomainObj); 418 419 if(AbstractParentObject.class.isAssignableFrom(domainClass) 420 && domain.getMinOccurs() > 0) { 421 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Collection domains must have a minoccurs of 0"); 422 } 423 424 if(sDomainObj != null) { 425 domain.setDomainClass(sDomainObj); 426 } else { 427 domain = null; 428 } 429 430 } catch (ClassNotFoundException e) { 431 throw new WebDAVException( 432 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 433 e.getLocalizedMessage()); 434 } catch (NumberFormatException e) { 435 throw new WebDAVException( 436 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 437 e.getLocalizedMessage()); 438 } catch (PopulateException e) { 439 throw new WebDAVException( 440 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 441 e.getLocalizedMessage()); 442 } catch (DataAccessException e) { 443 throw new WebDAVException( 444 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 445 e.getLocalizedMessage()); 446 } catch (NameResolverException e) { 447 throw new WebDAVException( 448 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 449 e.getLocalizedMessage()); 450 } 451 452 return domain; 453 454 } 455 456 464 private String getObjectType(AbstractParentObject parent, String sResourceType, List contentTypes) throws DataAccessException { 465 String sObjType = null; 466 467 String parentName = parent.getClass().getName(); 468 469 if(sResourceType.equals("collection") == true) { 470 sObjType = parentName; 471 } else if(sResourceType.equals("value") == true) { 472 sObjType = Value.class.getName(); 473 } else if(sResourceType.equals("property") == true) { 474 sObjType = Profile.class.getName(); 475 } else if(sResourceType.equals("property-resource") == true) { 476 sObjType = Property.class.getName(); 477 } else { 478 List childClassTypes = parent.getChildClassNames(); 479 480 Iterator iter = childClassTypes.iterator(); 481 482 while (iter.hasNext() && sObjType == null) { 483 String className = (String ) iter.next(); 484 if(className.equals(parentName) == false) { 485 486 if(contentTypes == null || contentTypes.size() == 0) { 487 if(parentName.equals(Section.class.getName()) == true) { 488 if(parent.getFullPath().startsWith(HarmoniseNameResolver.ASSETS_PATH)) { 489 sObjType = Asset.class.getName(); 490 } else { 491 sObjType = org.openharmonise.rm.resources.content.Document.class.getName(); 492 } 493 } else { 494 sObjType = className; 495 } 496 497 } else { 498 if(parentName.equals(Section.class.getName()) == true) { 501 if(contentTypes.get(0).equals(MimeTypeMapping.XML.getMimeType()) == false || parent.getFullPath().startsWith(HarmoniseNameResolver.ASSETS_PATH)) { 502 sObjType = Asset.class.getName(); 503 } else { 504 sObjType = org.openharmonise.rm.resources.content.Document.class.getName(); 505 } 506 } 507 } 508 509 } 510 } 511 512 } 513 514 515 return sObjType; 516 } 517 518 } 519 | Popular Tags |