KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > faces > renderer > BaseRenderer


1 /*
2  * Copyright 2002,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
17 package org.apache.struts.faces.renderer;
18
19
20 import java.io.IOException JavaDoc;
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22
23 import javax.faces.FacesException;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.context.ResponseWriter;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28
29 import org.apache.commons.beanutils.MethodUtils;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33
34 /**
35  * <p><code>Renderer</code> implementation for the <code>base</code> tag
36  * from the <em>Struts-Faces Integration Library</em>.</p>
37  *
38  * @version $Rev: 54934 $ $Date: 2004-10-16 18:07:50 +0100 (Sat, 16 Oct 2004) $
39  */

40
41 public class BaseRenderer extends AbstractRenderer {
42
43
44     // -------------------------------------------------------- Static Variables
45

46
47     /**
48      * <p>The <code>Log</code> instance for this class.</p>
49      */

50     private static Log log = LogFactory.getLog(BaseRenderer.class);
51
52
53     // ---------------------------------------------------------- Public Methods
54

55
56     /**
57      * <p>Render an HTML <code>base</code> element.</p>
58      *
59      * @param context FacesContext for the request we are processing
60      * @param component UIComponent to be rendered
61      *
62      * @exception IOException if an input/output error occurs while rendering
63      * @exception NullPointerException if <code>context</code>
64      * or <code>component</code> is null
65      */

66     public void encodeEnd(FacesContext context, UIComponent component)
67         throws IOException JavaDoc {
68
69         if ((context == null) || (component == null)) {
70             throw new NullPointerException JavaDoc();
71         }
72
73         if (log.isTraceEnabled()) {
74             log.trace("viewId='" + context.getViewRoot().getViewId() +
75                       "' --> uri='" + uri(context) + "'");
76         }
77
78         ResponseWriter writer = context.getResponseWriter();
79         writer.startElement("base", component);
80         writer.writeURIAttribute("href", uri(context), null);
81         String JavaDoc target = (String JavaDoc) component.getAttributes().get("target");
82         if (target != null) {
83             writer.writeAttribute("target", target, "target");
84         }
85         writer.endElement("base");
86         writer.writeText("\n", null);
87
88     }
89
90
91
92     // ------------------------------------------------------- Protected Methods
93

94
95     /**
96      * <p>Return <code>true</code> if this is a portlet request instance.
97      * NOTE: Implementation must not require portlet API classes to be
98      * present.</p>
99      *
100      * @param context <code>FacesContext</code> for the current request
101      */

102     protected boolean isPortletRequest(FacesContext context) {
103
104         Object JavaDoc request = context.getExternalContext().getRequest();
105         Class JavaDoc clazz = request.getClass();
106         while (clazz != null) {
107             // Does this class implement PortletRequest?
108
Class JavaDoc interfaces[] = clazz.getInterfaces();
109             if (interfaces == null) {
110                 interfaces = new Class JavaDoc[0];
111             }
112             for (int i = 0; i < interfaces.length; i++) {
113                 if ("javax.portlet.PortletRequest".equals
114                     (interfaces[i].getName())) {
115                     return (true);
116                 }
117             }
118             // Try our superclass (if any)
119
clazz = clazz.getSuperclass();
120         }
121         return (false);
122
123     }
124
125
126     /**
127      * <p>Return <code>true</code> if this is a servlet request instance.</p>
128      *
129      * @param context <code>FacesContext</code> for the current request
130      */

131     protected boolean isServletRequest(FacesContext context) {
132
133         Object JavaDoc request = context.getExternalContext().getRequest();
134         return (request instanceof HttpServletRequest JavaDoc);
135
136     }
137
138
139     /**
140      * <p>Return an absolute URI for the current page suitable for use
141      * in a portlet environment. NOTE: Implementation must not require
142      * portlet API classes to be present, so use reflection as needed.</p>
143      *
144      * @param context <code>FacesContext</code> for the current request
145      */

146     protected String JavaDoc portletUri(FacesContext context) {
147
148         Object JavaDoc request = context.getExternalContext().getRequest();
149         try {
150             String JavaDoc scheme = (String JavaDoc)
151                 MethodUtils.invokeMethod(request, "getScheme", null);
152             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(scheme);
153             sb.append("://");
154             sb.append(MethodUtils.invokeMethod(request, "getServerName", null));
155             Integer JavaDoc port = (Integer JavaDoc)
156                 MethodUtils.invokeMethod(request, "getServerPort", null);
157             if ("http".equals(scheme) && (port.intValue() == 80)) {
158                 ;
159             } else if ("https".equals(scheme) && (port.intValue() == 443)) {
160                 ;
161             } else {
162                 sb.append(":" + port);
163             }
164             sb.append
165                 (MethodUtils.invokeMethod(request, "getContextPath", null));
166             sb.append(context.getViewRoot().getViewId());
167             return (sb.toString());
168         } catch (InvocationTargetException JavaDoc e) {
169             throw new FacesException(e.getTargetException());
170         } catch (Exception JavaDoc e) {
171             throw new FacesException(e);
172         }
173
174     }
175
176
177     /**
178      * <p>Return an absolute URI for the current page suitable for use
179      * in a servlet environment.</p>
180      *
181      * @param context <code>FacesContext</code> for the current request
182      */

183     protected String JavaDoc servletUri(FacesContext context) {
184
185         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)
186             context.getExternalContext().getRequest();
187         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(request.getScheme());
188         sb.append("://");
189         sb.append(request.getServerName());
190         if ("http".equals(request.getScheme()) &&
191             (80 == request.getServerPort())) {
192             ;
193         } else if ("https".equals(request.getScheme()) &&
194                    (443 == request.getServerPort())) {
195             ;
196         } else {
197             sb.append(":" + request.getServerPort());
198         }
199         sb.append(request.getContextPath());
200         sb.append(context.getViewRoot().getViewId());
201         return (sb.toString());
202
203     }
204
205
206     /**
207      * <p>Return the absolute URI to be rendered as the value of the
208      * <code>href</code> attribute.</p>
209      *
210      * @param context <code>FacesContext</code> for the current request
211      */

212     protected String JavaDoc uri(FacesContext context) {
213
214         if (isServletRequest(context)) {
215             return (servletUri(context));
216         } else if (isPortletRequest(context)) {
217             return (portletUri(context));
218         } else {
219             throw new IllegalArgumentException JavaDoc
220                 ("Request is neither HttpServletRequest nor PortletRequest");
221         }
222
223     }
224
225
226 }
227
Popular Tags