Source Code Cross Referenced for CompressionFilter.java in  » Workflow-Engines » JFolder » compressionFilters » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Workflow Engines » JFolder » compressionFilters 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004 The Apache Software Foundation
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package compressionFilters;
018:
019:        import java.io.IOException;
020:        import java.util.Enumeration;
021:        import javax.servlet.Filter;
022:        import javax.servlet.FilterChain;
023:        import javax.servlet.FilterConfig;
024:        import javax.servlet.ServletException;
025:        import javax.servlet.ServletRequest;
026:        import javax.servlet.ServletResponse;
027:        import javax.servlet.http.HttpServletRequest;
028:        import javax.servlet.http.HttpServletResponse;
029:
030:        /**
031:         * Implementation of <code>javax.servlet.Filter</code> used to compress
032:         * the ServletResponse if it is bigger than a threshold.
033:         *
034:         * @author Amy Roh
035:         * @author Dmitri Valdin
036:         * @version $Revision: 1.2 $, $Date: 2004/03/18 16:40:33 $
037:         */
038:
039:        public class CompressionFilter implements  Filter {
040:
041:            /**
042:             * The filter configuration object we are associated with.  If this value
043:             * is null, this filter instance is not currently configured.
044:             */
045:            private FilterConfig config = null;
046:
047:            /**
048:             * Minimal reasonable threshold
049:             */
050:            private int minThreshold = 128;
051:
052:            /**
053:             * The threshold number to compress
054:             */
055:            protected int compressionThreshold;
056:
057:            /**
058:             * Debug level for this filter
059:             */
060:            private int debug = 0;
061:
062:            /**
063:             * Place this filter into service.
064:             *
065:             * @param filterConfig The filter configuration object
066:             */
067:
068:            public void init(FilterConfig filterConfig) {
069:
070:                config = filterConfig;
071:                if (filterConfig != null) {
072:                    String value = filterConfig.getInitParameter("debug");
073:                    if (value != null) {
074:                        debug = Integer.parseInt(value);
075:                    } else {
076:                        debug = 0;
077:                    }
078:                    String str = filterConfig
079:                            .getInitParameter("compressionThreshold");
080:                    if (str != null) {
081:                        compressionThreshold = Integer.parseInt(str);
082:                        if (compressionThreshold != 0
083:                                && compressionThreshold < minThreshold) {
084:                            if (debug > 0) {
085:                                System.out
086:                                        .println("compressionThreshold should be either 0 - no compression or >= "
087:                                                + minThreshold);
088:                                System.out
089:                                        .println("compressionThreshold set to "
090:                                                + minThreshold);
091:                            }
092:                            compressionThreshold = minThreshold;
093:                        }
094:                    } else {
095:                        compressionThreshold = 0;
096:                    }
097:
098:                } else {
099:                    compressionThreshold = 0;
100:                }
101:
102:            }
103:
104:            /**
105:             * Take this filter out of service.
106:             */
107:            public void destroy() {
108:
109:                this .config = null;
110:
111:            }
112:
113:            /**
114:             * The <code>doFilter</code> method of the Filter is called by the container
115:             * each time a request/response pair is passed through the chain due
116:             * to a client request for a resource at the end of the chain.
117:             * The FilterChain passed into this method allows the Filter to pass on the
118:             * request and response to the next entity in the chain.<p>
119:             * This method first examines the request to check whether the client support
120:             * compression. <br>
121:             * It simply just pass the request and response if there is no support for
122:             * compression.<br>
123:             * If the compression support is available, it creates a
124:             * CompressionServletResponseWrapper object which compresses the content and
125:             * modifies the header if the content length is big enough.
126:             * It then invokes the next entity in the chain using the FilterChain object
127:             * (<code>chain.doFilter()</code>), <br>
128:             **/
129:
130:            public void doFilter(ServletRequest request,
131:                    ServletResponse response, FilterChain chain)
132:                    throws IOException, ServletException {
133:
134:                if (debug > 0) {
135:                    System.out.println("@doFilter");
136:                }
137:
138:                if (compressionThreshold == 0) {
139:                    if (debug > 0) {
140:                        System.out
141:                                .println("doFilter gets called, but compressionTreshold is set to 0 - no compression");
142:                    }
143:                    chain.doFilter(request, response);
144:                    return;
145:                }
146:
147:                boolean supportCompression = false;
148:                if (request instanceof  HttpServletRequest) {
149:                    if (debug > 1) {
150:                        System.out.println("requestURI = "
151:                                + ((HttpServletRequest) request)
152:                                        .getRequestURI());
153:                    }
154:
155:                    // Are we allowed to compress ?
156:                    String s = (String) ((HttpServletRequest) request)
157:                            .getParameter("gzip");
158:                    if ("false".equals(s)) {
159:                        if (debug > 0) {
160:                            System.out
161:                                    .println("got parameter gzip=false --> don't compress, just chain filter");
162:                        }
163:                        chain.doFilter(request, response);
164:                        return;
165:                    }
166:
167:                    Enumeration e = ((HttpServletRequest) request)
168:                            .getHeaders("Accept-Encoding");
169:                    while (e.hasMoreElements()) {
170:                        String name = (String) e.nextElement();
171:                        if (name.indexOf("gzip") != -1) {
172:                            if (debug > 0) {
173:                                System.out.println("supports compression");
174:                            }
175:                            supportCompression = true;
176:                        } else {
177:                            if (debug > 0) {
178:                                System.out.println("no support for compresion");
179:                            }
180:                        }
181:                    }
182:                }
183:
184:                if (!supportCompression) {
185:                    if (debug > 0) {
186:                        System.out
187:                                .println("doFilter gets called wo compression");
188:                    }
189:                    chain.doFilter(request, response);
190:                    return;
191:                } else {
192:                    if (response instanceof  HttpServletResponse) {
193:                        CompressionServletResponseWrapper wrappedResponse = new CompressionServletResponseWrapper(
194:                                (HttpServletResponse) response);
195:                        wrappedResponse.setDebugLevel(debug);
196:                        wrappedResponse
197:                                .setCompressionThreshold(compressionThreshold);
198:                        if (debug > 0) {
199:                            System.out
200:                                    .println("doFilter gets called with compression");
201:                        }
202:                        try {
203:                            chain.doFilter(request, wrappedResponse);
204:                        } finally {
205:                            wrappedResponse.finishResponse();
206:                        }
207:                        return;
208:                    }
209:                }
210:            }
211:
212:            /**
213:             * Set filter config
214:             * This function is equivalent to init. Required by Weblogic 6.1
215:             *
216:             * @param filterConfig The filter configuration object
217:             */
218:            public void setFilterConfig(FilterConfig filterConfig) {
219:                init(filterConfig);
220:            }
221:
222:            /**
223:             * Return filter config
224:             * Required by Weblogic 6.1
225:             */
226:            public FilterConfig getFilterConfig() {
227:                return config;
228:            }
229:
230:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.