1 package org.apache.maven.archiva.webdav;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.jackrabbit.webdav.*;
23 import org.apache.jackrabbit.webdav.property.DavPropertySet;
24 import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
25 import org.apache.jackrabbit.webdav.property.DavProperty;
26 import org.apache.jackrabbit.webdav.property.DavPropertyName;
27 import org.apache.jackrabbit.webdav.io.InputContext;
28 import org.apache.jackrabbit.webdav.io.OutputContext;
29 import org.apache.jackrabbit.webdav.lock.*;
30 import org.apache.jackrabbit.util.Text;
31 import org.apache.commons.io.IOUtils;
32 import org.apache.commons.io.FileUtils;
33 import org.apache.maven.archiva.webdav.util.MimeTypes;
34 import org.apache.maven.archiva.webdav.util.IndexWriter;
36 import javax.servlet.http.HttpServletResponse;
37 import java.util.List;
38 import java.util.Date;
42 * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
44 public class ArchivaDavResource implements DavResource
46 private final MimeTypes mimeTypes;
48 private final DavResourceLocator locator;
50 private final DavResourceFactory factory;
52 private final DavSession session;
54 private final File localResource;
56 private final String logicalResource;
58 private static final String METHODS = "OPTIONS, GET, HEAD, POST, TRACE, PROPFIND, PROPPATCH, MKCOL, COPY, PUT, DELETE, MOVE";
60 private static final String COMPLIANCE_CLASS = "1";
62 private DavPropertySet properties;
64 public ArchivaDavResource(String localResource, String logicalResource, MimeTypes mimeTypes, DavResourceLocator locator, DavResourceFactory factory, DavSession session)
66 this.mimeTypes = mimeTypes;
67 this.localResource = new File(localResource);
68 this.logicalResource = logicalResource;
69 this.locator = locator;
70 this.factory = factory;
71 this.session = session;
72 this.properties = new DavPropertySet();
75 public String getContentType()
77 return mimeTypes.getMimeType(localResource.getName());
80 public String getComplianceClass()
82 return COMPLIANCE_CLASS;
85 public String getSupportedMethods()
90 public boolean exists()
92 return localResource.exists();
95 public boolean isCollection()
97 return localResource.isDirectory();
100 public String getDisplayName()
102 String resPath = getResourcePath();
103 return (resPath != null) ? Text.getName(resPath) : resPath;
106 public DavResourceLocator getLocator()
111 public String getResourcePath()
113 return locator.getResourcePath();
116 public String getHref()
118 return locator.getHref(isCollection());
121 public long getModificationTime()
123 return localResource.lastModified();
126 public long getContentLength()
128 return localResource.length();
131 public void spool(OutputContext outputContext) throws IOException
135 IOUtils.copy(new FileInputStream(localResource), outputContext.getOutputStream());
139 IndexWriter writer = new IndexWriter(this, localResource, logicalResource);
140 writer.write(outputContext);
144 public DavPropertyName[] getPropertyNames()
146 return new DavPropertyName[0];
149 public DavProperty getProperty(DavPropertyName name)
154 public DavPropertySet getProperties()
159 public void setProperty(DavProperty property) throws DavException
163 public void removeProperty(DavPropertyName propertyName) throws DavException
167 public MultiStatusResponse alterProperties(DavPropertySet setProperties, DavPropertyNameSet removePropertyNames) throws DavException
172 public MultiStatusResponse alterProperties(List changeList) throws DavException
177 public DavResource getCollection()
179 DavResource parent = null;
180 if (getResourcePath() != null && !getResourcePath().equals("/")) {
181 String parentPath = Text.getRelativeParent(getResourcePath(), 1);
182 if (parentPath.equals("")) {
185 DavResourceLocator parentloc = locator.getFactory().createResourceLocator(locator.getPrefix(), locator.getWorkspacePath(), parentPath);
187 parent = factory.createResource(parentloc, session);
188 } catch (DavException e) {
195 public void addMember(DavResource resource, InputContext inputContext) throws DavException
197 File localFile = new File(localResource, resource.getDisplayName());
198 if (!resource.isCollection() && isCollection() && inputContext.hasStream()) //New File
200 boolean deleteFile = false;
201 FileOutputStream stream = null;
204 stream = new FileOutputStream(localFile);
205 IOUtils.copy(inputContext.getInputStream(), stream);
206 if (inputContext.getContentLength() != localFile.length())
209 throw new DavException(HttpServletResponse.SC_BAD_REQUEST, "Content Header length was "
210 + inputContext.getContentLength() + " but was " + localFile.length());
213 catch (IOException e)
215 throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
219 IOUtils.closeQuietly(stream);
222 FileUtils.deleteQuietly(localFile);
226 else if (resource.isCollection() && isCollection()) //New directory
232 throw new DavException(HttpServletResponse.SC_BAD_REQUEST, "Could not write member "
233 + resource.getResourcePath() + " at " + getResourcePath());
237 public DavResourceIterator getMembers()
242 public void removeMember(DavResource member) throws DavException
246 public void move(DavResource destination) throws DavException
250 public void copy(DavResource destination, boolean shallow) throws DavException
254 public boolean isLockable(Type type, Scope scope)
259 public boolean hasLock(Type type, Scope scope)
264 public ActiveLock getLock(Type type, Scope scope)
269 public ActiveLock[] getLocks()
271 return new ActiveLock[0];
274 public ActiveLock lock(LockInfo reqLockInfo) throws DavException
279 public ActiveLock refreshLock(LockInfo reqLockInfo, String lockToken) throws DavException
284 public void unlock(String lockToken) throws DavException
288 public void addLockManager(LockManager lockmgr)
292 public DavResourceFactory getFactory()
297 public DavSession getSession()