1 | |
package org.apache.maven.archiva.webdav; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import javax.servlet.ServletConfig; |
27 | |
import javax.servlet.ServletException; |
28 | |
import javax.servlet.http.HttpServletRequest; |
29 | |
import javax.servlet.http.HttpServletResponse; |
30 | |
|
31 | |
import org.apache.jackrabbit.webdav.DavException; |
32 | |
import org.apache.jackrabbit.webdav.DavLocatorFactory; |
33 | |
import org.apache.jackrabbit.webdav.DavMethods; |
34 | |
import org.apache.jackrabbit.webdav.DavResource; |
35 | |
import org.apache.jackrabbit.webdav.DavResourceFactory; |
36 | |
import org.apache.jackrabbit.webdav.DavServletResponse; |
37 | |
import org.apache.jackrabbit.webdav.DavSessionProvider; |
38 | |
import org.apache.jackrabbit.webdav.WebdavRequest; |
39 | |
import org.apache.jackrabbit.webdav.WebdavRequestImpl; |
40 | |
import org.apache.jackrabbit.webdav.WebdavResponse; |
41 | |
import org.apache.jackrabbit.webdav.WebdavResponseImpl; |
42 | |
import org.apache.jackrabbit.webdav.server.AbstractWebdavServlet; |
43 | |
import org.apache.maven.archiva.configuration.ArchivaConfiguration; |
44 | |
import org.apache.maven.archiva.configuration.ConfigurationEvent; |
45 | |
import org.apache.maven.archiva.configuration.ConfigurationListener; |
46 | |
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; |
47 | |
import org.apache.maven.archiva.security.ServletAuthenticator; |
48 | |
import org.codehaus.plexus.spring.PlexusToSpringUtils; |
49 | |
import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator; |
50 | |
import org.slf4j.Logger; |
51 | |
import org.slf4j.LoggerFactory; |
52 | |
import org.springframework.context.ConfigurableApplicationContext; |
53 | |
import org.springframework.web.context.WebApplicationContext; |
54 | |
import org.springframework.web.context.support.WebApplicationContextUtils; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | public class RepositoryServlet |
62 | |
extends AbstractWebdavServlet |
63 | |
implements ConfigurationListener |
64 | |
{ |
65 | 0 | private Logger log = LoggerFactory.getLogger( RepositoryServlet.class ); |
66 | |
|
67 | |
private ArchivaConfiguration configuration; |
68 | |
|
69 | |
private Map<String, ManagedRepositoryConfiguration> repositoryMap; |
70 | |
|
71 | |
private DavLocatorFactory locatorFactory; |
72 | |
|
73 | |
private DavResourceFactory resourceFactory; |
74 | |
|
75 | |
private DavSessionProvider sessionProvider; |
76 | |
|
77 | 0 | private final Object reloadLock = new Object(); |
78 | |
|
79 | |
public void init( javax.servlet.ServletConfig servletConfig ) |
80 | |
throws ServletException |
81 | |
{ |
82 | 0 | super.init( servletConfig ); |
83 | 0 | initServers( servletConfig ); |
84 | 0 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
@Override |
96 | |
protected void service( HttpServletRequest request, HttpServletResponse response ) |
97 | |
throws ServletException, IOException |
98 | |
{ |
99 | 0 | WebdavRequest webdavRequest = new WebdavRequestImpl( request, getLocatorFactory() ); |
100 | |
|
101 | 0 | int methodCode = DavMethods.getMethodCode( request.getMethod() ); |
102 | 0 | boolean noCache = |
103 | |
DavMethods.isDeltaVMethod( webdavRequest ) |
104 | |
&& !( DavMethods.DAV_VERSION_CONTROL == methodCode || DavMethods.DAV_REPORT == methodCode ); |
105 | 0 | WebdavResponse webdavResponse = new WebdavResponseImpl( response, noCache ); |
106 | 0 | DavResource resource = null; |
107 | |
|
108 | |
try |
109 | |
{ |
110 | |
|
111 | 0 | if ( !getDavSessionProvider().attachSession( webdavRequest ) ) |
112 | |
{ |
113 | |
return; |
114 | |
} |
115 | |
|
116 | |
|
117 | 0 | resource = |
118 | |
getResourceFactory().createResource( webdavRequest.getRequestLocator(), webdavRequest, webdavResponse ); |
119 | |
|
120 | 0 | if ( !isPreconditionValid( webdavRequest, resource ) ) |
121 | |
{ |
122 | 0 | webdavResponse.sendError( DavServletResponse.SC_PRECONDITION_FAILED ); |
123 | |
return; |
124 | |
} |
125 | 0 | if ( !execute( webdavRequest, webdavResponse, methodCode, resource ) ) |
126 | |
{ |
127 | 0 | super.service( request, response ); |
128 | |
} |
129 | |
|
130 | |
} |
131 | 0 | catch ( UnauthorizedDavException e ) |
132 | |
{ |
133 | 0 | webdavResponse.setHeader( "WWW-Authenticate", getAuthenticateHeaderValue( e.getRepositoryName() ) ); |
134 | 0 | webdavResponse.sendError( e.getErrorCode(), e.getStatusPhrase() ); |
135 | |
} |
136 | 0 | catch ( BrowserRedirectException e ) |
137 | |
{ |
138 | 0 | response.sendRedirect( e.getLocation() ); |
139 | |
} |
140 | 0 | catch ( DavException e ) |
141 | |
{ |
142 | 0 | if ( e.getErrorCode() == HttpServletResponse.SC_UNAUTHORIZED ) |
143 | |
{ |
144 | 0 | final String msg = "Should throw " + UnauthorizedDavException.class.getName(); |
145 | 0 | log.error( msg ); |
146 | 0 | webdavResponse.sendError( e.getErrorCode(), msg ); |
147 | 0 | } |
148 | 0 | else if ( e.getCause() != null ) |
149 | |
{ |
150 | 0 | webdavResponse.sendError( e.getErrorCode(), e.getCause().getMessage() ); |
151 | |
} |
152 | |
else |
153 | |
{ |
154 | 0 | webdavResponse.sendError( e.getErrorCode(), e.getMessage() ); |
155 | |
} |
156 | |
} |
157 | |
finally |
158 | |
{ |
159 | 0 | getDavSessionProvider().releaseSession( webdavRequest ); |
160 | 0 | } |
161 | 0 | } |
162 | |
|
163 | |
public synchronized void initServers( ServletConfig servletConfig ) |
164 | |
{ |
165 | 0 | WebApplicationContext wac = |
166 | |
WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() ); |
167 | |
|
168 | 0 | configuration = |
169 | |
(ArchivaConfiguration) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaConfiguration.class.getName() ) ); |
170 | 0 | configuration.addListener( this ); |
171 | |
|
172 | 0 | repositoryMap = configuration.getConfiguration().getManagedRepositoriesAsMap(); |
173 | |
|
174 | 0 | for ( ManagedRepositoryConfiguration repo : repositoryMap.values() ) |
175 | |
{ |
176 | 0 | File repoDir = new File( repo.getLocation() ); |
177 | |
|
178 | 0 | if ( !repoDir.exists() ) |
179 | |
{ |
180 | 0 | if ( !repoDir.mkdirs() ) |
181 | |
{ |
182 | |
|
183 | 0 | log( "Unable to create missing directory for " + repo.getLocation() ); |
184 | 0 | continue; |
185 | |
} |
186 | |
} |
187 | 0 | } |
188 | |
|
189 | 0 | resourceFactory = |
190 | |
(DavResourceFactory) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaDavResourceFactory.class ) ); |
191 | 0 | locatorFactory = new ArchivaDavLocatorFactory(); |
192 | |
|
193 | 0 | ServletAuthenticator servletAuth = |
194 | |
(ServletAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( ServletAuthenticator.class.getName() ) ); |
195 | 0 | HttpAuthenticator httpAuth = |
196 | |
(HttpAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( HttpAuthenticator.ROLE, "basic" ) ); |
197 | |
|
198 | 0 | sessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth ); |
199 | 0 | } |
200 | |
|
201 | |
public void configurationEvent( ConfigurationEvent event ) |
202 | |
{ |
203 | 0 | if ( event.getType() == ConfigurationEvent.SAVED ) |
204 | |
{ |
205 | 0 | initRepositories(); |
206 | |
} |
207 | 0 | } |
208 | |
|
209 | |
private void initRepositories() |
210 | |
{ |
211 | 0 | synchronized ( repositoryMap ) |
212 | |
{ |
213 | 0 | repositoryMap.clear(); |
214 | 0 | repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() ); |
215 | 0 | } |
216 | |
|
217 | 0 | synchronized ( reloadLock ) |
218 | |
{ |
219 | 0 | initServers( getServletConfig() ); |
220 | 0 | } |
221 | 0 | } |
222 | |
|
223 | |
public synchronized ManagedRepositoryConfiguration getRepository( String prefix ) |
224 | |
{ |
225 | 0 | if ( repositoryMap.isEmpty() ) |
226 | |
{ |
227 | 0 | repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() ); |
228 | |
} |
229 | 0 | return repositoryMap.get( prefix ); |
230 | |
} |
231 | |
|
232 | |
ArchivaConfiguration getConfiguration() |
233 | |
{ |
234 | 0 | return configuration; |
235 | |
} |
236 | |
|
237 | |
protected boolean isPreconditionValid( final WebdavRequest request, final DavResource davResource ) |
238 | |
{ |
239 | |
|
240 | |
|
241 | 0 | return true; |
242 | |
} |
243 | |
|
244 | |
public DavSessionProvider getDavSessionProvider() |
245 | |
{ |
246 | 0 | return sessionProvider; |
247 | |
} |
248 | |
|
249 | |
public void setDavSessionProvider( final DavSessionProvider davSessionProvider ) |
250 | |
{ |
251 | 0 | this.sessionProvider = davSessionProvider; |
252 | 0 | } |
253 | |
|
254 | |
public DavLocatorFactory getLocatorFactory() |
255 | |
{ |
256 | 0 | return locatorFactory; |
257 | |
} |
258 | |
|
259 | |
public void setLocatorFactory( final DavLocatorFactory davLocatorFactory ) |
260 | |
{ |
261 | 0 | locatorFactory = davLocatorFactory; |
262 | 0 | } |
263 | |
|
264 | |
public DavResourceFactory getResourceFactory() |
265 | |
{ |
266 | 0 | return resourceFactory; |
267 | |
} |
268 | |
|
269 | |
public void setResourceFactory( final DavResourceFactory davResourceFactory ) |
270 | |
{ |
271 | 0 | resourceFactory = davResourceFactory; |
272 | 0 | } |
273 | |
|
274 | |
public String getAuthenticateHeaderValue() |
275 | |
{ |
276 | 0 | throw new UnsupportedOperationException(); |
277 | |
} |
278 | |
|
279 | |
public String getAuthenticateHeaderValue( String repository ) |
280 | |
{ |
281 | 0 | return "Basic realm=\"Repository Archiva Managed " + repository + " Repository\""; |
282 | |
} |
283 | |
|
284 | |
@Override |
285 | |
public void destroy() |
286 | |
{ |
287 | 0 | configuration.removeListener( this ); |
288 | |
|
289 | 0 | resourceFactory = null; |
290 | 0 | configuration = null; |
291 | 0 | locatorFactory = null; |
292 | 0 | sessionProvider = null; |
293 | 0 | repositoryMap.clear(); |
294 | 0 | repositoryMap = null; |
295 | |
|
296 | 0 | WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext( getServletContext() ); |
297 | |
|
298 | 0 | if ( wac instanceof ConfigurableApplicationContext ) |
299 | |
{ |
300 | 0 | ( (ConfigurableApplicationContext) wac ).close(); |
301 | |
} |
302 | 0 | super.destroy(); |
303 | 0 | } |
304 | |
} |