1 23 24 package org.apache.slide.webdav.method.report; 25 26 import java.io.IOException ; 27 import java.util.ArrayList ; 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.RequestedProperty; 36 import org.apache.slide.common.ServiceAccessException; 37 import org.apache.slide.common.SlideException; 38 import org.apache.slide.common.SlideToken; 39 import org.apache.slide.content.NodeRevisionNumber; 40 import org.apache.slide.search.RequestedResource; 41 import org.apache.slide.search.SearchQuery; 42 import org.apache.slide.search.basic.Literals; 43 import org.apache.slide.structure.SubjectNode; 44 import org.apache.slide.webdav.WebdavServletConfig; 45 import org.apache.slide.webdav.util.AclConstants; 46 import org.apache.slide.webdav.util.PreconditionViolationException; 47 import org.apache.slide.webdav.util.ViolatedPrecondition; 48 import org.apache.slide.webdav.util.WebdavStatus; 49 import org.apache.slide.webdav.util.WebdavUtils; 50 import org.jdom.Element; 51 import org.jdom.Namespace; 52 import org.jdom.output.XMLOutputter; 53 54 58 public class PrincipalPropertySearchReport extends AbstractReport implements AclConstants { 59 60 private RequestedProperties requestedProperties = null; 61 private List conditionList = null; 62 63 72 public PrincipalPropertySearchReport(SlideToken slideToken, NamespaceAccessToken token, WebdavServletConfig config, String slideContextPath) { 73 super(slideToken, token, config, slideContextPath); 74 } 75 76 84 public void init(String resourcePath, Element principalPropertySearchElm) throws PreconditionViolationException { 85 if (principalPropertySearchElm.getChildren().size() == 0) { 86 throw new PreconditionViolationException( 87 new ViolatedPrecondition("at-least-one-child-element", 88 WebdavStatus.SC_BAD_REQUEST, 89 "DAV:principal-property-search element must have at least one child"), 90 resourcePath 91 ); 92 } 93 List propertySearchElmL = principalPropertySearchElm.getChildren(E_PROPERTY_SEARCH, DNSP); 94 if (propertySearchElmL.size() == 0) { 95 throw new PreconditionViolationException( 96 new ViolatedPrecondition("at-least-one-property-search-element", 97 WebdavStatus.SC_BAD_REQUEST, 98 "DAV:principal-property-search element must contain at least one DAV:property-search element"), 99 resourcePath 100 ); 101 } 102 Iterator ps = propertySearchElmL.iterator(); 103 this.conditionList = new ArrayList (); 104 while (ps.hasNext()) { 105 Element propertySearchElm = (Element)ps.next(); 106 Element matchElm = propertySearchElm.getChild(E_MATCH, DNSP); 107 if (matchElm == null) { 108 continue; 109 } 110 Element propElm = propertySearchElm.getChild(E_PROP, DNSP); 111 Iterator pElms = propElm.getChildren().iterator(); 112 while (pElms.hasNext()) { 113 Element pElm = (Element)pElms.next(); 114 Element newpropElm = new Element(E_PROP, DNSP); 115 newpropElm.addContent(new Element(pElm.getName(), pElm.getNamespace())); 116 Element propcontainsElm = new Element("propcontains", Namespace.getNamespace("http://jakarta.apache.org/slide/")); 117 Element literalElm = new Element(Literals.LITERAL, DNSP); 118 literalElm.addContent(matchElm.getTextTrim()); 119 propcontainsElm.addContent(newpropElm); 120 propcontainsElm.addContent(literalElm); 121 conditionList.add(propcontainsElm); 122 } 123 } 124 List propElmL = principalPropertySearchElm.getChildren(E_PROP, DNSP); 125 if (propElmL.size() > 1) { 126 throw new PreconditionViolationException( 127 new ViolatedPrecondition("at-most-one-prop-element", 128 WebdavStatus.SC_BAD_REQUEST, 129 "DAV:principal-property-search element must have at most one DAV:prop child"), 130 resourcePath 131 ); 132 } 133 134 if (propElmL.size() == 1) { 135 Element propElm = (Element)propElmL.get(0); 136 try { 137 this.requestedProperties = new RequestedPropertiesImpl(propElm); 138 } 139 catch (PropertyParseException e) { 140 throw new PreconditionViolationException( 141 new ViolatedPrecondition("invalid-prop", 142 WebdavStatus.SC_BAD_REQUEST, 143 e.getMessage()), 144 resourcePath 145 ); 146 } 147 } 148 } 149 150 160 public void execute(String resourcePath, Element multistatusElm, int depth) throws SlideException, IOException { 161 SubjectNode currentUserNode = (SubjectNode)security.getPrincipal(slideToken); 162 Element queryElm = getQueryElement(resourcePath, currentUserNode); 163 new XMLOutputter(org.jdom.output.Format.getPrettyFormat()).output(queryElm, System.out); 164 165 String absUri = WebdavUtils.getAbsolutePath (resourcePath, slideContextPath, config); 166 167 SearchQuery query = 168 search.createSearchQuery(queryElm.getNamespaceURI(), 169 queryElm, 170 slideToken, 171 config.getDepthLimit(), 172 absUri); 173 174 Iterator result = query.execute().iterator(); 175 while (result.hasNext()) { 176 RequestedResource r = (RequestedResource)result.next(); 177 multistatusElm.addContent(getResponseElement(slideToken, r.getUri(), new NodeRevisionNumber(), requestedProperties)); 178 } 179 180 } 181 182 private Element getQueryElement(String resourcePath, SubjectNode currentUserNode) { 183 Element result = new Element(Literals.BASICSEARCH, DNSP); 184 Element selectElm = new Element(Literals.SELECT, DNSP); 186 result.addContent(selectElm); 187 Element propElm = new Element(E_PROP, DNSP); 188 selectElm.addContent(propElm); 189 Iterator props = requestedProperties.getRequestedProperties(); 190 while (props.hasNext()) { 191 RequestedProperty p = (RequestedProperty)props.next(); 192 Namespace nsp = (DNSP.getURI().equals(p.getNamespace())) 193 ? DNSP 194 : Namespace.getNamespace(p.getNamespace()); 195 propElm.addContent(new Element(p.getName(), nsp)); 196 } 197 Element fromElm = new Element(Literals.FROM, DNSP); 199 result.addContent(fromElm); 200 Element scopeElm = new Element(Literals.SCOPE, DNSP); 201 fromElm.addContent(scopeElm); 202 Element hrefElm = new Element(E_HREF, DNSP); 203 hrefElm.setText(WebdavUtils.getAbsolutePath (resourcePath, slideContextPath, config)); 204 205 scopeElm.addContent(hrefElm); 206 if (conditionList.size() > 0) { 208 Element whereElm = new Element(Literals.WHERE, DNSP); 209 result.addContent(whereElm); 210 if (conditionList.size() == 1) { 211 whereElm.addContent((Element)conditionList.get(0)); 212 } 213 else { 214 Element andElm = new Element(Literals.AND, DNSP); 215 whereElm.addContent(andElm); 216 Iterator conditions = conditionList.iterator(); 217 while (conditions.hasNext()) { 218 andElm.addContent((Element)conditions.next()); 219 } 220 } 221 } 222 return result; 223 } 224 225 232 public void checkPreconditions(String resourcePath, int depth) throws PreconditionViolationException, ServiceAccessException { 233 if (depth != 0) { 234 throw new PreconditionViolationException( 235 new ViolatedPrecondition("depth-must-be-zero", 236 WebdavStatus.SC_BAD_REQUEST, 237 "This report is only defined for depth=0."), 238 resourcePath 239 ); 240 } 241 } 242 } 243 | Popular Tags |