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.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  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          this.config = config;
57          this.context = config.getServletContext();
58      }
59  
60      /*
61       * (non-Javadoc)
62       * @see javax.servlet.Servlet#getServletConfig()
63       */
64      public ServletConfig getServletConfig()
65      {
66          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          HttpServletRequest request = (HttpServletRequest) req;
77          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         response.setHeader( "Date", null );
115         response.setHeader( "Server", null );
116 
117         log( "Proxy Requested file = " + request.getRequestURI() );
118         String filePath =
119             System.getProperty( "org.apache.maven.archetype.repository.directory" ).trim() + "/"
120                 + request.getRequestURI();
121         filePath = StringUtil.replace( filePath, "\\", "/" );
122         filePath = StringUtil.replace( filePath, "/", File.separator );
123         log( "Complete file path = " + filePath );
124 
125         String method = request.getMethod();
126 
127         if ( "GET".equalsIgnoreCase( method ) )
128         {
129             log( "Getting file" );
130             try
131             {
132                 File requestedFile = new File( filePath );
133 
134                 InputStream is = new FileInputStream( requestedFile );
135 
136                 if ( is != null )
137                 {
138                     IO.copy( is, response.getOutputStream() );
139                     response.setStatus( HttpServletResponse.SC_OK );
140                     log( "File sent" );
141                 }
142                 else
143                 {
144                     log( "Can not send file no content" );
145                 }
146             }
147             catch ( FileNotFoundException fileNotFoundException )
148             {
149                 response.setStatus( HttpServletResponse.SC_NOT_FOUND );
150                 log( "Requested file not found ", fileNotFoundException );
151             }
152             catch ( IOException iOException )
153             {
154                 response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
155                 log( "Can not send file", iOException );
156             }
157         }
158         else if ( "PUT".equalsIgnoreCase( method ) )
159         {
160             log( "Putting file" );
161             File uploadedFile = new File( filePath );
162             if ( uploadedFile.exists() )
163             {
164                 uploadedFile.delete();
165                 log( "Removed old file" );
166             }
167             else if ( !uploadedFile.getParentFile().exists() )
168             {
169                 uploadedFile.getParentFile().mkdirs();
170                 log( "Created directory " + uploadedFile.getParent() );
171             }
172 
173             try
174             {
175                 FileWriter fw = new FileWriter( uploadedFile );
176                 IO.copy( request.getReader(), fw );
177                 response.setStatus( HttpServletResponse.SC_OK );
178                 log( "File copied" );
179             }
180             catch ( IOException iOException )
181             {
182 
183                 response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
184                 log( "Can not send file", iOException );
185             }
186         }
187         else
188         {
189 
190             response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
191             try
192             {
193                 log( "Method " + request.getMethod() );
194                 log( "ContextPath " + request.getContextPath() );
195                 log( "QueryString" + request.getQueryString() );
196                 log( "PathInfo " + request.getPathInfo() );
197                 log( "ServletPath " + request.getServletPath() );
198                 log( "AttributeNames " + request.getAttributeNames().toString() );
199                 log( "HeaderNames " + request.getHeaderNames().toString() );
200                 log( "RequestURL " + request.getRequestURL().toString() );
201                 log( "ParameterNames " + request.getParameterNames().toString() );
202                 StringWriter w = new StringWriter();
203                 IO.copy( request.getReader(), w );
204                 log( "Content " + w.toString() );
205             }
206             catch ( IOException iOException )
207             {
208                 log( "Error in unnknown method", iOException );
209             }
210         }
211     }
212 
213     /*
214      * (non-Javadoc)
215      * @see javax.servlet.Servlet#getServletInfo()
216      */
217     public String getServletInfo()
218     {
219         return "Repository Servlet";
220     }
221 
222     /*
223      * (non-Javadoc)
224      * @see javax.servlet.Servlet#destroy()
225      */
226     public void destroy()
227     {
228     }
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         response.setContentType( "text/html;charset=UTF-8" );
240         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             out.close();
253         }
254     }
255 
256     public String getServletName()
257     {
258         return "Repository Servlet";
259     }
260 
261 }