Coverage Report - org.apache.maven.archetype.proxy.RepositoryServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
RepositoryServlet
0%
0/74
0%
0/10
2.286
 
 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.proxy;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.FileInputStream;
 21  
 import java.io.FileNotFoundException;
 22  
 import java.io.FileWriter;
 23  
 import java.io.IOException;
 24  
 import java.io.InputStream;
 25  
 import java.io.PrintWriter;
 26  
 import java.io.StringWriter;
 27  
 import javax.servlet.ServletConfig;
 28  
 import javax.servlet.ServletContext;
 29  
 import javax.servlet.ServletException;
 30  
 import javax.servlet.ServletRequest;
 31  
 import javax.servlet.ServletResponse;
 32  
 import javax.servlet.http.HttpServlet;
 33  
 import javax.servlet.http.HttpServletRequest;
 34  
 import javax.servlet.http.HttpServletResponse;
 35  
 import org.mortbay.util.IO;
 36  
 import org.mortbay.util.StringUtil;
 37  
 
 38  
 /**
 39  
  *
 40  
  * @author rafale
 41  
  */
 42  0
 public class RepositoryServlet
 43  
     extends HttpServlet
 44  
 {
 45  
     private ServletConfig config;
 46  
 
 47  
     private ServletContext context;
 48  
 
 49  
     /*
 50  
      * (non-Javadoc)
 51  
      * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
 52  
      */
 53  
     public void init( ServletConfig config )
 54  
         throws ServletException
 55  
     {
 56  0
         this.config = config;
 57  0
         this.context = config.getServletContext();
 58  0
     }
 59  
 
 60  
     /*
 61  
      * (non-Javadoc)
 62  
      * @see javax.servlet.Servlet#getServletConfig()
 63  
      */
 64  
     public ServletConfig getServletConfig()
 65  
     {
 66  0
         return config;
 67  
     }
 68  
 
 69  
     /*
 70  
      * (non-Javadoc)
 71  
      * @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
 72  
      */
 73  
     public void service( ServletRequest req, ServletResponse res )
 74  
         throws ServletException
 75  
     {
 76  0
         HttpServletRequest request = (HttpServletRequest) req;
 77  0
         HttpServletResponse response = (HttpServletResponse) res;
 78  
 
 79  
         // log( "A = " + request.getAuthType() );
 80  
         // log( "A = " + request.getCharacterEncoding() );
 81  
         // log( "A = " + request.getContentType() );
 82  
         // log( "B = " + request.getContextPath() );
 83  
         // log( "B = " + request.getLocalAddr() );
 84  
         // log( "B = " + request.getLocalName() );
 85  
         // log( "C = " + request.getMethod() );
 86  
         // log( "C = " + request.getPathInfo() );
 87  
         // log( "C = " + request.getPathTranslated() );
 88  
         // log( "D = " + request.getProtocol() );
 89  
         // log( "D = " + request.getQueryString() );
 90  
         // log( "D = " + request.getRemoteAddr() );
 91  
         // log( "E = " + request.getRemoteHost() );
 92  
         // log( "E = " + request.getRemoteUser() );
 93  
         // log( "E = " + request.getRequestURI() );
 94  
         // log( "F = " + request.getRequestedSessionId() );
 95  
         // log( "F = " + request.getScheme() );
 96  
         // log( "F = " + request.getServerName() );
 97  
         // log( "G = " + request.getServletPath() );
 98  
         // log( "G = " + request.getAttributeNames() );
 99  
         // log( "G = " + request.getCookies() );
 100  
         // log( "H = " + request.getHeaderNames() );
 101  
 
 102  
         // log( "H = " + request.get );
 103  
         // log( "H = " + request.get );
 104  
         // log( "I = " + request.get );
 105  
         // log( "I = " + request.get );
 106  
         // log( "I = " + request.get );
 107  
         // log( "J = " + request.get );
 108  
         // log( "J = " + request.get );
 109  
         // log( "J = " + request.get );
 110  
         // log( "K = " + request.get );
 111  
         // log( "K = " + request.get );
 112  
         // log( "K = " + request.get );
 113  
 
 114  0
         response.setHeader( "Date", null );
 115  0
         response.setHeader( "Server", null );
 116  
 
 117  0
         log( "Proxy Requested file = " + request.getRequestURI() );
 118  0
         String filePath =
 119  
             System.getProperty( "org.apache.maven.archetype.repository.directory" ).trim() + "/"
 120  
                 + request.getRequestURI();
 121  0
         filePath = StringUtil.replace( filePath, "\\", "/" );
 122  0
         filePath = StringUtil.replace( filePath, "/", File.separator );
 123  0
         log( "Complete file path = " + filePath );
 124  
 
 125  0
         String method = request.getMethod();
 126  
 
 127  0
         if ( "GET".equalsIgnoreCase( method ) )
 128  
         {
 129  0
             log( "Getting file" );
 130  
             try
 131  
             {
 132  0
                 File requestedFile = new File( filePath );
 133  
 
 134  0
                 InputStream is = new FileInputStream( requestedFile );
 135  
 
 136  0
                 if ( is != null )
 137  
                 {
 138  0
                     IO.copy( is, response.getOutputStream() );
 139  0
                     response.setStatus( HttpServletResponse.SC_OK );
 140  0
                     log( "File sent" );
 141  
                 }
 142  
                 else
 143  
                 {
 144  0
                     log( "Can not send file no content" );
 145  
                 }
 146  
             }
 147  0
             catch ( FileNotFoundException fileNotFoundException )
 148  
             {
 149  0
                 response.setStatus( HttpServletResponse.SC_NOT_FOUND );
 150  0
                 log( "Requested file not found ", fileNotFoundException );
 151  
             }
 152  0
             catch ( IOException iOException )
 153  
             {
 154  0
                 response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
 155  0
                 log( "Can not send file", iOException );
 156  0
             }
 157  
         }
 158  0
         else if ( "PUT".equalsIgnoreCase( method ) )
 159  
         {
 160  0
             log( "Putting file" );
 161  0
             File uploadedFile = new File( filePath );
 162  0
             if ( uploadedFile.exists() )
 163  
             {
 164  0
                 uploadedFile.delete();
 165  0
                 log( "Removed old file" );
 166  
             }
 167  0
             else if ( !uploadedFile.getParentFile().exists() )
 168  
             {
 169  0
                 uploadedFile.getParentFile().mkdirs();
 170  0
                 log( "Created directory " + uploadedFile.getParent() );
 171  
             }
 172  
 
 173  
             try
 174  
             {
 175  0
                 FileWriter fw = new FileWriter( uploadedFile );
 176  0
                 IO.copy( request.getReader(), fw );
 177  0
                 response.setStatus( HttpServletResponse.SC_OK );
 178  0
                 log( "File copied" );
 179  
             }
 180  0
             catch ( IOException iOException )
 181  
             {
 182  
 
 183  0
                 response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
 184  0
                 log( "Can not send file", iOException );
 185  0
             }
 186  0
         }
 187  
         else
 188  
         {
 189  
 
 190  0
             response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
 191  
             try
 192  
             {
 193  0
                 log( "Method " + request.getMethod() );
 194  0
                 log( "ContextPath " + request.getContextPath() );
 195  0
                 log( "QueryString" + request.getQueryString() );
 196  0
                 log( "PathInfo " + request.getPathInfo() );
 197  0
                 log( "ServletPath " + request.getServletPath() );
 198  0
                 log( "AttributeNames " + request.getAttributeNames().toString() );
 199  0
                 log( "HeaderNames " + request.getHeaderNames().toString() );
 200  0
                 log( "RequestURL " + request.getRequestURL().toString() );
 201  0
                 log( "ParameterNames " + request.getParameterNames().toString() );
 202  0
                 StringWriter w = new StringWriter();
 203  0
                 IO.copy( request.getReader(), w );
 204  0
                 log( "Content " + w.toString() );
 205  
             }
 206  0
             catch ( IOException iOException )
 207  
             {
 208  0
                 log( "Error in unnknown method", iOException );
 209  0
             }
 210  
         }
 211  0
     }
 212  
 
 213  
     /*
 214  
      * (non-Javadoc)
 215  
      * @see javax.servlet.Servlet#getServletInfo()
 216  
      */
 217  
     public String getServletInfo()
 218  
     {
 219  0
         return "Repository Servlet";
 220  
     }
 221  
 
 222  
     /*
 223  
      * (non-Javadoc)
 224  
      * @see javax.servlet.Servlet#destroy()
 225  
      */
 226  
     public void destroy()
 227  
     {
 228  0
     }
 229  
 
 230  
     /**
 231  
      * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 232  
      * 
 233  
      * @param request servlet request
 234  
      * @param response servlet response
 235  
      */
 236  
     protected void processRequest( HttpServletRequest request, HttpServletResponse response )
 237  
         throws ServletException, IOException
 238  
     {
 239  0
         response.setContentType( "text/html;charset=UTF-8" );
 240  0
         PrintWriter out = response.getWriter();
 241  
         try
 242  
         {
 243  
             /*
 244  
              * TODO output your page here out.println("<html>"); out.println("<head>");
 245  
              * out.println("<title>Servlet RepositoryServlet</title>"); out.println("</head>"); out.println("<body>");
 246  
              * out.println("<h1>Servlet RepositoryServlet at " + request.getContextPath () + "</h1>");
 247  
              * out.println("</body>"); out.println("</html>");
 248  
              */
 249  
         }
 250  
         finally
 251  
         {
 252  0
             out.close();
 253  0
         }
 254  0
     }
 255  
 
 256  
     public String getServletName()
 257  
     {
 258  0
         return "Repository Servlet";
 259  
     }
 260  
 
 261  
 }