/// <summary>Resolves the requester hostname and delegates the request to the chain.</summary> /// <remarks> /// Resolves the requester hostname and delegates the request to the chain. /// <p> /// The requester hostname is available via the /// <see cref="Get()"/> /// method. /// </remarks> /// <param name="request">servlet request.</param> /// <param name="response">servlet response.</param> /// <param name="chain">filter chain.</param> /// <exception cref="System.IO.IOException">thrown if an IO error occurrs.</exception> /// <exception cref="Javax.Servlet.ServletException">thrown if a servet error occurrs. /// </exception> public virtual void DoFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { string hostname; try { string address = request.GetRemoteAddr(); if (address != null) { hostname = Sharpen.Extensions.GetAddressByName(address).ToString(); } else { log.Warn("Request remote address is NULL"); hostname = "???"; } } catch (UnknownHostException ex) { log.Warn("Request remote address could not be resolved, {0}", ex.ToString(), ex); hostname = "???"; } HostnameTl.Set(hostname); chain.DoFilter(request, response); } finally { HostnameTl.Remove(); } }
public virtual void TestMissingHostname() { ServletRequest request = Org.Mockito.Mockito.Mock <ServletRequest>(); Org.Mockito.Mockito.When(request.GetRemoteAddr()).ThenReturn(null); ServletResponse response = Org.Mockito.Mockito.Mock <ServletResponse>(); AtomicBoolean invoked = new AtomicBoolean(); FilterChain chain = new _FilterChain_79(invoked); Filter filter = new HostnameFilter(); filter.Init(null); NUnit.Framework.Assert.IsNull(HostnameFilter.Get()); filter.DoFilter(request, response, chain); NUnit.Framework.Assert.IsTrue(invoked.Get()); NUnit.Framework.Assert.IsNull(HostnameFilter.Get()); filter.Destroy(); }
public virtual void Hostname() { ServletRequest request = Org.Mockito.Mockito.Mock <ServletRequest>(); Org.Mockito.Mockito.When(request.GetRemoteAddr()).ThenReturn("localhost"); ServletResponse response = Org.Mockito.Mockito.Mock <ServletResponse>(); AtomicBoolean invoked = new AtomicBoolean(); FilterChain chain = new _FilterChain_49(invoked); // Hostname was set to "localhost", but may get resolved automatically to // "127.0.0.1" depending on OS. Filter filter = new HostnameFilter(); filter.Init(null); NUnit.Framework.Assert.IsNull(HostnameFilter.Get()); filter.DoFilter(request, response, chain); NUnit.Framework.Assert.IsTrue(invoked.Get()); NUnit.Framework.Assert.IsNull(HostnameFilter.Get()); filter.Destroy(); }