You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArchivaVirtualDavResource.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package org.apache.archiva.webdav;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.webdav.util.IndexWriter;
  21. import org.apache.archiva.webdav.util.MimeTypes;
  22. import org.apache.jackrabbit.util.Text;
  23. import org.apache.jackrabbit.webdav.DavException;
  24. import org.apache.jackrabbit.webdav.DavResource;
  25. import org.apache.jackrabbit.webdav.DavResourceFactory;
  26. import org.apache.jackrabbit.webdav.DavResourceIterator;
  27. import org.apache.jackrabbit.webdav.DavResourceLocator;
  28. import org.apache.jackrabbit.webdav.DavSession;
  29. import org.apache.jackrabbit.webdav.MultiStatusResponse;
  30. import org.apache.jackrabbit.webdav.io.InputContext;
  31. import org.apache.jackrabbit.webdav.io.OutputContext;
  32. import org.apache.jackrabbit.webdav.lock.ActiveLock;
  33. import org.apache.jackrabbit.webdav.lock.LockInfo;
  34. import org.apache.jackrabbit.webdav.lock.LockManager;
  35. import org.apache.jackrabbit.webdav.lock.Scope;
  36. import org.apache.jackrabbit.webdav.lock.Type;
  37. import org.apache.jackrabbit.webdav.property.DavProperty;
  38. import org.apache.jackrabbit.webdav.property.DavPropertyName;
  39. import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
  40. import org.apache.jackrabbit.webdav.property.DavPropertySet;
  41. import org.apache.jackrabbit.webdav.property.DefaultDavProperty;
  42. import org.apache.jackrabbit.webdav.property.ResourceType;
  43. import org.joda.time.DateTime;
  44. import org.joda.time.format.DateTimeFormatter;
  45. import org.joda.time.format.ISODateTimeFormat;
  46. import java.io.IOException;
  47. import java.nio.file.Files;
  48. import java.nio.file.Path;
  49. import java.util.ArrayList;
  50. import java.util.Collections;
  51. import java.util.List;
  52. /**
  53. * DavResource for virtual repositories
  54. */
  55. public class ArchivaVirtualDavResource
  56. implements DavResource
  57. {
  58. private static final String COMPLIANCE_CLASS = "1";
  59. private ArchivaDavResourceLocator locator;
  60. private DavResourceFactory factory;
  61. private String logicalResource;
  62. private DavPropertySet properties;
  63. private boolean propsInitialized = false;
  64. private static final String METHODS = "OPTIONS, GET, HEAD, POST, TRACE, PROPFIND, PROPPATCH, MKCOL";
  65. private final List<Path> localResources;
  66. public ArchivaVirtualDavResource( List<Path> localResources, String logicalResource, MimeTypes mimeTypes,
  67. ArchivaDavResourceLocator locator, DavResourceFactory factory )
  68. {
  69. this.localResources = localResources;
  70. this.logicalResource = logicalResource;
  71. this.locator = locator;
  72. this.factory = factory;
  73. this.properties = new DavPropertySet();
  74. }
  75. @Override
  76. public void spool( OutputContext outputContext )
  77. throws IOException
  78. {
  79. if ( outputContext.hasStream() )
  80. {
  81. Collections.sort( localResources );
  82. List<Path> localResourceFiles = new ArrayList<>();
  83. for ( Path resourceFile : localResources )
  84. {
  85. if ( Files.exists(resourceFile) )
  86. {
  87. localResourceFiles.add( resourceFile );
  88. }
  89. }
  90. IndexWriter writer = new IndexWriter( this, localResourceFiles, logicalResource );
  91. writer.write( outputContext );
  92. }
  93. }
  94. @Override
  95. public void addLockManager( LockManager arg0 )
  96. {
  97. }
  98. @Override
  99. public void addMember( DavResource arg0, InputContext arg1 )
  100. throws DavException
  101. {
  102. }
  103. @SuppressWarnings( "unchecked" )
  104. @Override
  105. public MultiStatusResponse alterProperties( List arg0 )
  106. throws DavException
  107. {
  108. return null;
  109. }
  110. public MultiStatusResponse alterProperties( DavPropertySet arg0, DavPropertyNameSet arg1 )
  111. throws DavException
  112. {
  113. return null;
  114. }
  115. @Override
  116. public void copy( DavResource arg0, boolean arg1 )
  117. throws DavException
  118. {
  119. }
  120. @Override
  121. public boolean exists()
  122. {
  123. // localResources are already filtered (all files in the list are already existing)
  124. return true;
  125. }
  126. @Override
  127. public ActiveLock getLock( Type arg0, Scope arg1 )
  128. {
  129. return null;
  130. }
  131. @Override
  132. public ActiveLock[] getLocks()
  133. {
  134. return null;
  135. }
  136. @Override
  137. public DavResourceIterator getMembers()
  138. {
  139. return null;
  140. }
  141. @Override
  142. public String getSupportedMethods()
  143. {
  144. return METHODS;
  145. }
  146. @Override
  147. public long getModificationTime()
  148. {
  149. return 0;
  150. }
  151. @Override
  152. public boolean hasLock( Type arg0, Scope arg1 )
  153. {
  154. return false;
  155. }
  156. @Override
  157. public boolean isCollection()
  158. {
  159. return true;
  160. }
  161. @Override
  162. public boolean isLockable( Type arg0, Scope arg1 )
  163. {
  164. return false;
  165. }
  166. @Override
  167. public ActiveLock lock( LockInfo arg0 )
  168. throws DavException
  169. {
  170. return null;
  171. }
  172. @Override
  173. public void move( DavResource arg0 )
  174. throws DavException
  175. {
  176. }
  177. @Override
  178. public ActiveLock refreshLock( LockInfo arg0, String arg1 )
  179. throws DavException
  180. {
  181. return null;
  182. }
  183. @Override
  184. public void removeMember( DavResource arg0 )
  185. throws DavException
  186. {
  187. }
  188. @Override
  189. public void unlock( String arg0 )
  190. throws DavException
  191. {
  192. }
  193. @Override
  194. public String getComplianceClass()
  195. {
  196. return COMPLIANCE_CLASS;
  197. }
  198. @Override
  199. public DavResourceLocator getLocator()
  200. {
  201. return locator;
  202. }
  203. @Override
  204. public String getResourcePath()
  205. {
  206. return locator.getResourcePath();
  207. }
  208. @Override
  209. public String getHref()
  210. {
  211. return locator.getHref( isCollection() );
  212. }
  213. @Override
  214. public DavResourceFactory getFactory()
  215. {
  216. return factory;
  217. }
  218. @Override
  219. public String getDisplayName()
  220. {
  221. String resPath = getResourcePath();
  222. return ( resPath != null ) ? Text.getName( resPath ) : resPath;
  223. }
  224. @Override
  225. public DavSession getSession()
  226. {
  227. return null;
  228. }
  229. @Override
  230. public DavPropertyName[] getPropertyNames()
  231. {
  232. return getProperties().getPropertyNames();
  233. }
  234. @Override
  235. public DavProperty getProperty( DavPropertyName name )
  236. {
  237. initProperties();
  238. return properties.get( name );
  239. }
  240. @Override
  241. public DavPropertySet getProperties()
  242. {
  243. initProperties();
  244. return properties;
  245. }
  246. @Override
  247. public void setProperty( DavProperty property )
  248. throws DavException
  249. {
  250. }
  251. @Override
  252. public void removeProperty( DavPropertyName propertyName )
  253. throws DavException
  254. {
  255. }
  256. @Override
  257. public DavResource getCollection()
  258. {
  259. DavResource parent = null;
  260. if ( getResourcePath() != null && !getResourcePath().equals( "/" ) )
  261. {
  262. String parentPath = Text.getRelativeParent( getResourcePath(), 1 );
  263. if ( parentPath.equals( "" ) )
  264. {
  265. parentPath = "/";
  266. }
  267. DavResourceLocator parentloc =
  268. locator.getFactory().createResourceLocator( locator.getPrefix(), parentPath );
  269. try
  270. {
  271. // go back to ArchivaDavResourceFactory!
  272. parent = factory.createResource( parentloc, null );
  273. }
  274. catch ( DavException e )
  275. {
  276. // should not occur
  277. }
  278. }
  279. return parent;
  280. }
  281. /**
  282. * Fill the set of properties
  283. */
  284. protected void initProperties()
  285. {
  286. if ( !exists() || propsInitialized )
  287. {
  288. return;
  289. }
  290. // set (or reset) fundamental properties
  291. if ( getDisplayName() != null )
  292. {
  293. properties.add( new DefaultDavProperty<>( DavPropertyName.DISPLAYNAME, getDisplayName() ) );
  294. }
  295. if ( isCollection() )
  296. {
  297. properties.add( new ResourceType( ResourceType.COLLECTION ) );
  298. // Windows XP support
  299. properties.add( new DefaultDavProperty<>( DavPropertyName.ISCOLLECTION, "1" ) );
  300. }
  301. else
  302. {
  303. properties.add( new ResourceType( ResourceType.DEFAULT_RESOURCE ) );
  304. // Windows XP support
  305. properties.add( new DefaultDavProperty<>( DavPropertyName.ISCOLLECTION, "0" ) );
  306. }
  307. // Need to get the ISO8601 date for properties
  308. DateTime dt = new DateTime( 0 );
  309. DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
  310. String modifiedDate = fmt.print( dt );
  311. properties.add( new DefaultDavProperty<>( DavPropertyName.GETLASTMODIFIED, modifiedDate ) );
  312. properties.add( new DefaultDavProperty<>( DavPropertyName.CREATIONDATE, modifiedDate ) );
  313. properties.add( new DefaultDavProperty<>( DavPropertyName.GETCONTENTLENGTH, 0 ) );
  314. propsInitialized = true;
  315. }
  316. public String getLogicalResource()
  317. {
  318. return logicalResource;
  319. }
  320. public void setLogicalResource( String logicalResource )
  321. {
  322. this.logicalResource = logicalResource;
  323. }
  324. }