001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.servlet.filters;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.BaseFilter;
020    import com.liferay.portal.kernel.util.CharPool;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.GroupConstants;
025    
026    import java.util.HashSet;
027    import java.util.Set;
028    
029    import javax.servlet.FilterChain;
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    /**
034     * @author Amos Fong
035     */
036    public class LiferayPackageFilter extends BaseFilter {
037    
038            public LiferayPackageFilter(
039                    String servletContextName, String licensePageURL) {
040    
041                    _licensePageURL = licensePageURL;
042                    _servletContextName = servletContextName;
043            }
044    
045            @Override
046            protected Log getLog() {
047                    return _log;
048            }
049    
050            @Override
051            protected void processFilter(
052                            HttpServletRequest request, HttpServletResponse response,
053                            FilterChain filterChain)
054                    throws Exception {
055    
056                    _clientIPAddresses.add(request.getRemoteAddr());
057    
058                    if (_clientIPAddresses.size() > 10) {
059                            if (_isControlPanel(request)) {
060                                    response.sendRedirect(_licensePageURL);
061    
062                                    return;
063                            }
064                            else if (_isLicensePage(request)) {
065                                    StringBundler sb = new StringBundler(4);
066    
067                                    sb.append("You have exceeded the developer license ");
068                                    sb.append("connection limit for ");
069                                    sb.append(_servletContextName);
070                                    sb.append(".");
071    
072                                    request.setAttribute("ERROR_MESSAGE", sb.toString());
073                            }
074                    }
075    
076                    processFilter(
077                            LiferayPackageFilter.class, request, response, filterChain);
078            }
079    
080            private boolean _isControlPanel(HttpServletRequest request) {
081                    String pathInfo = request.getPathInfo();
082    
083                    if (Validator.isNull(pathInfo) ||
084                            !pathInfo.startsWith(StringPool.SLASH)) {
085    
086                            return false;
087                    }
088    
089                    String friendlyURL = null;
090    
091                    int pos = pathInfo.indexOf(CharPool.SLASH, 1);
092    
093                    if (pos != -1) {
094                            friendlyURL = pathInfo.substring(0, pos);
095                    }
096                    else if (pathInfo.length() > 1) {
097                            friendlyURL = pathInfo;
098                    }
099    
100                    if (Validator.isNull(friendlyURL)) {
101                            return false;
102                    }
103    
104                    if (friendlyURL.equals(GroupConstants.CONTROL_PANEL_FRIENDLY_URL)) {
105                            return true;
106                    }
107                    else {
108                            return false;
109                    }
110            }
111    
112            private boolean _isLicensePage(HttpServletRequest request) {
113                    String pathInfo = request.getPathInfo();
114    
115                    if (Validator.isNull(pathInfo) ||
116                            !pathInfo.startsWith(StringPool.SLASH)) {
117    
118                            return false;
119                    }
120    
121                    if (pathInfo.equals("/portal/license")) {
122                            return true;
123                    }
124                    else {
125                            return false;
126                    }
127            }
128    
129            private static Log _log = LogFactoryUtil.getLog(LiferayPackageFilter.class);
130    
131            private Set<String> _clientIPAddresses = new HashSet<String>();
132            private String _licensePageURL;
133            private String _servletContextName;
134    
135    }