001
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.ClassUtil;
023 import com.liferay.portal.kernel.util.InstanceFactory;
024 import com.liferay.portal.kernel.util.PropsKeys;
025 import com.liferay.portal.util.ClassLoaderUtil;
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
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 throws CaptchaException {
068
069 _initialize();
070
071 return _captcha.isEnabled(request);
072 }
073
074 @Override
075 public boolean isEnabled(PortletRequest portletRequest)
076 throws CaptchaException {
077
078 _initialize();
079
080 return _captcha.isEnabled(portletRequest);
081 }
082
083 @Override
084 public void serveImage(
085 HttpServletRequest request, HttpServletResponse response)
086 throws IOException {
087
088 _initialize();
089
090 _captcha.serveImage(request, response);
091 }
092
093 @Override
094 public void serveImage(
095 ResourceRequest resourceRequest, ResourceResponse resourceResponse)
096 throws IOException {
097
098 _initialize();
099
100 _captcha.serveImage(resourceRequest, resourceResponse);
101 }
102
103 public void setCaptcha(Captcha captcha) {
104 _initialize();
105
106 if (captcha == null) {
107 if (_log.isInfoEnabled()) {
108 _log.info(
109 "Restoring " + ClassUtil.getClassName(_originalCaptcha));
110 }
111
112 _captcha = _originalCaptcha;
113 }
114 else {
115 if (_log.isInfoEnabled()) {
116 _log.info("Setting " + ClassUtil.getClassName(captcha));
117 }
118
119 _captcha = captcha;
120 }
121 }
122
123 private void _initialize() {
124 if (_captcha != null) {
125 return;
126 }
127
128 synchronized (this) {
129 if (_captcha != null) {
130 return;
131 }
132
133 try {
134 String captchaClassName = PrefsPropsUtil.getString(
135 PropsKeys.CAPTCHA_ENGINE_IMPL,
136 PropsValues.CAPTCHA_ENGINE_IMPL);
137
138 if (_log.isInfoEnabled()) {
139 _log.info("Initializing " + captchaClassName);
140 }
141
142 _captcha = (Captcha)InstanceFactory.newInstance(
143 ClassLoaderUtil.getPortalClassLoader(), captchaClassName);
144
145 _originalCaptcha = _captcha;
146 }
147 catch (Exception e) {
148 _log.error(e, e);
149 }
150 }
151 }
152
153 private static Log _log = LogFactoryUtil.getLog(CaptchaImpl.class);
154
155 private volatile Captcha _captcha;
156 private Captcha _originalCaptcha;
157
158 }