Author: phuong_vu
Date: 2010-11-17 05:15:32 -0500 (Wed, 17 Nov 2010)
New Revision: 5137
Added:
exo/portal/branches/3.1.x/portlet/exoadmin/src/main/java/org/exoplatform/account/webui/component/RegisterPortletApplicationController.java
Removed:
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/security/CaptchaServlet.java
Modified:
exo/portal/branches/3.1.x/portlet/exoadmin/src/main/java/org/exoplatform/account/webui/component/UIRegisterForm.java
exo/portal/branches/3.1.x/portlet/exoadmin/src/main/webapp/WEB-INF/portlet.xml
exo/portal/branches/3.1.x/web/portal/src/main/webapp/WEB-INF/web.xml
exo/portal/branches/3.1.x/webui/portal/src/main/java/org/exoplatform/portal/webui/CaptchaValidator.java
exo/portal/branches/3.1.x/webui/portal/src/main/java/org/exoplatform/portal/webui/UICaptcha.java
Log:
EXOGTN-153 [PLF] Implement Captcha image as a portlet resource serving
Deleted:
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/security/CaptchaServlet.java
===================================================================
---
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/security/CaptchaServlet.java 2010-11-17
09:57:11 UTC (rev 5136)
+++
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/security/CaptchaServlet.java 2010-11-17
10:15:32 UTC (rev 5137)
@@ -1,88 +0,0 @@
-/******************************************************************************
- * JBoss by Red Hat *
- * Copyright 2010, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This is free software; you can redistribute it and/or modify it *
- * under the terms of the GNU Lesser General Public License as *
- * published by the Free Software Foundation; either version 2.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
- * Lesser General Public License for more details. *
- * *
- * You should have received a copy of the GNU Lesser General Public *
- * License along with this software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
- ******************************************************************************/
-package org.exoplatform.web.security;
-
-import static nl.captcha.Captcha.NAME;
-
-import java.io.IOException;
-
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-
-import nl.captcha.Captcha;
-import nl.captcha.servlet.CaptchaServletUtil;
-
-/**
- * @author <a href="mailto:theute@redhat.com">Thomas Heute</a>
- * @version $Revision$
- */
-public class CaptchaServlet extends HttpServlet
-{
-
- private static final long serialVersionUID = 1L;
-
- private static final String PARAM_HEIGHT = "height";
-
- private static final String PARAM_WIDTH = "width";
-
- protected int _width = 200;
-
- protected int _height = 50;
-
- @Override
- public void init() throws ServletException
- {
- if (getInitParameter(PARAM_HEIGHT) != null)
- {
- _height = Integer.valueOf(getInitParameter(PARAM_HEIGHT));
- }
-
- if (getInitParameter(PARAM_WIDTH) != null)
- {
- _width = Integer.valueOf(getInitParameter(PARAM_WIDTH));
- }
- }
-
- @Override
- public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException
- {
- HttpSession session = req.getSession();
- Captcha captcha;
- if (session.getAttribute(NAME) == null)
- {
- captcha = new Captcha.Builder(_width,
_height).addText().gimp().addNoise().addBackground().build();
-
- session.setAttribute(NAME, captcha);
- CaptchaServletUtil.writeImage(resp, captcha.getImage());
-
- return;
- }
-
- captcha = (Captcha) session.getAttribute(NAME);
- CaptchaServletUtil.writeImage(resp, captcha.getImage());
- }
-
-}
Added:
exo/portal/branches/3.1.x/portlet/exoadmin/src/main/java/org/exoplatform/account/webui/component/RegisterPortletApplicationController.java
===================================================================
---
exo/portal/branches/3.1.x/portlet/exoadmin/src/main/java/org/exoplatform/account/webui/component/RegisterPortletApplicationController.java
(rev 0)
+++
exo/portal/branches/3.1.x/portlet/exoadmin/src/main/java/org/exoplatform/account/webui/component/RegisterPortletApplicationController.java 2010-11-17
10:15:32 UTC (rev 5137)
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.account.webui.component;
+
+import nl.captcha.Captcha;
+import nl.captcha.servlet.CaptchaServletUtil;
+import org.exoplatform.webui.application.portlet.PortletApplicationController;
+
+import javax.portlet.PortletException;
+import javax.portlet.PortletSession;
+import javax.portlet.ResourceRequest;
+import javax.portlet.ResourceResponse;
+import javax.portlet.ResourceServingPortlet;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+
+import static nl.captcha.Captcha.NAME;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class RegisterPortletApplicationController extends PortletApplicationController
implements ResourceServingPortlet
+{
+
+ private static final String PARAM_HEIGHT = "height";
+
+ private static final String PARAM_WIDTH = "width";
+
+ protected int _width = 200;
+
+ protected int _height = 50;
+
+ @Override
+ public void init() throws PortletException
+ {
+ if (getInitParameter(PARAM_HEIGHT) != null)
+ {
+ _height = Integer.valueOf(getInitParameter(PARAM_HEIGHT));
+ }
+
+ if (getInitParameter(PARAM_WIDTH) != null)
+ {
+ _width = Integer.valueOf(getInitParameter(PARAM_WIDTH));
+ }
+ }
+
+ public void serveResource(ResourceRequest req, ResourceResponse resp)
+ throws PortletException, java.io.IOException
+ {
+ PortletSession session = req.getPortletSession();
+ Captcha captcha;
+ if (session.getAttribute(NAME) == null)
+ {
+ captcha = new Captcha.Builder(_width,
_height).addText().gimp().addNoise().addBackground().build();
+
+
+ session.setAttribute(NAME, captcha);
+ writeImage(resp, captcha.getImage());
+
+ return;
+ }
+
+ captcha = (Captcha)session.getAttribute(NAME);
+ writeImage(resp, captcha.getImage());
+
+ }
+
+ public static void writeImage(ResourceResponse response, BufferedImage bi)
+ {
+ response.setProperty("Cache-Control",
"private,no-cache,no-store");
+ response.setContentType("image/png"); // PNGs allow for transparency.
JPGs do not.
+ try
+ {
+ CaptchaServletUtil.writeImage(response.getPortletOutputStream(), bi);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ }
+}
Modified:
exo/portal/branches/3.1.x/portlet/exoadmin/src/main/java/org/exoplatform/account/webui/component/UIRegisterForm.java
===================================================================
---
exo/portal/branches/3.1.x/portlet/exoadmin/src/main/java/org/exoplatform/account/webui/component/UIRegisterForm.java 2010-11-17
09:57:11 UTC (rev 5136)
+++
exo/portal/branches/3.1.x/portlet/exoadmin/src/main/java/org/exoplatform/account/webui/component/UIRegisterForm.java 2010-11-17
10:15:32 UTC (rev 5137)
@@ -27,6 +27,7 @@
import org.exoplatform.services.organization.UserHandler;
import org.exoplatform.web.application.ApplicationMessage;
import org.exoplatform.webui.application.WebuiRequestContext;
+import org.exoplatform.webui.application.portlet.PortletRequestContext;
import org.exoplatform.webui.config.annotation.ComponentConfig;
import org.exoplatform.webui.config.annotation.EventConfig;
import org.exoplatform.webui.core.UIApplication;
@@ -45,6 +46,7 @@
import java.util.ArrayList;
import java.util.List;
+import javax.portlet.PortletSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@@ -99,10 +101,9 @@
if(popupMessages.getWarnings().size() > 0 || popupMessages.getErrors().size()
> 0)
{
//Invalidate the capcha
- PortalRequestContext prContext = Util.getPortalRequestContext();
- HttpServletRequest request = prContext.getRequest();
- HttpSession session = request.getSession();
- session.removeAttribute(Captcha.NAME);
+ PortletRequestContext pContext = (PortletRequestContext)context;
+ PortletSession pSession = pContext.getRequest().getPortletSession(true);
+ pSession.removeAttribute(Captcha.NAME);
context.addUIComponentToUpdateByAjax(getChild(UIRegisterInputSet.class));
}
}
@@ -112,9 +113,6 @@
@Override
public void execute(Event<UIRegisterForm> event) throws Exception
{
- // Invalidate the captcha image
- PortalRequestContext prContext = Util.getPortalRequestContext();
-
UIRegisterForm registerForm = event.getSource();
OrganizationService orgService =
registerForm.getApplicationComponent(OrganizationService.class);
UserHandler userHandler = orgService.getUserHandler();
@@ -127,9 +125,9 @@
UIApplication uiApp = context.getUIApplication();
uiApp.addMessage(new
ApplicationMessage("UIRegisterForm.registerWithSuccess.message", null));
}
- HttpServletRequest request = prContext.getRequest();
- HttpSession session = request.getSession();
- session.removeAttribute(Captcha.NAME);
+ PortletRequestContext pContext =
(PortletRequestContext)event.getRequestContext();
+ PortletSession pSession = pContext.getRequest().getPortletSession(true);
+ pSession.removeAttribute(Captcha.NAME);
}
}
Modified: exo/portal/branches/3.1.x/portlet/exoadmin/src/main/webapp/WEB-INF/portlet.xml
===================================================================
---
exo/portal/branches/3.1.x/portlet/exoadmin/src/main/webapp/WEB-INF/portlet.xml 2010-11-17
09:57:11 UTC (rev 5136)
+++
exo/portal/branches/3.1.x/portlet/exoadmin/src/main/webapp/WEB-INF/portlet.xml 2010-11-17
10:15:32 UTC (rev 5137)
@@ -85,12 +85,20 @@
<description xml:lang="EN">Register Portlet</description>
<portlet-name>RegisterPortlet</portlet-name>
<display-name xml:lang="EN">Register Portlet</display-name>
-
<portlet-class>org.exoplatform.webui.application.portlet.PortletApplicationController</portlet-class>
+
<portlet-class>org.exoplatform.account.webui.component.RegisterPortletApplicationController</portlet-class>
<init-param>
<name>webui.configuration</name>
<value>/WEB-INF/conf/portlet/exoadmin/RegisterPortlet/webui/configuration.xml</value>
</init-param>
+ <init-param>
+ <name>width</name>
+ <value>200</value>
+ </init-param>
+ <init-param>
+ <name>height</name>
+ <value>75</value>
+ </init-param>
<expiration-cache>0</expiration-cache>
<supports>
Modified: exo/portal/branches/3.1.x/web/portal/src/main/webapp/WEB-INF/web.xml
===================================================================
--- exo/portal/branches/3.1.x/web/portal/src/main/webapp/WEB-INF/web.xml 2010-11-17
09:57:11 UTC (rev 5136)
+++ exo/portal/branches/3.1.x/web/portal/src/main/webapp/WEB-INF/web.xml 2010-11-17
10:15:32 UTC (rev 5137)
@@ -226,25 +226,6 @@
<load-on-startup>0</load-on-startup>
</servlet>
- <servlet>
- <servlet-name>StickyCaptcha</servlet-name>
-
<servlet-class>org.exoplatform.web.security.CaptchaServlet</servlet-class>
- <init-param>
- <param-name>width</param-name>
- <param-value>200</param-value>
- </init-param>
- <init-param>
- <param-name>height</param-name>
- <param-value>75</param-value>
- </init-param>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>StickyCaptcha</servlet-name>
- <url-pattern>/captcha</url-pattern>
- </servlet-mapping>
-
-
<!-- ================================================================= -->
<servlet-mapping>
<servlet-name>InitiateLoginServlet</servlet-name>
Modified:
exo/portal/branches/3.1.x/webui/portal/src/main/java/org/exoplatform/portal/webui/CaptchaValidator.java
===================================================================
---
exo/portal/branches/3.1.x/webui/portal/src/main/java/org/exoplatform/portal/webui/CaptchaValidator.java 2010-11-17
09:57:11 UTC (rev 5136)
+++
exo/portal/branches/3.1.x/webui/portal/src/main/java/org/exoplatform/portal/webui/CaptchaValidator.java 2010-11-17
10:15:32 UTC (rev 5137)
@@ -19,9 +19,8 @@
package org.exoplatform.portal.webui;
-import org.exoplatform.portal.application.PortalRequestContext;
-import org.exoplatform.portal.webui.util.Util;
import org.exoplatform.web.application.ApplicationMessage;
+import org.exoplatform.webui.application.portlet.PortletRequestContext;
import org.exoplatform.webui.core.UIComponent;
import org.exoplatform.webui.exception.MessageException;
import org.exoplatform.webui.form.UIForm;
@@ -32,8 +31,8 @@
import java.io.Serializable;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSession;
+import javax.portlet.PortletRequest;
+import javax.portlet.PortletSession;
/**
* @author <a href="mailto:theute@redhat.com">Thomas Heute</a>
@@ -46,9 +45,9 @@
public void validate(UIFormInput uiInput) throws Exception
{
- PortalRequestContext prContext = Util.getPortalRequestContext();
- HttpServletRequest request = prContext.getRequest();
- HttpSession session = request.getSession();
+ PortletRequestContext ctx = PortletRequestContext.getCurrentInstance();
+ PortletRequest req = ctx.getRequest();
+ PortletSession session = req.getPortletSession();
Captcha captcha = (Captcha) session.getAttribute(Captcha.NAME);
Modified:
exo/portal/branches/3.1.x/webui/portal/src/main/java/org/exoplatform/portal/webui/UICaptcha.java
===================================================================
---
exo/portal/branches/3.1.x/webui/portal/src/main/java/org/exoplatform/portal/webui/UICaptcha.java 2010-11-17
09:57:11 UTC (rev 5136)
+++
exo/portal/branches/3.1.x/webui/portal/src/main/java/org/exoplatform/portal/webui/UICaptcha.java 2010-11-17
10:15:32 UTC (rev 5137)
@@ -22,11 +22,13 @@
******************************************************************************/
package org.exoplatform.portal.webui;
-import java.util.Calendar;
-
import org.exoplatform.webui.application.WebuiRequestContext;
import org.exoplatform.webui.form.UIFormStringInput;
+import java.util.Calendar;
+import javax.portlet.RenderResponse;
+import javax.portlet.ResourceURL;
+
/**
* @author <a href="mailto:theute@redhat.com">Thomas Heute</a>
* @version $Revision$
@@ -40,9 +42,17 @@
}
public void processRender(WebuiRequestContext context) throws Exception
- {
- context.getWriter().write("<div id='" + getId() +
"'><img src=\"" + context.getPortalContextPath() +
"/captcha?v=" +
- Calendar.getInstance().getTimeInMillis() + "\"
/><br/>");
+ {
+
+ RenderResponse resp = context.getResponse();
+
+ //
+ ResourceURL url = resp.createResourceURL();
+
+ // context.getPortalContextPath() + "/captcha?v=" +
Calendar.getInstance().getTimeInMillis()
+ url.setParameter("uniqueCaptchaURL",
String.valueOf(Calendar.getInstance().getTimeInMillis()));
+
+ context.getWriter().write("<div id='" + getId() +
"'><img src=\"" + url.toString() + "\"
/><br/>");
super.processRender(context);
context.getWriter().write("</div>");
}