001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.captcha;
016    
017    import com.liferay.portal.kernel.captcha.Captcha;
018    import com.liferay.portal.kernel.captcha.CaptchaException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.InstanceFactory;
022    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.util.PrefsPropsUtil;
025    import com.liferay.portal.util.PropsValues;
026    
027    import java.io.IOException;
028    
029    import javax.portlet.PortletRequest;
030    import javax.portlet.PortletResponse;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpServletResponse;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class CaptchaImpl implements Captcha {
039    
040            public void check(HttpServletRequest request) throws CaptchaException {
041                    _initialize();
042    
043                    _captcha.check(request);
044            }
045    
046            public void check(PortletRequest portletRequest) throws CaptchaException {
047                    _initialize();
048    
049                    _captcha.check(portletRequest);
050            }
051    
052            public String getTaglibPath() {
053                    _initialize();
054    
055                    return _captcha.getTaglibPath();
056            }
057    
058            public boolean isEnabled(HttpServletRequest request)
059                    throws CaptchaException {
060    
061                    _initialize();
062    
063                    return _captcha.isEnabled(request);
064            }
065    
066            public boolean isEnabled(PortletRequest portletRequest)
067                    throws CaptchaException {
068    
069                    _initialize();
070    
071                    return _captcha.isEnabled(portletRequest);
072            }
073    
074            public void serveImage(
075                            HttpServletRequest request, HttpServletResponse response)
076                    throws IOException {
077    
078                    _initialize();
079    
080                    _captcha.serveImage(request, response);
081            }
082    
083            public void serveImage(
084                            PortletRequest portletRequest, PortletResponse portletResponse)
085                    throws IOException {
086    
087                    _initialize();
088    
089                    _captcha.serveImage(portletRequest, portletResponse);
090            }
091    
092            public void setCaptcha(Captcha captcha) {
093                    _initialize();
094    
095                    if (captcha == null) {
096                            if (_log.isInfoEnabled()) {
097                                    _log.info(
098                                            "Restoring " + _originalCaptcha.getClass().getName());
099                            }
100    
101                            _captcha = _originalCaptcha;
102                    }
103                    else {
104                            if (_log.isInfoEnabled()) {
105                                    _log.info("Setting " + captcha.getClass().getName());
106                            }
107    
108                            _captcha = captcha;
109                    }
110            }
111    
112            private void _initialize() {
113                    if (_captcha != null) {
114                            return;
115                    }
116    
117                    synchronized (this) {
118                            if (_captcha != null) {
119                                    return;
120                            }
121    
122                            try {
123                                    String captchaClassName = PrefsPropsUtil.getString(
124                                            PropsKeys.CAPTCHA_ENGINE_IMPL,
125                                            PropsValues.CAPTCHA_ENGINE_IMPL);
126    
127                                    if (_log.isInfoEnabled()) {
128                                            _log.info("Initializing " + captchaClassName);
129                                    }
130    
131                                    _captcha = (Captcha)InstanceFactory.newInstance(
132                                            PortalClassLoaderUtil.getClassLoader(), captchaClassName);
133    
134                                    _originalCaptcha = _captcha;
135                            }
136                            catch (Exception e) {
137                                    _log.error(e, e);
138                            }
139                    }
140            }
141    
142            private static Log _log = LogFactoryUtil.getLog(CaptchaImpl.class);
143    
144            private volatile Captcha _captcha;
145            private Captcha _originalCaptcha;
146    
147    }