Coverage Report - org.apache.maven.archetype.repository.RepositoryServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
RepositoryServlet
0 %
0/27
N/A
2
 
 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  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  
 }