[haiku-depot-web] [haiku-depot-web-app] push by haiku.li...@xxxxxxxxx - guava update to 15.0... on 2013-11-21 17:53 GMT

  • From: haiku-depot-web-app@xxxxxxxxxxxxxx
  • To: haiku-depot-web@xxxxxxxxxxxxx
  • Date: Thu, 21 Nov 2013 17:54:00 +0000

Revision: b977902e6e35
Author:   Andrew Lindesay <apl@xxxxxxxxxxxxxx>
Date:     Mon Nov 18 08:32:38 2013 UTC
Log:      guava update to 15.0
shift captcha logic to use orm

http://code.google.com/p/haiku-depot-web-app/source/detail?r=b977902e6e35

Added:
/haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/model/Captcha.java /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/model/Response.java /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/model/auto/_Captcha.java /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/model/auto/_Response.java
 /haikudepotserver-webapp/src/main/resources/Captcha.map.xml
/haikudepotserver-webapp/src/main/resources/db/captcha/migration/V1.1__Cayenne_Conversion.sql
Modified:
 /haikudepotserver-parent/pom.xml
/haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/captcha/CaptchaService.java /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/captcha/DatabaseCaptchaRepository.java /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/captcha/model/CaptchaRepository.java
 /haikudepotserver-webapp/src/main/resources/cayenne-haikudepotserver.xml
 /haikudepotserver-webapp/src/main/resources/spring/application-context.xml

=======================================
--- /dev/null
+++ /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/model/Captcha.java Mon Nov 18 08:32:38 2013 UTC
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2013, Andrew Lindesay
+ * Distributed under the terms of the MIT License.
+ */
+
+package org.haikuos.haikudepotserver.model;
+
+import org.haikuos.haikudepotserver.model.auto._Captcha;
+
+public class Captcha extends _Captcha {
+
+    private static Captcha instance;
+
+    private Captcha() {}
+
+    public static Captcha getInstance() {
+        if(instance == null) {
+            instance = new Captcha();
+        }
+
+        return instance;
+    }
+}
=======================================
--- /dev/null
+++ /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/model/Response.java Mon Nov 18 08:32:38 2013 UTC
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2013, Andrew Lindesay
+ * Distributed under the terms of the MIT License.
+ */
+
+package org.haikuos.haikudepotserver.model;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Iterables;
+import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.exp.ExpressionFactory;
+import org.apache.cayenne.query.SelectQuery;
+import org.haikuos.haikudepotserver.model.auto._Response;
+
+import java.util.List;
+
+public class Response extends _Response {
+
+ public static Optional<Response> getByToken(ObjectContext context, String token) {
+        return Optional.fromNullable(Iterables.getOnlyElement(
+                (List<Response>) context.performQuery(new SelectQuery(
+                        Response.class,
+ ExpressionFactory.matchExp(Response.TOKEN_PROPERTY, token))),
+                null));
+    }
+
+}
=======================================
--- /dev/null
+++ /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/model/auto/_Captcha.java Mon Nov 18 08:32:38 2013 UTC
@@ -0,0 +1,12 @@
+package org.haikuos.haikudepotserver.model.auto;
+
+
+
+/**
+ * This class was generated by Cayenne.
+ * It is probably a good idea to avoid changing this class manually,
+ * since it may be overwritten next time code is regenerated.
+ * If you need to make any customizations, please use subclass.
+ */
+public class _Captcha {
+}
=======================================
--- /dev/null
+++ /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/model/auto/_Response.java Mon Nov 18 08:32:38 2013 UTC
@@ -0,0 +1,42 @@
+package org.haikuos.haikudepotserver.model.auto;
+
+import java.util.Date;
+
+import org.haikuos.haikudepotserver.model.support.AbstractDataObject;
+
+/**
+ * Class _Response was generated by Cayenne.
+ * It is probably a good idea to avoid changing this class manually,
+ * since it may be overwritten next time code is regenerated.
+ * If you need to make any customizations, please use subclass.
+ */
+public abstract class _Response extends AbstractDataObject {
+
+ public static final String CREATE_TIMESTAMP_PROPERTY = "createTimestamp";
+    public static final String RESPONSE_PROPERTY = "response";
+    public static final String TOKEN_PROPERTY = "token";
+
+    public static final String ID_PK_COLUMN = "id";
+
+    public void setCreateTimestamp(Date createTimestamp) {
+        writeProperty(CREATE_TIMESTAMP_PROPERTY, createTimestamp);
+    }
+    public Date getCreateTimestamp() {
+        return (Date)readProperty(CREATE_TIMESTAMP_PROPERTY);
+    }
+
+    public void setResponse(String response) {
+        writeProperty(RESPONSE_PROPERTY, response);
+    }
+    public String getResponse() {
+        return (String)readProperty(RESPONSE_PROPERTY);
+    }
+
+    public void setToken(String token) {
+        writeProperty(TOKEN_PROPERTY, token);
+    }
+    public String getToken() {
+        return (String)readProperty(TOKEN_PROPERTY);
+    }
+
+}
=======================================
--- /dev/null
+++ /haikudepotserver-webapp/src/main/resources/Captcha.map.xml Mon Nov 18 08:32:38 2013 UTC
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<data-map xmlns="http://cayenne.apache.org/schema/3.0/modelMap";
+        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+ xsi:schemaLocation="http://cayenne.apache.org/schema/3.0/modelMap http://cayenne.apache.org/schema/3.0/modelMap.xsd";
+        project-version="6">
+       <property name="defaultLockType" value="optimistic"/>
+ <property name="defaultPackage" value="org.haikuos.haikudepotserver.model"/>
+       <property name="defaultSchema" value="captcha"/>
+ <property name="defaultSuperclass" value="org.haikuos.haikudepotserver.model.support.AbstractDataObject"/>
+       <db-entity name="response" schema="captcha">
+ <db-attribute name="create_timestamp" type="TIMESTAMP" isMandatory="true"/> + <db-attribute name="id" type="BIGINT" isPrimaryKey="true" isMandatory="true"/> + <db-attribute name="response" type="VARCHAR" isMandatory="true" length="255"/>
+               <db-attribute name="token" type="CHAR" isMandatory="true" 
length="36"/>
+               <db-key-generator>
+                       <db-generator-type>ORACLE</db-generator-type>
+                       
<db-generator-name>captcha.response_seq</db-generator-name>
+                       <db-key-cache-size>10</db-key-cache-size>
+               </db-key-generator>
+       </db-entity>
+ <obj-entity name="Response" className="org.haikuos.haikudepotserver.model.Response" lock-type="optimistic" dbEntityName="response" superClassName="org.haikuos.haikudepotserver.model.support.AbstractDataObject"> + <obj-attribute name="createTimestamp" type="java.util.Date" db-attribute-path="create_timestamp"/> + <obj-attribute name="response" type="java.lang.String" lock="true" db-attribute-path="response"/> + <obj-attribute name="token" type="java.lang.String" lock="true" db-attribute-path="token"/>
+       </obj-entity>
+</data-map>
=======================================
--- /dev/null
+++ /haikudepotserver-webapp/src/main/resources/db/captcha/migration/V1.1__Cayenne_Conversion.sql Mon Nov 18 08:32:38 2013 UTC
@@ -0,0 +1,13 @@
+-- ------------------------------------------------------
+-- CONVERT CAPTCHA TO USE ORM
+-- ------------------------------------------------------
+
+DELETE FROM captcha.responses;
+
+ALTER TABLE captcha.responses RENAME TO response;
+
+CREATE SEQUENCE captcha.response_seq START WITH 7294 INCREMENT BY 10;
+
+ALTER TABLE captcha.response ADD COLUMN id BIGINT NOT NULL;
+
+ALTER TABLE captcha.response ADD PRIMARY KEY (id);
=======================================
--- /haikudepotserver-parent/pom.xml    Fri Nov 15 08:51:45 2013 UTC
+++ /haikudepotserver-parent/pom.xml    Mon Nov 18 08:32:38 2013 UTC
@@ -55,7 +55,7 @@
             <dependency>
                 <groupId>com.google.guava</groupId>
                 <artifactId>guava</artifactId>
-                <version>14.0.1</version>
+                <version>15.0</version>
             </dependency>

             <!-- WEB / API -->
=======================================
--- /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/captcha/CaptchaService.java Fri Nov 15 08:51:45 2013 UTC +++ /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/captcha/CaptchaService.java Mon Nov 18 08:32:38 2013 UTC
@@ -44,6 +44,10 @@
      */

     public Captcha generate() {
+
+        // maybe better done less frequently?
+        captchaRepository.purgeExpired();
+
         Captcha captcha = captchaAlgorithm.generate();
         captchaRepository.store(captcha.getToken(), captcha.getResponse());
         return captcha;
=======================================
--- /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/captcha/DatabaseCaptchaRepository.java Fri Nov 15 08:51:45 2013 UTC +++ /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/captcha/DatabaseCaptchaRepository.java Mon Nov 18 08:32:38 2013 UTC
@@ -5,39 +5,40 @@

 package org.haikuos.haikudepotserver.captcha;

+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
+import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.configuration.server.ServerRuntime;
+import org.apache.cayenne.query.EJBQLQuery;
 import org.haikuos.haikudepotserver.captcha.model.CaptchaRepository;
-import org.haikuos.haikudepotserver.support.Closeables;
+import org.haikuos.haikudepotserver.model.Response;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.stereotype.Repository;

-import javax.annotation.PostConstruct;
-import javax.annotation.Resource;
-import javax.sql.DataSource;
-import java.sql.*;
-import java.util.UUID;
+import java.sql.Timestamp;
+import java.util.Date;
 import java.util.concurrent.TimeUnit;

 /**
- * <p>This object stores the captchas in a database for later retrieval.</p> + * <p>This object stores the captchas in a database for later retrieval. It uses the Cayenne object-relational
+ * system to access the database objects.</p>
  */

 public class DatabaseCaptchaRepository implements CaptchaRepository {

protected static Logger logger = LoggerFactory.getLogger(DatabaseCaptchaRepository.class);

-    private DataSource dataSource;
+    private ServerRuntime serverRuntime;
+
     private Long expirySeconds;

-    public DataSource getDataSource() {
-        return dataSource;
+    public ServerRuntime getServerRuntime() {
+        return serverRuntime;
     }

-    public void setDataSource(DataSource dataSource) {
-        this.dataSource = dataSource;
+    public void setServerRuntime(ServerRuntime serverRuntime) {
+        this.serverRuntime = serverRuntime;
     }

     public Long getExpirySeconds() {
@@ -52,120 +53,72 @@
         purgeExpired();
     }

-    public int purgeExpired() {
-        Connection connection = null;
-        PreparedStatement preparedStatement = null;
+    @Override
+    public void purgeExpired() {
+        Preconditions.checkNotNull(serverRuntime);

-        try {
-            connection = dataSource.getConnection();
- preparedStatement = connection.prepareStatement("DELETE FROM captcha.responses WHERE create_timestamp < ?"); - preparedStatement.setTimestamp(1,new Timestamp(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(expirySeconds)));
-            int deleted = preparedStatement.executeUpdate();
+        EJBQLQuery q = new EJBQLQuery(String.format(
+ "DELETE FROM %s r WHERE r.createTimestamp < :expiryTimestamp",
+                Response.class.getSimpleName()));

-            if(0!=deleted) {
- logger.info("did delete {} expired captcha responses",deleted);
-            }
-
-            return deleted;
-        }
-        catch(SQLException se) {
- throw new RuntimeException("unable to purge expired captcha tokens",se);
-        }
-        finally {
-            Closeables.closeQuietly(preparedStatement);
-            Closeables.closeQuietly(connection);
-        }
+ q.setParameter("expiryTimestamp", new Timestamp(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(expirySeconds)));
+        getServerRuntime().getContext().performQuery(q);
     }

     @Override
     public boolean delete(String token) {
         Preconditions.checkState(!Strings.isNullOrEmpty(token));
+        Preconditions.checkNotNull(serverRuntime);

-        Connection connection = null;
-        PreparedStatement preparedStatement = null;
+        ObjectContext objectContext = serverRuntime.getContext();

-        try {
-            connection = dataSource.getConnection();
- preparedStatement = connection.prepareStatement("DELETE FROM captcha.responses WHERE token=?");
-            preparedStatement.setString(1,token);
-            int d = preparedStatement.executeUpdate();
-            logger.info("did delete captcha token {}",token);
-            return 1==d;
-        }
-        catch(SQLException se) {
- throw new RuntimeException("unable to delete captcha token",se);
-        }
-        finally {
-            Closeables.closeQuietly(preparedStatement);
-            Closeables.closeQuietly(connection);
+ Optional<Response> responseOptional = Response.getByToken(objectContext, token);
+
+        if(responseOptional.isPresent()) {
+            objectContext.deleteObjects(responseOptional.get());
+            objectContext.commitChanges();
+ logger.info("did delete captcha response with token; {}", token);
+            return true;
         }
+
+        return false;
     }

     @Override
     public String get(String token) {
         Preconditions.checkState(!Strings.isNullOrEmpty(token));
-
-        Connection connection = null;
-        PreparedStatement preparedStatement = null;
-        ResultSet resultSet = null;
-
-        try {
-            connection = dataSource.getConnection();
- preparedStatement = connection.prepareStatement("SELECT response FROM captcha.responses WHERE token=? AND create_timestamp > ?");
-            preparedStatement.setString(1,token);
- preparedStatement.setTimestamp(2,new Timestamp(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(expirySeconds)));
-            resultSet = preparedStatement.executeQuery();
-
-            if(!resultSet.next()) {
-                return null;
-            }
+        Preconditions.checkNotNull(serverRuntime);

-            String result = resultSet.getString(1);
+        ObjectContext objectContext = serverRuntime.getContext();

-            if(resultSet.next()) {
- throw new IllegalStateException("found more than one captcha for "+token);
-            }
+ Optional<Response> responseOptional = Response.getByToken(objectContext, token);

+        if(responseOptional.isPresent()) {
+            String result = responseOptional.get().getResponse();
+            delete(token);
             return result;
         }
-        catch(SQLException se) {
- throw new RuntimeException("unable to verify captcha token",se);
-        }
-        finally {
-            Closeables.closeQuietly(preparedStatement);
-            Closeables.closeQuietly(connection);
-            Closeables.closeQuietly(resultSet);
-        }
+
+        return null;
     }

     @Override
     public void store(String token, String response) {
         Preconditions.checkState(!Strings.isNullOrEmpty(token));
         Preconditions.checkState(!Strings.isNullOrEmpty(response));
+        Preconditions.checkNotNull(serverRuntime);
+
+        ObjectContext objectContext = serverRuntime.getContext();

-        Connection connection = null;
-        PreparedStatement preparedStatement = null;
+        Response r = objectContext.newObject(Response.class);

-        try {
-            connection = dataSource.getConnection();
- preparedStatement = connection.prepareStatement("INSERT INTO captcha.responses (create_timestamp, token, response) VALUES (?,?,?)"); - preparedStatement.setTimestamp(1,new Timestamp(System.currentTimeMillis()));
-            preparedStatement.setString(2,token);
-            preparedStatement.setString(3,response);
+        r.setToken(token);
+        r.setResponse(response);
+        r.setCreateTimestamp(new Date());

-            if(1!=preparedStatement.executeUpdate()) {
- throw new IllegalStateException("unable to store the captcha token "+token);
-            }
+        objectContext.commitChanges();

-            logger.info("stored captcha token {}",token);
-        }
-        catch(SQLException se) {
- throw new RuntimeException("unable to delete captcha token",se);
-        }
-        finally {
-            Closeables.closeQuietly(preparedStatement);
-            Closeables.closeQuietly(connection);
-        }
+        logger.info("stored captcha response with token {}",token);
     }

 }
=======================================
--- /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/captcha/model/CaptchaRepository.java Fri Nov 15 08:51:45 2013 UTC +++ /haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/captcha/model/CaptchaRepository.java Mon Nov 18 08:32:38 2013 UTC
@@ -15,7 +15,7 @@
      * <p>This method will remove those captchas that have expired.</p>
      */

-    public int purgeExpired();
+    public void purgeExpired();

     /**
* <p>This method will delete the captcha identified by the UUID supplied.</p>
=======================================
--- /haikudepotserver-webapp/src/main/resources/cayenne-haikudepotserver.xml Fri Nov 15 08:51:45 2013 UTC +++ /haikudepotserver-webapp/src/main/resources/cayenne-haikudepotserver.xml Mon Nov 18 08:32:38 2013 UTC
@@ -1,11 +1,13 @@
 <?xml version="1.0" encoding="utf-8"?>
 <domain project-version="6">
+       <map name="Captcha"/>
        <map name="HaikuDepot"/>

        <node name="HaikuDepotServer"
factory="org.apache.cayenne.configuration.server.XMLPoolingDataSourceFactory" schema-update-strategy="org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy"
                >
+               <map-ref name="Captcha"/>
                <map-ref name="HaikuDepot"/>
                <data-source>
                        <driver/>
=======================================
--- /haikudepotserver-webapp/src/main/resources/spring/application-context.xml Fri Nov 15 08:51:45 2013 UTC +++ /haikudepotserver-webapp/src/main/resources/spring/application-context.xml Mon Nov 18 08:32:38 2013 UTC
@@ -32,7 +32,7 @@
         </property>
         <property name="captchaRepository">
<bean class="org.haikuos.haikudepotserver.captcha.DatabaseCaptchaRepository">
-                <property name="dataSource" ref="dataSource"/>
+ <property name="serverRuntime" ref="haikuDepotServerRuntime"/> <property name="expirySeconds" value="${captcha.expiryseconds:120}"/>
             </bean>
         </property>

Other related posts:

  • » [haiku-depot-web] [haiku-depot-web-app] push by haiku.li...@xxxxxxxxx - guava update to 15.0... on 2013-11-21 17:53 GMT - haiku-depot-web-app