001    /**
002     * Copyright (c) 2000-present 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.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.security.pacl.DoPrivileged;
022    import com.liferay.portal.kernel.util.ClassLoaderUtil;
023    import com.liferay.portal.kernel.util.ClassUtil;
024    import com.liferay.portal.kernel.util.InstanceFactory;
025    import com.liferay.portal.kernel.util.PropsKeys;
026    import com.liferay.portal.util.PrefsPropsUtil;
027    import com.liferay.portal.util.PropsValues;
028    
029    import java.io.IOException;
030    
031    import javax.portlet.PortletRequest;
032    import javax.portlet.ResourceRequest;
033    import javax.portlet.ResourceResponse;
034    
035    import javax.servlet.http.HttpServletRequest;
036    import javax.servlet.http.HttpServletResponse;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    @DoPrivileged
042    public class CaptchaImpl implements Captcha {
043    
044            @Override
045            public void check(HttpServletRequest request) throws CaptchaException {
046                    _initialize();
047    
048                    _captcha.check(request);
049            }
050    
051            @Override
052            public void check(PortletRequest portletRequest) throws CaptchaException {
053                    _initialize();
054    
055                    _captcha.check(portletRequest);
056            }
057    
058            @Override
059            public String getTaglibPath() {
060                    _initialize();
061    
062                    return _captcha.getTaglibPath();
063            }
064    
065            @Override
066            public boolean isEnabled(HttpServletRequest request) {
067                    _initialize();
068    
069                    return _captcha.isEnabled(request);
070            }
071    
072            @Override
073            public boolean isEnabled(PortletRequest portletRequest) {
074                    _initialize();
075    
076                    return _captcha.isEnabled(portletRequest);
077            }
078    
079            @Override
080            public void serveImage(
081                            HttpServletRequest request, HttpServletResponse response)
082                    throws IOException {
083    
084                    _initialize();
085    
086                    _captcha.serveImage(request, response);
087            }
088    
089            @Override
090            public void serveImage(
091                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
092                    throws IOException {
093    
094                    _initialize();
095    
096                    _captcha.serveImage(resourceRequest, resourceResponse);
097            }
098    
099            public void setCaptcha(Captcha captcha) {
100                    _initialize();
101    
102                    if (captcha == null) {
103                            if (_log.isInfoEnabled()) {
104                                    _log.info(
105                                            "Restoring " + ClassUtil.getClassName(_originalCaptcha));
106                            }
107    
108                            _captcha = _originalCaptcha;
109                    }
110                    else {
111                            if (_log.isInfoEnabled()) {
112                                    _log.info("Setting " + ClassUtil.getClassName(captcha));
113                            }
114    
115                            _captcha = captcha;
116                    }
117            }
118    
119            private void _initialize() {
120                    if (_captcha != null) {
121                            return;
122                    }
123    
124                    synchronized (this) {
125                            if (_captcha != null) {
126                                    return;
127                            }
128    
129                            try {
130                                    String captchaClassName = PrefsPropsUtil.getString(
131                                            PropsKeys.CAPTCHA_ENGINE_IMPL,
132                                            PropsValues.CAPTCHA_ENGINE_IMPL);
133    
134                                    if (_log.isInfoEnabled()) {
135                                            _log.info("Initializing " + captchaClassName);
136                                    }
137    
138                                    _captcha = (Captcha)InstanceFactory.newInstance(
139                                            ClassLoaderUtil.getPortalClassLoader(), captchaClassName);
140    
141                                    _originalCaptcha = _captcha;
142                            }
143                            catch (Exception e) {
144                                    _log.error(e, e);
145                            }
146                    }
147            }
148    
149            private static final Log _log = LogFactoryUtil.getLog(CaptchaImpl.class);
150    
151            private volatile Captcha _captcha;
152            private Captcha _originalCaptcha;
153    
154    }