1
14
15 package com.liferay.portal.captcha;
16
17 import com.liferay.portal.kernel.captcha.Captcha;
18 import com.liferay.portal.kernel.captcha.CaptchaException;
19 import com.liferay.portal.kernel.log.Log;
20 import com.liferay.portal.kernel.log.LogFactoryUtil;
21 import com.liferay.portal.kernel.util.InstanceFactory;
22 import com.liferay.portal.kernel.util.PropsKeys;
23 import com.liferay.portal.util.PrefsPropsUtil;
24 import com.liferay.portal.util.PropsValues;
25
26 import java.io.IOException;
27
28 import javax.portlet.PortletRequest;
29 import javax.portlet.PortletResponse;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34
39 public class CaptchaImpl implements Captcha {
40
41 public void check(HttpServletRequest request) throws CaptchaException {
42 _initialize();
43
44 _captcha.check(request);
45 }
46
47 public void check(PortletRequest portletRequest) throws CaptchaException {
48 _initialize();
49
50 _captcha.check(portletRequest);
51 }
52
53 public String getTaglibPath() {
54 _initialize();
55
56 return _captcha.getTaglibPath();
57 }
58
59 public boolean isEnabled(HttpServletRequest request)
60 throws CaptchaException {
61
62 _initialize();
63
64 return _captcha.isEnabled(request);
65 }
66
67 public boolean isEnabled(PortletRequest portletRequest)
68 throws CaptchaException {
69
70 _initialize();
71
72 return _captcha.isEnabled(portletRequest);
73 }
74
75 public void serveImage(
76 HttpServletRequest request, HttpServletResponse response)
77 throws IOException {
78
79 _initialize();
80
81 _captcha.serveImage(request, response);
82 }
83
84 public void serveImage(
85 PortletRequest portletRequest, PortletResponse portletResponse)
86 throws IOException {
87
88 _initialize();
89
90 _captcha.serveImage(portletRequest, portletResponse);
91 }
92
93 public void setCaptcha(Captcha captcha) {
94 _initialize();
95
96 if (captcha == null) {
97 if (_log.isInfoEnabled()) {
98 _log.info(
99 "Restoring " + _originalCaptcha.getClass().getName());
100 }
101
102 _captcha = _originalCaptcha;
103 }
104 else {
105 if (_log.isInfoEnabled()) {
106 _log.info("Setting " + captcha.getClass().getName());
107 }
108
109 _captcha = captcha;
110 }
111 }
112
113 private void _initialize() {
114 if (_captcha != null) {
115 return;
116 }
117
118 synchronized (this) {
119 if (_captcha != null) {
120 return;
121 }
122
123 try {
124 String captchaClassName = PrefsPropsUtil.getString(
125 PropsKeys.CAPTCHA_ENGINE_IMPL,
126 PropsValues.CAPTCHA_ENGINE_IMPL);
127
128 if (_log.isInfoEnabled()) {
129 _log.info("Initializing " + captchaClassName);
130 }
131
132 _captcha = (Captcha)InstanceFactory.newInstance(
133 captchaClassName);
134
135 _originalCaptcha = _captcha;
136 }
137 catch (Exception e) {
138 _log.error(e, e);
139 }
140 }
141 }
142
143 private static Log _log = LogFactoryUtil.getLog(CaptchaImpl.class);
144
145 private Captcha _captcha;
146 private Captcha _originalCaptcha;
147
148 }