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