KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > databinding > datagrid > runtime > util > JspUtil


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.databinding.datagrid.runtime.util;
19
20 import java.net.URISyntaxException JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Locale JavaDoc;
24 import javax.servlet.jsp.JspContext JavaDoc;
25 import javax.servlet.jsp.PageContext JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils;
30 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
31 import org.apache.beehive.netui.tags.internal.PageFlowTagUtils;
32 import org.apache.beehive.netui.util.Bundle;
33
34 /**
35  * <p>
36  * Utility class used for operations related to JSPs.
37  * </p>
38  */

39 public final class JspUtil {
40
41     /* do not construct */
42     private JspUtil() {
43     }
44
45     /**
46      * Attempt to obtain a {@link HttpServletRequest} from a {@link JspContext}.
47      * @param jspContext the jsp context
48      * @return the {@link HttpServletRequest}
49      * @throws IllegalStateException if the {@link JspContext} is unable to provide a {@link HttpServletRequest}
50      */

51     public static final HttpServletRequest JavaDoc getRequest(JspContext JavaDoc jspContext) {
52         PageContext JavaDoc pageContext = getPageContext(jspContext);
53         return (HttpServletRequest JavaDoc)pageContext.getRequest();
54     }
55
56     /**
57      * Attempt to convert a {@link JspContext} into a {@link PageContext}.
58      * @param jspContext the jsp context
59      * @return the page context
60      * @throws IllegalStateException if the {@link JspContext} can't be converted into a {@link PageContext}
61      */

62     public static final PageContext JavaDoc getPageContext(JspContext JavaDoc jspContext) {
63         if(!(jspContext instanceof PageContext JavaDoc))
64             throw new IllegalStateException JavaDoc(Bundle.getErrorString("DataGridUtil_IllegalJspContext", new Object JavaDoc[]{(jspContext != null ? jspContext.getClass().getName() : "null")}));
65         else
66             return (PageContext JavaDoc)jspContext;
67     }
68
69     /**
70      * <p>
71      * Utility method used by data grid related classes to create URLs. If both an action name and href are provided,
72      * the action name will be used to construct the URL string.
73      * </p>
74      * @param href the href
75      * @param action the action
76      * @param location the intra page location
77      * @param scope the scope into which to create the URL
78      * @param params a map of parameters to attach to the URL
79      * @param jspContext the jsp context
80      * @return a URL represented as a string. This URL will be correctly encoded by calling {@link HttpServletResponse#encodeURL(String)}
81      * @throws URISyntaxException
82      */

83     public static final String JavaDoc createURL(String JavaDoc href, String JavaDoc action, String JavaDoc location, String JavaDoc scope, Map JavaDoc params, JspContext JavaDoc jspContext)
84             throws URISyntaxException JavaDoc {
85         PageContext JavaDoc pageContext = getPageContext(jspContext);
86
87         /* add the jpfScopeID parameter, if the scope attribute is present. */
88         if(scope != null) {
89             if(params == null)
90                 params = new HashMap JavaDoc();
91
92             params.put(ScopedServletUtils.SCOPE_ID_PARAM, scope);
93         }
94
95         String JavaDoc uri = null;
96         if(action != null)
97             uri = PageFlowTagUtils.rewriteActionURL(pageContext, action, params, location);
98         else if(href != null)
99             uri = PageFlowTagUtils.rewriteHrefURL(pageContext, href, params, location);
100         else
101             return ((HttpServletRequest JavaDoc)pageContext.getRequest()).getPathTranslated();
102
103         assert uri != null;
104
105         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc)pageContext.getResponse();
106         return response.encodeURL(uri);
107     }
108
109     /**
110      * Get the {@link Locale} from the {@link JspContext}
111      * @param jspContext the jsp context
112      * @return the current locale
113      */

114     public static final Locale JavaDoc getLocale(JspContext JavaDoc jspContext) {
115         return InternalUtils.lookupLocale(jspContext);
116     }
117 }
118
Popular Tags