001: /*
002: * $Id: BaseRenderer.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.faces.renderer;
023:
024: import java.io.IOException;
025: import java.lang.reflect.InvocationTargetException;
026:
027: import javax.faces.FacesException;
028: import javax.faces.component.UIComponent;
029: import javax.faces.context.FacesContext;
030: import javax.faces.context.ResponseWriter;
031: import javax.servlet.http.HttpServletRequest;
032:
033: import org.apache.commons.beanutils.MethodUtils;
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: /**
038: * <p><code>Renderer</code> implementation for the <code>base</code> tag
039: * from the <em>Struts-Faces Integration Library</em>.</p>
040: *
041: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
042: */
043:
044: public class BaseRenderer extends AbstractRenderer {
045:
046: // -------------------------------------------------------- Static Variables
047:
048: /**
049: * <p>The <code>Log</code> instance for this class.</p>
050: */
051: private static Log log = LogFactory.getLog(BaseRenderer.class);
052:
053: // ---------------------------------------------------------- Public Methods
054:
055: /**
056: * <p>Render an HTML <code>base</code> element.</p>
057: *
058: * @param context FacesContext for the request we are processing
059: * @param component UIComponent to be rendered
060: *
061: * @exception IOException if an input/output error occurs while rendering
062: * @exception NullPointerException if <code>context</code>
063: * or <code>component</code> is null
064: */
065: public void encodeEnd(FacesContext context, UIComponent component)
066: throws IOException {
067:
068: if ((context == null) || (component == null)) {
069: throw new NullPointerException();
070: }
071:
072: if (log.isTraceEnabled()) {
073: log.trace("viewId='" + context.getViewRoot().getViewId()
074: + "' --> uri='" + uri(context) + "'");
075: }
076:
077: ResponseWriter writer = context.getResponseWriter();
078: writer.startElement("base", component);
079: writer.writeURIAttribute("href", uri(context), null);
080: String target = (String) component.getAttributes()
081: .get("target");
082: if (target != null) {
083: writer.writeAttribute("target", target, "target");
084: }
085: writer.endElement("base");
086: writer.writeText("\n", null);
087:
088: }
089:
090: // ------------------------------------------------------- Protected Methods
091:
092: /**
093: * <p>Return <code>true</code> if this is a portlet request instance.
094: * NOTE: Implementation must not require portlet API classes to be
095: * present.</p>
096: *
097: * @param context <code>FacesContext</code> for the current request
098: */
099: protected boolean isPortletRequest(FacesContext context) {
100:
101: Object request = context.getExternalContext().getRequest();
102: Class clazz = request.getClass();
103: while (clazz != null) {
104: // Does this class implement PortletRequest?
105: Class interfaces[] = clazz.getInterfaces();
106: if (interfaces == null) {
107: interfaces = new Class[0];
108: }
109: for (int i = 0; i < interfaces.length; i++) {
110: if ("javax.portlet.PortletRequest".equals(interfaces[i]
111: .getName())) {
112: return (true);
113: }
114: }
115: // Try our superclass (if any)
116: clazz = clazz.getSuperclass();
117: }
118: return (false);
119:
120: }
121:
122: /**
123: * <p>Return <code>true</code> if this is a servlet request instance.</p>
124: *
125: * @param context <code>FacesContext</code> for the current request
126: */
127: protected boolean isServletRequest(FacesContext context) {
128:
129: Object request = context.getExternalContext().getRequest();
130: return (request instanceof HttpServletRequest);
131:
132: }
133:
134: /**
135: * <p>Return an absolute URI for the current page suitable for use
136: * in a portlet environment. NOTE: Implementation must not require
137: * portlet API classes to be present, so use reflection as needed.</p>
138: *
139: * @param context <code>FacesContext</code> for the current request
140: */
141: protected String portletUri(FacesContext context) {
142:
143: Object request = context.getExternalContext().getRequest();
144: try {
145: String scheme = (String) MethodUtils.invokeMethod(request,
146: "getScheme", null);
147: StringBuffer sb = new StringBuffer(scheme);
148: sb.append("://");
149: sb.append(MethodUtils.invokeMethod(request,
150: "getServerName", null));
151: Integer port = (Integer) MethodUtils.invokeMethod(request,
152: "getServerPort", null);
153: if ("http".equals(scheme) && (port.intValue() == 80)) {
154: ;
155: } else if ("https".equals(scheme)
156: && (port.intValue() == 443)) {
157: ;
158: } else {
159: sb.append(":" + port);
160: }
161: sb.append(MethodUtils.invokeMethod(request,
162: "getContextPath", null));
163: sb.append(context.getViewRoot().getViewId());
164: return (sb.toString());
165: } catch (InvocationTargetException e) {
166: throw new FacesException(e.getTargetException());
167: } catch (Exception e) {
168: throw new FacesException(e);
169: }
170:
171: }
172:
173: /**
174: * <p>Return an absolute URI for the current page suitable for use
175: * in a servlet environment.</p>
176: *
177: * @param context <code>FacesContext</code> for the current request
178: */
179: protected String servletUri(FacesContext context) {
180:
181: HttpServletRequest request = (HttpServletRequest) context
182: .getExternalContext().getRequest();
183: StringBuffer sb = new StringBuffer(request.getScheme());
184: sb.append("://");
185: sb.append(request.getServerName());
186: if ("http".equals(request.getScheme())
187: && (80 == request.getServerPort())) {
188: ;
189: } else if ("https".equals(request.getScheme())
190: && (443 == request.getServerPort())) {
191: ;
192: } else {
193: sb.append(":" + request.getServerPort());
194: }
195: sb.append(request.getContextPath());
196: sb.append(context.getViewRoot().getViewId());
197: return (sb.toString());
198:
199: }
200:
201: /**
202: * <p>Return the absolute URI to be rendered as the value of the
203: * <code>href</code> attribute.</p>
204: *
205: * @param context <code>FacesContext</code> for the current request
206: */
207: protected String uri(FacesContext context) {
208:
209: if (isServletRequest(context)) {
210: return (servletUri(context));
211: } else if (isPortletRequest(context)) {
212: return (portletUri(context));
213: } else {
214: throw new IllegalArgumentException(
215: "Request is neither HttpServletRequest nor PortletRequest");
216: }
217:
218: }
219:
220: }
|