01: /**
02: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
03: *
04: * Permission is hereby granted, free of charge, to any person obtaining a copy
05: * of this software and associated documentation files (the "Software"), to deal
06: * in the Software without restriction, including without limitation the rights
07: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
08: * copies of the Software, and to permit persons to whom the Software is
09: * furnished to do so, subject to the following conditions:
10: *
11: * The above copyright notice and this permission notice shall be included in
12: * all copies or substantial portions of the Software.
13: *
14: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20: * SOFTWARE.
21: */package com.liferay.portal.captcha;
22:
23: import com.liferay.portal.struts.ActionConstants;
24: import com.liferay.portal.struts.PortletAction;
25: import com.liferay.portal.util.PropsFiles;
26: import com.liferay.portal.util.WebKeys;
27: import com.liferay.portlet.ActionResponseImpl;
28: import com.liferay.util.ExtPropertiesLoader;
29:
30: import java.util.Properties;
31:
32: import javax.portlet.ActionRequest;
33: import javax.portlet.ActionResponse;
34: import javax.portlet.PortletConfig;
35: import javax.portlet.PortletSession;
36:
37: import javax.servlet.http.HttpServletResponse;
38:
39: import nl.captcha.servlet.CaptchaProducer;
40: import nl.captcha.util.Helper;
41:
42: import org.apache.commons.logging.Log;
43: import org.apache.commons.logging.LogFactory;
44: import org.apache.struts.action.ActionForm;
45: import org.apache.struts.action.ActionMapping;
46:
47: /**
48: * <a href="CaptchaPortletAction.java.html"><b><i>View Source</i></b></a>
49: *
50: * @author Brian Wing Shun Chan
51: *
52: */
53: public class CaptchaPortletAction extends PortletAction {
54:
55: public CaptchaPortletAction() {
56: Properties props = ExtPropertiesLoader.getInstance(
57: PropsFiles.CAPTCHA).getProperties();
58:
59: _producer = (CaptchaProducer) Helper.ThingFactory.loadImpl(
60: Helper.ThingFactory.CPROD, props);
61: }
62:
63: public void processAction(ActionMapping mapping, ActionForm form,
64: PortletConfig config, ActionRequest req, ActionResponse res)
65: throws Exception {
66:
67: try {
68: PortletSession ses = req.getPortletSession();
69:
70: String captchaText = _producer.createText();
71:
72: ses.setAttribute(WebKeys.CAPTCHA_TEXT, captchaText);
73:
74: HttpServletResponse httpRes = ((ActionResponseImpl) res)
75: .getHttpServletResponse();
76:
77: _producer.createImage(httpRes.getOutputStream(),
78: captchaText);
79:
80: setForward(req, ActionConstants.COMMON_NULL);
81: } catch (Exception e) {
82: _log.error(e);
83: }
84: }
85:
86: protected boolean isCheckMethodOnProcessAction() {
87: return _CHECK_METHOD_ON_PROCESS_ACTION;
88: }
89:
90: private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
91:
92: private static Log _log = LogFactory
93: .getLog(CaptchaPortletAction.class);
94:
95: private CaptchaProducer _producer;
96:
97: }
|