KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > session > SmartURLsFilter


1 /* $Id: SmartURLsFilter.java,v 1.5 2005/01/28 14:47:51 blueshift Exp $ */
2 /*
3  * $Id: SmartURLsFilter.java,v 1.5 2005/01/28 14:47:51 blueshift Exp $
4  * Copyright 2000,2005 wingS development team.
5  *
6  * This file is part of wingS (http://www.j-wings.org).
7  *
8  * wingS is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1
11  * of the License, or (at your option) any later version.
12  *
13  * Please see COPYING for the complete licence.
14  */

15 package org.wings.session;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20 import javax.servlet.*;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.nio.CharBuffer JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.regex.Matcher JavaDoc;
33 import java.util.regex.Pattern JavaDoc;
34
35 /**
36  * @author hengels
37  */

38 public class SmartURLsFilter
39         implements Filter {
40     private final transient static Log log = LogFactory.getLog(SmartURLsFilter.class);
41     private String JavaDoc parameterSeparator = ";";
42     private String JavaDoc nameValueSeparator = ",";
43     private Pattern JavaDoc encodePattern;
44     private Pattern JavaDoc decodePattern;
45
46     public void init(FilterConfig filterConfig) throws ServletException {
47         if (filterConfig.getInitParameter("wings.servlet.smarturls.parameterSeparator") != null)
48             parameterSeparator = filterConfig.getInitParameter("wings.servlet.smarturls.parameterSeparator");
49
50         if (filterConfig.getInitParameter("wings.servlet.smarturls.nameValueSeparator") != null)
51             nameValueSeparator = filterConfig.getInitParameter("wings.servlet.smarturls.nameValueSeparator");
52
53         log.info("wings.servlet.smarturls.parameterSeparator " + parameterSeparator);
54         log.info("wings.servlet.smarturls.nameValueSeparator " + nameValueSeparator);
55
56         encodePattern = Pattern.compile("(" + "\\?|&" + ")([a-zA-Z0-9%+.-[*]_]*)" +
57                 "(" + "=" + ")([a-zA-Z0-9%+.-[*]_=/:]*)");
58         decodePattern = Pattern.compile("(" + parameterSeparator + ")([a-zA-Z0-9%+.-[*]_]*)" +
59                 "(" + nameValueSeparator + ")([a-zA-Z0-9%+.-[*]_=/:]*)");
60     }
61
62     public void doFilter(ServletRequest JavaDoc servletRequest, ServletResponse JavaDoc servletResponse, FilterChain filterChain)
63             throws IOException JavaDoc, ServletException {
64         ServletRequest JavaDoc request = servletRequest;
65         ServletResponse JavaDoc response = servletResponse;
66
67         if (servletRequest instanceof HttpServletRequest JavaDoc) {
68             HttpServletRequest JavaDoc httpServletRequest = (HttpServletRequest JavaDoc) servletRequest;
69
70             MyHttpServletRequestWrapper requestWrapper = new MyHttpServletRequestWrapper(httpServletRequest);
71             if (requestWrapper.getPathInfo() == null || requestWrapper.getPathInfo().indexOf('.') == -1) {
72                 response = new MyHttpServletResponseWrapper(servletResponse);
73
74                 if ("get".equalsIgnoreCase(httpServletRequest.getMethod()))
75                     request = requestWrapper;
76
77                 log.debug("wrap " + requestWrapper.getPathInfo());
78             } else
79                 log.debug("don't wrap " + requestWrapper.getPathInfo());
80         }
81         filterChain.doFilter(request, response);
82     }
83
84     public void destroy() {
85     }
86
87     public static final String JavaDoc replace(String JavaDoc s,
88                                        String JavaDoc toFind, String JavaDoc replace) {
89         StringBuffer JavaDoc erg = new StringBuffer JavaDoc();
90
91         int lastindex = 0;
92         int indexOf = s.indexOf(toFind);
93         if (indexOf == -1) return s;
94         while (indexOf != -1) {
95             erg.append(s.substring(lastindex, indexOf)).append(replace);
96             lastindex = indexOf + toFind.length();
97             indexOf = s.indexOf(toFind, lastindex);
98         }
99
100         erg.append(s.substring(lastindex));
101
102         return erg.toString();
103     }
104
105     private class MyHttpServletRequestWrapper extends HttpServletRequestWrapper JavaDoc {
106         private Map JavaDoc parameterMap;
107         private String JavaDoc pathInfo;
108         private String JavaDoc queryString;
109
110         public MyHttpServletRequestWrapper(HttpServletRequest JavaDoc httpServletRequest) {
111             super(httpServletRequest);
112
113             pathInfo = httpServletRequest.getPathInfo();
114             queryString = httpServletRequest.getQueryString();
115             parameterMap = httpServletRequest.getParameterMap();
116             log.debug("pathInfo = " + pathInfo);
117             log.debug("queryString = " + queryString);
118             log.debug("parameterMap = " + parameterMap);
119
120             if (pathInfo == null)
121                 return;
122
123             int pos = pathInfo.indexOf(parameterSeparator);
124             if (pos != -1) {
125                 queryString = pathInfo.substring(pos);
126                 pathInfo = pathInfo.substring(0, pos);
127
128                 parameterMap = new HashMap JavaDoc();
129                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(queryString.length());
130                 Matcher JavaDoc matcher = decodePattern.matcher(queryString);
131                 while (matcher.find()) {
132                     String JavaDoc param = matcher.group();
133                     pos = param.indexOf(nameValueSeparator);
134                     parameterMap.put(param.substring(1, pos), param.substring(pos + 1));
135                     matcher.appendReplacement(buffer, "&$2=$4");
136                 }
137                 queryString = buffer.substring(1);
138                 log.debug("modified pathInfo = " + pathInfo);
139                 log.debug("modified queryString = " + queryString);
140                 log.debug("modified parameterMap = " + parameterMap);
141             }
142         }
143
144         public String JavaDoc getRequestURI() {
145             return super.getRequestURI();
146         }
147
148         public Map JavaDoc getParameterMap() {
149             return parameterMap;
150         }
151
152         public String JavaDoc getPathInfo() {
153             return pathInfo;
154         }
155
156         public String JavaDoc getQueryString() {
157             return queryString;
158         }
159
160         public String JavaDoc getParameter(String JavaDoc string) {
161             Object JavaDoc value = getParameterMap().get(string);
162             if (value instanceof String JavaDoc)
163                 return (String JavaDoc) value;
164             else
165                 return ((String JavaDoc[]) value)[0];
166         }
167
168         public Enumeration JavaDoc getParameterNames() {
169             return Collections.enumeration(getParameterMap().keySet());
170         }
171
172         public String JavaDoc[] getParameterValues(String JavaDoc string) {
173             Object JavaDoc value = getParameterMap().get(string);
174             if (value instanceof String JavaDoc)
175                 return new String JavaDoc[]{(String JavaDoc) value};
176             else
177                 return (String JavaDoc[]) value;
178         }
179     }
180
181     private static class MyHttpServletResponseWrapper extends HttpServletResponseWrapper JavaDoc {
182         public MyHttpServletResponseWrapper(ServletResponse JavaDoc servletResponse) {
183             super((HttpServletResponse JavaDoc) servletResponse);
184         }
185
186         public ServletOutputStream getOutputStream() throws IOException JavaDoc {
187             final ServletOutputStream superOut = super.getOutputStream();
188             return new ServletOutputStream() {
189                 ByteArrayOutputStream JavaDoc bytes = new ByteArrayOutputStream JavaDoc(1000);
190
191                 public void write(int b) throws IOException JavaDoc {
192                     bytes.write(b);
193                 }
194
195                 public void close() throws IOException JavaDoc {
196                     encode(bytes, superOut);
197                     superOut.close();
198                 }
199             };
200         }
201     }
202
203
204     private static void encode(ByteArrayOutputStream JavaDoc bytes, ServletOutputStream out) throws IOException JavaDoc {
205         String JavaDoc regex = "(href|src|action) *= *\"([^\"]*)\"";
206
207         SmartURLsFilter smartURLsFilter = new SmartURLsFilter();
208         String JavaDoc replacement =
209                 smartURLsFilter.parameterSeparator + "$2" +
210                 smartURLsFilter.nameValueSeparator + "$4";
211
212         smartURLsFilter.encodePattern = Pattern.compile("(" + "\\?|&" + ")([a-zA-Z0-9%+.-[*]_]*)" +
213                 "(" + "=" + ")([a-zA-Z0-9%+.-[*]_=/]*)");
214
215         CharBuffer JavaDoc chars = CharBuffer.wrap(bytes.toString());
216         Pattern JavaDoc pattern = Pattern.compile(regex);
217         Matcher JavaDoc matcher = pattern.matcher(chars);
218         int pos = 0;
219         while (matcher.find()) {
220             out.print(chars.subSequence(pos, matcher.start()).toString());
221             pos = matcher.end();
222             Matcher JavaDoc matcher2 = smartURLsFilter.encodePattern.matcher(matcher.group(2));
223             String JavaDoc group2 = matcher2.replaceAll(replacement);
224
225             out.print(matcher.group(1) + "=\"" + group2 + "\"");
226         }
227         out.print(chars.subSequence(pos, bytes.size()).toString());
228     }
229 }
230
Popular Tags