1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.portlet; |
18 |
|
|
19 |
|
import java.io.BufferedInputStream; |
20 |
|
import java.io.ByteArrayInputStream; |
21 |
|
import java.io.ByteArrayOutputStream; |
22 |
|
import java.io.FileReader; |
23 |
|
import java.io.IOException; |
24 |
|
import java.io.InputStream; |
25 |
|
import java.io.InputStreamReader; |
26 |
|
import java.io.OutputStreamWriter; |
27 |
|
import java.io.PrintWriter; |
28 |
|
import java.io.Reader; |
29 |
|
import java.io.StringWriter; |
30 |
|
import java.io.UnsupportedEncodingException; |
31 |
|
import java.io.Writer; |
32 |
|
import java.net.URL; |
33 |
|
import java.util.ArrayList; |
34 |
|
import java.util.Arrays; |
35 |
|
import java.util.HashMap; |
36 |
|
import java.util.Iterator; |
37 |
|
import java.util.Map; |
38 |
|
import java.util.StringTokenizer; |
39 |
|
|
40 |
|
import javax.portlet.ActionRequest; |
41 |
|
import javax.portlet.ActionResponse; |
42 |
|
import javax.portlet.PortletConfig; |
43 |
|
import javax.portlet.PortletContext; |
44 |
|
import javax.portlet.PortletException; |
45 |
|
import javax.portlet.PortletMode; |
46 |
|
import javax.portlet.PortletURL; |
47 |
|
import javax.portlet.RenderRequest; |
48 |
|
import javax.portlet.RenderResponse; |
49 |
|
|
50 |
|
import org.apache.commons.httpclient.Cookie; |
51 |
|
import org.apache.commons.httpclient.Header; |
52 |
|
import org.apache.commons.httpclient.HttpClient; |
53 |
|
import org.apache.commons.httpclient.HttpMethod; |
54 |
|
import org.apache.commons.httpclient.HttpMethodBase; |
55 |
|
import org.apache.commons.httpclient.NameValuePair; |
56 |
|
import org.apache.commons.httpclient.methods.GetMethod; |
57 |
|
import org.apache.commons.httpclient.methods.MultipartPostMethod; |
58 |
|
import org.apache.commons.httpclient.methods.PostMethod; |
59 |
|
import org.apache.commons.logging.Log; |
60 |
|
import org.apache.commons.logging.LogFactory; |
61 |
|
import org.apache.jetspeed.portlet.webcontent.WebContentHistoryList; |
62 |
|
import org.apache.jetspeed.portlet.webcontent.WebContentHistoryPage; |
63 |
|
import org.apache.jetspeed.rewriter.JetspeedRewriterController; |
64 |
|
import org.apache.jetspeed.rewriter.RewriterController; |
65 |
|
import org.apache.jetspeed.rewriter.RewriterException; |
66 |
|
import org.apache.jetspeed.rewriter.RulesetRewriter; |
67 |
|
import org.apache.jetspeed.rewriter.WebContentRewriter; |
68 |
|
import org.apache.jetspeed.rewriter.html.neko.NekoParserAdaptor; |
69 |
|
import org.apache.jetspeed.rewriter.rules.Ruleset; |
70 |
|
import org.apache.jetspeed.rewriter.xml.SaxParserAdaptor; |
71 |
|
import org.apache.portals.bridges.velocity.GenericVelocityPortlet; |
72 |
|
import org.apache.portals.messaging.PortletMessaging; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
public class WebContentPortlet extends GenericVelocityPortlet |
85 |
|
{ |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
public static final String VIEW_SOURCE_PARAM = "viewSource"; |
96 |
|
public static final String EDIT_SOURCE_PARAM = "editSource"; |
97 |
|
|
98 |
|
|
99 |
|
public static final String BROWSER_ACTION_PARAM = "wcBrowserAction"; |
100 |
|
public static final String BROWSER_ACTION_PREVIOUS_PAGE = "previousPage"; |
101 |
|
public static final String BROWSER_ACTION_REFRESH_PAGE = "refreshPage"; |
102 |
|
public static final String BROWSER_ACTION_NEXT_PAGE = "nextPage"; |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
public static final String HISTORY = "webcontent.history"; |
111 |
|
public static final String HTTP_STATE = "webcontent.http.state"; |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
0 |
protected final static Log log = LogFactory.getLog(WebContentPortlet.class); |
116 |
|
public final static String defaultEncoding = "UTF-8"; |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
0 |
private RulesetRewriter rewriter = null; |
121 |
0 |
private RewriterController rewriteController = null; |
122 |
|
|
123 |
|
public static final String FORM_POST_METHOD = "post"; |
124 |
|
public static final String FORM_GET_METHOD = "get"; |
125 |
|
public static final String FORM_MULTIPART_METHOD = "multipart"; |
126 |
|
|
127 |
|
public WebContentPortlet() |
128 |
|
{ |
129 |
0 |
super(); |
130 |
0 |
} |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
public void init(PortletConfig config) throws PortletException |
136 |
|
{ |
137 |
0 |
super.init(config); |
138 |
0 |
} |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, |
150 |
|
IOException |
151 |
|
{ |
152 |
|
|
153 |
0 |
String browserAction = actionRequest.getParameter(BROWSER_ACTION_PARAM); |
154 |
0 |
if (browserAction != null) |
155 |
|
{ |
156 |
0 |
if (!browserAction.equalsIgnoreCase(BROWSER_ACTION_REFRESH_PAGE)) |
157 |
|
{ |
158 |
|
|
159 |
0 |
WebContentHistoryList history = (WebContentHistoryList)PortletMessaging.receive(actionRequest, HISTORY); |
160 |
|
|
161 |
0 |
if (browserAction.equalsIgnoreCase(BROWSER_ACTION_PREVIOUS_PAGE)) |
162 |
|
{ |
163 |
0 |
if (history.hasPreviousPage()) |
164 |
0 |
history.getPreviousPage(); |
165 |
|
} |
166 |
0 |
else if (browserAction.equalsIgnoreCase(BROWSER_ACTION_NEXT_PAGE)) |
167 |
|
{ |
168 |
0 |
if (history.hasNextPage()) |
169 |
0 |
history.getNextPage(); |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
0 |
return ; |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
0 |
String webContentURL = actionRequest.getParameter(WebContentRewriter.ACTION_PARAMETER_URL); |
178 |
0 |
String webContentMethod = actionRequest.getParameter(WebContentRewriter.ACTION_PARAMETER_METHOD); |
179 |
0 |
Map webContentParams = new HashMap(actionRequest.getParameterMap()) ; |
180 |
|
|
181 |
|
|
182 |
0 |
if (webContentMethod == null) webContentMethod = "" ; |
183 |
|
|
184 |
|
|
185 |
0 |
webContentParams.remove(WebContentRewriter.ACTION_PARAMETER_URL); |
186 |
0 |
webContentParams.remove(WebContentRewriter.ACTION_PARAMETER_METHOD); |
187 |
|
|
188 |
0 |
if (webContentURL == null || actionRequest.getPortletMode() == PortletMode.EDIT) |
189 |
|
{ |
190 |
0 |
processPreferencesAction(actionRequest, actionResponse); |
191 |
0 |
webContentURL = actionRequest.getPreferences().getValue("SRC", "http://portals.apache.org"); |
192 |
|
|
193 |
|
|
194 |
0 |
webContentParams.clear(); |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
0 |
if (webContentURL != null && webContentURL.length() > 0) |
201 |
|
{ |
202 |
|
|
203 |
0 |
WebContentHistoryList history = (WebContentHistoryList)PortletMessaging.receive(actionRequest, HISTORY); |
204 |
0 |
if (history == null) |
205 |
0 |
history = new WebContentHistoryList(); |
206 |
0 |
history.visitPage(new WebContentHistoryPage(webContentURL,webContentParams,webContentMethod)); |
207 |
0 |
PortletMessaging.publish(actionRequest, HISTORY, history); |
208 |
|
} |
209 |
0 |
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException |
216 |
|
{ |
217 |
0 |
String viewPage = (String)request.getAttribute(PARAM_VIEW_PAGE); |
218 |
0 |
if (viewPage != null) |
219 |
|
{ |
220 |
0 |
super.doView(request, response); |
221 |
0 |
return; |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
0 |
WebContentHistoryList history = (WebContentHistoryList)PortletMessaging.receive(request, HISTORY); |
226 |
0 |
if (history == null) |
227 |
0 |
history = new WebContentHistoryList(); |
228 |
0 |
WebContentHistoryPage currentPage = history.getCurrentPage(); |
229 |
0 |
if (currentPage == null) |
230 |
|
{ |
231 |
0 |
String sourceURL = request.getPreferences().getValue("SRC", ""); |
232 |
0 |
if (sourceURL == null) |
233 |
|
{ |
234 |
|
|
235 |
0 |
throw new PortletException("WebContent source not specified. Go to edit mode and specify an URL."); |
236 |
|
} |
237 |
0 |
currentPage = new WebContentHistoryPage(sourceURL); |
238 |
|
} |
239 |
|
|
240 |
|
|
241 |
0 |
if (rewriteController == null) |
242 |
|
{ |
243 |
0 |
PortletContext portletApplication = getPortletContext(); |
244 |
0 |
String path = portletApplication.getRealPath("/WEB-INF"); |
245 |
0 |
String contextPath = path + "/"; |
246 |
|
try |
247 |
|
{ |
248 |
|
|
249 |
0 |
rewriteController = getController(contextPath); |
250 |
|
} |
251 |
0 |
catch (Exception e) |
252 |
|
{ |
253 |
|
|
254 |
0 |
String msg = "WebContentPortlet failed to create rewriter controller."; |
255 |
0 |
log.error(msg,e); |
256 |
0 |
throw new PortletException(e.getMessage()); |
257 |
0 |
} |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
0 |
response.setContentType("text/html"); |
262 |
0 |
byte[] content = doWebContent(currentPage.getUrl(), currentPage.getParams(), currentPage.isPost(), request, response); |
263 |
|
|
264 |
|
|
265 |
|
|
266 |
0 |
PrintWriter writer = response.getWriter(); |
267 |
0 |
writer.print("<block>"); |
268 |
0 |
if (history.hasPreviousPage()) |
269 |
|
{ |
270 |
0 |
PortletURL prevAction = response.createActionURL() ; |
271 |
0 |
prevAction.setParameter(BROWSER_ACTION_PARAM, BROWSER_ACTION_PREVIOUS_PAGE); |
272 |
0 |
writer.print(" [<a href=\"" + prevAction.toString() +"\">Previous Page</a>] "); |
273 |
|
} |
274 |
0 |
PortletURL refreshAction = response.createActionURL() ; |
275 |
0 |
refreshAction.setParameter(BROWSER_ACTION_PARAM, BROWSER_ACTION_REFRESH_PAGE); |
276 |
0 |
writer.print(" [<a href=\"" + refreshAction.toString() +"\">Refresh Page</a>] "); |
277 |
0 |
if (history.hasNextPage()) |
278 |
|
{ |
279 |
0 |
PortletURL nextAction = response.createActionURL() ; |
280 |
0 |
nextAction.setParameter(BROWSER_ACTION_PARAM, BROWSER_ACTION_NEXT_PAGE); |
281 |
0 |
writer.print(" [<a href=\"" + nextAction.toString() +"\">Next Page</a>] "); |
282 |
|
} |
283 |
0 |
writer.print("</block><hr/>"); |
284 |
|
|
285 |
|
|
286 |
0 |
ByteArrayInputStream bais = new ByteArrayInputStream(content); |
287 |
0 |
drain(new InputStreamReader(bais, WebContentPortlet.defaultEncoding), writer); |
288 |
0 |
bais.close(); |
289 |
|
|
290 |
|
|
291 |
0 |
history.visitPage(currentPage); |
292 |
0 |
PortletMessaging.publish(request, HISTORY, history); |
293 |
0 |
} |
294 |
|
|
295 |
|
public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException |
296 |
|
{ |
297 |
0 |
response.setContentType("text/html"); |
298 |
0 |
doPreferencesEdit(request, response); |
299 |
0 |
} |
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
protected byte[] doWebContent(String sourceAttr, Map sourceParams, boolean isPost, RenderRequest request, RenderResponse response) |
305 |
|
throws PortletException |
306 |
|
{ |
307 |
0 |
HttpMethod httpMethod = null ; |
308 |
|
|
309 |
|
try |
310 |
|
{ |
311 |
|
|
312 |
0 |
PortletURL action = response.createActionURL(); |
313 |
0 |
((WebContentRewriter) rewriter).setActionURL(action); |
314 |
0 |
URL baseURL = new URL(sourceAttr); |
315 |
0 |
rewriter.setBaseUrl(baseURL.toString()); |
316 |
|
|
317 |
|
|
318 |
0 |
if (baseURL.getProtocol().equals("file")) |
319 |
|
{ |
320 |
0 |
Reader reader = new InputStreamReader((InputStream)baseURL.getContent()); |
321 |
0 |
StringWriter writer = new StringWriter(); |
322 |
0 |
rewriter.rewrite(rewriteController.createParserAdaptor("text/html"), reader, writer); |
323 |
0 |
writer.flush(); |
324 |
0 |
return writer.toString().getBytes(); |
325 |
|
} |
326 |
|
|
327 |
|
|
328 |
|
|
329 |
0 |
HttpClient httpClient = getHttpClient(request) ; |
330 |
0 |
String method = (isPost) ? FORM_POST_METHOD : FORM_GET_METHOD; |
331 |
0 |
httpMethod = getHttpMethod(httpClient, getURLSource(sourceAttr, sourceParams, request, response), sourceParams, method, request); |
332 |
0 |
byte[] result = doPreemptiveAuthentication(httpClient, httpMethod, request, response); |
333 |
|
|
334 |
|
|
335 |
0 |
if (result == null) { |
336 |
0 |
return doHttpWebContent(httpClient, httpMethod, 0, request, response); |
337 |
|
} else { |
338 |
0 |
return result; |
339 |
|
} |
340 |
|
} |
341 |
0 |
catch (PortletException pex) |
342 |
|
{ |
343 |
|
|
344 |
0 |
throw pex; |
345 |
|
} |
346 |
0 |
catch (Exception ex) |
347 |
|
{ |
348 |
0 |
String msg = "Exception while rewritting HTML content" ; |
349 |
0 |
log.error(msg,ex); |
350 |
0 |
throw new PortletException(msg+", Error: "+ex.getMessage()); |
351 |
|
} |
352 |
|
finally |
353 |
|
{ |
354 |
|
|
355 |
0 |
if (httpMethod != null) |
356 |
0 |
httpMethod.releaseConnection(); |
357 |
|
} |
358 |
|
} |
359 |
|
|
360 |
|
protected byte[] doHttpWebContent(HttpClient httpClient, HttpMethod httpMethod, int retryCount, RenderRequest request, RenderResponse response) |
361 |
|
throws PortletException |
362 |
|
{ |
363 |
|
try |
364 |
|
{ |
365 |
|
|
366 |
|
|
367 |
|
|
368 |
|
|
369 |
0 |
httpClient.executeMethod(httpMethod); |
370 |
|
|
371 |
|
|
372 |
0 |
rewriter.setBaseUrl( rewriter.getBaseRelativeUrl( httpMethod.getPath() )) ; |
373 |
|
|
374 |
|
|
375 |
|
|
376 |
0 |
Cookie[] cookies = httpClient.getState().getCookies(); |
377 |
0 |
PortletMessaging.publish(request, HTTP_STATE, cookies); |
378 |
|
|
379 |
|
|
380 |
|
|
381 |
|
|
382 |
0 |
int responseCode = httpMethod.getStatusCode(); |
383 |
0 |
if (responseCode >= 300 && responseCode <= 399) |
384 |
|
{ |
385 |
|
|
386 |
0 |
Header locationHeader = httpMethod.getResponseHeader("location"); |
387 |
0 |
String redirectLocation = locationHeader != null ? locationHeader.getValue() : class="keyword">null ; |
388 |
0 |
if (redirectLocation != null) |
389 |
|
{ |
390 |
|
|
391 |
|
|
392 |
|
|
393 |
0 |
return doWebContent( redirectLocation, new HashMap(), false, request, response ) ; |
394 |
|
} |
395 |
|
else |
396 |
|
{ |
397 |
|
|
398 |
0 |
throw new PortletException("Redirection code: "+responseCode+", but with no redirectionLocation set."); |
399 |
|
} |
400 |
|
} |
401 |
0 |
else if ( responseCode >= 400 ) |
402 |
|
{ |
403 |
0 |
if ( responseCode == 401 ) |
404 |
|
{ |
405 |
0 |
if (httpMethod.getHostAuthState().isAuthRequested() && retryCount++ < 1 && doRequestedAuthentication( httpClient, httpMethod, request, response)) |
406 |
|
{ |
407 |
|
|
408 |
0 |
return doHttpWebContent(httpClient, httpMethod, retryCount, request, response); |
409 |
|
} |
410 |
|
else |
411 |
|
{ |
412 |
|
|
413 |
0 |
throw new PortletException("Site requested authorization, but we are unable to provide credentials"); |
414 |
|
} |
415 |
|
} |
416 |
0 |
else if (retryCount++ < 3) |
417 |
|
{ |
418 |
0 |
log.info("WebContentPortlet.doHttpWebContent() - retrying: "+httpMethod.getPath()+", response code: "+responseCode); |
419 |
|
|
420 |
|
|
421 |
0 |
return doHttpWebContent(httpClient, httpMethod, retryCount, request, response); |
422 |
|
} |
423 |
|
else |
424 |
|
{ |
425 |
|
|
426 |
0 |
throw new PortletException("Failure reading: "+httpMethod.getPath()+", response code: "+responseCode); |
427 |
|
} |
428 |
|
} |
429 |
|
|
430 |
|
|
431 |
|
|
432 |
|
|
433 |
0 |
BufferedInputStream bis = new BufferedInputStream(httpMethod.getResponseBodyAsStream()); |
434 |
0 |
String encoding = ((HttpMethodBase)httpMethod).getResponseCharSet(); |
435 |
0 |
if (encoding == null) |
436 |
0 |
encoding = getContentCharSet(bis); |
437 |
0 |
Reader htmlReader = new InputStreamReader(bis, encoding); |
438 |
|
|
439 |
|
|
440 |
0 |
if (encoding == null) |
441 |
0 |
encoding = WebContentPortlet.defaultEncoding ; |
442 |
0 |
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); |
443 |
0 |
Writer htmlWriter = new OutputStreamWriter(byteOutputStream, encoding); |
444 |
|
|
445 |
|
|
446 |
0 |
rewriter.rewrite(rewriteController.createParserAdaptor("text/html"), htmlReader, htmlWriter); |
447 |
0 |
htmlWriter.flush(); |
448 |
|
|
449 |
|
|
450 |
|
|
451 |
|
|
452 |
0 |
return byteOutputStream.toByteArray(); |
453 |
|
} |
454 |
0 |
catch (UnsupportedEncodingException ueex) |
455 |
|
{ |
456 |
0 |
throw new PortletException("Encoding " + defaultEncoding + " not supported. Error: " + ueex.getMessage()); |
457 |
|
} |
458 |
0 |
catch (RewriterException rwe) |
459 |
|
{ |
460 |
0 |
throw new PortletException("Failed to rewrite HTML page. Error: " + rwe.getMessage()); |
461 |
|
} |
462 |
0 |
catch (Exception e) |
463 |
|
{ |
464 |
0 |
throw new PortletException("Exception while rewritting HTML page. Error: " + e.getMessage()); |
465 |
|
} |
466 |
|
} |
467 |
|
|
468 |
|
protected String getURLSource(String source, Map params, RenderRequest request, RenderResponse response) |
469 |
|
{ |
470 |
0 |
return source; |
471 |
|
} |
472 |
|
|
473 |
|
protected byte[] doPreemptiveAuthentication(HttpClient clent,HttpMethod method, RenderRequest request, RenderResponse response) |
474 |
|
{ |
475 |
|
|
476 |
0 |
return null ; |
477 |
|
} |
478 |
|
|
479 |
|
protected boolean doRequestedAuthentication(HttpClient clent,HttpMethod method, RenderRequest request, RenderResponse response) |
480 |
|
{ |
481 |
|
|
482 |
0 |
return false ; |
483 |
|
} |
484 |
|
|
485 |
|
|
486 |
|
|
487 |
|
|
488 |
|
private RewriterController getController(String contextPath) throws Exception |
489 |
|
{ |
490 |
0 |
Class[] rewriterClasses = new Class[] |
491 |
|
{ WebContentRewriter.class, WebContentRewriter.class}; |
492 |
|
|
493 |
0 |
Class[] adaptorClasses = new Class[] |
494 |
|
{ NekoParserAdaptor.class, SaxParserAdaptor.class}; |
495 |
0 |
RewriterController rwc = new JetspeedRewriterController(contextPath + "conf/rewriter-rules-mapping.xml", Arrays |
496 |
|
.asList(rewriterClasses), Arrays.asList(adaptorClasses)); |
497 |
|
|
498 |
0 |
FileReader reader = new FileReader(contextPath + "conf/default-rewriter-rules.xml"); |
499 |
|
|
500 |
0 |
Ruleset ruleset = rwc.loadRuleset(reader); |
501 |
0 |
reader.close(); |
502 |
0 |
rewriter = rwc.createRewriter(ruleset); |
503 |
0 |
return rwc; |
504 |
|
} |
505 |
|
|
506 |
|
protected HttpClient getHttpClient(RenderRequest request) throws IOException |
507 |
|
{ |
508 |
|
|
509 |
0 |
HttpClient client = new HttpClient(); |
510 |
|
|
511 |
|
|
512 |
0 |
Cookie[] cookies = (Cookie[])PortletMessaging.receive(request, HTTP_STATE); |
513 |
0 |
if (cookies != null) |
514 |
|
{ |
515 |
|
|
516 |
0 |
client.getState().addCookies(cookies); |
517 |
|
|
518 |
|
|
519 |
|
|
520 |
|
} |
521 |
|
|
522 |
0 |
return client ; |
523 |
|
} |
524 |
|
|
525 |
|
protected HttpMethodBase getHttpMethod(HttpClient client, String uri, Map params, String formMethod, RenderRequest request) throws IOException |
526 |
|
{ |
527 |
0 |
formMethod = FORM_MULTIPART_METHOD; |
528 |
0 |
HttpMethodBase httpMethod = null; |
529 |
0 |
String useragentProperty = request.getProperty("User-Agent"); |
530 |
0 |
if(formMethod.equalsIgnoreCase(FORM_MULTIPART_METHOD)){ |
531 |
|
|
532 |
0 |
MultipartPostMethod mutlitPart = (MultipartPostMethod)( httpMethod = new MultipartPostMethod(uri)) ; |
533 |
0 |
if (params != null && !params.isEmpty()) |
534 |
|
{ |
535 |
0 |
Iterator iter = params.entrySet().iterator(); |
536 |
0 |
while (iter.hasNext()) |
537 |
|
{ |
538 |
0 |
Map.Entry entry = (Map.Entry)iter.next(); |
539 |
0 |
String name = (String)entry.getKey(); |
540 |
0 |
String[] values = (String[])entry.getValue(); |
541 |
0 |
if (values != null) |
542 |
0 |
for (int i=0,limit=values.length; i<limit; i++) |
543 |
|
{ |
544 |
|
|
545 |
|
|
546 |
0 |
mutlitPart.addParameter(name, values[i]); |
547 |
|
} |
548 |
0 |
} |
549 |
|
} |
550 |
|
|
551 |
0 |
}else if (formMethod.equalsIgnoreCase(FORM_GET_METHOD)){ |
552 |
|
|
553 |
|
|
554 |
|
|
555 |
0 |
httpMethod = new GetMethod(uri); |
556 |
0 |
if (params != null && !params.isEmpty()) |
557 |
|
{ |
558 |
0 |
ArrayList pairs = new ArrayList(); |
559 |
0 |
Iterator iter = params.entrySet().iterator(); |
560 |
0 |
while (iter.hasNext()) |
561 |
|
{ |
562 |
0 |
Map.Entry entry = (Map.Entry)iter.next() ; |
563 |
0 |
String name = (String)entry.getKey() ; |
564 |
0 |
String[] values = (String [])entry.getValue() ; |
565 |
0 |
if (values != null) |
566 |
0 |
for (int i = 0,limit = values.length; i < limit; i++) |
567 |
|
{ |
568 |
|
|
569 |
0 |
pairs.add(new NameValuePair(name, values[i])); |
570 |
|
} |
571 |
0 |
} |
572 |
0 |
httpMethod.setQueryString((NameValuePair[])pairs.toArray(new NameValuePair[pairs.size()])); |
573 |
|
} |
574 |
|
|
575 |
|
|
576 |
0 |
httpMethod.setFollowRedirects(true); |
577 |
0 |
}else if (formMethod.equalsIgnoreCase(FORM_POST_METHOD)) { |
578 |
|
|
579 |
|
|
580 |
|
|
581 |
0 |
PostMethod postMethod = (PostMethod)( httpMethod = new PostMethod(uri)) ; |
582 |
0 |
if (params != null && !params.isEmpty()) |
583 |
|
{ |
584 |
0 |
Iterator iter = params.entrySet().iterator(); |
585 |
0 |
while (iter.hasNext()) |
586 |
|
{ |
587 |
0 |
Map.Entry entry = (Map.Entry)iter.next(); |
588 |
0 |
String name = (String)entry.getKey(); |
589 |
0 |
String[] values = (String[])entry.getValue(); |
590 |
0 |
if (values != null) |
591 |
0 |
for (int i=0,limit=values.length; i<limit; i++) |
592 |
|
{ |
593 |
|
|
594 |
|
|
595 |
0 |
postMethod.addParameter(name, values[i]); |
596 |
|
} |
597 |
0 |
} |
598 |
|
} |
599 |
|
} |
600 |
|
|
601 |
|
|
602 |
0 |
httpMethod.addRequestHeader( "User-Agent", useragentProperty ); |
603 |
|
|
604 |
|
|
605 |
|
|
606 |
|
|
607 |
|
|
608 |
0 |
return httpMethod ; |
609 |
|
} |
610 |
|
|
611 |
|
|
612 |
|
|
613 |
|
|
614 |
|
static final int BLOCK_SIZE = 4096; |
615 |
|
|
616 |
|
|
617 |
|
|
618 |
|
|
619 |
|
|
620 |
|
|
621 |
|
|
622 |
|
|
623 |
|
|
624 |
|
|
625 |
|
|
626 |
|
|
627 |
|
|
628 |
|
|
629 |
|
|
630 |
|
|
631 |
|
|
632 |
|
|
633 |
|
|
634 |
|
|
635 |
|
|
636 |
|
|
637 |
|
|
638 |
|
|
639 |
|
private void drain(Reader r, Writer w) throws IOException |
640 |
|
{ |
641 |
0 |
char[] bytes = new class="keyword">char[BLOCK_SIZE]; |
642 |
|
try |
643 |
|
{ |
644 |
0 |
int length = r.read(bytes); |
645 |
0 |
while (length != -1) |
646 |
|
{ |
647 |
0 |
if (length != 0) |
648 |
|
{ |
649 |
0 |
w.write(bytes, 0, length); |
650 |
|
} |
651 |
0 |
length = r.read(bytes); |
652 |
|
} |
653 |
|
} |
654 |
|
finally |
655 |
|
{ |
656 |
0 |
bytes = null; |
657 |
0 |
} |
658 |
|
|
659 |
0 |
} |
660 |
|
|
661 |
|
|
662 |
|
|
663 |
|
|
664 |
|
|
665 |
|
|
666 |
|
|
667 |
|
|
668 |
|
|
669 |
|
|
670 |
|
private String getContentCharSet(InputStream is) throws IOException |
671 |
|
{ |
672 |
0 |
if (!is.markSupported()) |
673 |
|
{ |
674 |
0 |
return null; |
675 |
|
} |
676 |
|
|
677 |
0 |
byte[] buf = new byte[BLOCK_SIZE]; |
678 |
|
try |
679 |
|
{ |
680 |
0 |
is.mark(BLOCK_SIZE); |
681 |
0 |
is.read(buf, 0, BLOCK_SIZE); |
682 |
0 |
String content = new String(buf, "ISO-8859-1"); |
683 |
0 |
String lowerCaseContent = content.toLowerCase(); |
684 |
0 |
int startIndex = lowerCaseContent.indexOf("<head"); |
685 |
0 |
if (startIndex == -1) |
686 |
|
{ |
687 |
0 |
startIndex = 0; |
688 |
|
} |
689 |
0 |
int endIndex = lowerCaseContent.indexOf("</head"); |
690 |
0 |
if (endIndex == -1) |
691 |
|
{ |
692 |
0 |
endIndex = content.length(); |
693 |
|
} |
694 |
0 |
content = content.substring(startIndex, endIndex); |
695 |
|
|
696 |
0 |
StringTokenizer st = new StringTokenizer(content, "<>"); |
697 |
0 |
while (st.hasMoreTokens()) |
698 |
|
{ |
699 |
0 |
String element = st.nextToken(); |
700 |
0 |
String lowerCaseElement = element.toLowerCase(); |
701 |
0 |
if (lowerCaseElement.startsWith("meta") && lowerCaseElement.indexOf("content-type") > 0) |
702 |
|
{ |
703 |
0 |
StringTokenizer est = new StringTokenizer(element, " =\"\';"); |
704 |
0 |
while (est.hasMoreTokens()) |
705 |
|
{ |
706 |
0 |
if (est.nextToken().equalsIgnoreCase("charset")) |
707 |
|
{ |
708 |
0 |
if (est.hasMoreTokens()) |
709 |
|
{ |
710 |
0 |
return est.nextToken(); |
711 |
|
} |
712 |
|
} |
713 |
|
} |
714 |
|
} |
715 |
0 |
} |
716 |
|
} |
717 |
0 |
catch (IOException e) |
718 |
|
{ |
719 |
|
} |
720 |
|
finally |
721 |
|
{ |
722 |
0 |
is.reset(); |
723 |
0 |
} |
724 |
|
|
725 |
0 |
return null; |
726 |
|
} |
727 |
|
} |