Author: jacopoc
Date: Fri May 11 19:22:18 2012 New Revision: 1337335 URL: http://svn.apache.org/viewvc?rev=1337335&view=rev Log: Applied fix from trunk for revision: 1335047 === First pass in fixing the buggy code for captcha support; this is a first pass (the next one will probably be to save the image in the session rather than in the file system); the main problems fixed are related to concurrent access: the class was not thread safe and this, especially in servers with high traffic, could cause a series of issues like: * captcha images created and then lost * captcha images created in the wrong location (for example out of the OFBiz folder) * the folder runtime/tempfiles/captcha could be locked Modified: ofbiz/branches/release11.04/ (props changed) ofbiz/branches/release11.04/framework/common/src/org/ofbiz/common/Captcha.java ofbiz/branches/release11.04/specialpurpose/ecommerce/webapp/ecommerce/customer/AnonContactus.ftl ofbiz/branches/release11.04/specialpurpose/myportal/script/org/ofbiz/myportal/Events.xml ofbiz/branches/release11.04/specialpurpose/myportal/widget/reloadCaptchaCode.ftl ofbiz/branches/release11.04/specialpurpose/myportal/widget/reloadCaptchaImage.ftl Propchange: ofbiz/branches/release11.04/ ------------------------------------------------------------------------------ Merged /ofbiz/trunk:r1335047 Modified: ofbiz/branches/release11.04/framework/common/src/org/ofbiz/common/Captcha.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release11.04/framework/common/src/org/ofbiz/common/Captcha.java?rev=1337335&r1=1337334&r2=1337335&view=diff ============================================================================== --- ofbiz/branches/release11.04/framework/common/src/org/ofbiz/common/Captcha.java (original) +++ ofbiz/branches/release11.04/framework/common/src/org/ofbiz/common/Captcha.java Fri May 11 19:22:18 2012 @@ -38,12 +38,13 @@ import org.ofbiz.base.util.UtilDateTime; public class Captcha { - public static String ID_KEY = null; - public static String CAPTCHA_FILE_NAME = null; - public static String CAPTCHA_FILE_PATH = null; + public static final String CAPTCHA_FILE_PATH = System.getProperty("ofbiz.home") + File.separator + "runtime" + File.separator + "tempfiles" + File.separator + "captcha" + File.separator; - public static String getCodeCaptcha(HttpServletRequest request,HttpServletResponse response) { - if (CAPTCHA_FILE_PATH != null) deleteFile(); + public static String getCodeCaptcha(HttpServletRequest request, HttpServletResponse response) { + File test = new File(CAPTCHA_FILE_PATH); + if (!test.exists()) { + test.mkdir(); + } StringBuilder finalString = new StringBuilder(); String elegibleChars = "ABCDEFGHJKLMPQRSTUVWXYabcdefhjkmnpqrstuvwxy23456789"; int charsToPrint = 6; @@ -55,20 +56,15 @@ public class Captcha { char characterToShow = chars[randomIndex]; finalString.append(characterToShow); } - ID_KEY = finalString.toString(); - if (createImageCaptcha (request,response)) return "success"; - return "error"; - } + String idKey = finalString.toString(); - public static boolean createImageCaptcha (HttpServletRequest request,HttpServletResponse response) { try { - //It is possible to pass the font size, image width and height with the request as well + // 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, paramInt(request, "fontSize", 22)); - int charsToPrint = 6; int width = paramInt(request, "width", 149); int height = paramInt(request, "height", 40); int circlesToDraw = 6; @@ -97,7 +93,7 @@ public class Captcha { int fontHeight = fontMetrics.getHeight(); //We are not using certain characters, which might confuse users - String characterToShow = ID_KEY; + String characterToShow = idKey; float spaceForLetters = -horizMargin * 2 + width; float spacePerChar = spaceForLetters / (charsToPrint - 1.0f); @@ -134,40 +130,16 @@ public class Captcha { g.setColor(borderColor); g.drawRect(0, 0, width - 1, height - 1); g.dispose(); - Captcha.writeImage(bufferedImage, request); - } catch (Exception ioe) { - return false; - } - //Adding this because we called response.getOutputStream() above. This will prevent and illegal state exception being thrown - return true; - } + String captchaFileName = UtilDateTime.nowAsString().concat(".jpg"); + request.setAttribute("captchaFileName", "/tempfiles/captcha/" + captchaFileName); + request.setAttribute("ID_KEY", idKey); + ImageIO.write(bufferedImage, "jpg", new File(CAPTCHA_FILE_PATH + captchaFileName)); - public static void writeImage(BufferedImage image, HttpServletRequest request) - { - try { - String FILE_PATH = File.separator + "runtime" + File.separator + "tempfiles" + File.separator + "captcha" + File.separator; - String URL_FILE_PATH = "/tempfiles/captcha/"; - CAPTCHA_FILE_PATH = new File(".").getCanonicalPath(); - CAPTCHA_FILE_PATH += FILE_PATH; - File test = new File(CAPTCHA_FILE_PATH); - if (!test.exists()) { - test.mkdir(); - } - CAPTCHA_FILE_NAME = UtilDateTime.nowAsString().concat(".jpg"); - request.setAttribute("captchaFileName", URL_FILE_PATH + CAPTCHA_FILE_NAME); - request.setAttribute("ID_KEY", ID_KEY); - ImageIO.write(image, "jpg", new File(CAPTCHA_FILE_PATH + CAPTCHA_FILE_NAME)); - } catch (IOException e) { - return; - } - } - - public static void deleteFile() { - if (CAPTCHA_FILE_PATH != null) { - File file = new File(CAPTCHA_FILE_PATH); - file.delete(); + } catch (Exception ioe) { + return "error"; } + return "success"; } public static String paramString(HttpServletRequest request, String paramName, Modified: ofbiz/branches/release11.04/specialpurpose/ecommerce/webapp/ecommerce/customer/AnonContactus.ftl URL: http://svn.apache.org/viewvc/ofbiz/branches/release11.04/specialpurpose/ecommerce/webapp/ecommerce/customer/AnonContactus.ftl?rev=1337335&r1=1337334&r2=1337335&view=diff ============================================================================== --- ofbiz/branches/release11.04/specialpurpose/ecommerce/webapp/ecommerce/customer/AnonContactus.ftl (original) +++ ofbiz/branches/release11.04/specialpurpose/ecommerce/webapp/ecommerce/customer/AnonContactus.ftl Fri May 11 19:22:18 2012 @@ -132,4 +132,4 @@ under the License. </form> </div> </#if> -</div> \ No newline at end of file +</div> Modified: ofbiz/branches/release11.04/specialpurpose/myportal/script/org/ofbiz/myportal/Events.xml URL: http://svn.apache.org/viewvc/ofbiz/branches/release11.04/specialpurpose/myportal/script/org/ofbiz/myportal/Events.xml?rev=1337335&r1=1337334&r2=1337335&view=diff ============================================================================== --- ofbiz/branches/release11.04/specialpurpose/myportal/script/org/ofbiz/myportal/Events.xml (original) +++ ofbiz/branches/release11.04/specialpurpose/myportal/script/org/ofbiz/myportal/Events.xml Fri May 11 19:22:18 2012 @@ -137,6 +137,5 @@ under the License. </else> </if-compare> <check-errors error-list-name="error_list" error-code="resultPage"/> - <call-class-method class-name="org.ofbiz.common.Captcha" method-name="deleteFile"/> </simple-method> </simple-methods> Modified: ofbiz/branches/release11.04/specialpurpose/myportal/widget/reloadCaptchaCode.ftl URL: http://svn.apache.org/viewvc/ofbiz/branches/release11.04/specialpurpose/myportal/widget/reloadCaptchaCode.ftl?rev=1337335&r1=1337334&r2=1337335&view=diff ============================================================================== --- ofbiz/branches/release11.04/specialpurpose/myportal/widget/reloadCaptchaCode.ftl (original) +++ ofbiz/branches/release11.04/specialpurpose/myportal/widget/reloadCaptchaCode.ftl Fri May 11 19:22:18 2012 @@ -17,6 +17,6 @@ specific language governing permissions under the License. --> -<#assign idkey = Static["org.ofbiz.common.Captcha"].ID_KEY> +<#assign idkey = requestAttributes.ID_KEY?if_exists> <input type="hidden" value="${idkey?if_exists}" name="captchaCode"/> \ No newline at end of file Modified: ofbiz/branches/release11.04/specialpurpose/myportal/widget/reloadCaptchaImage.ftl URL: http://svn.apache.org/viewvc/ofbiz/branches/release11.04/specialpurpose/myportal/widget/reloadCaptchaImage.ftl?rev=1337335&r1=1337334&r2=1337335&view=diff ============================================================================== --- ofbiz/branches/release11.04/specialpurpose/myportal/widget/reloadCaptchaImage.ftl (original) +++ ofbiz/branches/release11.04/specialpurpose/myportal/widget/reloadCaptchaImage.ftl Fri May 11 19:22:18 2012 @@ -19,6 +19,6 @@ under the License. <#-- For add Captcha Capture --> <#assign fileName = Static["org.ofbiz.common.Captcha"].getCodeCaptcha(request,response)> -<#assign fileName = Static["org.ofbiz.common.Captcha"].CAPTCHA_FILE_NAME> +<#assign fileName = requestAttributes.captchaFileName?if_exists> <img src="<@ofbizContentUrl>/tempfiles/captcha/${fileName?if_exists}</@ofbizContentUrl>" alt="" /> |
Free forum by Nabble | Edit this page |