View Javadoc
1   package org.apache.maven.archetype.repository;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author rafale
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  }