1 18 package org.apache.beehive.netui.pageflow.internal; 19 20 import javax.servlet.ServletContext ; 21 import javax.servlet.ServletRequest ; 22 import javax.servlet.ServletResponse ; 23 import javax.servlet.http.HttpServletRequest ; 24 25 import org.apache.beehive.netui.core.urls.MutableURI; 26 import org.apache.beehive.netui.core.urls.URLRewriter; 27 import org.apache.beehive.netui.core.urls.URLType; 28 import org.apache.beehive.netui.pageflow.ServletContainerAdapter; 29 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils; 30 import org.apache.beehive.netui.util.logging.Logger; 31 32 33 public class DefaultURLRewriter extends URLRewriter 34 { 35 private static final Logger _log = Logger.getInstance( DefaultURLRewriter.class ); 36 37 public String getNamePrefix( ServletContext servletContext, ServletRequest request, String name ) 38 { 39 return ""; 40 } 41 42 public void rewriteURL( ServletContext servletContext, ServletRequest request, 43 ServletResponse response, MutableURI url, URLType type, boolean needsToBeSecure ) 44 { 45 ServletContainerAdapter servletContainerAdapter = AdapterManager.getServletContainerAdapter( servletContext ); 46 47 if ( !url.isAbsolute() ) 49 { 50 if ( needsToBeSecure ) 51 { 52 if ( !request.isSecure() ) 53 { 54 int securePort = servletContainerAdapter.getSecureListenPort( (HttpServletRequest ) request ); 55 56 if (securePort != -1) 57 { 58 internalRewriteUrl( url, "https", securePort, request.getServerName() ); 59 } 60 else 61 { 62 if (_log.isWarnEnabled()) 63 { 64 _log.warn("Could not rewrite URL " + url.getURIString( null ) + " to be secure because" + 65 " a secure port was not provided by the ServletContainerAdapter."); 66 } 67 } 68 } 69 } 70 else 71 { 72 if ( request.isSecure() ) 73 { 74 int listenPort = servletContainerAdapter.getListenPort((HttpServletRequest ) request); 75 76 if (listenPort != -1 ) 77 { 78 internalRewriteUrl( url, "http", listenPort, request.getServerName() ); 79 } 80 else 81 { 82 if (_log.isWarnEnabled()) 83 { 84 _log.warn("Could not rewrite URL " + url.getURIString( null ) + " to be non-secure" + 85 " because a port was not provided by the ServletContainerAdapter."); 86 } 87 } 88 } 89 } 90 } 91 92 String scopeID = request.getParameter( ScopedServletUtils.SCOPE_ID_PARAM ); 97 if ( scopeID != null ) 98 { 99 if ( url.getParameter( ScopedServletUtils.SCOPE_ID_PARAM ) == null ) 101 { 102 url.addParameter( ScopedServletUtils.SCOPE_ID_PARAM, scopeID, true ); 103 } 104 } 105 } 106 107 private static void internalRewriteUrl( MutableURI url, String protocol, int port, String serverName ) 108 { 109 url.setScheme( protocol ); 111 url.setHost( serverName ); 112 url.setPort( port ); 113 } 114 115 122 123 public boolean equals( Object object ) 124 { 125 if ( object == this ) { return true; } 126 127 if ( object == null || !object.getClass().equals( this.getClass() ) ) 128 { 129 return false; 130 } 131 132 return true; 133 } 134 135 143 144 public int hashCode() 145 { 146 return 0; 147 } 148 } 149 | Popular Tags |