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