1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.struts.faces.renderer;
18
19
20 import java.io.IOException;
21 import java.lang.reflect.InvocationTargetException;
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;
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: 421138 $ $Date: 2006-07-11 22:41:40 -0700 (Tue, 11 Jul 2006) $
39 */
40
41 public class BaseRenderer extends AbstractRenderer {
42
43
44
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
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 {
68
69 if ((context == null) || (component == null)) {
70 throw new NullPointerException();
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 target = (String) 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
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 request = context.getExternalContext().getRequest();
105 Class clazz = request.getClass();
106 while (clazz != null) {
107
108 Class interfaces[] = clazz.getInterfaces();
109 if (interfaces == null) {
110 interfaces = new Class[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
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 request = context.getExternalContext().getRequest();
134 return (request instanceof HttpServletRequest);
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 portletUri(FacesContext context) {
147
148 Object request = context.getExternalContext().getRequest();
149 try {
150 String scheme = (String)
151 MethodUtils.invokeMethod(request, "getScheme", null);
152 StringBuffer sb = new StringBuffer(scheme);
153 sb.append("://");
154 sb.append(MethodUtils.invokeMethod(request, "getServerName", null));
155 Integer port = (Integer)
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 e) {
169 throw new FacesException(e.getTargetException());
170 } catch (Exception 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 servletUri(FacesContext context) {
184
185 HttpServletRequest request = (HttpServletRequest)
186 context.getExternalContext().getRequest();
187 StringBuffer sb = new StringBuffer(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 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
220 ("Request is neither HttpServletRequest nor PortletRequest");
221 }
222
223 }
224
225
226 }