KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > BaseWebPageRequest


1 /*
2  * Created on Jul 28, 2003
3  *
4  /*
5  Copyright (c) 2003 eInnovation Inc. All rights reserved
6
7  This library is free software; you can redistribute it and/or modify it under the terms
8  of the GNU Lesser General Public License as published by the Free Software Foundation;
9  either version 2.1 of the License, or (at your option) any later version.
10
11  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  See the GNU Lesser General Public License for more details.
14  */

15 package com.openedit;
16
17 import java.io.IOException JavaDoc;
18 import java.io.OutputStream JavaDoc;
19 import java.io.Writer JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.http.HttpSession JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 import com.openedit.page.Page;
35 import com.openedit.page.PageAction;
36 import com.openedit.page.PageRequestKeys;
37 import com.openedit.page.PageStreamer;
38 import com.openedit.users.User;
39 import com.openedit.util.PathUtilities;
40 import com.openedit.util.SessionMap;
41 import com.openedit.util.URLUtilities;
42 import com.openedit.web.Browser;
43
44 /**
45  * @author Matt Avery, mavery@einnovation.com
46  */

47 public class BaseWebPageRequest implements WebPageRequest, PageRequestKeys
48 {
49     private static final Log log = LogFactory.getLog(BaseWebPageRequest.class);
50
51     protected HttpServletRequest JavaDoc fieldHttpServletRequest;
52     protected HttpServletResponse JavaDoc fieldHttpServletResponse;
53     protected HttpSession JavaDoc fieldHttpSession;
54
55     protected WebPageRequest fieldParent;
56     protected Map JavaDoc fieldVariables;
57     protected Set JavaDoc fieldProtectedFields;
58     protected Map JavaDoc fieldParameters;
59     protected Map JavaDoc fieldBackUpSession;
60     protected boolean fieldHasRedirected;
61     protected boolean fieldHasForwarded;
62     protected boolean fieldEditable;
63     public BaseWebPageRequest(WebPageRequest parent)
64     {
65         fieldParent = parent;
66         setEditable(parent.isEditable());
67         while( parent != null)
68         {
69             if (parent == this)
70             {
71                 throw new OpenEditRuntimeException("can't set parent to self");
72             }
73             parent = parent.getParent();
74         }
75     }
76
77     public BaseWebPageRequest()
78     {
79     }
80
81     protected Set JavaDoc getProtectedFields()
82     {
83         if (fieldProtectedFields == null )
84         {
85             fieldProtectedFields = new HashSet JavaDoc();
86         }
87         return fieldProtectedFields;
88     }
89
90     public WebPageRequest getParent()
91     {
92         return fieldParent;
93     }
94
95     public HttpServletRequest JavaDoc getRequest()
96     {
97         if (fieldHttpServletRequest == null && getParent() != null)
98         {
99             return getParent().getRequest();
100         }
101         return fieldHttpServletRequest;
102     }
103
104     public HttpServletResponse JavaDoc getResponse()
105     {
106         if (fieldHttpServletResponse == null && getParent() != null)
107         {
108             return getParent().getResponse();
109         }
110         return fieldHttpServletResponse;
111     }
112
113     public HttpSession JavaDoc getSession()
114     {
115         if (fieldHttpSession == null && getParent() != null)
116         {
117             return getParent().getSession();
118         }
119         return fieldHttpSession;
120     }
121
122     public void forward(String JavaDoc inUrl) throws OpenEditException
123     {
124         //fieldHasRedirected = true;
125
getPageStreamer().forward(inUrl, this);
126     }
127
128     public String JavaDoc getRequestParameter(String JavaDoc inKey)
129     {
130         if (getLocalParameters().containsKey(inKey) || getRequest() == null)
131         {
132             return (String JavaDoc) getLocalParameters().get(inKey);
133         }
134         String JavaDoc value = getRequest().getParameter(inKey);
135         if ( value == null && getParent() != null)
136         {
137             value = getParent().getRequestParameter(inKey);
138             return value;
139         }
140         else
141         {
142             if ( value != null && value.length() == 0)
143             {
144                 value = null; //null out blank strings
145
}
146             return value;
147         }
148     }
149
150     /*
151      * @see com.openedit.WebAppContext#getParameterMap()
152      */

153     public Map JavaDoc getParameterMap()
154     {
155         if (getRequest() != null)
156         {
157             Map JavaDoc combinedparams = new HashMap JavaDoc();
158             Enumeration JavaDoc enumeration = getRequest().getParameterNames();
159             while (enumeration.hasMoreElements())
160             {
161                 String JavaDoc key = (String JavaDoc) enumeration.nextElement();
162                 String JavaDoc[] allv = getRequest().getParameterValues(key);
163                 if( allv != null && allv.length == 1)
164                 {
165                     combinedparams.put(key, allv[0]);
166                 }
167                 else
168                 {
169                     combinedparams.put(key, allv);
170                 }
171             }
172             combinedparams.putAll(getLocalParameters());
173             return combinedparams;
174         }
175         else
176         {
177             return getLocalParameters();
178         }
179     }
180
181     protected Map JavaDoc getLocalParameters()
182     {
183         if (fieldParameters == null)
184         {
185             fieldParameters = new HashMap JavaDoc();
186         }
187         return fieldParameters;
188     }
189
190     /*
191      * @see com.openedit.WebAppContext#getRequiredParameter(java.lang.String)
192      */

193     public String JavaDoc getRequiredParameter(String JavaDoc inParameterName) throws OpenEditException
194     {
195         String JavaDoc req = getRequestParameter(inParameterName);
196         if (req == null)
197         {
198             throw new OpenEditException("Required parameter not found " + inParameterName);
199         }
200         return req;
201     }
202
203     /*
204      * @see com.openedit.WebAppContext#getVariable(java.lang.String)
205      */

206     public void put(String JavaDoc inKey, Object JavaDoc inValue) throws OpenEditException
207     {
208         putPageValue(inKey, inValue);
209     }
210
211     public Object JavaDoc get(String JavaDoc inKey)
212     {
213         return getPageValue(inKey);
214     }
215
216     public Object JavaDoc getPageValue(String JavaDoc inKey)
217     {
218         Object JavaDoc ret = getVariables().get(inKey);
219         if (ret == null && getParent() != null)
220         {
221             return getParent().getPageValue(inKey);
222         }
223         return ret;
224     }
225
226     /*
227      * @see com.openedit.WebAppContext#redirect(java.lang.String)
228      */

229     public void redirect(String JavaDoc inUrl)
230     {
231         boolean alreadyRedirected = getPageValue("redirect") != null;
232         String JavaDoc home = (String JavaDoc) getPageValue("home");
233         if (alreadyRedirected)
234         {
235             log.debug("Previous redirect to " + getPageValue("redirect")
236                 + " requested, cannot redirect to " + inUrl);
237             return;
238         }
239
240         try
241         {
242             if (inUrl != null)
243             {
244                 if (!inUrl.startsWith("http"))
245                 {
246                     if( inUrl.contains("./"))
247                     {
248                         inUrl = PathUtilities.resolveRelativePath(inUrl, getPath() );
249                     }
250                     inUrl = home + inUrl;
251                 }
252                 log.debug("Redirecting to: " + inUrl);
253                 putPageValue("redirect", inUrl);
254                 if (getResponse() != null)
255                 {
256                     getResponse().sendRedirect(inUrl);
257                 }
258                 else
259                 {
260                     log.error("No response set");
261                 }
262                 setHasRedirected(true);
263             }
264         }
265         catch (IOException JavaDoc e)
266         {
267             throw new OpenEditRuntimeException(e);
268         }
269     }
270     /**
271      * This was added because a customer needed it for google indexes.
272      * I wonder if we can not just use permenanet redirects all the time?
273      * @param inPath
274      */

275     public void redirectPermanently( String JavaDoc inPath)
276     {
277         String JavaDoc home = (String JavaDoc) getPageValue("home");
278         try
279         {
280             if (inPath != null)
281             {
282                 log.debug("Perma redirect to: " + inPath);
283                 if (!inPath.startsWith("http"))
284                 {
285                     inPath = home + inPath;
286                 }
287                 putPageValue("redirect", inPath);
288                 if (getResponse() != null)
289                 {
290                     getResponse().setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
291                     getResponse().setHeader("Location", inPath);
292                     getResponse().flushBuffer();
293                 }
294                 else
295                 {
296                     log.error("No response set");
297                 }
298                 setHasRedirected(true);
299             }
300         }
301         catch (IOException JavaDoc e)
302         {
303             throw new OpenEditRuntimeException(e);
304         }
305         
306     }
307     protected Map JavaDoc getVariables()
308     {
309         if (fieldVariables == null)
310         {
311             fieldVariables = new HashMap JavaDoc();
312             fieldVariables.put("context", this);
313         }
314         return fieldVariables;
315     }
316
317     /**
318      * @see com.openedit.WebAppContext#setVariable(java.lang.String, java.lang.Object)
319      */

320     public void putPageValue(String JavaDoc inKey, Object JavaDoc inObject)
321     {
322         if (getProtectedFields().contains(inKey) && getParent() != null)
323         {
324             throw new RuntimeException JavaDoc("Restricted variables can only be set at the root level");
325         }
326         if (inObject == null)
327         {
328             getVariables().remove(inKey);
329         }
330         else
331         {
332             getVariables().put(inKey, inObject);
333         }
334     }
335
336     public void putProtectedPageValue(String JavaDoc inKey, Object JavaDoc inObject)
337     {
338         getProtectedFields().remove(inKey);
339         putPageValue(inKey, inObject);
340         getProtectedFields().add(inKey);
341     }
342
343     /*
344      * @see com.openedit.WebAppContext#removeVariable(java.lang.String)
345      */

346     public void removePageValue(String JavaDoc inKey)
347     {
348         getVariables().remove(inKey);
349     }
350
351     public void setRequest(HttpServletRequest JavaDoc inReq)
352     {
353         fieldHttpServletRequest = inReq;
354     }
355
356     public void setResponse(HttpServletResponse JavaDoc inRes)
357     {
358         fieldHttpServletResponse = inRes;
359     }
360
361     public void setSession(HttpSession JavaDoc inS)
362     {
363         fieldHttpSession = inS;
364     }
365
366     /*
367      * @see com.openedit.WebPageContext#setRequestParameter(java.lang.String, java.lang.String)
368      */

369     public void setRequestParameter(String JavaDoc inKey, String JavaDoc inValue)
370     {
371         getLocalParameters().put(inKey, inValue);
372     }
373
374     public void setRequestParameter(String JavaDoc inKey, String JavaDoc[] inValue)
375     {
376         getLocalParameters().put(inKey, inValue);
377     }
378
379     /*
380      * @see com.openedit.WebPageContext#getVariableMap()
381      */

382     public Map JavaDoc getPageMap()
383     {
384         Map JavaDoc combined = new HashMap JavaDoc();
385         if (getParent() != null)
386         {
387             combined.putAll(getParent().getPageMap());
388         }
389         combined.putAll(getVariables());
390         return combined;
391     }
392
393     /*
394      * @see com.openedit.WebPageContext#getRequestParameters(java.lang.String)
395      */

396     public String JavaDoc[] getRequestParameters(String JavaDoc inKey)
397     {
398         Object JavaDoc parameter = null;
399         if (getLocalParameters().containsKey(inKey))
400         {
401             parameter = getLocalParameters().get(inKey);
402         }
403         else if ( getRequest() != null)
404         {
405             parameter = getRequest().getParameterValues(inKey);
406         }
407         if( parameter == null && getParent() != null)
408         {
409             return getParent().getRequestParameters(inKey);
410         }
411         if (parameter instanceof String JavaDoc[] || parameter == null)
412         {
413             return (String JavaDoc[]) parameter;
414         }
415         return new String JavaDoc[]{(String JavaDoc) parameter};
416     }
417
418     /*
419      * @see com.openedit.WebPageContext#getStoredVariable(java.lang.String)
420      */

421     public Object JavaDoc getSessionValue(String JavaDoc inKey)
422     {
423         if (getSession() == null)
424         {
425             return getSessionValues().get(inKey);
426         }
427         return getSession().getAttribute(inKey);
428     }
429
430     /*
431      * @see com.openedit.WebPageContext#setStoredVariable(java.lang.String, java.lang.Object)
432      */

433     public void putSessionValue(String JavaDoc inKey, Object JavaDoc inObject)
434     {
435         if (getSession() == null)
436         {
437             if( inObject == null)
438             {
439                 getSessionValues().remove(inKey);
440             }
441             else
442             {
443                 getSessionValues().put(inKey, inObject);
444             }
445             return;
446         }
447         if( inObject == null)
448         {
449             getSession().removeAttribute(inKey);
450         }
451         else
452         {
453             getSession().setAttribute(inKey, inObject);
454         }
455         //All session values also go in page values
456
putPageValue(inKey, inObject);
457     }
458
459     /*
460      * @see com.openedit.WebPageContext#removeStoredVariable(java.lang.String)
461      */

462     public void removeSessionValue(String JavaDoc inKey)
463     {
464         if (getSession() != null)
465         {
466             getSession().removeAttribute(inKey);
467         }
468         else
469         {
470             getSessionValues().remove(inKey);
471         }
472     }
473
474     public String JavaDoc getPath()
475     {
476         if ( getPage() == null)
477         {
478             return null;
479         }
480         return getPage().getPath();
481     }
482
483     /**
484      * TODO: copy this to standard WebPageContext API
485      * @param inMap
486      */

487     public void putSessionValues(SessionMap inMap)
488     {
489         for (Iterator JavaDoc iter = inMap.keySet().iterator(); iter.hasNext();)
490         {
491             String JavaDoc key = (String JavaDoc) iter.next();
492             putSessionValue(key, inMap.get(key));
493         }
494     }
495
496     /**
497      * TODO: copy this to standard WebPageContext API
498      * @param inMap
499      */

500     public void putPageValues(SessionMap inMap)
501     {
502         getVariables().putAll(inMap);
503     }
504
505     /* (non-javadoc)
506      * @see com.openedit.WebPageContext#getPathUrl()
507      */

508     public String JavaDoc getPathUrl()
509     {
510         URLUtilities util = (URLUtilities) get(URL_UTILITIES);
511         return util.requestPathWithArguments();
512     }
513
514     public OutputStream JavaDoc getOutputStream()
515     {
516         return getPageStreamer().getOutput().getStream();
517     }
518
519     public Writer JavaDoc getWriter()
520     {
521         return getPageStreamer().getOutput().getWriter();
522     }
523
524     public PageStreamer getPageStreamer()
525     {
526         return (PageStreamer) getPageValue(PAGES);
527     }
528
529     public void putPageStreamer(PageStreamer inStreamer)
530     {
531         putPageValue(PAGES, inStreamer);
532     }
533
534     public Page getPage()
535     {
536         Page content = (Page) getPageValue(PAGE);
537         return content;
538     }
539     public void setPage(Page inPage)
540     {
541         putPageValue(PAGE,inPage);
542     }
543
544     public Page getContentPage()
545     {
546         Page content = (Page) getPageValue(CONTENT);
547         return content;
548     }
549
550     /**
551      * This is used only if getSession() is null
552      * @return
553      */

554     protected Map JavaDoc getSessionValues()
555     {
556         if (fieldBackUpSession == null)
557         {
558             fieldBackUpSession = new HashMap JavaDoc();
559         }
560         return fieldBackUpSession;
561     }
562
563     /* (non-javadoc)
564      * @see com.openedit.WebPageContext#hasRedirected()
565      */

566     public boolean hasRedirected()
567     {
568         return fieldHasRedirected;
569     }
570     public void setHasRedirected( boolean inBol)
571     {
572         fieldHasRedirected = inBol;
573     }
574     
575     /**
576      * @param inOutputStream
577      */

578     public void setWriter(Writer JavaDoc inOutputStream)
579     {
580         putProtectedPageValue(OUTPUT_WRITER, inOutputStream);
581     }
582     
583     /**
584      * Determine whether this page can be edited by the given user. The page is editable if:
585      *
586      * <ul>
587      * <li>
588      * the page's repository is not read-only;
589      * </li>
590      * <li>
591      * the "editable" property is not present or equal to "true"; and
592      * </li>
593      * <li>
594      * the edit filter is not present or passes the given user.
595      * </li>
596      * </ul>
597      *
598      *
599      * @param inUser The user to query
600      * @param inContext DOCME
601      *
602      * @return boolean <code>true</code> if the page is editable by the user, <code>false</code>
603      * if not
604      *
605      * @throws OpenEditException DOCME
606      */

607     public boolean isEditable()
608     {
609         return fieldEditable;
610     }
611     public void setEditable( boolean inEditable)
612     {
613         fieldEditable = inEditable;
614     }
615
616     public String JavaDoc[] getRequestActions()
617     {
618         String JavaDoc[] actions = getRequestParameters( "oe-action" );
619         if ( actions == null)
620         {
621             actions = getRequestParameters( "wsp-action" ); //to support OE 3.0
622
}
623         return actions;
624     }
625
626     /* (non-javadoc)
627      * @see com.openedit.WebPageRequest#copy()
628      */

629     public WebPageRequest copy()
630     {
631         return new BaseWebPageRequest(this);
632     }
633
634     /* (non-javadoc)
635      * @see com.openedit.WebPageRequest#copy(com.openedit.page.Page)
636      */

637     public WebPageRequest copy(Page inPage)
638     {
639         BaseWebPageRequest req = new BaseWebPageRequest(this);
640         req.putProtectedPageValue(PageRequestKeys.PAGE,inPage);
641         return req;
642     }
643
644     /* (non-javadoc)
645      * @see com.openedit.WebPageRequest#setUser(com.openedit.users.User)
646      */

647     public void setUser(User inUser)
648     {
649         if( inUser == null)
650         {
651             getVariables().remove(USER);
652             if (getParent() != null)
653             {
654                 getParent().setUser(null);
655             }
656         }
657         else
658         {
659             putPageValue(USER,inUser);
660         }
661     }
662
663
664     public User getUser()
665     {
666         return (User) getPageValue(USER);
667     }
668
669     /**
670      * @param inPage
671      */

672     public void setContentPage(Page inPage)
673     {
674         putPageValue(CONTENT,inPage);
675     }
676
677     /* (non-javadoc)
678      * @see com.openedit.WebPageRequest#setCurrentAction(com.openedit.page.PageAction)
679      */

680     public void setCurrentAction(PageAction inAction)
681     {
682         putPageValue("exec-action",inAction);
683     }
684
685     /* (non-javadoc)
686      * @see com.openedit.WebPageRequest#getCurrentAction()
687      */

688     public PageAction getCurrentAction()
689     {
690         return (PageAction)getPageValue("exec-action");
691     }
692     public String JavaDoc toString()
693     {
694         Object JavaDoc ret = getVariables().get("page");
695         if ( ret != null)
696         {
697             return "page="+ ret.toString();
698         }
699         else if ( getParent() != null)
700         {
701             return "child of " + getParent().toString();
702         }
703         return "no parent";
704     }
705
706     public String JavaDoc getContentProperty(String JavaDoc inKey)
707     {
708         Page page = getContentPage();
709         String JavaDoc locale = getLocale();
710         String JavaDoc prop = page.getProperty(inKey, locale );
711         return prop;
712     }
713
714     public String JavaDoc getPageProperty(String JavaDoc inKey)
715     {
716         Page page = getPage();
717         String JavaDoc locale = getLocale();
718         String JavaDoc prop = page.getProperty(inKey, locale );
719         return prop;
720     }
721
722     public String JavaDoc getLocale()
723     {
724         String JavaDoc locale = (String JavaDoc)getPageValue("sessionlocale");
725         if( locale == null || locale.length() == 0)
726         {
727             User user = getUser();
728             if( user != null)
729             {
730                 locale = (String JavaDoc)user.get("locale");
731             }
732         }
733         if( locale == null || locale.length() == 0)
734         {
735             Browser browser = (Browser)getPageValue("browser");
736             if( browser != null && browser.getLocale() != null)
737             {
738                 locale = browser.getLocale().toString();
739             }
740         }
741
742         return locale;
743     }
744
745     public String JavaDoc getLanguage()
746     {
747         String JavaDoc language = getLocale();
748         if( language != null)
749         {
750             int unds = language.indexOf('_');
751             if( unds > -1)
752             {
753                 language = language.substring(0,unds);
754             }
755         }
756         return language;
757     }
758
759     public boolean hasForwarded()
760     {
761         return fieldHasForwarded;
762     }
763
764     public void setHasForwarded(boolean inB)
765     {
766         fieldHasForwarded = inB;
767     }
768
769     public String JavaDoc getUserName()
770     {
771         User user = getUser();
772         if( user != null)
773         {
774             return user.getUserName();
775         }
776         return "none";
777     }
778     
779 }
780
Popular Tags