1 package org.apache.continuum.webdav;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.io.IOUtils;
23 import org.apache.jackrabbit.util.Text;
24 import org.apache.jackrabbit.webdav.DavException;
25 import org.apache.jackrabbit.webdav.DavResource;
26 import org.apache.jackrabbit.webdav.DavResourceFactory;
27 import org.apache.jackrabbit.webdav.DavResourceIterator;
28 import org.apache.jackrabbit.webdav.DavResourceIteratorImpl;
29 import org.apache.jackrabbit.webdav.DavResourceLocator;
30 import org.apache.jackrabbit.webdav.DavSession;
31 import org.apache.jackrabbit.webdav.MultiStatusResponse;
32 import org.apache.jackrabbit.webdav.io.InputContext;
33 import org.apache.jackrabbit.webdav.io.OutputContext;
34 import org.apache.jackrabbit.webdav.lock.ActiveLock;
35 import org.apache.jackrabbit.webdav.lock.LockInfo;
36 import org.apache.jackrabbit.webdav.lock.LockManager;
37 import org.apache.jackrabbit.webdav.lock.Scope;
38 import org.apache.jackrabbit.webdav.lock.Type;
39 import org.apache.jackrabbit.webdav.property.DavProperty;
40 import org.apache.jackrabbit.webdav.property.DavPropertyName;
41 import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
42 import org.apache.jackrabbit.webdav.property.DavPropertySet;
43 import org.apache.jackrabbit.webdav.property.DefaultDavProperty;
44 import org.apache.jackrabbit.webdav.property.ResourceType;
45 import org.joda.time.DateTime;
46 import org.joda.time.format.DateTimeFormatter;
47 import org.joda.time.format.ISODateTimeFormat;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 import java.io.File;
52 import java.io.FileInputStream;
53 import java.io.IOException;
54 import java.util.ArrayList;
55 import java.util.List;
56 import javax.activation.MimetypesFileTypeMap;
57
58 public class ContinuumBuildAgentDavResource
59 implements DavResource
60 {
61 private static final Logger log = LoggerFactory.getLogger( ContinuumBuildAgentDavResource.class );
62
63 private final ContinuumBuildAgentDavResourceLocator locator;
64
65 private final DavResourceFactory factory;
66
67 private final File localResource;
68
69 private final String logicalResource;
70
71 private final DavSession session;
72
73 private final MimetypesFileTypeMap mimeTypes;
74
75 private DavPropertySet properties = null;
76
77 public static final String COMPLIANCE_CLASS = "1, 2";
78
79 public static final String HIDDEN_PATH_PREFIX = ".";
80
81 public static final String SUPPORTED_METHODS = "OPTIONS, GET, HEAD, TRACE, PROPFIND";
82
83 public ContinuumBuildAgentDavResource( String localResource, String logicalResource, DavSession session,
84 ContinuumBuildAgentDavResourceLocator locator, DavResourceFactory factory,
85 MimetypesFileTypeMap mimeTypes )
86 {
87 this.localResource = new File( localResource );
88 this.logicalResource = logicalResource;
89 this.locator = locator;
90 this.factory = factory;
91 this.session = session;
92 this.mimeTypes = mimeTypes;
93 }
94
95 public void addLockManager( LockManager lockManager )
96 {
97 }
98
99 public void addMember( DavResource davResource, InputContext inputContext )
100 throws DavException
101 {
102 throw new UnsupportedOperationException( "Not supported" );
103 }
104
105 public MultiStatusResponse alterProperties( List changeList )
106 throws DavException
107 {
108 return null;
109 }
110
111 public MultiStatusResponse alterProperties( DavPropertySet setProperties, DavPropertyNameSet removePropertyNames )
112 throws DavException
113 {
114 return null;
115 }
116
117 public void copy( DavResource destination, boolean shallow )
118 throws DavException
119 {
120 throw new UnsupportedOperationException( "Not supported" );
121 }
122
123 public boolean exists()
124 {
125 return localResource.exists();
126 }
127
128 public DavResource getCollection()
129 {
130 DavResource parent = null;
131 if ( getResourcePath() != null && !"/".equals( getResourcePath() ) )
132 {
133 String parentPath = Text.getRelativeParent( getResourcePath(), 1 );
134 if ( "".equals( parentPath ) )
135 {
136 parentPath = "/";
137 }
138
139 DavResourceLocator parentloc = locator.getFactory().createResourceLocator( locator.getPrefix(),
140 parentPath );
141
142 try
143 {
144 parent = factory.createResource( parentloc, session );
145 }
146 catch ( DavException e )
147 {
148
149 }
150 }
151 return parent;
152 }
153
154 public String getComplianceClass()
155 {
156 return COMPLIANCE_CLASS;
157 }
158
159 public String getDisplayName()
160 {
161 String resPath = getResourcePath();
162 return ( resPath != null ) ? Text.getName( resPath ) : resPath;
163 }
164
165 public DavResourceFactory getFactory()
166 {
167 return factory;
168 }
169
170 public String getHref()
171 {
172 return locator.getHref( isCollection() );
173 }
174
175 public File getLocalResource()
176 {
177 return localResource;
178 }
179
180 public DavResourceLocator getLocator()
181 {
182 return locator;
183 }
184
185 public ActiveLock getLock( Type type, Scope scope )
186 {
187 return null;
188 }
189
190 public ActiveLock[] getLocks()
191 {
192 return null;
193 }
194
195 public String getLogicalResource()
196 {
197 return logicalResource;
198 }
199
200 public DavResourceIterator getMembers()
201 {
202 List<DavResource> list = new ArrayList<DavResource>();
203 if ( exists() && isCollection() )
204 {
205 for ( String item : localResource.list() )
206 {
207 try
208 {
209 if ( !item.startsWith( HIDDEN_PATH_PREFIX ) )
210 {
211 String path = locator.getResourcePath() + '/' + item;
212 DavResourceLocator resourceLocator = locator.getFactory().createResourceLocator(
213 locator.getPrefix(), path );
214 DavResource resource = factory.createResource( resourceLocator, session );
215
216 if ( resource != null )
217 {
218 log.debug( "Retrieved resource: " + resource.getResourcePath() );
219 list.add( resource );
220 }
221 }
222 }
223 catch ( DavException e )
224 {
225
226 }
227 }
228 }
229
230 return new DavResourceIteratorImpl( list );
231 }
232
233 public MimetypesFileTypeMap getMimeTypes()
234 {
235 return mimeTypes;
236 }
237
238 public long getModificationTime()
239 {
240 return localResource.lastModified();
241 }
242
243 public DavPropertySet getProperties()
244 {
245 return initProperties();
246 }
247
248 public DavProperty getProperty( DavPropertyName propertyName )
249 {
250 return getProperties().get( propertyName );
251 }
252
253 public DavPropertyName[] getPropertyNames()
254 {
255 return getProperties().getPropertyNames();
256 }
257
258 public String getResourcePath()
259 {
260 return locator.getResourcePath();
261 }
262
263 public DavSession getSession()
264 {
265 return session;
266 }
267
268 public String getSupportedMethods()
269 {
270 return SUPPORTED_METHODS;
271 }
272
273 public boolean hasLock( Type type, Scope scope )
274 {
275 return false;
276 }
277
278 public boolean isCollection()
279 {
280 return localResource.isDirectory();
281 }
282
283 public boolean isLockable( Type type, Scope scope )
284 {
285 return false;
286 }
287
288 public ActiveLock lock( LockInfo lockInfo )
289 throws DavException
290 {
291 return null;
292 }
293
294 public void move( DavResource destination )
295 throws DavException
296 {
297 throw new UnsupportedOperationException( "Not supported" );
298 }
299
300 public ActiveLock refreshLock( LockInfo lockInfo, String lockTocken )
301 throws DavException
302 {
303 return null;
304 }
305
306 public void removeMember( DavResource member )
307 throws DavException
308 {
309 throw new UnsupportedOperationException( "Not supported" );
310 }
311
312 public void removeProperty( DavPropertyName propertyName )
313 throws DavException
314 {
315 throw new UnsupportedOperationException( "Not supported" );
316 }
317
318 public void setProperty( DavProperty property )
319 throws DavException
320 {
321 throw new UnsupportedOperationException( "Not supported" );
322 }
323
324 public void spool( OutputContext outputContext )
325 throws IOException
326 {
327 if ( !isCollection() )
328 {
329 outputContext.setContentLength( localResource.length() );
330 outputContext.setContentType( mimeTypes.getContentType( localResource ) );
331 }
332
333 if ( !isCollection() && outputContext.hasStream() )
334 {
335 FileInputStream is = null;
336 try
337 {
338
339 is = new FileInputStream( localResource );
340 IOUtils.copy( is, outputContext.getOutputStream() );
341 }
342 finally
343 {
344 IOUtils.closeQuietly( is );
345 }
346 }
347 }
348
349 public void unlock( String lockTocken )
350 throws DavException
351 {
352 }
353
354
355
356
357 protected DavPropertySet initProperties()
358 {
359 if ( !exists() )
360 {
361 properties = new DavPropertySet();
362 }
363
364 if ( properties != null )
365 {
366 return properties;
367 }
368
369 DavPropertySet properties = new DavPropertySet();
370
371
372 if ( getDisplayName() != null )
373 {
374 properties.add( new DefaultDavProperty( DavPropertyName.DISPLAYNAME, getDisplayName() ) );
375 }
376 if ( isCollection() )
377 {
378 properties.add( new ResourceType( ResourceType.COLLECTION ) );
379
380 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "1" ) );
381 }
382 else
383 {
384 properties.add( new ResourceType( ResourceType.DEFAULT_RESOURCE ) );
385
386
387 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "0" ) );
388 }
389
390
391 DateTime dt = new DateTime( localResource.lastModified() );
392 DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
393 String modifiedDate = fmt.print( dt );
394
395 properties.add( new DefaultDavProperty( DavPropertyName.GETLASTMODIFIED, modifiedDate ) );
396
397 properties.add( new DefaultDavProperty( DavPropertyName.CREATIONDATE, modifiedDate ) );
398
399 properties.add( new DefaultDavProperty( DavPropertyName.GETCONTENTLENGTH, localResource.length() ) );
400
401 this.properties = properties;
402
403 return properties;
404 }
405 }