KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > web > http > impl > HttpProcessor


1 package org.jahia.clipbuilder.html.web.http.impl;
2
3 import java.io.*;
4 import java.net.*;
5 import java.util.*;
6
7 import org.jahia.clipbuilder.html.bean.*;
8 import org.jahia.clipbuilder.html.util.*;
9 import org.jahia.clipbuilder.html.web.http.*;
10 import org.org.apache.commons.httpclient.*;
11 import org.org.apache.commons.httpclient.methods.*;
12 import org.org.apache.commons.httpclient.params.*;
13
14 /**
15  * Give mathod to get the requested document
16  *
17  *@author Tlili Khaled
18  */

19 public class HttpProcessor implements HTMLClient {
20     // URL
21
private UrlBean urlBean;
22
23     private boolean javascriptEnable;
24
25     //Proxy
26
private String JavaDoc proxyHost;
27     private int proxyPort = 0;
28
29     // Client to use
30
private HttpClient defaultClient;
31
32     //Method to call: Get, post,...
33
private HttpMethod method = null;
34     private NameValuePair[] paramAndValue;
35
36     // redirection
37
private GetMethod redirect = null;
38
39     //state
40
private HttpState stateBeforeExecution;
41     private HttpState stateAfterExecution;
42     private String JavaDoc htmlAsString;
43     private String JavaDoc charEncoding;
44
45     private boolean errorHasOccured = false;
46     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(HttpProcessor.class);
47
48
49     /**
50      * Constructor for the HttpProcessor object
51      *
52      *@param httpMethod Description of Parameter
53      *@param uBean Description of Parameter
54      *@exception WebClippingException Description of Exception
55      */

56
57     public HttpProcessor(String JavaDoc httpMethod, UrlBean uBean) throws WebClippingException {
58         this(httpMethod, uBean, false);
59     }
60
61
62
63     /**
64      * Constructor for the HttpProcessor object
65      *
66      *@param httpMethod Description of Parameter
67      *@param uBean Description of Parameter
68      *@param process Description of Parameter
69      *@exception WebClippingException Description of Exception
70      */

71     public HttpProcessor(String JavaDoc httpMethod, UrlBean uBean, boolean process) throws WebClippingException {
72 // DefaultHttpParams.getDefaultParams().setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);
73
setMethod(httpMethod);
74         setUrlBean(uBean);
75         initMethod();
76         if (process) {
77             execute();
78         }
79
80     }
81
82
83
84     /**
85      * Sets the ParamAndValue attribute of the HttpProcessor object
86      *
87      *@param paramAndValue The new ParamAndValue value
88      */

89     public void setParamAndValue(NameValuePair[] paramAndValue) {
90         this.paramAndValue = paramAndValue;
91     }
92
93
94     /**
95      * Sets the HttpMethod attribute of the HttpProcessor object
96      *
97      *@param httpMethod The new HttpMethod value
98      */

99     public void setMethod(String JavaDoc httpMethod) {
100         // Create a method instance.
101
logger.debug("[ Method is " + httpMethod + "]");
102         if (httpMethod.equalsIgnoreCase("GET")) {
103             GetMethod method = new GetMethod();
104             method.setFollowRedirects(false);
105             method.addRequestHeader("User-Agent", "Mozilla/5.0 (Macintosh; U; PPC MacOS X; en)");
106             setObjectMethod(method);
107
108         }
109         else if (httpMethod.equalsIgnoreCase("POST")) {
110             PostMethod method = new PostMethod();
111             method.setFollowRedirects(false);
112             method.addRequestHeader("User-Agent", "Mozilla/5.0 (Macintosh; U; PPC MacOS X; en)");
113             setObjectMethod(method);
114             ((PostMethod) getObjectMethod()).addParameters(getParamAndValue());
115         }
116
117         // Provide custom retry handler is necessary
118
getObjectMethod().getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
119
120         // set content-type header
121
getObjectMethod().addRequestHeader(" Content-Type", " text/html; charset=UTF-8");
122
123     }
124
125
126     /**
127      * Sets the MethodParameters attribute of the HttpProcessor object
128      *
129      *@param httpMethod The new MethodParameters value
130      */

131     public void setMethodParameters(String JavaDoc httpMethod) {
132         // Create a method instance.
133
logger.debug("[ Method is " + httpMethod + "]");
134         if (httpMethod == null) {
135             logger.error("Http Method is null !!");
136             logger.debug("Http Method is null !!");
137         }
138         if (httpMethod.equalsIgnoreCase("GET")) {
139             method = new GetMethod();
140
141         }
142         else if (httpMethod.equalsIgnoreCase("POST")) {
143             ((PostMethod) method).addParameters(getParamAndValue());
144         }
145
146     }
147
148
149
150     /**
151      * Sets the UrlBean attribute of the HttpProcessor object
152      *
153      *@param urlBean The new UrlBean value
154      */

155     public void setUrlBean(UrlBean urlBean) {
156         this.urlBean = urlBean;
157     }
158
159
160     /**
161      * Sets the Client attribute of the HttpProcessor object
162      *
163      *@param client The new Client value
164      */

165     public void setDefaultClient(HttpClient client) {
166         this.defaultClient = client;
167     }
168
169
170
171     /**
172      * Sets the StateBeforeExecution attribute of the HttpProcessor object
173      *
174      *@param state The new StateBeforeExecution value
175      */

176     public void setStateBeforeExecution(Object JavaDoc state) {
177         if (state != null) {
178             logger.debug("[ State updated]");
179             this.stateBeforeExecution = (HttpState) state;
180         }
181         else {
182             logger.debug("[ State is null ]");
183         }
184     }
185
186
187     /**
188      * Sets the StateAfterBeforeExecution attribute of the HttpProcessor object
189      *
190      *@param state The new StateAfterExecution value
191      */

192     public void setStateAfterExecution(HttpState state) {
193         this.stateAfterExecution = state;
194     }
195
196
197     /**
198      * Sets the HtmlAsString attribute of the HttpProcessor object
199      *
200      *@param htmlAsString The new HtmlAsString value
201      */

202     public void setHtmlAsString(String JavaDoc htmlAsString) {
203         this.htmlAsString = htmlAsString;
204     }
205
206
207     /**
208      * Sets the ProxyPort attribute of the HttpProcessor object
209      *
210      *@param proxyPort The new ProxyPort value
211      */

212     public void setProxyPort(int proxyPort) {
213         this.proxyPort = proxyPort;
214     }
215
216
217     /**
218      * Sets the ProxyHost attribute of the HttpProcessor object
219      *
220      *@param proxyHost The new ProxyHost value
221      */

222     public void setProxyHost(String JavaDoc proxyHost) {
223         this.proxyHost = proxyHost;
224     }
225
226
227     /**
228      * Sets the CharEncoding attribute of the HttpProcessor object
229      *
230      *@param charEncoding The new CharEncoding value
231      */

232     public void setCharEncoding(String JavaDoc charEncoding) {
233         this.charEncoding = charEncoding;
234     }
235
236
237
238     /**
239      * Gets the JavascriptEnable attribute of the HTMLClient object
240      *
241      *@return The JavascriptEnable value
242      */

243     public boolean isJavascriptEnable() {
244         return this.javascriptEnable;
245     }
246
247
248     /**
249      * Gets the ParamAndValue attribute of the HttpProcessor object
250      *
251      *@return The ParamAndValue value
252      */

253     public NameValuePair[] getParamAndValue() {
254         return paramAndValue;
255     }
256
257
258
259     /**
260      * Gets the UrlBean attribute of the HttpProcessor object
261      *
262      *@return The UrlBean value
263      */

264     public UrlBean getUrlBean() {
265         return urlBean;
266     }
267
268
269     /**
270      * Gets the Client attribute of the HttpProcessor object
271      *
272      *@return The Client value
273      */

274     public HttpClient getHttpClient() {
275         if (defaultClient == null) {
276             defaultClient = getNewHttpClient();
277         }
278         return defaultClient;
279     }
280
281
282     /**
283      * Gets the Method attribute of the HttpProcessor object
284      *
285      *@return The Method value
286      */

287     public String JavaDoc getMethod() {
288         return getObjectMethod().getName();
289     }
290
291
292
293     /**
294      * Gets the StateBeforeExecution attribute of the HttpProcessor object
295      *
296      *@return The StateBeforeExecution value
297      */

298     public HttpState getStateBeforeExecution() {
299         return stateBeforeExecution;
300     }
301
302
303     /**
304      * Gets the StateAfterBeforeExecution attribute of the HttpProcessor object
305      *
306      *@return The StateAfterBeforeExecution value
307      */

308     public HttpState getStateAfterExecution() {
309         return stateAfterExecution;
310     }
311
312
313     /**
314      * Gets the HtmlAsString attribute of the HttpProcessor object
315      *
316      *@return The HtmlAsString value
317      */

318     public String JavaDoc getHtmlAsString() {
319         return htmlAsString;
320     }
321
322
323     /**
324      * Gets the ProxyPort attribute of the HttpProcessor object
325      *
326      *@return The ProxyPort value
327      */

328     public int getProxyPort() {
329         return proxyPort;
330     }
331
332
333     /**
334      * Gets the ProxyHost attribute of the HttpProcessor object
335      *
336      *@return The ProxyHost value
337      */

338     public String JavaDoc getProxyHost() {
339         return proxyHost;
340     }
341
342
343     /**
344      * Gets the CharEncoding attribute of the HttpProcessor object
345      *
346      *@return The CharEncoding value
347      */

348     public String JavaDoc getCharEncoding() {
349         return charEncoding;
350     }
351
352
353
354     /**
355      * Gets the Headers attribute of the HttpProcessor object
356      *
357      *@return The Headers value
358      */

359     public Header[] getRequestHeaders() {
360         return method.getRequestHeaders();
361     }
362
363
364     /**
365      * Description of the Method
366      */

367     public void removeLastUrl() {
368         // do nothing.
369
}
370
371
372     /**
373      * Enable/disable javascript processing
374      *
375      *@param enable Description of Parameter
376      */

377     public void enabledJavascript(boolean enable) {
378         this.javascriptEnable = enable;
379     }
380
381
382     /**
383      * Gets the ErrorHasOccured attribute of the HttpProcessor object
384      *
385      *@return The ErrorHasOccured value
386      */

387     public boolean errorHasOccured() {
388         return errorHasOccured;
389     }
390
391
392     /**
393      * Sets the UserAgent attribute of the HttpProcessor object
394      *
395      *@param params The new HeaderParams value
396      */

397     public void addHeader(Hashtable params) {
398         Iterator it = params.entrySet().iterator();
399         while (it.hasNext()) {
400             // get name and value
401
String JavaDoc name = (String JavaDoc) it.next();
402             String JavaDoc value = (String JavaDoc) params.get(name);
403             // create haeder object
404
method.addRequestHeader(name, value);
405         }
406
407     }
408
409
410     /**
411      * Process whith the default client
412      *
413      *@exception WebClippingException Description of Exception
414      */

415     public void execute() throws WebClippingException {
416         execute(getHttpClient());
417     }
418
419
420     /**
421      * Sets the Method attribute of the HttpProcessor object
422      *
423      *@param method The new Method value
424      */

425     private void setObjectMethod(HttpMethod method) {
426         this.method = method;
427     }
428
429
430     /**
431      * Sets the ParamAndValueFromUrlBean attribute of the HttpProcessor object
432      */

433     private void setParamAndValueFromUrlBean() {
434         setParamAndValue(getNameValuePairFromURLBean());
435         HttpMethod method = getObjectMethod();
436         if (method instanceof GetMethod) {
437             // set the query string
438
NameValuePair[] nvp = getParamAndValue();
439             String JavaDoc query = method.getQueryString();
440             if (query == null || query.equalsIgnoreCase("")) {
441                 method.setQueryString(nvp);
442                 return;
443             }
444             method.setQueryString(nvp);
445             String JavaDoc queryParam = method.getQueryString();
446             if (queryParam == null || queryParam.equalsIgnoreCase("")) {
447                 method.setQueryString(query);
448             }
449             else {
450                 method.setQueryString(query + "&" + queryParam);
451             }
452         }
453         else {
454             if (method instanceof PostMethod) {
455                 ((PostMethod) method).addParameters(getParamAndValue());
456                 String JavaDoc qqq = method.getQueryString();
457                 logger.debug("query is: " + qqq);
458             }
459         }
460
461     }
462
463
464     /**
465      * Sets the PathFromUrlBean attribute of the HttpProcessor object
466      */

467     private void setRequestedFileFromUrlBean() {
468         HttpMethod method = getObjectMethod();
469         URL processedUrl = getUrlBean().getAbsoluteURL();
470         String JavaDoc path = processedUrl.getPath();
471         logger.debug("query string from redirectUrl: " + processedUrl.getQuery());
472         method.setQueryString(processedUrl.getQuery());
473         method.setPath(path);
474     }
475
476
477     /**
478      * Sets the HostConfigurationFromUrlBean attribute of the HttpProcessor
479      * object
480      */

481     private void setHostConfigurationFromUrlBean() {
482         HostConfiguration hostConfig = getHttpClient().getHostConfiguration();
483         String JavaDoc host = getUrlBean().getBaseURL().getHost();
484         hostConfig.setHost(host);
485     }
486
487
488     /**
489      * Sets the ClientSate attribute of the HttpProcessor object
490      */

491     private void setClientSate() {
492         if (getStateBeforeExecution() != null) {
493             getHttpClient().setState(getStateBeforeExecution());
494         }
495     }
496
497
498     /**
499      * Gets the Method attribute of the HttpProcessor object
500      *
501      *@return The Method value
502      */

503     private HttpMethod getObjectMethod() {
504         return method;
505     }
506
507
508     /**
509      * Gets the UniqueClient attribute of the HttpProcessor class
510      *
511      *@return The UniqueClient value
512      */

513     private HttpClient getNewHttpClient() {
514         HttpClient client = new HttpClient();
515         // set host
516
client.getHostConfiguration().setHost(getUrlBean().getBaseURL().toExternalForm());
517         return client;
518     }
519
520
521     /**
522      * Description of the Method
523      *
524      *@return The NameValuePairFromURLBean value
525      */

526     private NameValuePair[] getNameValuePairFromURLBean() {
527         logger.debug("[ Convert Map To NameValuePair[] ]");
528         // retrieve the query parameters
529
if (this.getUrlBean() == null) {
530             logger.error("[ URL is not set]");
531             return null;
532         }
533         List queryParamBeanList = getUrlBean().getQueryParamBeanList();
534         logger.debug("[ Url: " + getUrlBean().getAbsoluteUrlValue() + " ]");
535         logger.debug("[ queryParamBeanList.size(): " + queryParamBeanList.size() + " ]");
536         //build array
537
NameValuePair[] paramAndValue = new NameValuePair[queryParamBeanList.size()];
538         for (int i = 0; i < queryParamBeanList.size(); i++) {
539             QueryParamBean qBean = (QueryParamBean) queryParamBeanList.get(i);
540             String JavaDoc name = qBean.getName();
541             String JavaDoc value = qBean.getDefaultValue();
542             logger.debug("[ Found Param: " + name + " with value " + value + " ]");
543             paramAndValue[i] = new NameValuePair(name, value);
544         }
545
546         return (paramAndValue);
547     }
548
549
550     /**
551      * build the target document as a HTMLDocuemennt
552      *
553      *@param client Description of Parameter
554      *@exception WebClippingException Description of Exception
555      */

556     private void execute(HttpClient client) throws WebClippingException {
557         //init client and method
558
initClient();
559
560         //HttpState
561
HttpState s = getHttpClient().getState();
562
563         Cookie[] cookiess = s.getCookies();
564
565         logger.debug("[ HttpClient: Begin Enumerate cookies before execution ]");
566         for (int i = 0; i < cookiess.length; i++) {
567             Cookie c = cookiess[i];
568             logger.debug("[ Found cookie before execution: " + c.getName() + " ]");
569         }
570         logger.debug("[ HttpClient: Finish Enumerate cookies before execution ]");
571
572         // Init
573
String JavaDoc html = null;
574         try {
575
576             // Execute the method.
577
logger.debug("[ Url bean att: " + this.getUrlBean().getAbsoluteUrlValue() + " ]");
578             logger.debug("[ Url bean att: " + this.getUrlBean().getRedirectUrl().toExternalForm() + " ]");
579             logger.debug("[ Url bean att: " + this.getUrlBean().getAbsoluteURL().getQuery() + " ]");
580             logger.debug("[ Url bean att: " + this.getUrlBean().getRedirectUrl().getQuery() + " ]");
581
582             logger.debug("[ Url bean att: " + this.getUrlBean().getAbsoluteUrlValue() + " ]");
583             logger.debug("[ Client host: " + client.getHostConfiguration().getHost() + " ]");
584             logger.debug("[ Requested file path: " + method.getPath() + " ]");
585             logger.debug("[ Query is: " + method.getQueryString() + " ]");
586             int statusCode = -1;
587
588             logger.debug("[ Execute with simple client]");
589             statusCode = client.executeMethod(method);
590             logger.debug("[ Success whith simple client ]");
591
592             // Use caution: ensure correct character encoding and is not binary data
593
logger.debug("[status code is " + statusCode + " ]");
594
595             // Handle redirection by tsting the status code
596
if ((statusCode == HttpStatus.SC_MOVED_TEMPORARILY) ||
597                     (statusCode == HttpStatus.SC_MOVED_PERMANENTLY) ||
598                     (statusCode == HttpStatus.SC_SEE_OTHER) ||
599                     (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
600
601                 //Get the redirection
602
Header locationHeader = method.getResponseHeader("location");
603                 logger.debug("[ Redirection: " + locationHeader + " ]");
604                 if (locationHeader != null) {
605                     String JavaDoc newuri = locationHeader.getValue();
606                     if ((newuri == null) || (newuri.equals(""))) {
607                         newuri = "/";
608                     }
609                     logger.debug("[ Redirect target: " + newuri + "");
610                     String JavaDoc absoluteRedirect = URLUtilities.getUrlAbsoluteValue(getUrlBean().getAbsoluteUrlValue(), newuri);
611
612                     redirect = new GetMethod(absoluteRedirect);
613                     client.executeMethod(redirect);
614
615                     // Get the reponse
616
html = redirect.getResponseBodyAsString();
617                     logger.debug("[ Redirect: " + redirect.getStatusLine().toString() + " ]");
618
619                     // set redirectedUrl
620
getUrlBean().setRedirectUrl(URLUtilities.getURL(absoluteRedirect));
621                     logger.debug("Redirect url is: " + URLUtilities.getURL(newuri));
622
623                 }
624                 else {
625                     // set redirectedUrl
626
getUrlBean().setRedirectUrl(URLUtilities.getURL(getUrlBean().getAbsoluteURL().getPath()));
627                     logger.error("[ Invalid redirect ]");
628                 }
629
630             }
631             else {
632
633                 html = method.getResponseBodyAsString();
634                 String JavaDoc charset = HTMLUtilities.getEncoding(html, "iso-8859-1");
635                 Header[] tab = method.getResponseHeaders();
636                 for (int i = 0; i < tab.length; i++) {
637                     Header h = tab[i];
638                     logger.debug("[header found: " + h.getName() + "," + h.getValue() + "]");
639                 }
640                 html = new String JavaDoc(method.getResponseBody(), charset);
641
642             }
643
644             // set html
645
setHtmlAsString(html);
646
647         }
648         catch (HttpException e) {
649             logger.error("Fatal protocol violation: " + e.getMessage());
650             errorHasOccured = true;
651             throw new WebClippingException("Fatal protocol violation", e);
652         }
653         catch (IOException e) {
654             logger.error("Fatal transport error: " + e.getMessage());
655             errorHasOccured = true;
656             throw new WebClippingException("Fatal transport error", e);
657         }
658         catch (Exception JavaDoc e) {
659             logger.error("Fatal Exception error: " + e.getMessage());
660             errorHasOccured = true;
661             e.printStackTrace();
662             throw new WebClippingException("Fatal Exception error", e);
663         }
664
665         // Release the connections.
666
if (method != null) {
667             method.releaseConnection();
668         }
669         if (redirect != null) {
670             redirect.releaseConnection();
671         }
672
673         //HttpState
674
HttpState state = getHttpClient().getState();
675         Cookie[] cookies = state.getCookies();
676         logger.debug("[ Begin Enumerate cookies after execution ]");
677         for (int i = 0; i < cookies.length; i++) {
678             Cookie c = cookies[i];
679             logger.debug("[ Found cookie after execution: " + c.getName() + " ]");
680         }
681         //update state
682
setStateAfterExecution(state);
683
684     }
685
686
687     /**
688      * Description of the Method
689      */

690     private void initClient() {
691         // Host Configuration
692
setHostConfigurationFromUrlBean();
693
694         // set client state
695
setClientSate();
696
697     }
698
699
700     /**
701      * Description of the Method
702      */

703     private void initMethod() {
704         //set path
705
setRequestedFileFromUrlBean();
706
707         //set query
708
setParamAndValueFromUrlBean();
709     }
710
711 }
712
Popular Tags