KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > struts > BrowseAction


1 package org.jahia.clipbuilder.html.struts;
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.*;
11 import org.jahia.clipbuilder.html.struts.Util.Constants;
12 import org.jahia.clipbuilder.html.struts.webBrowser.WebBrowserForm;
13 import org.jahia.clipbuilder.html.util.*;
14 import org.jahia.clipbuilder.html.web.Constant.*;
15 import org.jahia.clipbuilder.html.web.Url.*;
16 import org.jahia.clipbuilder.html.web.http.*;
17
18
19 /**
20  * Description of the Class
21  *
22  *@author Tlili Khaled
23  */

24 public class BrowseAction extends AbstractWizardAction {
25     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(BrowseAction.class);
26
27
28     /**
29      * Gets the KeyMethodMap attribute of the BrowseAction object
30      *
31      *@return The KeyMethodMap value
32      */

33     public Map getKeyMethodMap() {
34         Map map = super.getKeyMethodMap();
35         map.put("wizard.browse", "view");
36         map.put("browse.browse", "browse");
37         map.put("button.next", "goToNextStep");
38         map.put("browse.startRecord", "startRecord");
39         map.put("browse.resetRecord", "init");
40         map.put("browse.stopRecord", "stopRecord");
41         map.put("browse.removeLast", "removeLastRecordedUrl");
42         //map.put("browse.replay", "replay");
43
map.put("browse.button.addUrl", "addManuallyUrl");
44
45         // wizard button
46
return map;
47     }
48
49
50
51     /**
52      * Gets the FormId attribute of the BrowseAction object
53      *
54      *@return The FormId value
55      */

56     public int getFormId() {
57         return Constants.BROWSE;
58     }
59
60
61     /**
62      * Description of the Method
63      *
64      *@param actionMapping Description of Parameter
65      *@param actionForm Description of Parameter
66      *@param httpServletRequest Description of Parameter
67      *@param httpServletResponse Description of Parameter
68      *@return Description of the Returned Value
69      */

70     public ActionForward view(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
71         logger.debug("[ View ]");
72         AbstractWizardForm form = (AbstractWizardForm) actionForm;
73         SessionManager.getWebBrowserForm(httpServletRequest).setShow(Constants.WEB_BROWSER_SHOW_LAST_DOCUMENT);
74
75         return actionMapping.getInputForward();
76     }
77
78
79     /**
80      * Adds a feature to the ManuallyUrl attribute of the BrowseAction object
81      *
82      *@param actionMapping The feature to be added to the ManuallyUrl attribute
83      *@param actionForm The feature to be added to the ManuallyUrl attribute
84      *@param request The feature to be added to the ManuallyUrl attribute
85      *@param response The feature to be added to the ManuallyUrl attribute
86      *@return Description of the Returned Value
87      */

88     public ActionForward addManuallyUrl(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
89         logger.debug("[ Add Manually Url ]");
90         BrowseForm form = (BrowseForm) actionForm;
91         String JavaDoc newUrl = form.getSourceUrl();
92         WebBrowserForm webForm = SessionManager.getWebBrowserForm(request);
93
94         //build a wrapper and add it to the urlMap
95
String JavaDoc hash = HashUtilities.buildManulUrlHash(newUrl);
96         String JavaDoc from = WebConstants.FROM_MANUAL;
97         URLWrapper wrapper = new URLWrapper(newUrl, from, "GET", null, hash);
98         URLMap map = SessionManager.getHTMLDocumentBuilder(request).getUrlMap();
99         map.addSourceUrl(wrapper, hash);
100
101         //set webBrowser parama
102
webForm.setFrom(from);
103         webForm.setLinkHash(hash);
104         webForm.setShow(Constants.WEB_BROWSER_SHOW_BROWSE);
105
106         return actionMapping.getInputForward();
107     }
108
109
110
111     /**
112      * Action when the Next button is selected
113      *
114      *@param actionMapping Description of Parameter
115      *@param actionForm Description of Parameter
116      *@param request Description of Parameter
117      *@param response Description of Parameter
118      *@return Description of the Returned Value
119      *@exception Exception Description of Exception
120      */

121     public ActionForward goToNextStep(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception JavaDoc {
122         BrowseForm browseForm = (BrowseForm) actionForm;
123
124         //init bean from browse
125
updateSessionAttributes(request, Constants.BROWSE);
126         //Set the statut of the recording bean
127
HttpSession session = request.getSession();
128
129         RecordingBean rb = SessionManager.getRecorderBean(request);
130         rb.setStatut(RecordingBean.STOP);
131         SessionManager.setRecorderBean(request, rb);
132
133         //init the selectPart and edit part
134
session.setAttribute(Constants.SELECTPART_FORM, new SelectPartForm());
135
136         //test if at leat one url has been recorded
137
ClipperBean bean = SessionManager.getClipperBean(request);
138         if (bean.isEmpty()) {
139             logger.warn("[ No url has been recorded]");
140             return browse(actionMapping, actionForm, request, response);
141         }
142         logger.debug("[go to select part]");
143
144         return actionMapping.findForward("selectPart");
145     }
146
147
148     /**
149      * Action when the start button is selected
150      *
151      *@param actionMapping Description of Parameter
152      *@param actionForm Description of Parameter
153      *@param httpServletRequest Description of Parameter
154      *@param httpServletResponse Description of Parameter
155      *@return Description of the Returned Value
156      */

157     public ActionForward startRecord(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
158         BrowseForm browseForm = (BrowseForm) actionForm;
159         logger.debug("[Start record]");
160
161         //Set the statut of the recording bean
162
HttpSession session = httpServletRequest.getSession();
163         RecordingBean rb = (RecordingBean) session.getAttribute(WebConstants.RECORDING);
164
165         // add the bean of the recording bean
166
SessionManager.getClipperBean(httpServletRequest).addUrlBean(rb.getCurrentUrlBean());
167         rb.setStatut(RecordingBean.START);
168         session.setAttribute(WebConstants.RECORDING, rb);
169
170         // updtade the webBrowser state
171
SessionManager.getWebBrowserForm(httpServletRequest).setShow(Constants.WEB_BROWSER_SHOW_LAST_DOCUMENT);
172
173         return actionMapping.getInputForward();
174     }
175
176
177     /**
178      * Description of the Method
179      *
180      *@param actionMapping Description of Parameter
181      *@param actionForm Description of Parameter
182      *@param httpServletRequest Description of Parameter
183      *@param httpServletResponse Description of Parameter
184      *@return Description of the Returned Value
185      */

186     public ActionForward stopRecord(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
187         BrowseForm browseForm = (BrowseForm) actionForm;
188         logger.debug("[Start record]");
189
190         //Set the statut of the recording bean
191
HttpSession session = httpServletRequest.getSession();
192         RecordingBean rb = (RecordingBean) session.getAttribute(WebConstants.RECORDING);
193         rb.setStatut(RecordingBean.STOP);
194
195         // updtade the webBrowser state
196
SessionManager.getWebBrowserForm(httpServletRequest).setShow(Constants.WEB_BROWSER_SHOW_LAST_DOCUMENT);
197         session.setAttribute(WebConstants.RECORDING, rb);
198
199         return actionMapping.getInputForward();
200     }
201
202
203
204     /**
205      * Action when the stop button is selected
206      *
207      *@param actionMapping Description of Parameter
208      *@param actionForm Description of Parameter
209      *@param httpServletRequest Description of Parameter
210      *@param httpServletResponse Description of Parameter
211      *@return Description of the Returned Value
212      *@exception Exception Description of Exception
213      */

214     public ActionForward resetRecord(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception JavaDoc {
215         logger.debug("[Reset record]");
216         BrowseForm browseForm = (BrowseForm) actionForm;
217
218         // init form from description
219
init(actionMapping, actionForm, httpServletRequest, httpServletResponse, browseForm);
220
221         //init bean
222
updateSessionAttributes(httpServletRequest, Constants.DESCRIPTION);
223
224         return actionMapping.getInputForward();
225     }
226
227
228     /**
229      * Action when the browse is selected
230      *
231      *@param actionMapping Description of Parameter
232      *@param actionForm Description of Parameter
233      *@param httpServletRequest Description of Parameter
234      *@param httpServletResponse Description of Parameter
235      *@return Description of the Returned Value
236      *@exception Exception Description of Exception
237      */

238     public ActionForward browse(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception JavaDoc {
239         BrowseForm browseForm = (BrowseForm) actionForm;
240
241         // if the wizard is at the selection step go to selection step
242
Object JavaDoc o = httpServletRequest.getSession().getAttribute(Constants.SELECTPART_FORM);
243         if (o != null) {
244             return actionMapping.findForward("selectPart");
245         }
246         // set the browser
247
WebBrowserForm wForm = SessionManager.getWebBrowserForm(httpServletRequest);
248         wForm.setShow(Constants.WEB_BROWSER_SHOW_BROWSE);
249
250         //process only if it'st a new action (does'nt load from wizard)
251
logger.debug("[ Browse ]");
252
253         //Get the url from the ClipperBean object
254
ClipperBean bean = SessionManager.getClipperBean(httpServletRequest);
255
256         //Handing error
257
if (bean == null) {
258             return actionMapping.findForward("description");
259         }
260         return actionMapping.getInputForward();
261     }
262
263
264     /**
265      * Description of the Method
266      */

267     /*
268      * public ActionForward replay(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
269      * BrowseForm browseForm = (BrowseForm) actionForm;
270      * logger.debug("[ Replay ]");
271      * //Get all the url
272      * ClipperBean cBean = getClipperBean(httpServletRequest);
273      * List urlListBean = cBean.getUrlListBean();
274      * // for each url, rebuild the document
275      * HTMLDocument htmlDocument = null;
276      * UrlBean uBean = null;
277      * for (int i = 0; i < urlListBean.size(); i++) {
278      * // get the url bean
279      * uBean = (UrlBean) urlListBean.get(i);
280      * logger.debug("[ process url " + uBean.getAbsoluteUrlValue() + " ]");
281      * htmlDocument = getRequestedDocument(httpServletRequest, httpServletResponse, uBean, "GET");
282      * }
283      * // set the body content
284      * // browseForm.setBodyContent(htmlDocument.getTransformedDocumentAsString());
285      * return actionMapping.getInputForward();
286      * }
287      */

288     /**
289      * Description of the Method
290      *
291      *@param actionMapping Description of Parameter
292      *@param actionForm Description of Parameter
293      *@param httpServletRequest Description of Parameter
294      *@param httpServletResponse Description of Parameter
295      *@return Description of the Returned Value
296      */

297     public ActionForward init(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
298         BrowseForm browseForm = (BrowseForm) actionForm;
299         ActionErrors errors = new ActionErrors();
300         try {
301
302             //set all properties to null
303
browseForm.setAllPropertiesToNull();
304             // and init
305
init(actionMapping, actionForm, httpServletRequest, httpServletResponse, browseForm);
306
307             super.updateSessionAttributes(httpServletRequest, Constants.BROWSE);
308
309             //process only if it'st a new action (does'nt load from wizard)
310
logger.debug("[ Browse ]");
311
312             //Get the url from the ClipperBean object
313
ClipperBean bean = SessionManager.getClipperBean(httpServletRequest);
314             bean.clearRecordedUrl();
315
316             //Handing error
317
if (bean == null) {
318                 return actionMapping.findForward("description");
319             }
320
321         }
322
323         catch (Exception JavaDoc ex) {
324             ActionError e = new ActionError("errors.exception", ex.getStackTrace()[0].toString());
325             e = new ActionError("errors.exception", ex.toString());
326             errors.add("errors.exception", e);
327             ex.printStackTrace();
328         }
329
330         finally {
331             saveMessages(httpServletRequest, errors);
332             return actionMapping.getInputForward();
333         }
334
335     }
336
337
338     /**
339      * Description of the Method
340      *
341      *@param actionMapping Description of Parameter
342      *@param actionForm Description of Parameter
343      *@param httpServletRequest Description of Parameter
344      *@param httpServletResponse Description of Parameter
345      *@return Description of the Returned Value
346      */

347     public ActionForward removeLastRecordedUrl(ActionMapping actionMapping,
348             ActionForm actionForm,
349             HttpServletRequest
350             httpServletRequest,
351             HttpServletResponse
352             httpServletResponse) {
353         BrowseForm browseForm = (BrowseForm) actionForm;
354
355         //process only if it'st a new action (does'nt load from wizard)
356
logger.debug("[ Remove last recorded url ]");
357
358         //Get the url from the ClipperBean object
359
ClipperBean bean = SessionManager.getClipperBean(httpServletRequest);
360
361         //Handing error
362
if (bean == null) {
363             return actionMapping.findForward("description");
364         }
365
366         //Init targetUrl
367
//init(actionMapping, actionForm, httpServletRequest, httpServletResponse, browseForm);
368

369         //set the content
370
removeLastUrlandSetHTMLContent(httpServletRequest, httpServletResponse, browseForm);
371
372         SessionManager.getWebBrowserForm(httpServletRequest).setShow(Constants.WEB_BROWSER_SHOW_LAST_DOCUMENT);
373         //logger.debug("[ "+uBean.getDocument().getTransformedDocumentAsString()+" ]");
374
return actionMapping.getInputForward();
375     }
376
377
378     /**
379      * Sets the UsedValueForPreviousUrl attribute of the BrowseAction object
380      */

381     /*
382      * private void setUsedValueForPreviousUrl(HttpServletRequest httpServletRequest,
383      * UrlBean uBean, ClipperBean bean) {
384      * //don't process the first url
385      * if (uBean.getPosition() > 0) {
386      * //get the previous url
387      * UrlBean previousUrlBean = bean.getUrlBean(uBean.getPosition() - 1);
388      * //get its form param list
389      * String formParentHash = uBean.getHash();
390      * // get the queryBeanList for the current url <-> formBeanList of the previous url
391      * List queryParamList = uBean.getQueryParamBeanList();
392      * for (int i = 0; i < queryParamList.size(); i++) {
393      * QueryParamBean qBean = (QueryParamBean) queryParamList.get(i);
394      * String name = qBean.getName();
395      * FormParamBean fBean = previousUrlBean.getFormParamBeanByNameAndFormParentHash(formParentHash, name);
396      * if (fBean != null) {
397      * logger.debug("[ Form param bean for queryBean: " + name + " and Parent form: " + formParentHash + "found ]");
398      * fBean.setQueryParamBean(qBean);
399      * }
400      * else {
401      * logger.error("[ Form param bean for queryBean: " + name + " and form: " + formParentHash + " not found ]");
402      * }
403      * /*
404      * FormParamBean fBean = (FormParamBean) formParamBeanList.get(i);
405      * String[] paramValues = (String[]) queryMap.get(fBean.getName());
406      * if (paramValues != null) {
407      * String value = paramValues[0];
408      * fBean.setUsedValue(value);
409      * }
410      */

411     //}
412

413     // synchronize used value
414
/*
415      * for (int i = 0; i < formParamBeanList.size(); i++) {
416      * FormParamBean fBean = (FormParamBean) formParamBeanList.get(i);
417      * String[] paramValues = (String[]) queryMap.get(fBean.getName());
418      * if (paramValues != null) {
419      * String value = paramValues[0];
420      * fBean.setUsedValue(value);
421      * }
422      * }
423      */

424     // }
425

426     //}
427

428     /**
429      * Gets the RequestedAndTransformedDocument attribute of the BrowseAction
430      * object
431      */

432     /*
433      * private HTMLDocument getRequestedDocument(HttpServletRequest request, HttpServletResponse response, UrlBean uBean, String httpMethod) throws Exception {
434      * // Get a HMTLDocument builder
435      * HTMLDocumentBuilder builder = new HTMLDocumentBuilder(uBean, httpMethod);
436      * //Get the configuration object
437      * ConfigureBean configBean = getClipperBean(request).getConfigurationBean();
438      * //configure ssl
439      * String ssl = configBean.getEnableSSL();
440      * if (ssl == null) {
441      * org.jahia.clipbuilder.html.web.RegisterSSLProctocol.unregisterSSLProtocol();
442      * }
443      * else {
444      * org.jahia.clipbuilder.html.web.RegisterSSLProctocol.registerSSLProtocol();
445      * }
446      * //get browser javascript configuration
447      * int browserJavascriptEvent = Integer.parseInt(configBean.getBrowserJavascriptEvent());
448      * int browserJavascriptCode = Integer.parseInt(configBean.getBrowserJavascriptCode());
449      * // get the type of the client and the parser
450      * int clientType = Integer.parseInt(configBean.getClient());
451      * int parserType = Integer.parseInt(configBean.getHtmlDocument());
452      * // set the javascript
453      * String webClientJavascript = configBean.getEnableJavascript();
454      * boolean javascriptBool = true;
455      * if (webClientJavascript == null) {
456      * javascriptBool = false;
457      * }
458      * //set Css
459      * String css = configBean.getEnableCSS();
460      * boolean cssBool = true;
461      * if (css == null) {
462      * cssBool = false;
463      * }
464      * //set headers
465      * org.org.apache.commons.httpclient.Header[] headers = new org.org.apache.commons.httpclient.Header[1];
466      * headers = null;
467      * String encodingValue = response.getCharacterEncoding();
468      * logger.debug("[ Encoding is " + encodingValue + " ]");
469      * //headers[0] = new org.org.apache.commons.httpclient.Header("Content-Type", "text/html; charset="+encodingValue);
470      * //configure the builder
471      * builder.configure(clientType, parserType,browserJavascriptEvent,browserJavascriptCode, javascriptBool, cssBool, headers);
472      * // execute
473      * HTMLDocument doc = builder.execute(request, response);
474      * //Get and save all errors that's occured during execution
475      * /*
476      * List errors = builder.getParsingErrors();
477      * ActionErrors actionErrors = new ActionErrors();
478      * for (int i = 0; i < errors.size(); i++) {
479      * String error = errors.get(i).toString();
480      * ActionError e = new ActionError("errors.message", error);
481      * actionErrors.add("errors.message", e);
482      * //logger.debug("[ ERROR ]"+error);
483      * }
484      * addErrors(request, actionErrors);
485      * return doc;
486      * }
487      */

488     /**
489      * Description of the Method
490      *
491      *@param httpServletRequest Description of Parameter
492      *@param httpServletResponse Description of Parameter
493      *@param browseForm Description of Parameter
494      */

495     private void removeLastUrlandSetHTMLContent(HttpServletRequest
496             httpServletRequest,
497             HttpServletResponse
498             httpServletResponse,
499             BrowseForm browseForm) {
500         // Get beans
501
ClipperBean cBean = SessionManager.getClipperBean(httpServletRequest);
502         cBean.removeLastRecordedUrl();
503
504         // add the url to the clipper bean
505
logger.debug("[Last url removed]");
506
507         // set the urlBean to display
508
if (cBean.isEmpty()) {
509             RecordingBean rBean = SessionManager.getRecorderBean(httpServletRequest);
510             rBean.setStatut(RecordingBean.STOP);
511         }
512
513         // get the client
514
HttpSession session = httpServletRequest.getSession();
515         HTMLClient client = (HTMLClient) session.getAttribute(org.jahia.clipbuilder.html.web.Constant.WebConstants.ADVANCED_WEBCLIENT);
516         if (client != null) {
517             client.removeLastUrl();
518         }
519
520     }
521
522
523     /**
524      * Description of the Method
525      *
526      *@param actionMapping Description of Parameter
527      *@param actionForm Description of Parameter
528      *@param httpServletRequest Description of Parameter
529      *@param httpServletResponse Description of Parameter
530      *@param browseForm Description of Parameter
531      */

532     private void init(ActionMapping actionMapping, ActionForm actionForm,
533             HttpServletRequest httpServletRequest,
534             HttpServletResponse httpServletResponse,
535             BrowseForm browseForm) {
536         super.init(actionMapping, actionForm, httpServletRequest, httpServletResponse);
537         String JavaDoc sourceUrl = SessionManager.getClipperBean(httpServletRequest).getTargetUrl();
538         logger.debug("[Init sourceUrl property " + sourceUrl + " ]");
539         // the source url has been set by the user
540
browseForm.setSourceUrl(sourceUrl);
541         browseForm.setLinkHash(Constants.FIRST_URL_HASH);
542         browseForm.setFrom(Constants.FIRST_URL_FROM);
543     }
544
545 }
546
Popular Tags