1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts.faces.renderer;
23
24
25 import java.io.IOException;
26 import java.lang.reflect.InvocationTargetException;
27
28 import javax.faces.FacesException;
29 import javax.faces.component.UIComponent;
30 import javax.faces.context.FacesContext;
31 import javax.faces.context.ResponseWriter;
32 import javax.servlet.http.HttpServletRequest;
33
34 import org.apache.commons.beanutils.MethodUtils;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38
39
40
41
42
43
44
45
46 public class BaseRenderer extends AbstractRenderer {
47
48
49
50
51
52
53
54
55 private static Log log = LogFactory.getLog(BaseRenderer.class);
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public void encodeEnd(FacesContext context, UIComponent component)
72 throws IOException {
73
74 if ((context == null) || (component == null)) {
75 throw new NullPointerException();
76 }
77
78 if (log.isTraceEnabled()) {
79 log.trace("viewId='" + context.getViewRoot().getViewId() +
80 "' --> uri='" + uri(context) + "'");
81 }
82
83 ResponseWriter writer = context.getResponseWriter();
84 writer.startElement("base", component);
85 writer.writeURIAttribute("href", uri(context), null);
86 String target = (String) component.getAttributes().get("target");
87 if (target != null) {
88 writer.writeAttribute("target", target, "target");
89 }
90 writer.endElement("base");
91 writer.writeText("\n", null);
92
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107 protected boolean isPortletRequest(FacesContext context) {
108
109 Object request = context.getExternalContext().getRequest();
110 Class clazz = request.getClass();
111 while (clazz != null) {
112
113 Class interfaces[] = clazz.getInterfaces();
114 if (interfaces == null) {
115 interfaces = new Class[0];
116 }
117 for (int i = 0; i < interfaces.length; i++) {
118 if ("javax.portlet.PortletRequest".equals
119 (interfaces[i].getName())) {
120 return (true);
121 }
122 }
123
124 clazz = clazz.getSuperclass();
125 }
126 return (false);
127
128 }
129
130
131
132
133
134
135
136 protected boolean isServletRequest(FacesContext context) {
137
138 Object request = context.getExternalContext().getRequest();
139 return (request instanceof HttpServletRequest);
140
141 }
142
143
144
145
146
147
148
149
150
151 protected String portletUri(FacesContext context) {
152
153 Object request = context.getExternalContext().getRequest();
154 try {
155 String scheme = (String)
156 MethodUtils.invokeMethod(request, "getScheme", null);
157 StringBuffer sb = new StringBuffer(scheme);
158 sb.append("://");
159 sb.append(MethodUtils.invokeMethod(request, "getServerName", null));
160 Integer port = (Integer)
161 MethodUtils.invokeMethod(request, "getServerPort", null);
162 if ("http".equals(scheme) && (port.intValue() == 80)) {
163 ;
164 } else if ("https".equals(scheme) && (port.intValue() == 443)) {
165 ;
166 } else {
167 sb.append(":" + port);
168 }
169 sb.append
170 (MethodUtils.invokeMethod(request, "getContextPath", null));
171 sb.append(context.getViewRoot().getViewId());
172 return (sb.toString());
173 } catch (InvocationTargetException e) {
174 throw new FacesException(e.getTargetException());
175 } catch (Exception e) {
176 throw new FacesException(e);
177 }
178
179 }
180
181
182
183
184
185
186
187
188 protected String servletUri(FacesContext context) {
189
190 HttpServletRequest request = (HttpServletRequest)
191 context.getExternalContext().getRequest();
192 StringBuffer sb = new StringBuffer(request.getScheme());
193 sb.append("://");
194 sb.append(request.getServerName());
195 if ("http".equals(request.getScheme()) &&
196 (80 == request.getServerPort())) {
197 ;
198 } else if ("https".equals(request.getScheme()) &&
199 (443 == request.getServerPort())) {
200 ;
201 } else {
202 sb.append(":" + request.getServerPort());
203 }
204 sb.append(request.getContextPath());
205 sb.append(context.getViewRoot().getViewId());
206 return (sb.toString());
207
208 }
209
210
211
212
213
214
215
216
217 protected String uri(FacesContext context) {
218
219 if (isServletRequest(context)) {
220 return (servletUri(context));
221 } else if (isPortletRequest(context)) {
222 return (portletUri(context));
223 } else {
224 throw new IllegalArgumentException
225 ("Request is neither HttpServletRequest nor PortletRequest");
226 }
227
228 }
229
230
231 }