1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
 * Copyright (c) 2005-2007, Paul Tuckey
 * All rights reserved.
 * ====================================================================
 * Licensed under the BSD License. Text as follows.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   - Neither the name tuckey.org nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */
package org.tuckey.web.filters.urlrewrite;

import org.tuckey.web.filters.urlrewrite.utils.Log;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLDecoder;
import java.util.List;

/**
 * The main rewriter.
 *
 * @author Paul Tuckey
 * @version $Revision: 52 $ $Date: 2007-02-26 07:00:28 +1300 (Mon, 26 Feb 2007) $
 */
public class UrlRewriter {

    private static Log log = Log.getLog(UrlRewriter.class);

    /**
     * The conf for this filter.
     */
    private Conf conf;

    public UrlRewriter(Conf conf) {
        this.conf = conf;
    }

    /**
     * Helpful for testing but otherwise, don't use.
     */
    public RewrittenUrl processRequest(final HttpServletRequest hsRequest,
                                       final HttpServletResponse hsResponse)
            throws IOException, ServletException, InvocationTargetException {
        RuleChain chain = getNewChain(hsRequest, null);
        if (chain == null) return null;
        chain.process(hsRequest, hsResponse);
        return chain.getFinalRewrittenRequest();
    }

    /**
     * The main method called for each request that this filter is mapped for.
     *
     * @param hsRequest The request to process.
     * @return returns true when response has been handled by url rewriter false when it hasn't.
     */
    public boolean processRequest(final HttpServletRequest hsRequest, final HttpServletResponse hsResponse,
                                  FilterChain parentChain)
            throws IOException, ServletException {
        RuleChain chain = getNewChain(hsRequest, parentChain);
        if (chain == null) return false;
        chain.doRules(hsRequest, hsResponse);
        return chain.isResponseHandled();
    }


    /**
     * Return the path within the web application for the given request.
     * <p>Detects include request URL if called within a RequestDispatcher include.
     */
    public String getPathWithinApplication(HttpServletRequest request) {
        String requestUri = request.getRequestURI();
        if (requestUri == null) requestUri = "";
        String decodedRequestUri = decodeRequestString(request, requestUri);
        String contextPath = getContextPath(request);
        String path;
        if (StringUtils.startsWithIgnoreCase(decodedRequestUri, contextPath) && !conf.isUseContext()) {
            // Normal case: URI contains context path.
            path = decodedRequestUri.substring(contextPath.length());

        } else if (!StringUtils.startsWithIgnoreCase(decodedRequestUri, contextPath) && conf.isUseContext()) {
            // add the context path on
            path = contextPath + decodedRequestUri;

        } else {
            path = decodedRequestUri;
        }
        return StringUtils.isBlank(path) ? "/" : path;
    }

    /**
     * Return the context path for the given request, detecting an include request
     * URL if called within a RequestDispatcher include.
     * <p>As the value returned by <code>request.getContextPath()</code> is <i>not</i>
     * decoded by the servlet container, this method will decode it.
     */
    public String getContextPath(HttpServletRequest request) {
        String contextPath = request.getContextPath();
        if ("/".equals(contextPath)) {
            // Invalid case, but happens for includes on Jetty: silently adapt it.
            contextPath = "";
        }
        return decodeRequestString(request, contextPath);
    }

    /**
     * Decode the string with a URLDecoder. The encoding will be taken
     * from the request, falling back to the default for your platform ("ISO-8859-1" on windows).
     */
    public String decodeRequestString(HttpServletRequest request, String source) {
        if (conf.isDecodeUsingEncodingHeader()) {
            String enc = request.getCharacterEncoding();
            if (enc != null) {
                try {
                    return URLDecoder.decode(source, enc);
                } catch (UnsupportedEncodingException ex) {
                    if (log.isWarnEnabled()) {
                        log.warn("Could not decode: " + source + " (header encoding: '" + enc + "'); exception: " + ex.getMessage());
                    }
                }
            }
        }
        if (conf.isDecodeUsingCustomCharsetRequired()) {
            String enc = conf.getDecodeUsing();
            if (enc != null) {
                try {
                    return URLDecoder.decode(source, enc);
                } catch (UnsupportedEncodingException ex) {
                    if (log.isWarnEnabled()) {
                        log.warn("Could not decode: " + source + " (encoding: '" + enc + "') using default encoding; exception: " + ex.getMessage());
                    }
                }
            }
        }
        return source;
    }


    private RuleChain getNewChain(final HttpServletRequest hsRequest, FilterChain parentChain) {

        String originalUrl = getPathWithinApplication(hsRequest);

        if (originalUrl == null) {
            // for some reason the engine is not giving us the url
            // this isn't good
            log.debug("unable to fetch request uri from request.  This shouldn't happen, it may indicate that " +
                    "the web application server has a bug or that the request was not pased correctly.");
            return null;
        }

        if (log.isDebugEnabled()) {
            log.debug("processing request for " + originalUrl);
        }

        // add the query string on uri (note, some web app containers do this)
        if (originalUrl != null && originalUrl.indexOf("?") == -1 && conf.isUseQueryString()) {
            String query = hsRequest.getQueryString();
            if (query != null) {
                query = query.trim();
                if (query.length() > 0) {
                    originalUrl = originalUrl + "?" + query;
                    log.debug("query string added");
                }
            }
        }

        if (!conf.isOk()) {
            // when conf cannot be loaded for some sort of error
            // continue as normal without looking at the non-existent rules
            log.debug("configuration is not ok.  not rewriting request.");
            return null;
        }

        final List rules = conf.getRules();
        if (rules.size() == 0) {
            // no rules defined
            log.debug("there are no rules setup.  not rewriting request.");
            return null;
        }

        return new RuleChain(this, originalUrl, parentChain);
    }


    /**
     * Handle an exception thrown by a Run element.
     */
    public RewrittenUrl handleInvocationTargetException(final HttpServletRequest hsRequest,
                                                        final HttpServletResponse hsResponse, InvocationTargetException e)
            throws ServletException, IOException {

        Throwable originalThrowable = getOriginalException(e);

        if (log.isDebugEnabled()) {
            log.debug("attampting to find catch for exception " + originalThrowable.getClass().getName());
        }

        List catchElems = conf.getCatchElems();
        for (int i = 0; i < catchElems.size(); i++) {
            CatchElem catchElem = (CatchElem) catchElems.get(i);
            if (!catchElem.matches(originalThrowable)) continue;
            try {
                return catchElem.execute(hsRequest, hsResponse, originalThrowable);

            } catch (InvocationTargetException invocationExceptionInner) {
                originalThrowable = getOriginalException(invocationExceptionInner);
                log.warn("had exception processing catch, trying the rest of the catches with " +
                        originalThrowable.getClass().getName());
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("exception unhandled", e);
        }
        if (originalThrowable instanceof Error) throw (Error) originalThrowable;
        if (originalThrowable instanceof RuntimeException) throw (RuntimeException) originalThrowable;
        if (originalThrowable instanceof ServletException) throw (ServletException) originalThrowable;
        if (originalThrowable instanceof IOException) throw (IOException) originalThrowable;
        throw new ServletException(originalThrowable);
    }

    /**
     * Get the original exception that caused the InvocationTargetException.
     *
     * @param e the InvocationTargetException
     * @return the original exception.
     * @throws javax.servlet.ServletException If the exception is a servlet exception, it will be thrown.
     */
    private Throwable getOriginalException(InvocationTargetException e) throws ServletException {
        Throwable originalThrowable = e.getTargetException();
        if (originalThrowable == null) {
            originalThrowable = e.getCause();
            if (originalThrowable == null) {
                throw new ServletException(e);
            }
        }
        // unwrap if exception is a ServletException
        if (originalThrowable instanceof ServletException) {
            ServletException se = (ServletException) originalThrowable;
            // go 5 deep to see if we can get a real exception
            for (int i = 0; i < 5 && se.getCause() instanceof ServletException; i++) {
                se = (ServletException) se.getCause();
            }
            if (se.getCause() instanceof InvocationTargetException) {
                return getOriginalException((InvocationTargetException) se.getCause());
            } else {
                throw se;
            }
        }
        return originalThrowable;
    }


    public Conf getConf() {
        return conf;
    }


    /**
     * Handles rewriting urls in jsp's etc, i.e. response.encodeURL() is overriden in the response wrapper.
     *
     * @param hsResponse          response
     * @param hsRequest           request
     * @param encodeUrlHasBeenRun if encodeUrl has already been run on the originalOutboundUrl speficy this to be true
     * @param outboundUrl         url
     * @return RewrittenOutboundUrl
     * @see UrlRewriteWrappedResponse
     */
    protected RewrittenOutboundUrl processEncodeURL(HttpServletResponse hsResponse, HttpServletRequest hsRequest,
                                                    boolean encodeUrlHasBeenRun, String outboundUrl) {

        if (log.isDebugEnabled()) {
            log.debug("processing outbound url for " + outboundUrl);
        }

        if (outboundUrl == null) {
            // this probably means encode called with no url
            return new RewrittenOutboundUrl(null, true);
        }

        // attempt to match the rules
        boolean finalEncodeOutboundUrl = true;
        String finalToUrl = outboundUrl;
        final List outboundRules = conf.getOutboundRules();
        try {
            for (int i = 0; i < outboundRules.size(); i++) {
                final OutboundRule outboundRule = (OutboundRule) outboundRules.get(i);
                if (!encodeUrlHasBeenRun && outboundRule.isEncodeFirst()) {
                    continue;
                }
                if (encodeUrlHasBeenRun && !outboundRule.isEncodeFirst()) {
                    continue;
                }
                final RewrittenOutboundUrl rewrittenUrl = outboundRule.execute(finalToUrl, hsRequest, hsResponse);
                if (rewrittenUrl != null) {
                    // means this rule has matched
                    if (log.isDebugEnabled()) {
                        log.debug("\"" + outboundRule.getDisplayName() + "\" matched");
                    }
                    finalToUrl = rewrittenUrl.getTarget();
                    finalEncodeOutboundUrl = rewrittenUrl.isEncode();
                    if (outboundRule.isLast()) {
                        log.debug("rule is last");
                        // there can be no more matches on this request
                        break;
                    }
                }
            }
        } catch (InvocationTargetException e) {
            try {
                handleInvocationTargetException(hsRequest, hsResponse, e);
            } catch (ServletException e1) {
                log.error(e1);
            } catch (IOException e1) {
                log.error(e1);
            }
        }

        return new RewrittenOutboundUrl(finalToUrl, finalEncodeOutboundUrl);
    }

    /**
     * Destory the rewriter gracefully.
     */
    public void destroy() {
        conf.destroy();
    }

}



			
			

Browsed Source: [clear]