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 | 0 | 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 | 0 | this.config = config; |
57 | 0 | this.context = config.getServletContext(); |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public ServletConfig getServletConfig() |
65 | |
{ |
66 | 0 | return config; |
67 | |
} |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
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 | |
|
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 | 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 | |
|
215 | |
|
216 | |
|
217 | |
public String getServletInfo() |
218 | |
{ |
219 | 0 | return "Repository Servlet"; |
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
public void destroy() |
227 | |
{ |
228 | 0 | } |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
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 | |
|
245 | |
|
246 | |
|
247 | |
|
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 | |
} |