001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019:
020: package org.openharmonise.dav.server.property.ranges;
021:
022: import java.util.*;
023:
024: import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
025: import org.openharmonise.commons.xml.namespace.NamespaceType;
026: import org.openharmonise.dav.server.managers.HarmonisePropertiesManager;
027: import org.openharmonise.dav.server.utils.*;
028: import org.openharmonise.rm.DataAccessException;
029: import org.openharmonise.rm.factory.*;
030: import org.openharmonise.rm.resources.*;
031: import org.openharmonise.rm.resources.metadata.properties.PropertyGroup;
032: import org.openharmonise.rm.resources.metadata.properties.ranges.*;
033: import org.w3c.dom.*;
034:
035: import com.ibm.webdav.*;
036:
037: /**
038: * A subclass of <code>DAVRange</code> which handles
039: * compound property ranges, i.e. a range which specifies that a
040: * property has values which are property instances of other properties.
041: *
042: * @author Michael Bell
043: * @version $Revision: 1.2 $
044: * @since November 20, 2003
045: */
046: public class DAVPropertyRange extends DAVRange {
047:
048: /**
049: * @param dsi
050: */
051: public DAVPropertyRange(AbstractDataStoreInterface dsi) {
052: super (dsi, new ProfileRange());
053: }
054:
055: /**
056: * @param dsi
057: * @param range
058: */
059: public DAVPropertyRange(AbstractDataStoreInterface dsi, Range range) {
060: super (dsi, range);
061: }
062:
063: /**
064: * @param dsi
065: * @param davPropEl
066: */
067: public DAVPropertyRange(AbstractDataStoreInterface dsi,
068: Element davPropEl) throws WebDAVException {
069: super (dsi, new ProfileRange(), davPropEl);
070:
071: }
072:
073: /* (non-Javadoc)
074: * @see org.openharmonise.dav.server.property.ranges.DAVRange#addRangeDetails(org.w3c.dom.Element, org.openharmonise.rm.resources.metadata.properties.ranges.Range, org.w3c.dom.Document)
075: */
076: protected void addRangeDetails(Element rangeEl, Range range,
077: Document doc) throws WebDAVException {
078: Element resourceTypeEl = doc.createElementNS(NamespaceType.DAV
079: .getURI(), HarmonisePropertiesManager.TAG_RESOURCETYPE);
080: resourceTypeEl.setPrefix(NamespaceType.DAV.getPrefix());
081:
082: Element propResEl = doc.createElementNS(NamespaceType.DAV
083: .getURI(),
084: HarmonisePropertiesManager.TAG_PROPERTY_RESOURCE);
085: propResEl.setPrefix(NamespaceType.DAV.getPrefix());
086: resourceTypeEl.appendChild(propResEl);
087: rangeEl.appendChild(resourceTypeEl);
088:
089: List allowedPropGroups = ((ProfileRange) m_range)
090: .getAllowedPropertyParents();
091:
092: Iterator iter = allowedPropGroups.iterator();
093: try {
094: while (iter.hasNext()) {
095: String sPath = (String) iter.next();
096: if (sPath != null) {
097: AbstractParentObject parent = (AbstractParentObject) HarmoniseObjectFactory
098: .instantiateHarmoniseObject(m_dsi,
099: PropertyGroup.class.getName(),
100: sPath);
101: String sDAVPath = HarmoniseNameResolver
102: .getDAVPath(parent);
103: if (sDAVPath != null) {
104: Element hrefEl = doc.createElementNS(
105: NamespaceType.DAV.getURI(),
106: HarmonisePropertiesManager.TAG_HREF);
107: hrefEl.setPrefix(NamespaceType.DAV.getPrefix());
108: hrefEl
109: .appendChild(doc
110: .createTextNode(sDAVPath));
111: rangeEl.appendChild(hrefEl);
112: }
113:
114: }
115: }
116:
117: } catch (HarmoniseFactoryException e) {
118: throw new WebDAVException(
119: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
120: .getLocalizedMessage());
121: } catch (NameResolverException e) {
122: throw new WebDAVException(
123: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
124: .getLocalizedMessage());
125: }
126:
127: }
128:
129: /* (non-Javadoc)
130: * @see org.openharmonise.dav.server.property.ranges.DAVRange#populate(org.w3c.dom.Element)
131: */
132: public void populate(Element propEl) throws WebDAVException {
133: try {
134: //deal with hrefs
135: NodeList hrefNodes = propEl.getElementsByTagNameNS(
136: NamespaceType.DAV.getURI(),
137: HarmonisePropertiesManager.TAG_HREF);
138:
139: List ohPaths = new ArrayList();
140: AbstractParentObject parent = null;
141:
142: String sParentClassName = null;
143: for (int i = 0; i < hrefNodes.getLength(); i++) {
144: Element hrefEl = (Element) hrefNodes.item(i);
145:
146: String hrefVal = hrefEl.getChildNodes().item(0)
147: .getNodeValue();
148:
149: if (hrefVal != null && hrefVal.length() > 0) {
150: AbstractChildObject child = HarmoniseNameResolver
151: .getObjectFromURL(m_dsi, hrefVal);
152:
153: if ((child instanceof PropertyGroup) == false) {
154: throw new WebDAVException(
155: WebDAVStatus.SC_BAD_REQUEST,
156: "Invalid path - " + hrefVal);
157: }
158:
159: ohPaths.add(child.getFullPath());
160: parent = (AbstractParentObject) child;
161:
162: //ensure that we're not mixing parent types in the hrefs
163: if (sParentClassName != null
164: && parent.getClass().getName().equals(
165: sParentClassName) == false) {
166: throw new WebDAVException(
167: WebDAVStatus.SC_BAD_REQUEST,
168: "Invalid set of paths");
169: }
170:
171: sParentClassName = parent.getClass().getName();
172: }
173:
174: }
175:
176: ((ProfileRange) m_range).setAllowedPropertyParents(ohPaths);
177: } catch (DataAccessException e) {
178: throw new WebDAVException(
179: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
180: .getLocalizedMessage());
181: } catch (NameResolverException e) {
182: throw new WebDAVException(
183: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
184: .getLocalizedMessage());
185: }
186:
187: }
188:
189: }
|