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.structure.ObjectNode; 41 import org.apache.slide.util.XMLValue; 42 import org.apache.slide.webdav.WebdavServletConfig; 43 import org.apache.slide.webdav.util.DeltavConstants; 44 import org.apache.slide.webdav.util.PreconditionViolationException; 45 import org.apache.slide.webdav.util.UriHandler; 46 import org.apache.slide.webdav.util.ViolatedPrecondition; 47 import org.apache.slide.webdav.util.WebdavStatus; 48 import org.apache.slide.webdav.util.WebdavUtils; 49 import org.apache.slide.webdav.util.resourcekind.AbstractResourceKind; 50 import org.apache.slide.webdav.util.resourcekind.ResourceKind; 51 import org.apache.slide.webdav.util.resourcekind.VersionControlled; 52 import org.jdom.Element; 53 import org.jdom.JDOMException; 54 55 56 60 public class LocateByHistoryReport extends AbstractReport implements DeltavConstants { 61 62 protected RequestedProperties requestedProperties = null; 63 protected XMLValue versionHistorySet = null; 64 65 74 public LocateByHistoryReport(SlideToken slideToken, NamespaceAccessToken token, WebdavServletConfig config, String slideContextPath) { 75 super(slideToken, token, config, slideContextPath); 76 } 77 78 86 public void init(String resourcePath, Element locateByHistoryElm) throws PreconditionViolationException { 87 List childrenList = locateByHistoryElm.getChildren(); 88 if (childrenList.size() != 2) { 89 throw new PreconditionViolationException( 90 new ViolatedPrecondition("invalid-locate-by-history", 91 WebdavStatus.SC_BAD_REQUEST, 92 "DAV:locate-by-history element must have 2 children: DAV:version-history-set and DAV:prop"), 93 resourcePath 94 ); 95 } 96 97 Element versionHistorySetElm = null; 98 Element propElm = null; 99 if ( E_VERSION_HISTORY_SET.equals(((Element)childrenList.get(0)).getName()) && 100 E_PROP.equals(((Element)childrenList.get(1)).getName()) ) { 101 versionHistorySetElm = (Element)childrenList.get(0); 102 propElm = (Element)childrenList.get(1); 103 } 104 else if ( E_PROP.equals(((Element)childrenList.get(0)).getName()) && 105 E_VERSION_HISTORY_SET.equals(((Element)childrenList.get(1)).getName()) ) { 106 propElm = (Element)childrenList.get(0); 107 versionHistorySetElm = (Element)childrenList.get(1); 108 } 109 else { 110 throw new PreconditionViolationException( 111 new ViolatedPrecondition("invalid-locate-by-history", 112 WebdavStatus.SC_BAD_REQUEST, 113 "DAV:locate-by-history element must have 2 children: DAV:version-history-set and DAV:prop"), 114 resourcePath 115 ); 116 } 117 118 if ( ! WebdavUtils.isCollection(token, slideToken, resourcePath) ) { 120 throw new PreconditionViolationException( 121 new ViolatedPrecondition("must-be-collection", 122 WebdavStatus.SC_BAD_REQUEST, 123 "the request-URI must specify a collection"), 124 resourcePath 125 ); 126 } 127 128 this.versionHistorySet = 129 new XMLValue(versionHistorySetElm.getChildren(E_HREF, DNSP)); 130 131 UriHandler uriHandler = null; 133 String href = null; 134 boolean isVersionHistory = false; 135 Iterator iterator = versionHistorySet.getHrefStrings().iterator(); 136 while (iterator.hasNext()) { 137 href = (String )iterator.next(); 138 uriHandler = UriHandler.getUriHandler(WebdavUtils.getSlidePath(href, slideContextPath)); 139 isVersionHistory = uriHandler.isHistoryUri(); 140 if (!isVersionHistory) { 141 break; 142 } 143 } 144 if (!isVersionHistory) { 145 throw new PreconditionViolationException( 146 new ViolatedPrecondition(C_MUST_BE_VERSION_HISTORY, 147 WebdavStatus.SC_CONFLICT), 148 resourcePath 149 ); 150 } 151 152 try { 153 this.requestedProperties = new RequestedPropertiesImpl(propElm); 154 } 155 catch (PropertyParseException e) { 156 throw new PreconditionViolationException( 157 new ViolatedPrecondition("invalid-prop", 158 WebdavStatus.SC_BAD_REQUEST, 159 e.getMessage()), 160 resourcePath 161 ); 162 } 163 } 164 165 175 public void execute(String resourcePath, Element multistatusElm, int depth) throws SlideException, IOException { 176 if (depth < 0) { 177 return; 178 } 179 180 NodeRevisionDescriptor nrd = null; 181 NodeRevisionDescriptors nrds = null; 182 ResourceKind resourceKind = null; 183 String versionHistoryUri = null; 184 ObjectNode collectionNode = structure.retrieve(slideToken, resourcePath); 185 Enumeration children = structure.getChildren(slideToken, collectionNode); 186 while (children.hasMoreElements()) { 187 188 ObjectNode child = (ObjectNode)children.nextElement(); 189 if (child.hasChildren()) { 190 execute(child.getUri(), multistatusElm, depth-1); 191 } 192 else { 193 nrds = content.retrieve(slideToken, child.getUri()); 194 nrd = content.retrieve(slideToken, nrds); 195 resourceKind = AbstractResourceKind.determineResourceKind(token, child.getUri(), nrd); 196 versionHistoryUri = null; 197 198 if (resourceKind instanceof VersionControlled) { 199 versionHistoryUri = getHistoryUriOfVCR(nrd); 200 } 201 202 if (versionHistoryUri != null) { 203 boolean found = false; 204 Iterator iterator = versionHistorySet.iterator(); 205 String currentHistoryUri = null; 206 while ( !found && iterator.hasNext() ) { 207 currentHistoryUri = ((Element)iterator.next()).getText(); 208 if (currentHistoryUri != null) { 209 currentHistoryUri = WebdavUtils.getSlidePath(currentHistoryUri, slideContextPath); 210 } 211 found = versionHistoryUri.equals(currentHistoryUri); 212 } 213 if (found) { 214 multistatusElm.addContent(getResponseElement(slideToken, child.getUri(), nrd.getRevisionNumber(), requestedProperties)); 215 } 216 } 217 } 218 } 219 } 220 221 private String getHistoryUriOfVCR(NodeRevisionDescriptor revisionDescriptor) { 222 223 String historyUri = null; 224 NodeProperty property = revisionDescriptor.getProperty(P_CHECKED_IN); 225 if (property == null) { 226 property = revisionDescriptor.getProperty(P_CHECKED_OUT); 227 } 228 if ( (property != null) && (property.getValue() != null) ) { 229 try { 230 XMLValue xmlValue = new XMLValue(property.getValue().toString()); 231 Iterator iterator = xmlValue.iterator(); 232 if (iterator.hasNext()) { 233 String vrUri = ((Element)iterator.next()).getText(); 234 UriHandler uriHandler = UriHandler.getUriHandler(vrUri); 235 if (uriHandler.isVersionUri()) { 236 historyUri = uriHandler.getAssociatedHistoryUri(); 237 } 238 } 239 } 240 catch (JDOMException e) {} 241 } 242 243 return historyUri; 244 } 245 } 246 247 | Popular Tags |