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