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.InstanceFactory;
022 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
023 import com.liferay.portal.kernel.util.PropsKeys;
024 import com.liferay.portal.util.PrefsPropsUtil;
025 import com.liferay.portal.util.PropsValues;
026
027 import java.io.IOException;
028
029 import javax.portlet.PortletRequest;
030 import javax.portlet.PortletResponse;
031
032 import javax.servlet.http.HttpServletRequest;
033 import javax.servlet.http.HttpServletResponse;
034
035
038 public class CaptchaImpl implements Captcha {
039
040 public void check(HttpServletRequest request) throws CaptchaException {
041 _initialize();
042
043 _captcha.check(request);
044 }
045
046 public void check(PortletRequest portletRequest) throws CaptchaException {
047 _initialize();
048
049 _captcha.check(portletRequest);
050 }
051
052 public String getTaglibPath() {
053 _initialize();
054
055 return _captcha.getTaglibPath();
056 }
057
058 public boolean isEnabled(HttpServletRequest request)
059 throws CaptchaException {
060
061 _initialize();
062
063 return _captcha.isEnabled(request);
064 }
065
066 public boolean isEnabled(PortletRequest portletRequest)
067 throws CaptchaException {
068
069 _initialize();
070
071 return _captcha.isEnabled(portletRequest);
072 }
073
074 public void serveImage(
075 HttpServletRequest request, HttpServletResponse response)
076 throws IOException {
077
078 _initialize();
079
080 _captcha.serveImage(request, response);
081 }
082
083 public void serveImage(
084 PortletRequest portletRequest, PortletResponse portletResponse)
085 throws IOException {
086
087 _initialize();
088
089 _captcha.serveImage(portletRequest, portletResponse);
090 }
091
092 public void setCaptcha(Captcha captcha) {
093 _initialize();
094
095 if (captcha == null) {
096 if (_log.isInfoEnabled()) {
097 _log.info("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 PortalClassLoaderUtil.getClassLoader(), 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 volatile Captcha _captcha;
144 private Captcha _originalCaptcha;
145
146 }