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