1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.maven.archetype.repository; |
18 | |
|
19 | |
import java.io.File; |
20 | |
import java.io.FileInputStream; |
21 | |
import java.io.FileNotFoundException; |
22 | |
import java.io.IOException; |
23 | |
import java.io.InputStream; |
24 | |
import javax.servlet.ServletException; |
25 | |
import javax.servlet.http.HttpServlet; |
26 | |
import javax.servlet.http.HttpServletRequest; |
27 | |
import javax.servlet.http.HttpServletResponse; |
28 | |
|
29 | |
import org.mortbay.util.IO; |
30 | |
import org.mortbay.util.StringUtil; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public class RepositoryServlet |
37 | |
extends HttpServlet |
38 | |
{ |
39 | |
private File getFile( HttpServletRequest request ) |
40 | |
{ |
41 | 0 | log( "Requested file = " + request.getRequestURI() ); |
42 | |
|
43 | 0 | String filePath = |
44 | |
System.getProperty( "org.apache.maven.archetype.repository.directory" ).trim() + "/" |
45 | |
+ request.getRequestURI(); |
46 | 0 | filePath = StringUtil.replace( filePath, "\\", File.separator ); |
47 | 0 | filePath = StringUtil.replace( filePath, File.separator, "/" ); |
48 | 0 | filePath = filePath.replaceAll( "/repo/", "/" ); |
49 | 0 | filePath = filePath.replaceAll( "//", "/" ); |
50 | 0 | filePath = filePath.replaceAll( "//", "/" ); |
51 | 0 | filePath = filePath.replaceAll( "//", "/" ); |
52 | 0 | filePath = StringUtil.replace( filePath, "/", File.separator ); |
53 | 0 | log( "Complete file path = " + filePath ); |
54 | |
|
55 | 0 | return new File( filePath ); |
56 | |
} |
57 | |
|
58 | |
public void doGet( HttpServletRequest request, HttpServletResponse response ) |
59 | |
throws ServletException |
60 | |
{ |
61 | 0 | log( "Getting file" ); |
62 | 0 | InputStream is = null; |
63 | |
try |
64 | |
{ |
65 | 0 | is = new FileInputStream( getFile( request ) ); |
66 | |
|
67 | 0 | IO.copy( is, response.getOutputStream() ); |
68 | 0 | response.setStatus( HttpServletResponse.SC_OK ); |
69 | 0 | log( "File sent" ); |
70 | |
} |
71 | 0 | catch ( FileNotFoundException fileNotFoundException ) |
72 | |
{ |
73 | 0 | response.setStatus( HttpServletResponse.SC_NOT_FOUND ); |
74 | 0 | log( "Requested file not found " ); |
75 | |
} |
76 | 0 | catch ( IOException iOException ) |
77 | |
{ |
78 | 0 | response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); |
79 | 0 | log( "Cannot send file", iOException ); |
80 | |
} |
81 | |
finally |
82 | |
{ |
83 | 0 | IO.close( is ); |
84 | 0 | } |
85 | 0 | } |
86 | |
} |