001    /**
002     * Copyright (c) 2000-2012 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.PropsKeys;
023    import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
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("Restoring " + _originalCaptcha.getClass().getName());
098                            }
099    
100                            _captcha = _originalCaptcha;
101                    }
102                    else {
103                            if (_log.isInfoEnabled()) {
104                                    _log.info("Setting " + captcha.getClass().getName());
105                            }
106    
107                            _captcha = captcha;
108                    }
109            }
110    
111            private void _initialize() {
112                    if (_captcha != null) {
113                            return;
114                    }
115    
116                    synchronized (this) {
117                            if (_captcha != null) {
118                                    return;
119                            }
120    
121                            try {
122                                    String captchaClassName = PrefsPropsUtil.getString(
123                                            PropsKeys.CAPTCHA_ENGINE_IMPL,
124                                            PropsValues.CAPTCHA_ENGINE_IMPL);
125    
126                                    if (_log.isInfoEnabled()) {
127                                            _log.info("Initializing " + captchaClassName);
128                                    }
129    
130                                    _captcha = (Captcha)InstanceFactory.newInstance(
131                                            PACLClassLoaderUtil.getPortalClassLoader(),
132                                            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    }