All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.struts.faces.renderer.BaseRenderer Maven / Gradle / Ivy

There is a newer version: 1.3.8
Show newest version
/*
 * $Id: BaseRenderer.java 471754 2006-11-06 14:55:09Z husted $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.struts.faces.renderer;


import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * 

Renderer implementation for the base tag * from the Struts-Faces Integration Library.

* * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $ */ public class BaseRenderer extends AbstractRenderer { // -------------------------------------------------------- Static Variables /** *

The Log instance for this class.

*/ private static Log log = LogFactory.getLog(BaseRenderer.class); // ---------------------------------------------------------- Public Methods /** *

Render an HTML base element.

* * @param context FacesContext for the request we are processing * @param component UIComponent to be rendered * * @exception IOException if an input/output error occurs while rendering * @exception NullPointerException if context * or component is null */ public void encodeEnd(FacesContext context, UIComponent component) throws IOException { if ((context == null) || (component == null)) { throw new NullPointerException(); } if (log.isTraceEnabled()) { log.trace("viewId='" + context.getViewRoot().getViewId() + "' --> uri='" + uri(context) + "'"); } ResponseWriter writer = context.getResponseWriter(); writer.startElement("base", component); writer.writeURIAttribute("href", uri(context), null); String target = (String) component.getAttributes().get("target"); if (target != null) { writer.writeAttribute("target", target, "target"); } writer.endElement("base"); writer.writeText("\n", null); } // ------------------------------------------------------- Protected Methods /** *

Return true if this is a portlet request instance. * NOTE: Implementation must not require portlet API classes to be * present.

* * @param context FacesContext for the current request */ protected boolean isPortletRequest(FacesContext context) { Object request = context.getExternalContext().getRequest(); Class clazz = request.getClass(); while (clazz != null) { // Does this class implement PortletRequest? Class interfaces[] = clazz.getInterfaces(); if (interfaces == null) { interfaces = new Class[0]; } for (int i = 0; i < interfaces.length; i++) { if ("javax.portlet.PortletRequest".equals (interfaces[i].getName())) { return (true); } } // Try our superclass (if any) clazz = clazz.getSuperclass(); } return (false); } /** *

Return true if this is a servlet request instance.

* * @param context FacesContext for the current request */ protected boolean isServletRequest(FacesContext context) { Object request = context.getExternalContext().getRequest(); return (request instanceof HttpServletRequest); } /** *

Return an absolute URI for the current page suitable for use * in a portlet environment. NOTE: Implementation must not require * portlet API classes to be present, so use reflection as needed.

* * @param context FacesContext for the current request */ protected String portletUri(FacesContext context) { Object request = context.getExternalContext().getRequest(); try { String scheme = (String) MethodUtils.invokeMethod(request, "getScheme", null); StringBuffer sb = new StringBuffer(scheme); sb.append("://"); sb.append(MethodUtils.invokeMethod(request, "getServerName", null)); Integer port = (Integer) MethodUtils.invokeMethod(request, "getServerPort", null); if ("http".equals(scheme) && (port.intValue() == 80)) { ; } else if ("https".equals(scheme) && (port.intValue() == 443)) { ; } else { sb.append(":" + port); } sb.append (MethodUtils.invokeMethod(request, "getContextPath", null)); sb.append(context.getViewRoot().getViewId()); return (sb.toString()); } catch (InvocationTargetException e) { throw new FacesException(e.getTargetException()); } catch (Exception e) { throw new FacesException(e); } } /** *

Return an absolute URI for the current page suitable for use * in a servlet environment.

* * @param context FacesContext for the current request */ protected String servletUri(FacesContext context) { HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest(); StringBuffer sb = new StringBuffer(request.getScheme()); sb.append("://"); sb.append(request.getServerName()); if ("http".equals(request.getScheme()) && (80 == request.getServerPort())) { ; } else if ("https".equals(request.getScheme()) && (443 == request.getServerPort())) { ; } else { sb.append(":" + request.getServerPort()); } sb.append(request.getContextPath()); sb.append(context.getViewRoot().getViewId()); return (sb.toString()); } /** *

Return the absolute URI to be rendered as the value of the * href attribute.

* * @param context FacesContext for the current request */ protected String uri(FacesContext context) { if (isServletRequest(context)) { return (servletUri(context)); } else if (isPortletRequest(context)) { return (portletUri(context)); } else { throw new IllegalArgumentException ("Request is neither HttpServletRequest nor PortletRequest"); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy