KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > filters > EnhydraPortFilter


1 package org.enhydra.filters;
2
3 import java.io.IOException JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5
6 import javax.servlet.Filter JavaDoc;
7 import javax.servlet.FilterChain JavaDoc;
8 import javax.servlet.FilterConfig JavaDoc;
9 import javax.servlet.ServletException JavaDoc;
10 import javax.servlet.ServletRequest JavaDoc;
11 import javax.servlet.ServletResponse JavaDoc;
12
13 import org.enhydra.server.AppInfo;
14 import org.enhydra.server.EnhydraServer;
15
16 /**
17  * Filter that reads list of server ports on which servlet
18  * should not respond from config file (given as parameter) and disables
19  * application servlets to run on that ports.
20  *
21  * @author Strahinja Videnovic
22  * @version $Revision: 1.2 $ $Date: 2005/03/24 10:51:25 $
23  */

24
25 public class EnhydraPortFilter implements Filter JavaDoc {
26
27     /**
28      * The application name
29      */

30     private String JavaDoc appName = null;
31
32     /**
33      * The filter configuration object we are associated with. If this value
34      * is null, this filter instance is not currently configured.
35      */

36     private FilterConfig JavaDoc filterConfig = null;
37
38     /**
39      * Instance of server class which holds data about enabled ports
40      */

41     private EnhydraServer enhydraServer = null;
42
43     
44     private String JavaDoc[] enabledPorts = null;
45     
46
47     /**
48      * Reads port from request and gets list of ports from init file.
49      * In case that port from request eqauls some port from the list,
50      * an "Access not allowed" page is generated, and no following filters
51      * are executed.
52      *
53      * @param request The servlet request we are processing
54      * @param result The servlet response we are creating
55      * @param chain The filter chain we are processing
56      *
57      * @exception IOException if an input/output error occurs
58      * @exception ServletException if a servlet error occurs
59      */

60     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
61          FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
62
63         String JavaDoc port = String.valueOf(request.getServerPort());
64         
65         boolean enabled = false;
66         //if EnhydraServer not started enable connection (case when enhydra application is running under unknown Servlet Container) 11.04.2003
67
if(!enhydraServer.isStarted()){
68             chain.doFilter(request, response);
69             return;
70         }
71         String JavaDoc[] 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 JavaDoc out = response.getWriter();
87             String JavaDoc 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     /**
101      * Place this filter into service.
102      *
103      * @param filterConfig The filter configuration object
104      */

105     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
106
107         this.filterConfig = filterConfig;
108         String JavaDoc urlContextPath = this.filterConfig.getServletContext().getRealPath("").replace('\\','/');
109
110         this.enhydraServer = EnhydraServer.getInstance();
111         //28.03.2003
112
String JavaDoc[] 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)){ // found app
118
this.appName = appInfo.getName();
119                     break;
120                 }
121             }
122         }
123     }
124
125     /**
126      * Take this filter out of service.
127      */

128     public void destroy() {
129
130         this.appName = null;
131         this.filterConfig = null;
132
133     }
134
135 }
Popular Tags