001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.servlet.filters.sso.cas;
022:
023: import com.liferay.portal.kernel.log.Log;
024: import com.liferay.portal.kernel.log.LogFactoryUtil;
025: import com.liferay.portal.kernel.servlet.BaseFilter;
026: import com.liferay.portal.kernel.util.Validator;
027: import com.liferay.portal.util.PortalUtil;
028: import com.liferay.portal.util.PrefsPropsUtil;
029: import com.liferay.portal.util.PropsUtil;
030: import com.liferay.portal.util.PropsValues;
031: import com.liferay.util.CollectionFactory;
032: import com.liferay.util.servlet.filters.DynamicFilterConfig;
033:
034: import java.io.IOException;
035:
036: import java.util.Map;
037:
038: import javax.servlet.Filter;
039: import javax.servlet.FilterChain;
040: import javax.servlet.ServletContext;
041: import javax.servlet.ServletException;
042: import javax.servlet.ServletRequest;
043: import javax.servlet.ServletResponse;
044: import javax.servlet.http.HttpServletRequest;
045: import javax.servlet.http.HttpServletResponse;
046: import javax.servlet.http.HttpSession;
047:
048: /**
049: * <a href="CASFilter.java.html"><b><i>View Source</i></b></a>
050: *
051: * @author Michael Young
052: * @author Brian Wing Shun Chan
053: * @author Raymond Augé
054: *
055: */
056: public class CASFilter extends BaseFilter {
057:
058: public static void reload(long companyId) {
059: _casFilters.remove(new Long(companyId));
060: }
061:
062: public void doFilter(ServletRequest req, ServletResponse res,
063: FilterChain chain) throws IOException, ServletException {
064:
065: try {
066: HttpServletRequest httpReq = (HttpServletRequest) req;
067:
068: long companyId = PortalUtil.getCompanyId(httpReq);
069:
070: if (PrefsPropsUtil.getBoolean(companyId,
071: PropsUtil.CAS_AUTH_ENABLED,
072: PropsValues.CAS_AUTH_ENABLED)) {
073:
074: String pathInfo = httpReq.getPathInfo();
075:
076: if (pathInfo.indexOf("/portal/logout") != -1) {
077: HttpServletResponse httpRes = (HttpServletResponse) res;
078: HttpSession httpSes = httpReq.getSession();
079:
080: httpSes.invalidate();
081:
082: String logoutUrl = PrefsPropsUtil.getString(
083: companyId, PropsUtil.CAS_LOGOUT_URL);
084:
085: httpRes.sendRedirect(logoutUrl);
086: } else {
087: Filter casFilter = getCASFilter(companyId);
088:
089: casFilter.doFilter(req, res, chain);
090: }
091: } else {
092: doFilter(CASFilter.class, req, res, chain);
093: }
094: } catch (Exception e) {
095: _log.error(e, e);
096: }
097: }
098:
099: protected Filter getCASFilter(long companyId) throws Exception {
100: Long companyIdObj = new Long(companyId);
101:
102: edu.yale.its.tp.cas.client.filter.CASFilter casFilter = (edu.yale.its.tp.cas.client.filter.CASFilter) _casFilters
103: .get(companyIdObj);
104:
105: if (casFilter == null) {
106: casFilter = new edu.yale.its.tp.cas.client.filter.CASFilter();
107:
108: DynamicFilterConfig config = new DynamicFilterConfig(
109: _filterName, _ctx);
110:
111: String serverName = PrefsPropsUtil.getString(companyId,
112: PropsUtil.CAS_SERVER_NAME);
113: String serviceUrl = PrefsPropsUtil.getString(companyId,
114: PropsUtil.CAS_SERVICE_URL);
115:
116: config
117: .addInitParameter(
118: edu.yale.its.tp.cas.client.filter.CASFilter.LOGIN_INIT_PARAM,
119: PrefsPropsUtil.getString(companyId,
120: PropsUtil.CAS_LOGIN_URL));
121:
122: if (Validator.isNotNull(serviceUrl)) {
123: config
124: .addInitParameter(
125: edu.yale.its.tp.cas.client.filter.CASFilter.SERVICE_INIT_PARAM,
126: serviceUrl);
127: } else {
128: config
129: .addInitParameter(
130: edu.yale.its.tp.cas.client.filter.CASFilter.SERVERNAME_INIT_PARAM,
131: serverName);
132: }
133:
134: config
135: .addInitParameter(
136: edu.yale.its.tp.cas.client.filter.CASFilter.VALIDATE_INIT_PARAM,
137: PrefsPropsUtil.getString(companyId,
138: PropsUtil.CAS_VALIDATE_URL));
139:
140: casFilter.init(config);
141:
142: _casFilters.put(companyIdObj, casFilter);
143: }
144:
145: return casFilter;
146: }
147:
148: private static Log _log = LogFactoryUtil.getLog(CASFilter.class);
149:
150: private static Map _casFilters = CollectionFactory.getSyncHashMap();
151:
152: private String _filterName;
153: private ServletContext _ctx;
154:
155: }
|