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