1 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 40 41 public class BaseRenderer extends AbstractRenderer { 42 43 44 46 47 50 private static Log log = LogFactory.getLog(BaseRenderer.class); 51 52 53 55 56 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 94 95 102 protected boolean isPortletRequest(FacesContext context) { 103 104 Object request = context.getExternalContext().getRequest(); 105 Class clazz = request.getClass(); 106 while (clazz != null) { 107 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 clazz = clazz.getSuperclass(); 120 } 121 return (false); 122 123 } 124 125 126 131 protected boolean isServletRequest(FacesContext context) { 132 133 Object request = context.getExternalContext().getRequest(); 134 return (request instanceof HttpServletRequest ); 135 136 } 137 138 139 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 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 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 } 227 | Popular Tags |