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