KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > struts > webBrowser > WebBrowserAction


1 package org.jahia.clipbuilder.html.struts.webBrowser;
2
3 import java.util.*;
4
5 import javax.servlet.http.*;
6
7 import org.apache.struts.action.*;
8 import org.jahia.clipbuilder.html.*;
9 import org.jahia.clipbuilder.html.bean.*;
10 import org.jahia.clipbuilder.html.struts.Util.Constants;
11 import org.jahia.clipbuilder.html.util.*;
12 import org.jahia.clipbuilder.html.web.*;
13 import org.jahia.clipbuilder.html.web.Constant.*;
14 import org.jahia.clipbuilder.html.web.Url.*;
15 import org.jahia.clipbuilder.html.web.html.*;
16 import org.jahia.clipbuilder.html.web.html.Impl.ExtractorFilter.*;
17 import com.vipan.util.ExpiringCache;
18
19 /**
20  * Description of the Class
21  *
22  *@author Tlili Khaled
23  */

24 public class WebBrowserAction extends org.apache.struts.action.Action {
25     /**
26      * Description of the Field
27      */

28     private static HashMap cacheHacheMap = new HashMap();
29     /**
30      * Description of the Field
31      */

32     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(WebBrowserAction.class);
33
34
35     /**
36      * Gets the ClipperBean attribute of the ClipperAction object
37      *
38      *@param request Description of Parameter
39      *@return The ClipperBean value
40      */

41     public ClipperBean getClipperBean(HttpServletRequest request) {
42         ClipperBean bean = new ClipperBean();
43         bean = SessionManager.getClipperBean(request);
44         //Handing error
45
if (bean == null) {
46             logger.error("[ClipperBean error: Not found]");
47         }
48         return bean;
49     }
50
51
52
53     /**
54      * Description of the Method
55      *
56      *@param actionMapping Description of Parameter
57      *@param actionForm Description of Parameter
58      *@param request Description of Parameter
59      *@param response Description of Parameter
60      *@return Description of the Returned Value
61      *@exception Exception Description of Exception
62      */

63     public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception JavaDoc {
64         // this may occure when this servlet is in the portel
65
if (!SessionManager.sessionExist(request)) {
66             return actionMapping.findForward("showError");
67         }
68
69         // init
70
ActionForward forward = actionMapping.getInputForward();
71         WebBrowserForm form = (WebBrowserForm) actionForm;
72         ClipperBean cBean = SessionManager.getClipperBean(request);
73         if (cBean == null) {
74             logger.error("[WebBrowser - Execute - ClipperBean not found ]");
75             forward = actionMapping.findForward("description");
76         }
77
78         //execute
79
try {
80             logger.debug("[WebBrowser - Execute ]");
81
82             //init
83
if (SessionManager.getHTMLDocumentBuilder(request) == null) {
84                 logger.debug("[WebBrowser - Execute - Init link hash ]");
85                 form.setLinkHash(Constants.FIRST_URL_HASH);
86                 form.setFrom(Constants.FIRST_URL_FROM);
87                 logger.debug("add sourceUrl ");
88                 initHTMLDocumentBuilder(request, response);
89             }
90
91             // synchronize urlEncoder
92
synchronizeUrlEncoder(request, response);
93
94             //process only if it'st a new action (does'nt load from wizard)
95
logger.debug("[ WebBrowser ]");
96
97             //set the html content
98
//show request document
99
String JavaDoc showParam = form.getShow();
100             logger.debug("Show Param: " + showParam);
101             String JavaDoc html = "Error HTML not Set";
102             if (showParam != null) {
103                 if (showParam.equalsIgnoreCase(Constants.WEB_BROWSER_SHOW_BROWSE)) {
104                     logger.debug(" SHOW BROWSE DOCUMENT ");
105
106                     //add the url and get the transformed documeent
107
HTMLDocument doc = addUrlBeanAndGetHTMLDocument(request, response, form);
108                     html = doc.getTransformedDocumentAsString();
109
110                     //process pages whith frames
111
List framesList = doc.getFramesList();
112                     if (framesList != null && framesList.size() > 0) {
113                         form.setFrameUrlList(framesList);
114                         forward = actionMapping.findForward("showFrameSelection");
115                     }
116
117                 }
118
119                 else if (showParam.equalsIgnoreCase(Constants.WEB_BROWSER_SHOW_LAST_DOCUMENT)) {
120                     logger.debug(" SHOW LAST DOCUMENT ");
121
122                     //get last document
123
HTMLDocument doc = getLastRecordedHTMLDocument(request, response, form);
124
125                     // update htmlDocumentbuilder
126
doc.getTransformer().setBuilder(SessionManager.getHTMLDocumentBuilder(request));
127
128                     // get the transformedDocument
129
html = doc.getTransformedDocumentAsString();
130                 }
131                 else if (showParam.equalsIgnoreCase(Constants.WEB_BROWSER_SHOW_EDIPARAMS)) {
132                     logger.debug(" SHOW EDIT PARAMS DOCUMENT ");
133                     int positionUrl = form.getPositionUrl();
134
135                     // get the corresponding htmlDocument
136
HTMLDocument doc = getClipperBean(request).getUrlBean(positionUrl).getDocument();
137
138                     //update HtmlDocumentBuilder
139
doc.getTransformer().setBuilder(SessionManager.getHTMLDocumentBuilder(request));
140
141                     // get the document whith labels
142
html = doc.getDocumentWhithParamLabelAsString();
143                 }
144                 else if (showParam.equalsIgnoreCase(Constants.WEB_BROWSER_SHOW_CHEW)) {
145                     logger.debug(" SHOW CHEW DOCUMENT ");
146
147                     // get the last recorded document
148
HTMLDocument doc = getLastRecordedHTMLDocument(request, response, form);
149
150                     // update the HtmlDocumentBuilder
151
doc.getTransformer().setBuilder(SessionManager.getHTMLDocumentBuilder(request));
152
153                     // get 'chew-tag'
154
FilterBean filterBean = cBean.getFilterBean();
155                     String JavaDoc tagName = filterBean.getKeyPart(ChewExtractorFilter.CHEW_TAG_NAME);
156
157                     // get the chew focument
158
html = doc.getChewDocumentAsString(tagName);
159                 }
160                 else if (showParam.equalsIgnoreCase(Constants.WEB_BROWSER_SHOW_PREVIEW)) {
161                     logger.debug(" SHOW PREVIEW");
162                     html = buildSelectedPart(request);
163                 }
164                 else if (showParam.equalsIgnoreCase(Constants.WEB_BROWSER_SHOW_TEST)) {
165                     logger.debug(" TEST PREVIEW");
166                     html = retrieveHTMLPart(request, response, form);
167                 }
168
169                 else {
170                     logger.error("Show Param Value not expected: " + form);
171                 }
172             }
173             else {
174                 logger.error("Show Param not found");
175             }
176
177             //manage errors
178
//actionErrors = getErrors(httpServletRequest);
179
/*
180              * if (actionErrors != null && actionErrors.size() > 0) {
181              * forward = actionMapping.findForward("showError");
182              * }
183              */

184             logger.debug("End execution: write html");
185             logger.debug(html);
186             form.setContent(StringUtilities.repareHTML(html));
187
188         }
189         catch (Exception JavaDoc ex) {
190         // ex.printStackTrace();
191
ActionErrors actionErrors = new ActionErrors();
192             logger.error("Error has occured");
193             forward = actionMapping.findForward("showError");
194             ActionError e = new ActionError("errors.message", ex.getMessage());
195             actionErrors.add("errors.message", e);
196             saveErrors(request, actionErrors);
197
198         }
199         finally {
200             return forward;
201         }
202     }
203
204
205
206     /**
207      * Action when the browse is selected
208      */

209
210     /**
211      * Sets the UsedValueForPreviousUrl attribute of the BrowseAction object
212      *
213      *@param newUrlBean The new QueryParamBeanForFormParamBean value
214      *@param previousUrlBean The new QueryParamBeanForFormParamBean value
215      *@param bean The new UsedValueForPreviousUrl value
216      */

217     private void setQueryParamBeanForFormParamBean(UrlBean newUrlBean, UrlBean previousUrlBean, ClipperBean bean) {
218         //get its form param list
219
String JavaDoc formParentHash = newUrlBean.getHash();
220
221         // get the queryBeanList for the current url <-> formBeanList of the previous url
222
List queryParamList = newUrlBean.getQueryParamBeanList();
223         for (int i = 0; i < queryParamList.size(); i++) {
224
225             //current queryBean
226
QueryParamBean qBean = (QueryParamBean) queryParamList.get(i);
227             String JavaDoc qName = qBean.getName();
228             int qPosition = qBean.getPosition();
229
230             //get the form param bean whith name = qName AND position = qPosition
231
FormParamBean fBean = previousUrlBean.getFormParamByNameAndFormParentHash(qName, qPosition, formParentHash);
232             if (fBean != null) {
233                 logger.debug("[ Form param bean for queryBean: " + qName + " and Parent form: " + formParentHash + "found ]");
234                 fBean.setQueryParamBean(qBean);
235             }
236             else {
237                 logger.error("Previous Url: " + previousUrlBean.getAbsoluteUrlValue());
238                 logger.error("[ Form param bean for queryBean: " + qName + " and form: " + formParentHash + " not found ]");
239             }
240         }
241
242     }
243
244
245     /**
246      * Gets the LastRecordedHTMLDocument attribute of the WebBrowserAction
247      * object
248      *
249      *@param request Description of Parameter
250      *@param response Description of Parameter
251      *@param form Description of Parameter
252      *@return The LastRecordedHTMLDocument value
253      */

254     private HTMLDocument getLastRecordedHTMLDocument(HttpServletRequest request, HttpServletResponse response, WebBrowserForm form) {
255         //Get the url from the ClipperBean object
256
ClipperBean bean = getClipperBean(request);
257
258         //First in the clipper bean
259
UrlBean uBean = bean.getLastRecordedUrlBean();
260         if (uBean == null) {
261             logger.debug("[ UrlBean found in recorder bean ]");
262             uBean = SessionManager.getRecorderBean(request).getCurrentUrlBean();
263         }
264         else {
265             int position = bean.getPositionLastRecordeUrl();
266             logger.debug("[Url " + bean.getUrlBean(position).getAbsoluteUrlValue() + " is currently in process]");
267             logger.debug("[ UrlBean found in clipper bean ]");
268
269         }
270
271         //get the document
272
HTMLDocument htmlDocument = uBean.getDocument();
273         if (htmlDocument == null) {
274             //process the url
275
logger.error("[Error htmlDocument not found]");
276         }
277         else {
278             logger.debug("[find url in the manager]");
279         }
280
281         //update form properties
282
// form.setFrom(uBean.getFrom());
283
form.setLinkHash(uBean.getHash());
284         // form.setPositionUrl(uBean.getPosition());
285
// form.setSourceUrl(uBean.getUrlValue());
286

287         return htmlDocument;
288     }
289
290
291     /**
292      * Gets the NameSpace attribute of the WebBrowserAction object
293      *
294      *@param request Description of Parameter
295      *@return The NameSpace value
296      */

297     private String JavaDoc getNameSpace(HttpServletRequest request) {
298         String JavaDoc nameSpace = RequestParameterManager.getNameSpace(request);
299         return nameSpace;
300     }
301
302
303     /**
304      * Sysnchronize UrlEncoder of HTMLDocumentBuilder
305      *
306      *@param request Description of Parameter
307      *@param response Description of Parameter
308      */

309     private void synchronizeUrlEncoder(HttpServletRequest request, HttpServletResponse response) {
310         HTMLDocumentBuilder builder = SessionManager.getHTMLDocumentBuilder(request);
311         if (builder != null) {
312             builder.setUrlEncoder(UrlEncoderFactory.getServletUrlEncoder(request, response));
313             SessionManager.setHTMLDocumentBuilder(request, builder);
314         }
315         else {
316             logger.error(" HTMLDocument is not in session ---> HTMLDocument is null !!");
317         }
318     }
319
320
321     /**
322      * Description of the Method
323      *
324      *@param request Description of Parameter
325      *@param response Description of Parameter
326      *@param form Description of Parameter
327      *@return Description of the Returned Value
328      *@exception Exception Description of Exception
329      */

330     private String JavaDoc retrieveHTMLPart(HttpServletRequest request, HttpServletResponse response, WebBrowserForm form) throws Exception JavaDoc {
331
332         ClipperBean cBean = SessionManager.getClipperBean(request);
333         String JavaDoc html = "WebClipping error";
334         //ExpiringCache cache = SessionManager.getCacheHandler(request, form.getNameSpace());
335
String JavaDoc cacheExpiration = cBean.getConfigurationBean().getPortletCacheExpiration();
336         // cache is enabled
337
String JavaDoc nameSpace = getNameSpace(request);
338         if (cacheExpiration != null && !cacheExpiration.equalsIgnoreCase("")) {
339             ExpiringCache cache = (ExpiringCache) cacheHacheMap.get(nameSpace);
340             if (cache == null) {
341                 // timeToLive, accessTimeout, maximumCachedQuantity, timerInterval (millisecs)
342
long ttl = Long.parseLong(cacheExpiration);
343                 cache = new ExpiringCache(ttl, 30 * 60 * 1000, 1, 2 * 60 * 60 * 1000);
344                 cacheHacheMap.put(nameSpace, cache);
345             }
346
347             // build key
348
String JavaDoc key = buildCacheKey(nameSpace, request.getRemoteUser(), cBean.getConfigurationBean().getPortletCacheContext());
349             //System.err.println("cache mode is: " + cBean.getConfigurationBean().getPortletCacheContext());
350
//System.err.println("cache key is: " + key);
351
// retrieve cahed Onject
352
Object JavaDoc cachedObject = cache.recover(key);
353
354             //not in cache: compute
355
if (cachedObject == null) {
356                 logger.debug("html not in cache");
357                 logger.debug("Run web clipper");
358                 //System.err.println("html not in cache");
359
html = runWebSimulator(request, response, form, cBean);
360
361                 if (cache != null) {
362                     cache.admit(key, html);
363                 }
364                 else {
365                     logger.debug("html not cached");
366                 }
367
368             }
369             // in cache: cast to String
370
else {
371                 logger.debug("html found in cache");
372                 //html is in the cache
373
html = (String JavaDoc) cachedObject;
374             }
375
376             //update cache
377
//SessionManager.setCacheHandler(request, form.getNameSpace(), cache);
378

379         }
380         else {
381             //cache disabled
382
html = runWebSimulator(request, response, form, cBean);
383         }
384         return html;
385     }
386
387
388
389     /**
390      * Gets the RequestedAndTransformedDocument attribute of the BrowseAction
391      * object
392      *
393      *@param request Description of Parameter
394      *@param response Description of Parameter
395      *@param uBean Description of Parameter
396      *@param httpMethod Description of Parameter
397      *@return The RequestedAndTransformedDocument value
398      *@exception Exception Description of Exception
399      */

400     private HTMLDocument buildHTMLDocument(HttpServletRequest request, HttpServletResponse response, UrlBean uBean, String JavaDoc httpMethod) throws Exception JavaDoc {
401
402         // Get a HMTLDocument builder
403
HTMLDocumentBuilder builder = SessionManager.getHTMLDocumentBuilder(request);
404         builder.setHttpMethod(httpMethod);
405         builder.setUBean(uBean);
406         builder.setUrlEncoder(UrlEncoderFactory.getServletUrlEncoder(request, response));
407
408
409         // execute
410
HTMLDocument doc = builder.execute(request, response);
411
412         //set in the session
413
SessionManager.setHTMLDocumentBuilder(request, builder);
414         return doc;
415     }
416
417
418     /**
419      * Description of the Method
420      *
421      *@param request Description of Parameter
422      *@param httpServletResponse Description of Parameter
423      *@param form Description of Parameter
424      *@param bean Description of Parameter
425      *@return Description of the Returned Value
426      *@exception Exception Description of Exception
427      */

428     private String JavaDoc runWebSimulator(HttpServletRequest request, HttpServletResponse httpServletResponse, WebBrowserForm form, ClipperBean bean) throws Exception JavaDoc {
429         String JavaDoc html;
430         // start web browser simulator
431
WebBrowserSimulator wbs = new WebBrowserSimulator(request, httpServletResponse, getClipperBean(request));
432
433         wbs.execute(WebBrowserSimulator.MODE_LOOP);
434
435         // get the good part
436
if (wbs.isFinished()) {
437             form.setFrom(Constants.WEB_BROWSER_SHOW_BROWSE);
438
439             html = wbs.getHTMLPart();
440             // html = wbs.getFinalHtmlResponse();
441
RecordingBean rc = new RecordingBean(RecordingBean.STOP);
442             logger.debug("[Set Session Attributes]");
443             SessionManager.setRecorderBean(request, rc);
444             SessionManager.removeWebBrowserSimulator(request);
445
446         }
447         else {
448             html = wbs.getFinalHtmlResponse();
449         }
450
451         return html;
452     }
453
454
455     /**
456      * Sets the ContentType attribute of the BrowseAction object
457      *
458      *@param request The feature to be added to the
459      * UrlBeanAndGetHTMLDocument attribute
460      *@param response The feature to be added to the
461      * UrlBeanAndGetHTMLDocument attribute
462      *@param form The feature to be added to the
463      * UrlandUpdateHTMLContent attribute
464      *@return Description of the Returned Value
465      *@exception Exception Description of Exception
466      */

467     private HTMLDocument addUrlBeanAndGetHTMLDocument(HttpServletRequest request, HttpServletResponse response, WebBrowserForm form) throws Exception JavaDoc {
468         //Build UrlBean
469
ClipperBean bean = SessionManager.getClipperBean(request);
470         RecordingBean rBean = SessionManager.getRecorderBean(request);
471
472         //build the urlBean
473
UrlBean uBean = buildUrlBeanWhithDoument(request, response, form);
474
475         //Get the HTMDocument
476
HTMLDocument doc = uBean.getDocument();
477
478         //Get the statut of the recording
479
boolean recording = false;
480         if (rBean != null) {
481             recording = rBean.getStatut() == RecordingBean.START;
482         }
483
484         // wizard is in recording statut
485
if (recording) {
486             logger.debug("[ Statut = Recording : " + uBean.getAbsoluteUrlValue() + "]");
487             // add the url to the clipper bean
488
logger.debug("[add the url " + uBean.getAbsoluteUrlValue() + " to the clipper bean]");
489             //the source url has been set by an action on the previous page
490
bean.addUrlBean(uBean);
491
492         }
493         else {
494             // update the bean sourceUrl
495
logger.debug("[ Statut = Not Recording : " + uBean.getAbsoluteUrlValue() + "]");
496             // update the url of the recording bean
497
rBean.setCurrentUrlBean(uBean);
498         }
499
500         //update sesion object
501
//SessionManager.setLastBuildedUrlBean(request, uBean);
502

503         //Get and save all errors that's occured during tansformation
504
List errors = doc.getTransformationErrors();
505         ActionErrors actionErrors = new ActionErrors();
506         for (int i = 0; i < errors.size(); i++) {
507             String JavaDoc error = errors.get(i).toString();
508             ActionMessage e = new ActionMessage("errors.message", error);
509             actionErrors.add("errors.message", e);
510         }
511         saveMessages(request, actionErrors);
512
513         return doc;
514     }
515
516
517     /**
518      * Description of the Method
519      *
520      *@param request Description of Parameter
521      *@param response Description of Parameter
522      *@param form Description of Parameter
523      *@return Description of the Returned Value
524      *@exception Exception Description of Exception
525      */

526     private UrlBean buildUrlBeanWhithDoument(HttpServletRequest request, HttpServletResponse response, WebBrowserForm form) throws Exception JavaDoc {
527         // get the clipper bean
528
ClipperBean cBean = getClipperBean(request);
529         UrlBean previousUrlBean = cBean.getLastRecordedUrlBean();
530         if (previousUrlBean == null) {
531             previousUrlBean = SessionManager.getRecorderBean(request).getCurrentUrlBean();
532         }
533
534         // get the UrlWrapper that correpond to the request
535
HTMLDocumentBuilder builder = SessionManager.getHTMLDocumentBuilder(request);
536         if (builder == null) {
537             initHTMLDocumentBuilder(request, response);
538             builder = SessionManager.getHTMLDocumentBuilder(request);
539         }
540
541         //get urm wrapper
542
String JavaDoc hash = form.getLinkHash();
543         URLMap map = builder.getUrlMap();
544         URLWrapper uw = map.getSourceUrl(hash);
545         if (uw == null) {
546             logger.error("Error");
547             logger.error("Key = " + hash);
548         }
549         String JavaDoc relativeURL = uw.getSourceUrl();
550
551         // get the base of the url
552
String JavaDoc baseUrl = "";
553         if (!URLUtilities.isRelatif(relativeURL)) {
554             baseUrl = relativeURL;
555         }
556         else {
557             // base url = base url of the previous url
558
baseUrl = previousUrlBean.getAbsoluteUrlValue();
559         }
560
561         //synchronize browseForme
562
form.setFrom(uw.getFrom());
563         String JavaDoc from = form.getFrom();
564         logger.debug("[ URL is: " + relativeURL + " ]");
565         logger.debug("WebBrowser action: from: " + from);
566         logger.debug("WebBrowse action: hash: " + hash);
567
568         //build urlBean
569
UrlBean uBean = new UrlBean(cBean, baseUrl, relativeURL, getParam(request), from, hash);
570
571         //Set the used value for the previous url before building the document
572
setQueryParamBeanForFormParamBean(uBean, previousUrlBean, cBean);
573
574         //Get the HTMLDocument
575
HTMLDocument htmlDocument = buildHTMLDocument(request, response, uBean, uw.getMethod());
576
577         //Set the document of the uBean
578
uBean.setDocument(htmlDocument);
579         return uBean;
580     }
581
582
583     /**
584      * Description of the Method
585      *
586      *@param request Description of Parameter
587      *@param response Description of Parameter
588      */

589     private void initHTMLDocumentBuilder(HttpServletRequest request, HttpServletResponse response) {
590         ClipperBean cBean = SessionManager.getClipperBean(request);
591
592                 // build first URLWrapper
593
URLWrapper uw = new URLWrapper();
594         uw.setSourceUrl(cBean.getTargetUrl());
595         uw.setMethod("GET");
596         uw.setFrom(Constants.FIRST_URL_HASH);
597         URLMap map = new URLMap();
598         map.addSourceUrl(uw, Constants.FIRST_URL_HASH);
599         HTMLDocumentBuilder builder = new HTMLDocumentBuilder(map, UrlEncoderFactory.getServletUrlEncoder(request, response));
600
601         //Configure builder
602
ConfigureBean configBean = getClipperBean(request).getConfigurationBean();
603
604         //configure ssl
605
String JavaDoc ssl = configBean.getEnableSSL();
606         if (ssl == null) {
607             org.jahia.clipbuilder.html.web.RegisterSSLProctocol.unregisterSSLProtocol();
608         }
609         else {
610             org.jahia.clipbuilder.html.web.RegisterSSLProctocol.registerSSLProtocol();
611         }
612
613         //get browser javascript configuration
614
int browserJavascriptEvent = Integer.parseInt(configBean.getBrowserJavascriptEvent());
615         int browserJavascriptCode = Integer.parseInt(configBean.getBrowserJavascriptCode());
616
617         // get the type of the client and the parser
618
int clientType = Integer.parseInt(configBean.getClient());
619         int parserType = Integer.parseInt(configBean.getHtmlDocument());
620
621         // set the javascript
622
String JavaDoc javascript = configBean.getEnableJavascript();
623         boolean javascriptBool = true;
624         if (javascript == null) {
625             javascriptBool = false;
626         }
627
628         //set Css
629
String JavaDoc css = configBean.getEnableCSS();
630         boolean cssBool = true;
631         if (css == null) {
632             cssBool = false;
633         }
634
635         //set headers
636
org.org.apache.commons.httpclient.Header[] headers = new org.org.apache.commons.httpclient.Header[1];
637         headers = null;
638         String JavaDoc encodingValue = response.getCharacterEncoding();
639
640         logger.debug("[ Encoding is " + encodingValue + " ]");
641         //headers[0] = new org.org.apache.commons.httpclient.Header("Content-Type", "text/html; charset="+encodingValue);
642

643         //configure the builder
644
builder.configure(clientType, parserType, browserJavascriptEvent, browserJavascriptCode, javascriptBool, cssBool, headers);
645
646                 // set in session
647
SessionManager.setHTMLDocumentBuilder(request, builder);
648     }
649
650
651     /**
652      * Description of the Method
653      *
654      *@param request Description of Parameter
655      *@return Description of the Returned Value
656      *@exception WebClippingException Description of Exception
657      */

658     private String JavaDoc buildSelectedPart(HttpServletRequest request) throws WebClippingException {
659         //Get Filter
660
String JavaDoc selectedPart = null;
661         try {
662             ClipperBean bean = getClipperBean(request);
663             if (bean == null) {
664
665                 logger.error("[ClipperBean not found]");
666             }
667             FilterBean filterBean = bean.getFilterBean();
668             logger.debug("[Filter class: " + filterBean.getClass().toString() + " ]");
669
670             // Get the target document
671
UrlBean uBean = bean.getLastRecordedUrlBean();
672             HTMLDocument doc = uBean.getDocument();
673             if (doc == null) {
674                 logger.debug("[ Url is: " + bean.getLastRecordedUrlBean().getAbsoluteURL() + " ]");
675                 logger.error("[ Document is null ]");
676             }
677
678             // update HtmlBuilder
679
doc.getTransformer().setBuilder(SessionManager.getHTMLDocumentBuilder(request));
680
681             //Get the selected part
682
ExtractorFilter filter = ExtractorFilterFactory.getInstance().getFilterImplementation(filterBean.getName());
683             Map keyMap = filterBean.getKeyMap();
684             filter.setKeyMap(keyMap);
685             int mode = filterBean.getMode();
686             filter.setMode(mode);
687             selectedPart = filter.getSelectedPart(doc, ExtractorFilter.ACTION_BUILD_KEY_PART);
688         }
689         catch (Exception JavaDoc ex) {
690             ex.printStackTrace();
691             selectedPart = " Error has occured";
692             throw new WebClippingException("Select part", ex);
693         }
694
695         //logger.debug("[Selected part is] --> " + selectedPart);
696

697         return selectedPart;
698     }
699
700
701     /**
702      * Sets the NewExpirationCache attribute of the WebBrowserAction class
703      *
704      *@param nameSpace The new NewExpirationCache value
705      *@param ttl The new NewExpirationCache value
706      */

707     public static void setNewExpirationCache(String JavaDoc nameSpace, long ttl) {
708         ExpiringCache cache = new ExpiringCache(ttl, 30 * 60 * 1000, 1, 2 * 60 * 60 * 1000);
709         cacheHacheMap.put(nameSpace, cache);
710     }
711
712
713     /**
714      * Gets the Param attribute of the ClipperAction object
715      *
716      *@param httpServletRequest Description of Parameter
717      *@return The Param value
718      */

719     public static Map getParam(HttpServletRequest httpServletRequest) {
720         Map paramsMap = new HashMap(httpServletRequest.getParameterMap());
721         // remove the paramter of
722
paramsMap.remove("linkHash");
723         paramsMap.remove("nameSpace");
724         paramsMap.remove("show");
725         return paramsMap;
726     }
727
728
729     /**
730      * Description of the Method
731      *
732      *@param nameSpace Description of Parameter
733      *@param remoteUser Description of Parameter
734      *@param cBean Description of Parameter
735      */

736     public static void removeObjectFromCache(String JavaDoc nameSpace, String JavaDoc remoteUser, ClipperBean cBean) {
737         String JavaDoc key = buildCacheKey(nameSpace, remoteUser, cBean.getConfigurationBean().getPortletCacheContext());
738         ExpiringCache cache = (ExpiringCache) cacheHacheMap.get(nameSpace);
739         if (cache != null) {
740             cache.admit(key, null);
741         }
742     }
743
744
745
746     /**
747      * Description of the Method
748      *
749      *@param nameSpace Description of Parameter
750      *@param user Description of Parameter
751      *@param cacheContext Description of Parameter
752      *@return Description of the Returned Value
753      */

754     private static String JavaDoc buildCacheKey(String JavaDoc nameSpace, String JavaDoc user, String JavaDoc cacheContext) {
755         String JavaDoc key = nameSpace;
756         int cc = Integer.parseInt(cacheContext);
757         if (cc == org.jahia.clipbuilder.html.util.Constants.PORTLET_CACHE_PORTAL) {
758             return key;
759         }
760         else if (cc == org.jahia.clipbuilder.html.util.Constants.PORTLET_CACHE_USER) {
761             return key + "::" + user;
762         }
763
764         logger.error("cache context value false: user portalcontext value");
765         return key;
766     }
767
768 }
769
Popular Tags