1 package org.enhydra.filters; 2 3 import java.io.IOException ; 4 import java.io.PrintWriter ; 5 6 import javax.servlet.Filter ; 7 import javax.servlet.FilterChain ; 8 import javax.servlet.FilterConfig ; 9 import javax.servlet.ServletException ; 10 import javax.servlet.ServletRequest ; 11 import javax.servlet.ServletResponse ; 12 13 import org.enhydra.server.AppInfo; 14 import org.enhydra.server.EnhydraServer; 15 16 24 25 public class EnhydraPortFilter implements Filter { 26 27 30 private String appName = null; 31 32 36 private FilterConfig filterConfig = null; 37 38 41 private EnhydraServer enhydraServer = null; 42 43 44 private String [] enabledPorts = null; 45 46 47 60 public void doFilter(ServletRequest request, ServletResponse response, 61 FilterChain chain) throws IOException , ServletException { 62 63 String port = String.valueOf(request.getServerPort()); 64 65 boolean enabled = false; 66 if(!enhydraServer.isStarted()){ 68 chain.doFilter(request, response); 69 return; 70 } 71 String [] enabledPorts = enhydraServer.getEnabledConnections(appName); 72 73 if (enabledPorts != null) { 74 for (int i=0; i<enabledPorts.length; i++) { 75 76 if (enabledPorts[i].equals(port)) { 77 enabled = true; 78 break; 79 } 80 } 81 } 82 83 if (enabled) 84 chain.doFilter(request, response); 85 else { 86 PrintWriter out = response.getWriter(); 87 String title = "Access not allowed"; 88 out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + 89 "Transitional//EN\">" + "\n" + 90 "<HTML>\n" + 91 "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n"+ 92 "<BODY BGCOLOR=\"#FDF5E6\">\n" + 93 "<H1>" + title + "</H1>\n" + 94 "<I>Port <B>" + port + "</B> is not a valid channel for this application.</I>\n" + 95 "</BODY></HTML>"); 96 out.close(); 97 } 98 } 99 100 105 public void init(FilterConfig filterConfig) throws ServletException { 106 107 this.filterConfig = filterConfig; 108 String urlContextPath = this.filterConfig.getServletContext().getRealPath("").replace('\\','/'); 109 110 this.enhydraServer = EnhydraServer.getInstance(); 111 String [] appNames = this.enhydraServer.getAppNames(); 113 AppInfo appInfo = null; 114 if(appNames != null){ 115 for(int i=0; i<appNames.length; i++){ 116 appInfo = this.enhydraServer.getAppInfo(appNames[i]); 117 if(appInfo.getUrlPath().equalsIgnoreCase(urlContextPath)){ this.appName = appInfo.getName(); 119 break; 120 } 121 } 122 } 123 } 124 125 128 public void destroy() { 129 130 this.appName = null; 131 this.filterConfig = null; 132 133 } 134 135 } | Popular Tags |