001: package org.enhydra.filters;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005:
006: import javax.servlet.Filter;
007: import javax.servlet.FilterChain;
008: import javax.servlet.FilterConfig;
009: import javax.servlet.ServletException;
010: import javax.servlet.ServletRequest;
011: import javax.servlet.ServletResponse;
012:
013: import org.enhydra.server.AppInfo;
014: import org.enhydra.server.EnhydraServer;
015:
016: /**
017: * Filter that reads list of server ports on which servlet
018: * should not respond from config file (given as parameter) and disables
019: * application servlets to run on that ports.
020: *
021: * @author Strahinja Videnovic
022: * @version $Revision: 1.3 $ $Date: 2007-10-19 10:05:39 $
023: */
024:
025: public class EnhydraPortFilter implements Filter {
026:
027: /**
028: * The application name
029: */
030: private String appName = null;
031:
032: /**
033: * The filter configuration object we are associated with. If this value
034: * is null, this filter instance is not currently configured.
035: */
036: private FilterConfig filterConfig = null;
037:
038: /**
039: * Instance of server class which holds data about enabled ports
040: */
041: private EnhydraServer enhydraServer = null;
042:
043: // private String[] enabledPorts = null;
044:
045: /**
046: * Reads port from request and gets list of ports from init file.
047: * In case that port from request eqauls some port from the list,
048: * an "Access not allowed" page is generated, and no following filters
049: * are executed.
050: *
051: * @param request The servlet request we are processing
052: * @param result The servlet response we are creating
053: * @param chain The filter chain we are processing
054: *
055: * @exception IOException if an input/output error occurs
056: * @exception ServletException if a servlet error occurs
057: */
058: public void doFilter(ServletRequest request,
059: ServletResponse response, FilterChain chain)
060: throws IOException, ServletException {
061:
062: String port = String.valueOf(request.getServerPort());
063:
064: boolean enabled = false;
065: //if EnhydraServer not started enable connection (case when enhydra application is running under unknown Servlet Container) 11.04.2003
066: if (!enhydraServer.isStarted()) {
067: chain.doFilter(request, response);
068: return;
069: }
070: String[] enabledPorts = enhydraServer
071: .getEnabledConnections(appName);
072:
073: if (enabledPorts != null) {
074: for (int i = 0; i < enabledPorts.length; i++) {
075:
076: if (enabledPorts[i].equals(port)) {
077: enabled = true;
078: break;
079: }
080: }
081: }
082:
083: if (enabled)
084: chain.doFilter(request, response);
085: else {
086: PrintWriter out = response.getWriter();
087: String title = "Access not allowed";
088: out
089: .write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
090: + "Transitional//EN\">"
091: + "\n"
092: + "<HTML>\n"
093: + "<HEAD><TITLE>"
094: + title
095: + "</TITLE></HEAD>\n"
096: + "<BODY BGCOLOR=\"#FDF5E6\">\n"
097: + "<H1>"
098: + title
099: + "</H1>\n"
100: + "<I>Port <B>"
101: + port
102: + "</B> is not a valid channel for this application.</I>\n"
103: + "</BODY></HTML>");
104: out.close();
105: }
106: }
107:
108: /**
109: * Place this filter into service.
110: *
111: * @param filterConfig The filter configuration object
112: */
113: public void init(FilterConfig filterConfig) throws ServletException {
114:
115: this .filterConfig = filterConfig;
116: String urlContextPath = this .filterConfig.getServletContext()
117: .getRealPath("").replace('\\', '/');
118:
119: this .enhydraServer = EnhydraServer.getInstance();
120: //28.03.2003
121: String[] appNames = this .enhydraServer.getAppNames();
122: AppInfo appInfo = null;
123: if (appNames != null) {
124: for (int i = 0; i < appNames.length; i++) {
125: appInfo = this .enhydraServer.getAppInfo(appNames[i]);
126: if (appInfo.getUrlPath().equalsIgnoreCase(
127: urlContextPath)) { // found app
128: this .appName = appInfo.getName();
129: break;
130: }
131: }
132: }
133: }
134:
135: /**
136: * Take this filter out of service.
137: */
138: public void destroy() {
139:
140: this.appName = null;
141: this.filterConfig = null;
142:
143: }
144:
145: }
|