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