1 23 24 package org.apache.slide.webdav.method; 25 26 import java.io.IOException ; 27 import java.util.Date ; 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.content.NodeProperty; 33 import org.apache.slide.content.NodeRevisionDescriptor; 34 import org.apache.slide.event.EventDispatcher; 35 import org.apache.slide.structure.LinkedObjectNotFoundException; 36 import org.apache.slide.structure.ObjectAlreadyExistsException; 37 import org.apache.slide.structure.ObjectNotFoundException; 38 import org.apache.slide.structure.SubjectNode; 39 import org.apache.slide.util.Configuration; 40 import org.apache.slide.webdav.WebdavException; 41 import org.apache.slide.webdav.WebdavServletConfig; 42 import org.apache.slide.webdav.event.WebdavEvent; 43 import org.apache.slide.webdav.util.DeltavConstants; 44 import org.apache.slide.webdav.util.UriHandler; 45 import org.apache.slide.webdav.util.VersioningHelper; 46 import org.apache.slide.webdav.util.WebdavStatus; 47 48 52 public class MkcolMethod 53 extends AbstractWebdavMethod 54 implements DeltavConstants, WriteMethod { 55 56 57 59 60 63 protected String colName; 64 65 66 72 public MkcolMethod(NamespaceAccessToken token, 73 WebdavServletConfig config) { 74 super(token, config); 75 } 76 77 78 80 81 84 protected void parseRequest() 85 throws WebdavException { 86 87 if (req.getContentLength() > 0) { 88 int statusCode = WebdavStatus.SC_UNSUPPORTED_MEDIA_TYPE; 89 sendError( statusCode, getClass().getName()+".requestBodyMustBeEmpty" ); 90 throw new WebdavException( statusCode ); 91 } 92 93 colName = requestUri; 94 if (colName == null) { 95 colName = "/"; 96 } 97 } 98 99 100 105 protected void executeRequest() throws WebdavException, IOException { 106 107 slideToken.setForceStoreEnlistment(true); 109 NodeRevisionDescriptor revisionDescriptor = null; 110 111 boolean isLockNull = false; 113 try { 114 revisionDescriptor = 115 content.retrieve(slideToken, content.retrieve(slideToken, colName)); 116 isLockNull = isLockNull( revisionDescriptor ); 117 } 118 catch (ServiceAccessException e) { 119 int statusCode = getErrorCode((Exception )e); 120 sendError( statusCode, e ); 121 throw new WebdavException( statusCode ); 122 } 123 catch (SlideException e) { 124 } 126 127 if (revisionDescriptor == null) { 128 revisionDescriptor = 129 new NodeRevisionDescriptor(0); 130 } 131 132 UriHandler destinationUriHandler = UriHandler.getUriHandler(colName); 134 if (destinationUriHandler.isRestrictedUri()) { 135 int statusCode = WebdavStatus.SC_FORBIDDEN; 136 sendError( statusCode, getClass().getName()+".restrictedDestinationUri", new Object []{colName} ); 137 throw new WebdavException( statusCode ); 138 } 139 140 revisionDescriptor.setResourceType(NodeRevisionDescriptor.COLLECTION_TYPE); 142 143 revisionDescriptor.setCreationDate(new Date ()); 145 146 revisionDescriptor.setLastModified(new Date ()); 148 149 revisionDescriptor.setContentLength(0); 151 152 revisionDescriptor.setSource(""); 154 155 try { 157 String creator = ((SubjectNode)security.getPrincipal(slideToken)).getPath().lastSegment(); 158 revisionDescriptor.setCreationUser(creator); 159 revisionDescriptor.setOwner(creator); 160 } catch (Exception e) { 161 int statusCode = getErrorCode( e ); 162 sendError( statusCode, e ); 163 throw new WebdavException( statusCode ); 164 } 165 166 if( Configuration.useVersionControl() ) { 168 VersioningHelper vHelp = VersioningHelper.getVersioningHelper( 169 slideToken, token, req, resp, getConfig() ); 170 171 vHelp.setWorkspaceProperty( colName, revisionDescriptor ); 173 } 174 176 if (isMsProprietarySupport()) { 177 178 NodeProperty property = null; 179 180 185 186 193 194 199 200 property = new NodeProperty("ishidden", "0", "MICROSOFT"); 202 revisionDescriptor.setProperty(property); 203 204 property = new NodeProperty("iscollection", "1", "MICROSOFT"); 206 revisionDescriptor.setProperty(property); 207 208 property = new NodeProperty("isreadonly", "0", "MICROSOFT"); 210 revisionDescriptor.setProperty(property); 211 212 property = new NodeProperty("lastaccessed", 214 (new Date ()).toString(), "MICROSOFT"); 215 revisionDescriptor.setProperty(property); 216 217 } 218 219 resp.setStatus(WebdavStatus.SC_CREATED); 221 222 try { 223 if ( WebdavEvent.MKCOL.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.MKCOL, new WebdavEvent(this)); 224 225 if (!isLockNull) { 226 SubjectNode collection = new SubjectNode(); 227 structure.create(slideToken, collection, colName); 228 content.create(slideToken, colName, revisionDescriptor, null); 229 } 230 else { 231 content.store(slideToken, colName, revisionDescriptor, null); 232 } 233 } catch (Exception e) { 234 int statusCode = getErrorCode( e ); 235 sendError( statusCode, e ); 236 throw new WebdavException( statusCode ); 237 } 238 239 243 247 249 } 250 251 252 255 protected int getErrorCode(Exception ex) { 256 try { 257 throw ex; 258 } catch (ObjectNotFoundException e) { 259 return WebdavStatus.SC_CONFLICT; 260 } catch (LinkedObjectNotFoundException e) { 261 return WebdavStatus.SC_NOT_FOUND; 262 } catch (ObjectAlreadyExistsException e) { 263 return WebdavStatus.SC_METHOD_NOT_ALLOWED; 264 } catch (Exception e) { 265 return super.getErrorCode(e); 266 } 267 268 } 269 270 } 271 272 | Popular Tags |