View Javadoc

1   /*
2    *  Copyright 2007 rafale.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *  under the License.
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   * @author rafale
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  }