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.UriHandler; 42 import org.apache.slide.webdav.util.ViolatedPrecondition; 43 import org.apache.slide.webdav.util.WebdavStatus; 44 import org.jdom.JDOMException; 45 46 50 public class RebindMethod extends AbstractWebdavMethod implements BindConstants, WriteMethod { 51 52 private String sourceUri; 53 private String sourceSegment; 54 private String sourceParentUri; 55 private String collectionUri; 56 private String segment; 57 private boolean overwrite; 58 private ObjectNode sourceNode = null; 59 private ObjectNode sourceParentNode = null; 60 private ObjectNode collectionNode = null; 61 62 63 69 public RebindMethod(NamespaceAccessToken token, WebdavServletConfig config) { 70 super(token, config); 71 } 72 73 78 protected void parseRequest() throws WebdavException { 79 collectionUri = requestUri; 80 if (collectionUri == null) { 81 collectionUri = "/"; 82 } 83 84 List content; 85 86 try{ 88 content = parseRequestContent(E_REBIND).getChildren(); 89 segment = MethodUtil.getChildText(content, E_SEGMENT); 90 sourceUri = parseUri(MethodUtil.getChildUrl(content, E_HREF)); 91 } 92 catch (IOException e) { int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR; 94 sendError( statusCode, e ); 95 throw new WebdavException( statusCode ); 96 } 97 catch (JDOMException e) { int statusCode = WebdavStatus.SC_BAD_REQUEST; 99 sendError( statusCode, e ); 100 throw new WebdavException( statusCode ); 101 } 102 103 collectionUri = requestUri; 104 if (collectionUri == null) { 105 collectionUri = "/"; 106 } 107 108 overwrite = requestHeaders.getOverwrite(true); 109 } 110 111 private void checkPreconditions() throws PreconditionViolationException, ServiceAccessException { 112 resp.setStatus( WebdavStatus.SC_CREATED ); 113 UriHandler sourceUh = UriHandler.getUriHandler(sourceUri); 114 UriHandler sourceParentUh = sourceUh.getParentUriHandler(); 115 sourceSegment = sourceUh.getName(); 116 if (sourceParentUh != null) { 117 sourceParentUri = sourceUh.getParentUriHandler().toString(); 118 } 119 120 try { 121 collectionNode = structure.retrieve( slideToken, collectionUri ); 122 } 123 catch( ServiceAccessException e ) { 124 throw e; 125 } 126 catch (SlideException e) {} 128 try { 129 sourceNode = structure.retrieve( slideToken, sourceUri ); 130 } 131 catch( ServiceAccessException e ) { 132 throw e; 133 } 134 catch (SlideException e) {} 136 try { 137 sourceParentNode = structure.retrieve( slideToken, sourceParentUri ); 138 } 139 catch( ServiceAccessException e ) { 140 throw e; 141 } 142 catch (SlideException e) {} 144 if (collectionNode == null || !isCollection(collectionUri)) { 145 throw new PreconditionViolationException( 146 new ViolatedPrecondition(C_REBIND_INTO_COLLECTION, WebdavStatus.SC_CONFLICT), collectionUri); 147 } 148 if (!MethodUtil.isValidSegment(segment)) { 149 throw new PreconditionViolationException( 150 new ViolatedPrecondition(C_NAME_ALLOWED, WebdavStatus.SC_FORBIDDEN), segment); 151 } 152 if (sourceNode == null) { 153 throw new PreconditionViolationException( 154 new ViolatedPrecondition(C_REBIND_SOURCE_EXISTS, WebdavStatus.SC_CONFLICT), sourceUri); 155 } 156 if (collectionNode.hasBinding(segment)) { 157 if (overwrite) { 158 resp.setStatus( WebdavStatus.SC_NO_CONTENT ); 159 } 160 else { 161 throw new PreconditionViolationException( 162 new ViolatedPrecondition(C_CAN_OVERWRITE, WebdavStatus.SC_FORBIDDEN), segment); 163 } 164 } 165 if (isCollection(sourceUri)) { 166 if (isDescendant(collectionNode, sourceNode)) { 167 throw new PreconditionViolationException( 168 new ViolatedPrecondition(C_CYCLE_ALLOWED, WebdavStatus.SC_FORBIDDEN), sourceUri); 169 } 170 } 171 } 172 173 178 protected void executeRequest() throws WebdavException, IOException { 179 180 slideToken.setForceStoreEnlistment(true); 182 183 try { 185 if (isLockNull(collectionUri)) { 186 int statusCode = WebdavStatus.SC_NOT_FOUND; 187 sendError( statusCode, "lock-null resource", new Object []{collectionUri} ); 188 throw new WebdavException( statusCode ); 189 } 190 } 191 catch (ServiceAccessException e) { 192 int statusCode = getErrorCode((Exception )e); 193 sendError( statusCode, e ); 194 throw new WebdavException( statusCode ); 195 } 196 197 try { 198 if ( WebdavEvent.REBIND.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.REBIND, new WebdavEvent(this)); 199 200 checkPreconditions(); 201 structure.addBinding( slideToken, collectionNode, segment, sourceNode ); 202 structure.removeBinding( slideToken, sourceParentNode, sourceSegment ); 203 } 204 catch (CrossServerBindingException e) { 205 sendPreconditionViolation( 206 new PreconditionViolationException( 207 new ViolatedPrecondition(C_CROSS_SERVER_BINDING, WebdavStatus.SC_FORBIDDEN), 208 collectionNode.getUri() 209 ) 210 ); 211 } 212 catch (ObjectLockedException e) { 213 ViolatedPrecondition violatedPrecondition; 214 if (collectionUri.equals(e.getObjectUri())) { 215 violatedPrecondition = 216 new ViolatedPrecondition(C_LOCKED_UPDATE_ALLOWED, WebdavStatus.SC_LOCKED); 217 } 218 else if (sourceParentUri.equals(e.getObjectUri())) { 219 violatedPrecondition = 220 new ViolatedPrecondition(C_LOCKED_SOURCE_COLLECTION_UPDATE_ALLOWED, WebdavStatus.SC_CONFLICT); 221 } 222 else if (sourceUri.equals(e.getObjectUri())) { 223 violatedPrecondition = 224 new ViolatedPrecondition(C_PROTECTED_SOURCE_URL_DELETION_ALLOWED, WebdavStatus.SC_CONFLICT); 225 } 226 else { 227 violatedPrecondition = 228 new ViolatedPrecondition(C_PROTECTED_URL_MODIFICATION_ALLOWED, WebdavStatus.SC_CONFLICT); 229 } 230 sendPreconditionViolation( 231 new PreconditionViolationException(violatedPrecondition, collectionNode.getUri()) 232 ); 233 } 234 catch (PreconditionViolationException e) { 235 sendPreconditionViolation(e); 236 throw e; 237 } 238 catch (Exception e) { 239 int statusCode = getErrorCode( e ); 240 sendError( statusCode, e ); 241 throw new WebdavException( statusCode ); 242 } 243 } 244 245 } 246 247 | Popular Tags |