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.continuum.buildagent.configuration.BuildAgentConfigurationService;
23 import org.apache.continuum.webdav.util.WebdavMethodUtil;
24 import org.apache.continuum.webdav.util.WorkingCopyPathUtil;
25 import org.apache.jackrabbit.webdav.DavException;
26 import org.apache.jackrabbit.webdav.DavResource;
27 import org.apache.jackrabbit.webdav.DavResourceFactory;
28 import org.apache.jackrabbit.webdav.DavResourceLocator;
29 import org.apache.jackrabbit.webdav.DavServletRequest;
30 import org.apache.jackrabbit.webdav.DavServletResponse;
31 import org.apache.jackrabbit.webdav.DavSession;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import java.io.File;
36 import javax.activation.MimetypesFileTypeMap;
37 import javax.servlet.http.HttpServletResponse;
38
39
40
41
42 public class ContinuumBuildAgentDavResourceFactory
43 implements DavResourceFactory
44 {
45 private static final Logger log = LoggerFactory.getLogger( ContinuumBuildAgentDavResourceFactory.class );
46
47 private static final MimetypesFileTypeMap mimeTypes;
48
49
50
51
52 private BuildAgentConfigurationService buildAgentConfigurationService;
53
54 static
55 {
56 mimeTypes = new MimetypesFileTypeMap();
57 mimeTypes.addMimeTypes( "application/java-archive jar war ear" );
58 mimeTypes.addMimeTypes( "application/java-class class" );
59 mimeTypes.addMimeTypes( "image/png png" );
60 }
61
62 public DavResource createResource( final DavResourceLocator locator, final DavSession davSession )
63 throws DavException
64 {
65 ContinuumBuildAgentDavResourceLocator continuumLocator = checkLocatorIsInstanceOfContinuumBuildAgentLocator(
66 locator );
67
68 String logicalResource = WorkingCopyPathUtil.getLogicalResource( locator.getResourcePath() );
69 if ( logicalResource.startsWith( "/" ) )
70 {
71 logicalResource = logicalResource.substring( 1 );
72 }
73
74 File resourceFile = getResourceFile( continuumLocator.getProjectId(), logicalResource );
75
76 if ( !resourceFile.exists() || ( continuumLocator.getHref( false ).endsWith( "/" ) &&
77 !resourceFile.isDirectory() ) )
78 {
79
80 log.error( "Resource file '" + resourceFile.getAbsolutePath() + "' does not exist" );
81 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource does not exist" );
82 }
83 else
84 {
85 return createResource( resourceFile, logicalResource, davSession, continuumLocator );
86 }
87 }
88
89 public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
90 DavServletResponse response )
91 throws DavException
92 {
93 ContinuumBuildAgentDavResourceLocator continuumLocator = checkLocatorIsInstanceOfContinuumBuildAgentLocator(
94 locator );
95
96 if ( !WebdavMethodUtil.isReadMethod( request.getMethod() ) )
97 {
98 throw new DavException( HttpServletResponse.SC_METHOD_NOT_ALLOWED,
99 "Write method not allowed in working copy" );
100 }
101
102 String logicalResource = WorkingCopyPathUtil.getLogicalResource( continuumLocator.getResourcePath() );
103 if ( logicalResource.startsWith( "/" ) )
104 {
105 logicalResource = logicalResource.substring( 1 );
106 }
107
108 File resourceFile = getResourceFile( continuumLocator.getProjectId(), logicalResource );
109
110 if ( !resourceFile.exists() || ( continuumLocator.getHref( false ).endsWith( "/" ) &&
111 !resourceFile.isDirectory() ) )
112 {
113
114 log.error( "Resource file '" + resourceFile.getAbsolutePath() + "' does not exist" );
115 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource does not exist" );
116 }
117 else
118 {
119 return createResource( resourceFile, logicalResource, request.getDavSession(), continuumLocator );
120 }
121 }
122
123 public BuildAgentConfigurationService getBuildAgentConfigurationService()
124 {
125 return buildAgentConfigurationService;
126 }
127
128 public MimetypesFileTypeMap getMimeTypes()
129 {
130 return mimeTypes;
131 }
132
133 public void setBuildAgentConfigurationService( BuildAgentConfigurationService buildAgentConfigurationService )
134 {
135 this.buildAgentConfigurationService = buildAgentConfigurationService;
136 }
137
138 private ContinuumBuildAgentDavResourceLocator checkLocatorIsInstanceOfContinuumBuildAgentLocator(
139 DavResourceLocator locator )
140 throws DavException
141 {
142 if ( !( locator instanceof ContinuumBuildAgentDavResourceLocator ) )
143 {
144 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
145 "Locator does not implement ContinuumBuildAgentLocator" );
146 }
147
148
149 if ( locator.getResourcePath().startsWith( ContinuumBuildAgentDavResource.HIDDEN_PATH_PREFIX ) )
150 {
151 throw new DavException( HttpServletResponse.SC_NOT_FOUND );
152 }
153
154 ContinuumBuildAgentDavResourceLocator continuumLocator = (ContinuumBuildAgentDavResourceLocator) locator;
155 if ( continuumLocator.getProjectId() <= 0 )
156 {
157 log.error( "Invalid project id: " + continuumLocator.getProjectId() );
158 throw new DavException( HttpServletResponse.SC_NO_CONTENT );
159 }
160
161 return continuumLocator;
162 }
163
164 protected File getResourceFile( int projectId, String logicalResource )
165 {
166 File workingDir = buildAgentConfigurationService.getWorkingDirectory( projectId );
167
168 return new File( workingDir, logicalResource );
169 }
170
171 protected DavResource createResource( File resourceFile, String logicalResource, DavSession session,
172 ContinuumBuildAgentDavResourceLocator locator )
173 {
174 return new ContinuumBuildAgentDavResource( resourceFile.getAbsolutePath(), logicalResource, session, locator,
175 this, mimeTypes );
176 }
177 }