1 23 24 package org.apache.slide.webdav.method.report; 25 26 import java.io.IOException ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.apache.slide.common.NamespaceAccessToken; 31 import org.apache.slide.common.PropertyParseException; 32 import org.apache.slide.common.RequestedProperties; 33 import org.apache.slide.common.RequestedPropertiesImpl; 34 import org.apache.slide.common.RequestedProperty; 35 import org.apache.slide.common.ServiceAccessException; 36 import org.apache.slide.common.SlideException; 37 import org.apache.slide.common.SlideToken; 38 import org.apache.slide.content.NodeRevisionNumber; 39 import org.apache.slide.search.RequestedResource; 40 import org.apache.slide.search.SearchQuery; 41 import org.apache.slide.search.basic.Literals; 42 import org.apache.slide.structure.SubjectNode; 43 import org.apache.slide.webdav.WebdavServletConfig; 44 import org.apache.slide.webdav.util.AclConstants; 45 import org.apache.slide.webdav.util.PreconditionViolationException; 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.jdom.Element; 50 import org.jdom.Namespace; 51 import org.jdom.output.XMLOutputter; 52 53 54 58 public class PrincipalMatchReport extends AbstractReport implements AclConstants { 59 60 private RequestedProperties requestedProperties = null; 61 private SubjectNode matchSubjectNode = null; 62 63 72 public PrincipalMatchReport(SlideToken slideToken, NamespaceAccessToken token, WebdavServletConfig config, String slideContextPath) { 73 super(slideToken, token, config, slideContextPath); 74 } 75 76 84 public void init(String resourcePath, Element principalMatchElm) throws PreconditionViolationException { 85 if (principalMatchElm.getChildren().size() == 0) { 86 throw new PreconditionViolationException( 87 new ViolatedPrecondition("at-least-one-child-element", 88 WebdavStatus.SC_BAD_REQUEST, 89 "DAV:principal-match element must have at least one child"), 90 resourcePath 91 ); 92 } 93 List principalPropertyElmL = principalMatchElm.getChildren(E_PRINCIPAL_PROPERTY, DNSP); 94 List selfElmL = principalMatchElm.getChildren(E_SELF, DNSP); 95 if (principalPropertyElmL.size()+selfElmL.size() != 1) { 96 throw new PreconditionViolationException( 97 new ViolatedPrecondition("either-one-principal-property-or-one-self-element", 98 WebdavStatus.SC_BAD_REQUEST, 99 "DAV:principal-match element must contain either a DAV:principal-property or a DAV:self element"), 100 resourcePath 101 ); 102 } 103 if (principalPropertyElmL.size() == 1) { 104 Element principalPropertyElm = (Element)principalPropertyElmL.get(0); 105 if (principalPropertyElm.getChildren(E_OWNER, DNSP).size() > 0) { 106 matchSubjectNode = SubjectNode.OWNER; 107 } 108 else { 109 } 111 } 112 else { 113 matchSubjectNode = SubjectNode.SELF; 114 } 115 List propElmL = principalMatchElm.getChildren(E_PROP, DNSP); 116 if (propElmL.size() > 1) { 117 throw new PreconditionViolationException( 118 new ViolatedPrecondition("at-most-one-prop-element", 119 WebdavStatus.SC_BAD_REQUEST, 120 "DAV:principal-match element must have at most one DAV:prop child"), 121 resourcePath 122 ); 123 } 124 125 if (propElmL.size() == 1) { 126 Element propElm = (Element)propElmL.get(0); 127 try { 128 this.requestedProperties = new RequestedPropertiesImpl(propElm); 129 } 130 catch (PropertyParseException e) { 131 throw new PreconditionViolationException( 132 new ViolatedPrecondition("invalid-prop", 133 WebdavStatus.SC_BAD_REQUEST, 134 e.getMessage()), 135 resourcePath 136 ); 137 } 138 } 139 } 140 141 151 public void execute(String resourcePath, Element multistatusElm, int depth) throws SlideException, IOException { 152 SubjectNode currentUserNode = (SubjectNode)security.getPrincipal(slideToken); 153 Element queryElm = getQueryElement(resourcePath, currentUserNode); 154 new XMLOutputter(org.jdom.output.Format.getPrettyFormat()).output(queryElm, System.out); 155 156 String absUri = WebdavUtils.getAbsolutePath (resourcePath, slideContextPath, config); 157 158 SearchQuery query = 159 search.createSearchQuery(queryElm.getNamespaceURI(), 160 queryElm, 161 slideToken, 162 config.getDepthLimit(), 163 absUri); 164 165 Iterator result = query.execute().iterator(); 166 while (result.hasNext()) { 167 RequestedResource r = (RequestedResource)result.next(); 168 multistatusElm.addContent(getResponseElement(slideToken, r.getUri(), new NodeRevisionNumber(), requestedProperties)); 169 } 170 171 } 172 173 private Element getQueryElement(String resourcePath, SubjectNode currentUserNode) throws SlideException { 174 if (matchSubjectNode.equals(SubjectNode.OWNER)) { 175 return getOwnerQueryElement(resourcePath, currentUserNode); 176 } 177 else if (matchSubjectNode.equals(SubjectNode.SELF)) { 178 return getSelfQueryElement(resourcePath, currentUserNode); 179 } 180 else { 181 return null; 183 } 184 } 185 186 private Element getSelfQueryElement(String resourcePath, SubjectNode currentUserNode) { 187 Element result = new Element(Literals.BASICSEARCH, DNSP); 188 Element selectElm = new Element(Literals.SELECT, DNSP); 190 result.addContent(selectElm); 191 Element propElm = new Element(E_PROP, DNSP); 192 selectElm.addContent(propElm); 193 Iterator props = requestedProperties.getRequestedProperties(); 194 while (props.hasNext()) { 195 RequestedProperty p = (RequestedProperty)props.next(); 196 Namespace nsp = (DNSP.getURI().equals(p.getNamespace())) 197 ? DNSP 198 : Namespace.getNamespace(p.getNamespace()); 199 propElm.addContent(new Element(p.getName(), nsp)); 200 } 201 Element fromElm = new Element(Literals.FROM, DNSP); 203 result.addContent(fromElm); 204 Element scopeElm = new Element(Literals.SCOPE, DNSP); 205 fromElm.addContent(scopeElm); 206 Element hrefElm = new Element(E_HREF, DNSP); 207 hrefElm.setText(WebdavUtils.getAbsolutePath (resourcePath, slideContextPath, config)); 208 209 scopeElm.addContent(hrefElm); 210 Element whereElm = new Element(Literals.WHERE, DNSP); 212 result.addContent(whereElm); 213 Element propcontainsElm = new Element("propcontains", Namespace.getNamespace("http://jakarta.apache.org/slide/")); 214 whereElm.addContent(propcontainsElm); 215 propElm = new Element(E_PROP, DNSP); 216 propElm.addContent(new Element(P_GROUP_MEMBER_SET, DNSP)); 217 Element literalElm = new Element(Literals.LITERAL, DNSP); 218 literalElm.addContent("/"+currentUserNode.getPath().lastSegment()+"<"); 219 propcontainsElm.addContent(propElm); 220 propcontainsElm.addContent(literalElm); 221 return result; 222 } 223 224 private Element getOwnerQueryElement(String resourcePath, SubjectNode currentUserNode) { 225 Element result = new Element(Literals.BASICSEARCH, DNSP); 226 Element selectElm = new Element(Literals.SELECT, DNSP); 228 result.addContent(selectElm); 229 Element propElm = new Element(E_PROP, DNSP); 230 selectElm.addContent(propElm); 231 Iterator props = requestedProperties.getRequestedProperties(); 232 while (props.hasNext()) { 233 RequestedProperty p = (RequestedProperty)props.next(); 234 Namespace nsp = (DNSP.getURI().equals(p.getNamespace())) 235 ? DNSP 236 : Namespace.getNamespace(p.getNamespace()); 237 propElm.addContent(new Element(p.getName(), nsp)); 238 } 239 Element fromElm = new Element(Literals.FROM, DNSP); 241 result.addContent(fromElm); 242 Element scopeElm = new Element(Literals.SCOPE, DNSP); 243 fromElm.addContent(scopeElm); 244 Element hrefElm = new Element(E_HREF, DNSP); 245 hrefElm.setText(WebdavUtils.getAbsolutePath (resourcePath, slideContextPath, config)); 246 247 scopeElm.addContent(hrefElm); 248 Element whereElm = new Element(Literals.WHERE, DNSP); 250 result.addContent(whereElm); 251 Element eqElm = new Element(Literals.EQ, DNSP); 252 whereElm.addContent(eqElm); 253 propElm = new Element(E_PROP, DNSP); 254 propElm.addContent(new Element(P_OWNER, DNSP)); 255 Element literalElm = new Element(Literals.LITERAL, DNSP); 256 literalElm.addContent(currentUserNode.getPath().lastSegment()); 257 eqElm.addContent(propElm); 258 eqElm.addContent(literalElm); 259 return result; 260 } 261 262 269 public void checkPreconditions(String resourcePath, int depth) throws PreconditionViolationException, ServiceAccessException { 270 if (depth != 0) { 271 throw new PreconditionViolationException( 272 new ViolatedPrecondition("depth-must-be-zero", 273 WebdavStatus.SC_BAD_REQUEST, 274 "This report is only defined for depth=0."), 275 resourcePath 276 ); 277 } 278 } 279 } 280 | Popular Tags |