001    /**
002     * Copyright (c) 2000-2012 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.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.ResourceRequest;
031    import javax.portlet.ResourceResponse;
032    
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class CaptchaImpl implements Captcha {
040    
041            public void check(HttpServletRequest request) throws CaptchaException {
042                    _initialize();
043    
044                    _captcha.check(request);
045            }
046    
047            public void check(PortletRequest portletRequest) throws CaptchaException {
048                    _initialize();
049    
050                    _captcha.check(portletRequest);
051            }
052    
053            public String getTaglibPath() {
054                    _initialize();
055    
056                    return _captcha.getTaglibPath();
057            }
058    
059            public boolean isEnabled(HttpServletRequest request)
060                    throws CaptchaException {
061    
062                    _initialize();
063    
064                    return _captcha.isEnabled(request);
065            }
066    
067            public boolean isEnabled(PortletRequest portletRequest)
068                    throws CaptchaException {
069    
070                    _initialize();
071    
072                    return _captcha.isEnabled(portletRequest);
073            }
074    
075            public void serveImage(
076                            HttpServletRequest request, HttpServletResponse response)
077                    throws IOException {
078    
079                    _initialize();
080    
081                    _captcha.serveImage(request, response);
082            }
083    
084            public void serveImage(
085                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
086                    throws IOException {
087    
088                    _initialize();
089    
090                    _captcha.serveImage(resourceRequest, resourceResponse);
091            }
092    
093            public void setCaptcha(Captcha captcha) {
094                    _initialize();
095    
096                    if (captcha == null) {
097                            if (_log.isInfoEnabled()) {
098                                    _log.info("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                                            PACLClassLoaderUtil.getPortalClassLoader(),
133                                            captchaClassName);
134    
135                                    _originalCaptcha = _captcha;
136                            }
137                            catch (Exception e) {
138                                    _log.error(e, e);
139                            }
140                    }
141            }
142    
143            private static Log _log = LogFactoryUtil.getLog(CaptchaImpl.class);
144    
145            private volatile Captcha _captcha;
146            private Captcha _originalCaptcha;
147    
148    }