1 23 24 package org.apache.slide.webdav.method; 25 26 import java.io.IOException ; 27 import java.util.List ; 28 29 import org.apache.slide.common.NamespaceAccessToken; 30 import org.apache.slide.common.ServiceAccessException; 31 import org.apache.slide.common.SlideException; 32 import org.apache.slide.event.EventDispatcher; 33 import org.apache.slide.lock.ObjectLockedException; 34 import org.apache.slide.structure.CrossServerBindingException; 35 import org.apache.slide.structure.ObjectNode; 36 import org.apache.slide.webdav.WebdavException; 37 import org.apache.slide.webdav.WebdavServletConfig; 38 import org.apache.slide.webdav.event.WebdavEvent; 39 import org.apache.slide.webdav.util.BindConstants; 40 import org.apache.slide.webdav.util.PreconditionViolationException; 41 import org.apache.slide.webdav.util.ViolatedPrecondition; 42 import org.apache.slide.webdav.util.WebdavStatus; 43 import org.jdom.JDOMException; 44 45 46 50 public class BindMethod extends AbstractWebdavMethod implements BindConstants, WriteMethod { 51 52 private String sourceUri; 53 private String collectionUri; 54 private String segment; 55 private boolean overwrite; 56 private ObjectNode sourceNode = null; 57 private ObjectNode collectionNode = null; 58 59 60 66 public BindMethod(NamespaceAccessToken token, WebdavServletConfig config) { 67 super(token, config); 68 } 69 70 75 protected void parseRequest() throws WebdavException { 76 List content; 77 78 try{ 80 content = parseRequestContent(E_BIND).getChildren(); 81 segment = MethodUtil.getChildText(content, E_SEGMENT); 82 sourceUri = parseUri(MethodUtil.getChildUrl(content, E_HREF)); 83 } 84 catch (IOException e) { int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR; 86 sendError( statusCode, e ); 87 throw new WebdavException( statusCode ); 88 } 89 catch (JDOMException e) { int statusCode = WebdavStatus.SC_BAD_REQUEST; 91 sendError( statusCode, e ); 92 throw new WebdavException( statusCode ); 93 } 94 95 collectionUri = requestUri; 96 if (collectionUri == null) { 97 collectionUri = "/"; 98 } 99 100 overwrite = requestHeaders.getOverwrite(true); 101 } 102 103 109 private void checkPreconditions() throws PreconditionViolationException, ServiceAccessException { 110 resp.setStatus( WebdavStatus.SC_CREATED ); 111 112 try { 113 collectionNode = structure.retrieve( slideToken, collectionUri ); 114 } 115 catch( ServiceAccessException e ) { 116 throw e; 117 } 118 catch (SlideException e) {} 120 try { 121 sourceNode = structure.retrieve( slideToken, sourceUri ); 122 } 123 catch( ServiceAccessException e ) { 124 throw e; 125 } 126 catch (Exception e) {} 128 if (collectionNode == null || !isCollection(collectionUri)) { 129 throw new PreconditionViolationException( 130 new ViolatedPrecondition(C_BIND_INTO_COLLECTION, WebdavStatus.SC_CONFLICT), collectionUri); 131 } 132 if (!MethodUtil.isValidSegment(segment)) { 133 throw new PreconditionViolationException( 134 new ViolatedPrecondition(C_NAME_ALLOWED, WebdavStatus.SC_FORBIDDEN), segment); 135 } 136 if (sourceNode == null) { 137 throw new PreconditionViolationException( 138 new ViolatedPrecondition(C_BIND_SOURCE_EXISTS, WebdavStatus.SC_CONFLICT), sourceUri); 139 } 140 if (collectionNode.hasBinding(segment)) { 141 if (overwrite) { 142 resp.setStatus( WebdavStatus.SC_NO_CONTENT ); 143 } 144 else { 145 throw new PreconditionViolationException( 146 new ViolatedPrecondition(C_CAN_OVERWRITE, WebdavStatus.SC_FORBIDDEN), segment); 147 } 148 } 149 if (isCollection(sourceUri)) { 150 if (isDescendant(collectionNode, sourceNode)) { 151 throw new PreconditionViolationException( 152 new ViolatedPrecondition(C_CYCLE_ALLOWED, WebdavStatus.SC_FORBIDDEN), sourceUri); 153 } 154 } 155 } 156 157 162 protected void executeRequest() throws WebdavException, IOException { 163 164 slideToken.setForceStoreEnlistment(true); 166 167 try { 169 if (isLockNull(collectionUri)) { 170 int statusCode = WebdavStatus.SC_NOT_FOUND; 171 sendError( statusCode, "lock-null resource", new Object []{collectionUri} ); 172 throw new WebdavException( statusCode ); 173 } 174 } 175 catch (ServiceAccessException e) { 176 int statusCode = getErrorCode( e ); 177 sendError( statusCode, e ); 178 throw new WebdavException( statusCode ); 179 } 180 181 try { 182 checkPreconditions(); 183 184 if ( WebdavEvent.BIND.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.BIND, new WebdavEvent(this)); 185 186 structure.addBinding( slideToken, collectionNode, segment, sourceNode ); 187 } 188 catch (ObjectLockedException e) { 189 ViolatedPrecondition violatedPrecondition; 190 if (collectionUri.equals(e.getObjectUri())) { 191 violatedPrecondition = 192 new ViolatedPrecondition(C_LOCKED_UPDATE_ALLOWED, WebdavStatus.SC_LOCKED); 193 } 194 else { 195 violatedPrecondition = 196 new ViolatedPrecondition(C_LOCKED_OVERWRITE_ALLOWED, WebdavStatus.SC_CONFLICT); 197 } 198 sendPreconditionViolation( 199 new PreconditionViolationException(violatedPrecondition, collectionNode.getUri()) 200 ); 201 } 202 catch (CrossServerBindingException e) { 203 sendPreconditionViolation( 204 new PreconditionViolationException( 205 new ViolatedPrecondition(C_CROSS_SERVER_BINDING, WebdavStatus.SC_FORBIDDEN), 206 collectionNode.getUri() 207 ) 208 ); 209 } 210 catch (PreconditionViolationException e) { 211 sendPreconditionViolation(e); 212 throw e; 213 } 214 catch (Exception e) { 215 int statusCode = getErrorCode( e ); 216 sendError( statusCode, e ); 217 throw new WebdavException( statusCode ); 218 } 219 } 220 221 } 222 223 | Popular Tags |