1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42 public class RepositoryServlet
43 extends HttpServlet
44 {
45 private ServletConfig config;
46
47 private ServletContext context;
48
49
50
51
52
53 public void init( ServletConfig config )
54 throws ServletException
55 {
56 this.config = config;
57 this.context = config.getServletContext();
58 }
59
60
61
62
63
64 public ServletConfig getServletConfig()
65 {
66 return config;
67 }
68
69
70
71
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
80
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 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
215
216
217 public String getServletInfo()
218 {
219 return "Repository Servlet";
220 }
221
222
223
224
225
226 public void destroy()
227 {
228 }
229
230
231
232
233
234
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
245
246
247
248
249 }
250 finally
251 {
252 out.close();
253 }
254 }
255
256 public String getServletName()
257 {
258 return "Repository Servlet";
259 }
260
261 }