Source code: com/sourcetap/license/LicenseFilter.java
1 /*
2 * $Id$
3 *
4 * Copyright (c) 2003 SourceTap - www.sourcetap.com
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24 package com.sourcetap.license;
25
26 import javax.servlet.*;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import java.io.*;
30
31 /**
32 * Servlet Filter which can be added to the filter chain in the web.xml to enable validation of
33 * the license before serving protected resources
34 *
35 * @author Steve Fowler
36 * @version $Revision$
37 */
38 public class LicenseFilter
39 implements Filter
40 {
41
42 public LicenseFilter()
43 {
44 config = null;
45 }
46
47 public void init(FilterConfig filterConfig)
48 throws ServletException
49 {
50 config = filterConfig;
51 }
52
53 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
54 throws IOException, ServletException
55 {
56 // see if this filter has already been applied to this request
57 if(servletRequest.getAttribute(FILTER_APPLIED) != null)
58 {
59 filterChain.doFilter(servletRequest, servletResponse);
60 return;
61 }
62 servletRequest.setAttribute(FILTER_APPLIED, Boolean.TRUE);
63 HttpServletRequest req = (HttpServletRequest)servletRequest;
64 String uri = req.getServletPath();
65
66 // if the uri is the license error URL, let it continue
67 if (uri.endsWith(INVALID_LICENSE_URL))
68 {
69 filterChain.doFilter(servletRequest, servletResponse);
70 return;
71 }
72
73 // get the license. if there are any errors, redirect to the INVALID_LICENSE_URL
74 License license = null;
75 try {
76 license = LicenseFactory.getLicenseManager().getLicense();
77 } catch ( LicenseException e)
78 {
79 // System.out.println(e.getMessage());
80 license = null;
81 }
82
83 if ( license == null || license.isExpired() )
84 {
85 ((HttpServletResponse)servletResponse).sendRedirect(req.getContextPath() + INVALID_LICENSE_URL);
86 return;
87 }
88 filterChain.doFilter(servletRequest, servletResponse);
89 }
90
91 public void destroy()
92 {
93 }
94
95 private static final String FILTER_APPLIED = "LICENSE_FILTER_APPLIED";
96 public static final String INVALID_LICENSE_URL = "/licenseError.jsp";
97 private FilterConfig config;
98
99 }