1 23 package org.apache.slide.webdav.method; 24 25 import java.io.IOException ; 26 import java.util.Iterator ; 27 28 import org.apache.slide.common.NamespaceAccessToken; 29 import org.apache.slide.common.ServiceAccessException; 30 import org.apache.slide.common.SlideException; 31 import org.apache.slide.event.EventDispatcher; 32 import org.apache.slide.util.Configuration; 33 import org.apache.slide.webdav.WebdavException; 34 import org.apache.slide.webdav.WebdavServletConfig; 35 import org.apache.slide.webdav.event.WebdavEvent; 36 import org.apache.slide.webdav.util.DeltavConstants; 37 import org.apache.slide.webdav.util.LabeledRevisionNotFoundException; 38 import org.apache.slide.webdav.util.PreconditionViolationException; 39 import org.apache.slide.webdav.util.VersioningHelper; 40 import org.apache.slide.webdav.util.ViolatedPrecondition; 41 import org.apache.slide.webdav.util.WebdavStatus; 42 import org.apache.slide.webdav.util.WebdavUtils; 43 import org.jdom.Element; 44 import org.jdom.JDOMException; 45 46 50 public class CheckoutMethod extends AbstractWebdavMethod implements DeltavConstants, WriteMethod { 51 52 53 private String resourcePath; 54 55 56 private boolean applyToVersion = false; 57 private boolean forkOk = false; 58 59 62 protected VersioningHelper versioningHelper = null; 63 64 65 67 68 74 public CheckoutMethod(NamespaceAccessToken token, 75 WebdavServletConfig config) { 76 super(token, config); 77 } 78 79 84 protected void parseRequest() throws WebdavException { 85 86 resourcePath = requestUri; 87 if (resourcePath == null) { 88 resourcePath = "/"; 89 } 90 91 versioningHelper = 92 VersioningHelper.getVersioningHelper(slideToken, token, req, resp, 93 config); 94 95 if (Configuration.useVersionControl()) { 97 try { 98 99 String labelHeader = WebdavUtils.fixTomcatHeader(requestHeaders.getLabel(), "UTF-8"); 100 101 applyToVersion = (labelHeader != null); 102 resourcePath = versioningHelper.getLabeledResourceUri(resourcePath, 103 labelHeader); 104 } 105 catch (LabeledRevisionNotFoundException e) { 106 ViolatedPrecondition violatedPrecondition = 107 new ViolatedPrecondition(DeltavConstants.C_MUST_SELECT_VERSION_IN_HISTORY, 108 WebdavStatus.SC_CONFLICT); 109 try { 110 sendPreconditionViolation(new PreconditionViolationException(violatedPrecondition, 111 resourcePath)); 112 } catch (IOException ioe) {} 113 throw new WebdavException(getErrorCode((Exception )e)); 114 } 115 catch (SlideException e) { 116 int statusCode = getErrorCode( e ); 117 sendError( statusCode, e ); 118 throw new WebdavException( statusCode ); 119 } 120 } 121 122 123 if( req.getContentLength() > 0 ) { 124 try{ 126 Iterator i = parseRequestContent(E_CHECKOUT).getChildren().iterator(); 127 while( i.hasNext() ) { 128 Element e = (Element)i.next(); 129 if ( e.getName().equals(E_APPLY_TO_VERSION) ) { 130 131 String labelHeader = WebdavUtils.fixTomcatHeader(requestHeaders.getLabel(), "UTF-8"); 132 133 if (labelHeader != null) { 134 ViolatedPrecondition violatedPrecondition = 135 new ViolatedPrecondition(DeltavConstants.C_MUST_NOT_HAVE_LABEL_AND_APPLY_TO_VERSION, 136 WebdavStatus.SC_CONFLICT); 137 sendPreconditionViolation(new PreconditionViolationException(violatedPrecondition, 138 resourcePath)); 139 throw new WebdavException(WebdavStatus.SC_CONFLICT); 140 } 141 applyToVersion = true; 142 } 143 if ( e.getName().equals(E_FORK_OK) ) { 144 forkOk = true; 145 } 146 } 147 } 148 catch (IOException e){ 149 int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR; 150 sendError( statusCode, e ); 151 throw new WebdavException( statusCode ); 152 } 153 catch (JDOMException e){ 154 int statusCode = WebdavStatus.SC_BAD_REQUEST; 155 sendError( statusCode, e ); 156 throw new WebdavException( statusCode ); 157 } 158 } 159 } 160 161 167 protected void executeRequest() throws WebdavException, IOException { 168 String locationValue = null; 169 170 slideToken.setForceStoreEnlistment(true); 172 173 try { 175 if (isLockNull(resourcePath)) { 176 int statusCode = WebdavStatus.SC_NOT_FOUND; 177 sendError( statusCode, "lock-null resource", new Object []{resourcePath} ); 178 throw new WebdavException( statusCode ); 179 } 180 } 181 catch (ServiceAccessException e) { 182 int statusCode = getErrorCode( e ); 183 sendError( statusCode, e ); 184 throw new WebdavException( statusCode ); 185 } 186 187 try { 188 if ( WebdavEvent.CHECKOUT.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.CHECKOUT, new WebdavEvent(this)); 189 190 locationValue = versioningHelper.checkout( resourcePath, forkOk, applyToVersion ); 191 } 192 catch (PreconditionViolationException e) { 193 sendPreconditionViolation(e); 194 throw e; 195 } 196 catch (Exception e) { 197 int statusCode = getErrorCode( e ); 198 sendError( statusCode, e ); 199 throw new WebdavException( statusCode ); 200 } 201 finally { 202 resp.setHeader(H_CACHE_CONTROL, NO_CACHE); 203 if( locationValue != null && locationValue.length() > 0 ) { 204 locationValue = getFullPath( locationValue ); 205 resp.setHeader( H_LOCATION, WebdavUtils.encodeURL(locationValue, "utf-8") ); 207 } 208 } 209 210 } 211 212 213 } 214 215 216 217 | Popular Tags |