Author: lektran
Date: Fri May 11 08:23:02 2012 New Revision: 1337057 URL: http://svn.apache.org/viewvc?rev=1337057&view=rev Log: Copied the captcha image generator into a single event that streams the image to the response for a direct request rather than trying to store it during screen rendering to be served up later. Added: ofbiz/trunk/framework/common/config/captcha.properties (with props) Modified: ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml Added: ofbiz/trunk/framework/common/config/captcha.properties URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/config/captcha.properties?rev=1337057&view=auto ============================================================================== --- ofbiz/trunk/framework/common/config/captcha.properties (added) +++ ofbiz/trunk/framework/common/config/captcha.properties Fri May 11 08:23:02 2012 @@ -0,0 +1,28 @@ +############################################################################### +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +############################################################################### +# +# Define captcha size configurations in the following manner: +# captcha.sizename=fontsize|imageheight|imagewidth +# You can then pass the sizename as a parameter to the captcha image generator using the parameter "captchaSize" +# e.g. https://localhost:8443/ecommerce/control/getCaptchaImage?captchaSize=default +captcha.default=22|40|149 +# The number of characters to use in the captcha +captcha.code_length=6 +# The character set to use +captcha.characters=abcdefhjkmnpstuvwxy2345678 \ No newline at end of file Propchange: ofbiz/trunk/framework/common/config/captcha.properties ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/common/config/captcha.properties ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/common/config/captcha.properties ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java?rev=1337057&r1=1337056&r2=1337057&view=diff ============================================================================== --- ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java (original) +++ ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java Fri May 11 08:23:02 2012 @@ -18,6 +18,12 @@ *******************************************************************************/ package org.ofbiz.common; +import java.awt.Color; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics2D; +import java.awt.geom.AffineTransform; +import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; @@ -28,8 +34,10 @@ import java.util.Locale; import java.util.Map; import java.util.Set; +import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import javolution.util.FastMap; import net.sf.json.JSON; @@ -37,6 +45,8 @@ import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; +import org.apache.commons.lang.RandomStringUtils; +import org.apache.commons.lang.StringUtils; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.StringUtil; import org.ofbiz.base.util.UtilGenerics; @@ -368,10 +378,96 @@ public class CommonEvents { writeJSONtoResponse(jsonUiLabel, response); return "success"; } -} - - - - + public static String getCaptcha(HttpServletRequest request,HttpServletResponse response) { + try { + final String captchaSizeConfigName = StringUtils.defaultIfEmpty(request.getParameter("captchaSize"), "default"); + final String captchaSizeConfig = UtilProperties.getPropertyValue("captcha.properties", "captcha." + captchaSizeConfigName); + final String[] captchaSizeConfigs = captchaSizeConfig.split("\\|"); + + final int fontSize = Integer.parseInt(captchaSizeConfigs[0]); + final int height = Integer.parseInt(captchaSizeConfigs[1]); + final int width = Integer.parseInt(captchaSizeConfigs[2]); + final int charsToPrint = UtilProperties.getPropertyAsInteger("captcha.properties", "captcha.code_length", 6); + final char[] availableChars = UtilProperties.getPropertyValue("captcha.properties", "captcha.characters").toCharArray(); + + //It is possible to pass the font size, image width and height with the request as well + Color backgroundColor = Color.gray; + Color borderColor = Color.DARK_GRAY; + Color textColor = Color.ORANGE; + Color circleColor = new Color(160, 160, 160); + Font textFont = new Font("Arial", Font.PLAIN, fontSize); + int circlesToDraw = 6; + float horizMargin = 20.0f; + double rotationRange = 0.7; // in radians + BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + + Graphics2D g = (Graphics2D) bufferedImage.getGraphics(); + + g.setColor(backgroundColor); + g.fillRect(0, 0, width, height); + + //Generating some circles for background noise + g.setColor(circleColor); + for (int i = 0; i < circlesToDraw; i++) { + int circleRadius = (int) (Math.random() * height / 2.0); + int circleX = (int) (Math.random() * width - circleRadius); + int circleY = (int) (Math.random() * height - circleRadius); + g.drawOval(circleX, circleY, circleRadius * 2, circleRadius * 2); + } + g.setColor(textColor); + g.setFont(textFont); + + FontMetrics fontMetrics = g.getFontMetrics(); + int maxAdvance = fontMetrics.getMaxAdvance(); + int fontHeight = fontMetrics.getHeight(); + + String captchaCode = RandomStringUtils.random(6, availableChars); + + float spaceForLetters = -horizMargin * 2 + width; + float spacePerChar = spaceForLetters / (charsToPrint - 1.0f); + + for (int i = 0; i < captchaCode.length(); i++) { + + // this is a separate canvas used for the character so that + // we can rotate it independently + int charWidth = fontMetrics.charWidth(captchaCode.charAt(i)); + int charDim = Math.max(maxAdvance, fontHeight); + int halfCharDim = (charDim / 2); + + BufferedImage charImage = + new BufferedImage(charDim, charDim, BufferedImage.TYPE_INT_ARGB); + Graphics2D charGraphics = charImage.createGraphics(); + charGraphics.translate(halfCharDim, halfCharDim); + double angle = (Math.random() - 0.5) * rotationRange; + charGraphics.transform(AffineTransform.getRotateInstance(angle)); + charGraphics.translate(-halfCharDim, -halfCharDim); + charGraphics.setColor(textColor); + charGraphics.setFont(textFont); + + int charX = (int) (0.5 * charDim - 0.5 * charWidth); + charGraphics.drawString("" + captchaCode.charAt(i), charX, + ((charDim - fontMetrics.getAscent()) / 2 + fontMetrics.getAscent())); + + float x = horizMargin + spacePerChar * (i) - charDim / 2.0f; + int y = ((height - charDim) / 2); + + g.drawImage(charImage, (int) x, y, charDim, charDim, null, null); + + charGraphics.dispose(); + } + // Drawing the image border + g.setColor(borderColor); + g.drawRect(0, 0, width - 1, height - 1); + g.dispose(); + response.setContentType("image/jpeg"); + ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); + HttpSession session = request.getSession(); + session.setAttribute("_CAPTCHA_CODE_", captchaCode); + } catch (Exception ioe) { + Debug.logError(ioe.getMessage(), module); + } + return "success"; + } +} Modified: ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml?rev=1337057&r1=1337056&r2=1337057&view=diff ============================================================================== --- ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml (original) +++ ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml Fri May 11 08:23:02 2012 @@ -201,6 +201,13 @@ under the License. <response name="success" type="view" value="main"/> </request-map> + <!-- Common captcha image request, the captcha code is stored in the session using _CAPTCHA_CODE_ as the key --> + <request-map uri="captcha.jpg"> + <security https="false" auth="false"/> + <event type="java" path="org.ofbiz.common.CommonEvents" invoke="getCaptcha"/> + <response name="success" type="none"/> + </request-map> + <!-- Lookup requests --> <request-map uri="LookupGeo"><security https="true" auth="true"/><response name="success" type="view" value="LookupGeo"/></request-map> <request-map uri="LookupGeoName"><security https="true" auth="true"/><response name="success" type="view" value="LookupGeoName"/></request-map> Modified: ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml?rev=1337057&r1=1337056&r2=1337057&view=diff ============================================================================== --- ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml (original) +++ ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml Fri May 11 08:23:02 2012 @@ -1952,7 +1952,13 @@ under the License. <response name="success" type="view" value="ProductUomDropDownOnly" save-last-view="true"/> </request-map> - <!-- Captcha --> + <!-- Captcha: Image request, the captcha code is stored in the session using _CAPTCHA_CODE_ as the key --> + <request-map uri="captcha.jpg"> + <security https="true" auth="false"/> + <event type="java" path="org.ofbiz.common.CommonEvents" invoke="getCaptcha"/> + <response name="success" type="none"/> + </request-map> + <request-map uri="reloadCaptchaImage"> <security https="true" auth="false"/> <response name="success" type="view" value="reloadCaptchaImage"/> |
Free forum by Nabble | Edit this page |