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.XMLUtils;
026: import org.openharmonise.commons.xml.namespace.NamespaceType;
027: import org.openharmonise.dav.server.managers.HarmonisePropertiesManager;
028: import org.openharmonise.dav.server.utils.*;
029: import org.openharmonise.rm.DataAccessException;
030: import org.openharmonise.rm.factory.*;
031: import org.openharmonise.rm.resources.*;
032: import org.openharmonise.rm.resources.content.*;
033: import org.openharmonise.rm.resources.metadata.properties.Property;
034: import org.openharmonise.rm.resources.metadata.properties.ranges.*;
035: import org.openharmonise.rm.resources.metadata.values.Value;
036: import org.w3c.dom.*;
037: import org.w3c.dom.Document;
038:
039: import com.ibm.webdav.*;
040:
041: /**
042: * Class to provide functionaliy to represent resource ranges.
043: *
044: * @author Michael Bell
045: * @version $Revision: 1.2 $
046: * @since November 20, 2003
047: */
048: public class DAVResourceRange extends DAVRange {
049:
050: /**
051: * @param dsi
052: */
053: public DAVResourceRange(AbstractDataStoreInterface dsi) {
054: super (dsi, new AbsoluteChildObjectRange());
055:
056: }
057:
058: /**
059: * @param dsi
060: * @param range
061: */
062: public DAVResourceRange(AbstractDataStoreInterface dsi, Range range) {
063: super (dsi, range);
064: }
065:
066: /**
067: * @param dsi
068: * @param davPropEl
069: */
070: public DAVResourceRange(AbstractDataStoreInterface dsi,
071: Element davPropEl) throws WebDAVException {
072: super (dsi, new AbsoluteChildObjectRange(), davPropEl);
073: }
074:
075: /**
076: * @param dsi
077: * @param davPropEl
078: */
079: public DAVResourceRange(AbstractDataStoreInterface dsi,
080: Range range, Element davPropEl) throws WebDAVException {
081: super (dsi, range, davPropEl);
082: }
083:
084: /**
085: * Adds range details for to the given range element for child object ranges
086: *
087: * @param rangeEl
088: * @param range
089: * @param doc
090: * @throws WebDAVException
091: */
092: protected void addRangeDetails(Element rangeEl, Range range,
093: Document doc) throws WebDAVException {
094:
095: try {
096:
097: Element resourceTypeEl = doc.createElementNS(
098: NamespaceType.DAV.getURI(),
099: HarmonisePropertiesManager.TAG_RESOURCETYPE);
100: resourceTypeEl.setPrefix(NamespaceType.DAV.getPrefix());
101:
102: String sRangeObj = ((ChildObjectRange) range)
103: .getChildObjectValueClassName((AbstractChildObject) null);
104: String sResourceType = HarmoniseNameResolver
105: .getResourceTypeFromClass(sRangeObj);
106:
107: Element valEl = doc.createElementNS(NamespaceType.DAV
108: .getURI(), sResourceType);
109: valEl.setPrefix(NamespaceType.DAV.getPrefix());
110: resourceTypeEl.appendChild(valEl);
111: rangeEl.appendChild(resourceTypeEl);
112:
113: Class clss = Class.forName(sRangeObj);
114:
115: AbstractChildObject child = (AbstractChildObject) clss
116: .newInstance();
117:
118: String sParentClass = child.getParentObjectClassName();
119:
120: List allowedParents = ((AbsoluteChildObjectRange) range)
121: .getAllowedParents();
122:
123: Iterator iter = allowedParents.iterator();
124:
125: while (iter.hasNext()) {
126: String sPath = (String) iter.next();
127:
128: AbstractParentObject parent = (AbstractParentObject) HarmoniseObjectFactory
129: .instantiateHarmoniseObject(m_dsi,
130: sParentClass, sPath);
131:
132: if (parent != null) {
133: String sDAVPath = HarmoniseNameResolver
134: .getDAVPath(parent);
135: Element hrefEl = doc.createElementNS(
136: NamespaceType.DAV.getURI(),
137: HarmonisePropertiesManager.TAG_HREF);
138: hrefEl.setPrefix(NamespaceType.DAV.getPrefix());
139: hrefEl.appendChild(doc.createTextNode(sDAVPath));
140: rangeEl.appendChild(hrefEl);
141: }
142: }
143:
144: } catch (DataAccessException e) {
145: throw new WebDAVException(
146: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
147: .getLocalizedMessage());
148: } catch (HarmoniseFactoryException e) {
149: throw new WebDAVException(
150: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
151: .getLocalizedMessage());
152: } catch (NameResolverException e) {
153: throw new WebDAVException(
154: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
155: .getLocalizedMessage());
156: } catch (ClassNotFoundException e) {
157: throw new WebDAVException(
158: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
159: .getLocalizedMessage());
160: } catch (InstantiationException e) {
161: throw new WebDAVException(
162: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
163: .getLocalizedMessage());
164: } catch (IllegalAccessException e) {
165: throw new WebDAVException(
166: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
167: .getLocalizedMessage());
168: }
169: }
170:
171: /* (non-Javadoc)
172: * @see org.openharmonise.dav.server.property.ranges.DAVRange#populate(org.w3c.dom.Element)
173: */
174: public void populate(Element propEl) throws WebDAVException {
175: try {
176: //get resource type
177: String sResourceType = null;
178:
179: Element resourceEl = XMLUtils.getFirstNamedChild(propEl,
180: HarmonisePropertiesManager.TAG_RESOURCETYPE);
181:
182: if (resourceEl != null) {
183: Element resourceTypeEl = XMLUtils
184: .getFirstElementChild(resourceEl);
185: sResourceType = resourceTypeEl.getLocalName();
186: }
187:
188: //deal with hrefs
189: NodeList hrefNodes = propEl.getElementsByTagNameNS(
190: NamespaceType.DAV.getURI(),
191: HarmonisePropertiesManager.TAG_HREF);
192:
193: List ohPaths = new ArrayList();
194: AbstractParentObject parent = null;
195:
196: String sParentClassName = null;
197: for (int i = 0; i < hrefNodes.getLength(); i++) {
198: Element hrefEl = (Element) hrefNodes.item(i);
199:
200: String hrefVal = hrefEl.getChildNodes().item(0)
201: .getNodeValue();
202:
203: if (hrefVal != null && hrefVal.length() > 0) {
204: AbstractChildObject child = HarmoniseNameResolver
205: .getObjectFromURL(m_dsi, hrefVal);
206:
207: ohPaths.add(child.getFullPath());
208: parent = (AbstractParentObject) child;
209:
210: //ensure that we're not mixing parent types in the hrefs
211: if (sParentClassName != null
212: && parent.getClass().getName().equals(
213: sParentClassName) == false) {
214: throw new WebDAVException(
215: WebDAVStatus.SC_BAD_REQUEST,
216: "Invalid set of paths");
217: }
218:
219: sParentClassName = parent.getClass().getName();
220: }
221:
222: }
223:
224: ((AbsoluteChildObjectRange) m_range)
225: .setAllowedParents(ohPaths);
226: String sClassname = null;
227: //get object type
228: if (ohPaths.size() > 0) {
229: if (sResourceType
230: .equals(HarmonisePropertiesManager.TAG_COLLECTION)) {
231: sClassname = parent.getClass().getName();
232: } else if (sResourceType.equals(TAG_VALUE)) {
233: sClassname = Value.class.getName();
234: } else if (sResourceType
235: .equals(HarmonisePropertiesManager.TAG_PROPERTY_RESOURCE) == true) {
236: sClassname = Property.class.getName();
237: } else {
238: List childClassTypes = parent.getChildClassNames();
239: String parentName = parent.getClass().getName();
240: Iterator iter = childClassTypes.iterator();
241:
242: while (iter.hasNext() && sClassname == null) {
243: String sTmpClass = (String) iter.next();
244: if (sTmpClass.equals(parentName) == false) {
245:
246: if (childClassTypes.size() == 2) {
247: sClassname = sTmpClass;
248: } else {
249: //get class name by content type - should only be needed
250: //for Asset/Document distinction
251: if (parentName.equals(Section.class
252: .getName()) == true) {
253: if (parent
254: .getFullPath()
255: .startsWith(
256: HarmoniseNameResolver.ASSETS_PATH)) {
257: sClassname = Asset.class
258: .getName();
259: } else {
260: sClassname = org.openharmonise.rm.resources.content.Document.class
261: .getName();
262: }
263: }
264: }
265:
266: }
267: }
268:
269: }
270: } else {
271: sClassname = HarmoniseNameResolver
272: .getClassFromResourceTypeDesc(sResourceType,
273: null);
274: }
275:
276: ((AbsoluteChildObjectRange) m_range)
277: .setObjectRestriction(sClassname);
278:
279: } catch (DataAccessException e) {
280: throw new WebDAVException(
281: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
282: .getLocalizedMessage());
283: } catch (NameResolverException e) {
284: throw new WebDAVException(
285: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
286: .getLocalizedMessage());
287: }
288:
289: }
290:
291: }
|