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());
136 outputContext.setContentLength(getContentLength());
137 outputContext.setContentType(getContentType());
141 IndexWriter writer = new IndexWriter(this, localResource, logicalResource);
142 writer.write(outputContext);
146 public DavPropertyName[] getPropertyNames()
148 return new DavPropertyName[0];
151 public DavProperty getProperty(DavPropertyName name)
156 public DavPropertySet getProperties()
161 public void setProperty(DavProperty property) throws DavException
165 public void removeProperty(DavPropertyName propertyName) throws DavException
169 public MultiStatusResponse alterProperties(DavPropertySet setProperties, DavPropertyNameSet removePropertyNames) throws DavException
174 public MultiStatusResponse alterProperties(List changeList) throws DavException
179 public DavResource getCollection()
181 DavResource parent = null;
182 if (getResourcePath() != null && !getResourcePath().equals("/")) {
183 String parentPath = Text.getRelativeParent(getResourcePath(), 1);
184 if (parentPath.equals("")) {
187 DavResourceLocator parentloc = locator.getFactory().createResourceLocator(locator.getPrefix(), locator.getWorkspacePath(), parentPath);
189 parent = factory.createResource(parentloc, session);
190 } catch (DavException e) {
197 public void addMember(DavResource resource, InputContext inputContext) throws DavException
199 File localFile = new File(localResource, resource.getDisplayName());
200 if (!resource.isCollection() && isCollection() && inputContext.hasStream()) //New File
202 boolean deleteFile = false;
203 FileOutputStream stream = null;
206 stream = new FileOutputStream(localFile);
207 IOUtils.copy(inputContext.getInputStream(), stream);
208 if (inputContext.getContentLength() != localFile.length())
211 throw new DavException(HttpServletResponse.SC_BAD_REQUEST, "Content Header length was "
212 + inputContext.getContentLength() + " but was " + localFile.length());
215 catch (IOException e)
217 throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
221 IOUtils.closeQuietly(stream);
224 FileUtils.deleteQuietly(localFile);
228 else if (resource.isCollection() && isCollection()) //New directory
234 throw new DavException(HttpServletResponse.SC_BAD_REQUEST, "Could not write member "
235 + resource.getResourcePath() + " at " + getResourcePath());
239 public DavResourceIterator getMembers()
244 public void removeMember(DavResource member) throws DavException
248 public void move(DavResource destination) throws DavException
252 public void copy(DavResource destination, boolean shallow) throws DavException
256 public boolean isLockable(Type type, Scope scope)
261 public boolean hasLock(Type type, Scope scope)
266 public ActiveLock getLock(Type type, Scope scope)
271 public ActiveLock[] getLocks()
273 return new ActiveLock[0];
276 public ActiveLock lock(LockInfo reqLockInfo) throws DavException
281 public ActiveLock refreshLock(LockInfo reqLockInfo, String lockToken) throws DavException
286 public void unlock(String lockToken) throws DavException
290 public void addLockManager(LockManager lockmgr)
294 public DavResourceFactory getFactory()
299 public DavSession getSession()