1 23 24 package org.apache.slide.webdav.method.report; 25 26 import java.io.IOException ; 27 import java.util.Enumeration ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import org.apache.slide.common.NamespaceAccessToken; 32 import org.apache.slide.common.PropertyParseException; 33 import org.apache.slide.common.RequestedProperties; 34 import org.apache.slide.common.RequestedPropertiesImpl; 35 import org.apache.slide.common.SlideException; 36 import org.apache.slide.common.SlideToken; 37 import org.apache.slide.content.NodeProperty; 38 import org.apache.slide.content.NodeRevisionDescriptor; 39 import org.apache.slide.content.NodeRevisionDescriptors; 40 import org.apache.slide.content.NodeRevisionNumber; 41 import org.apache.slide.structure.ObjectNode; 42 import org.apache.slide.util.Configuration; 43 import org.apache.slide.util.XMLValue; 44 import org.apache.slide.webdav.WebdavServletConfig; 45 import org.apache.slide.webdav.util.DeltavConstants; 46 import org.apache.slide.webdav.util.PreconditionViolationException; 47 import org.apache.slide.webdav.util.UriHandler; 48 import org.apache.slide.webdav.util.VersioningHelper; 49 import org.apache.slide.webdav.util.ViolatedPrecondition; 50 import org.apache.slide.webdav.util.WebdavStatus; 51 import org.apache.slide.webdav.util.resourcekind.AbstractResourceKind; 52 import org.apache.slide.webdav.util.resourcekind.ResourceKind; 53 import org.apache.slide.webdav.util.resourcekind.Version; 54 import org.apache.slide.webdav.util.resourcekind.VersionControlled; 55 import org.apache.slide.webdav.util.resourcekind.VersionHistory; 56 import org.jdom.Element; 57 import org.jdom.JDOMException; 58 59 63 public class VersionTreeReport extends AbstractReport implements DeltavConstants { 64 65 private RequestedProperties requestedProperties = null; 66 private VersioningHelper versioningHelper = null; 67 68 77 public VersionTreeReport(SlideToken slideToken, NamespaceAccessToken token, WebdavServletConfig config, String slideContextPath) { 78 super(slideToken, token, config, slideContextPath); 79 } 80 81 89 public void init(String resourcePath, Element versionTreeElm) throws PreconditionViolationException { 90 List childrenList = versionTreeElm.getChildren(E_PROP, DNSP); 91 if (childrenList.size() == 0) { 92 return; 93 } 94 else if (childrenList.size() > 1) { 95 throw new PreconditionViolationException( 96 new ViolatedPrecondition("at-most-one-prop-element", 97 WebdavStatus.SC_BAD_REQUEST, 98 "the DAV:version-tree element must contain at most one DAV:prop element"), 99 resourcePath 100 ); 101 } 102 103 Element propElm = (Element)childrenList.get(0); 104 try { 105 requestedProperties = new RequestedPropertiesImpl(propElm); 106 } 107 catch (PropertyParseException e) { 108 throw new PreconditionViolationException( 109 new ViolatedPrecondition("invalid-prop-element", 110 WebdavStatus.SC_BAD_REQUEST, 111 e.getMessage()), 112 resourcePath 113 ); 114 } 115 } 116 117 127 public void execute(String resourcePath, Element multistatusElm, int depth) throws SlideException, IOException { 128 if (depth < 0) { 129 return; 130 } 131 writeReport(resourcePath, multistatusElm); 132 ObjectNode onode = structure.retrieve(slideToken, resourcePath); 133 Enumeration childrenEnum = structure.getChildren(slideToken, onode); 134 while (childrenEnum.hasMoreElements()) { 135 ObjectNode cnode = (ObjectNode)childrenEnum.nextElement(); 136 execute(cnode.getUri(), multistatusElm, depth-1); 137 } 138 } 139 140 public void setVersioningHelper(VersioningHelper versioningHelper) { 141 this.versioningHelper = versioningHelper; 142 } 143 144 private void writeReport(String resourcePath, Element multistatusElm) throws SlideException { 145 NodeRevisionDescriptors nrds = content.retrieve(slideToken, resourcePath); 146 NodeRevisionDescriptor nrd = content.retrieve(slideToken, nrds); 147 ResourceKind resourceKind = 148 AbstractResourceKind.determineResourceKind(token, nrds, nrd); 149 150 if (resourceKind instanceof VersionControlled) { 151 resourcePath = versioningHelper.getUriOfAssociatedVR(resourcePath); 152 nrds = content.retrieve(slideToken, resourcePath); 153 nrd = content.retrieve(slideToken, nrds); 154 resourceKind = 155 AbstractResourceKind.determineResourceKind(token, nrds, nrd); 156 } 157 else if (resourceKind instanceof VersionHistory) { 158 NodeProperty rootVersion = nrd.getProperty(P_ROOT_VERSION); 159 if (rootVersion != null) { 160 XMLValue value; 161 if (rootVersion.getValue() instanceof XMLValue) { 162 value = (XMLValue)rootVersion.getValue(); 163 } else { 164 try { 165 value = new XMLValue(rootVersion.getValue().toString()); 166 } catch (JDOMException e) { 167 throw new SlideException("Could not parse DAV:root-version: "+nrds.getUri()); 168 } 169 } 170 Iterator i = value.iterator(); 171 if (i.hasNext()) { 172 resourcePath = ((Element)i.next()).getText(); 173 } 174 } 175 resourcePath += "/"; 176 resourcePath += nrds.getInitialRevision(); 177 nrds = content.retrieve(slideToken, resourcePath); 178 nrd = content.retrieve(slideToken, nrds); 179 resourceKind = 180 AbstractResourceKind.determineResourceKind(token, nrds, nrd); 181 } 182 183 if ( Configuration.useVersionControl() && (resourceKind instanceof Version) ) { 184 writeReport(nrds, slideToken, multistatusElm); 185 } 186 else { 187 multistatusElm.addContent(getErrorResponse(resourcePath, WebdavStatus.SC_FORBIDDEN, C_SUPPORTED_REPORT)); 188 } 189 } 190 191 private void writeReport(NodeRevisionDescriptors nrds, SlideToken slideToken, Element multistatusElm) throws SlideException { 192 NodeRevisionDescriptor hNrd = content.retrieve(slideToken, nrds, NodeRevisionNumber.HIDDEN_0_0); 194 NodeProperty versionSetProperty = hNrd.getProperty(P_VERSION_SET); 195 if ( (versionSetProperty != null) && (versionSetProperty.getValue() != null) ) { 196 XMLValue xmlValue; 197 try { 198 xmlValue = new XMLValue(versionSetProperty.getValue().toString()); 199 } 200 catch (JDOMException e) { 201 throw new SlideException("Could not parse DAV:version-set: "+nrds.getUri()); 202 } 203 Iterator hrefIterator = xmlValue.iterator(); 204 205 NodeRevisionNumber currentNrn= null; 206 UriHandler currentUh = null; 207 String currentPath = null; 208 209 while (hrefIterator.hasNext()) { 210 currentPath = ((Element)hrefIterator.next()).getText(); 211 currentUh = UriHandler.getUriHandler(currentPath); 212 currentNrn = new NodeRevisionNumber(currentUh.getVersionName()); 213 multistatusElm.addContent(getResponseElement(slideToken, currentPath, currentNrn, getRequestedVersionTreeProperties())); 214 } 215 } 216 } 217 218 private RequestedProperties getRequestedVersionTreeProperties() { 219 if (requestedProperties == null) { 220 requestedProperties = new RequestedPropertiesImpl(); 221 } 222 return requestedProperties; 223 } 224 } 225 226 | Popular Tags |