# HG changeset patch # User Ingo Weinzierl # Date 1307464067 0 # Node ID 22a90706d32d9f1c5e1187dc3adb255964f4f50b # Parent 2ce31a9414ff8418d944e6a56e1bec5f9517bb33 Enables the artifact server to set the TTL of a specific collection via REST call. artifacts/trunk@2070 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 2ce31a9414ff -r 22a90706d32d ChangeLog --- a/ChangeLog Tue Jun 07 11:06:54 2011 +0000 +++ b/ChangeLog Tue Jun 07 16:27:47 2011 +0000 @@ -1,3 +1,25 @@ +2011-06-07 Ingo Weinzierl + + * artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java, + artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java: + Added a setCollectionTTL() method that might be used to update the TTL + of a collection. The new value needs to be from type long. There are two + exceptions for the new values: + 1. the new value is "INF": this lets collections live forever. + 2. the new value is "DEFAULT": this sets the TTL of the collection to + the configured default value. + + * artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java: + Added a method to update the TTL attribute of a collection. + + * artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CollectionResource.java: + Dispatch the "settimetolive" action. + + * artifact-database/src/main/resources/sql/org-postgresql-driver.properties, + artifact-database/src/main/resources/sql/org-h2-driver.properties: Added + sql statements to update the TTL of a specific collection based on the + UUID of the collection. + 2011-06-07 Sascha L. Teichmann * artifact-database/src/main/java/de/intevation/artifactdatabase/state/StateEngine.java: diff -r 2ce31a9414ff -r 22a90706d32d artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java --- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java Tue Jun 07 11:06:54 2011 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java Tue Jun 07 16:27:47 2011 +0000 @@ -222,6 +222,12 @@ public static final String XPATH_COLLECTION_ITEM_ATTRIBUTE = "/art:action/art:type/art:artifact/art:attribute"; + /** + * XPath to figure out the time to live value for setting a new TTL. + */ + public static final String XPATH_COLLECTION_TTL = + "/art:action/art:type/art:ttl/@value"; + /** * This inner class allows the deferral of writing the output @@ -1485,6 +1491,59 @@ return result; } + public Document setCollectionTTL(String uuid, Document doc, CallMeta meta) + throws ArtifactDatabaseException + { + Document result = XMLUtils.newDocument(); + XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( + result, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + + Element root = ec.create("result"); + result.appendChild(root); + + String tmp = XMLUtils.xpathString( + doc, XPATH_COLLECTION_TTL, ArtifactNamespaceContext.INSTANCE); + + logger.info("Set TTL of artifact collection '" + uuid + "' to: " + tmp); + + if (tmp == null || tmp.length() == 0) { + logger.warn("No ttl for this collection specified."); + root.setTextContent(OPERATION_FAILURE); + + return result; + } + + Long ttl = null; + if ((tmp = tmp.toUpperCase()).equals("INF")) { + ttl = null; + } + else if (tmp.equals("DEFAULT")) { + ArtifactCollectionFactory acf = getArtifactCollectionFactory(); + ttl = acf.timeToLiveUntouched(null, context); + } + else { + try { + ttl = Long.valueOf(tmp); + + if (ttl < 0) { + throw new NumberFormatException("Negative value."); + } + } + catch (NumberFormatException nfe) { + logger.error("Could not determine TTL", nfe); + root.setTextContent(OPERATION_FAILURE); + return result; + } + } + + boolean success = backend.setCollectionTTL(uuid, ttl); + root.setTextContent(success ? OPERATION_SUCCESSFUL: OPERATION_FAILURE); + + return result; + } + public DeferredOutput outCollection( String collectionId, Document format, diff -r 2ce31a9414ff -r 22a90706d32d artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java --- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java Tue Jun 07 11:06:54 2011 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java Tue Jun 07 16:27:47 2011 +0000 @@ -126,6 +126,9 @@ public static final String SQL_OUTDATE_ARTIFACTS_COLLECTION = SQL.get("outdate.artifacts.collection"); + public static final String SQL_UPDATE_COLLECTION_TTL = + SQL.get("collections.update.ttl"); + public static final String SQL_OUTDATE_ARTIFACTS_USER = SQL.get("outdate.artifacts.user"); @@ -1513,5 +1516,30 @@ new CollectionItem[collectionItems.size()]) : null; } + + + public boolean setCollectionTTL(final String uuid, final Long ttl) { + if (!isValidIdentifier(uuid)) { + logger.debug("Invalid collection id: '" + uuid + "'"); + return false; + } + + return new SQLExecutor() { + public boolean doIt() throws SQLException { + prepareStatement(SQL_UPDATE_COLLECTION_TTL); + if (ttl == null) { + stmnt.setNull(1, Types.BIGINT); + } + else { + stmnt.setLong(1, ttl); + } + stmnt.setString(2, uuid); + stmnt.execute(); + conn.commit(); + + return true; + } + }.runWrite(); + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 2ce31a9414ff -r 22a90706d32d artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CollectionResource.java --- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CollectionResource.java Tue Jun 07 11:06:54 2011 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CollectionResource.java Tue Jun 07 16:27:47 2011 +0000 @@ -94,6 +94,9 @@ /** Action name for listing the artifacts of the collection.*/ public static final String ACTION_LIST_ARTIFACTS = "listartifacts"; + /** Action name for setting the ttl of a collection.*/ + public static final String ACTION_SET_TTL = "settimetolive"; + /** * Method to figure out which POST action was triggered and perform this @@ -170,6 +173,9 @@ logger.info("Retrieve attribute of artifact '" + art + "'"); out = db.getCollectionItemAttribute(identifier, art, meta); } + else if (action.equals(ACTION_SET_TTL)) { + out = db.setCollectionTTL(identifier, source, meta); + } else { throw new ArtifactDatabaseException(NO_SUCH_ACTION_MSG); } diff -r 2ce31a9414ff -r 22a90706d32d artifact-database/src/main/resources/sql/org-h2-driver.properties --- a/artifact-database/src/main/resources/sql/org-h2-driver.properties Tue Jun 07 11:06:54 2011 +0000 +++ b/artifact-database/src/main/resources/sql/org-h2-driver.properties Tue Jun 07 16:27:47 2011 +0000 @@ -97,6 +97,8 @@ AND DATEDIFF('MILLISECOND', c.last_access, CURRENT_TIMESTAMP) > c.ttl \ AND a.id NOT IN ($LOCKED_IDS$) +collections.update.ttl=UPDATE collections SET ttl = ? WHERE id = ? + collections.touch.trigger.function = \ UPDATE collections SET last_access = current_timestamp \ WHERE id IN \ diff -r 2ce31a9414ff -r 22a90706d32d artifact-database/src/main/resources/sql/org-postgresql-driver.properties --- a/artifact-database/src/main/resources/sql/org-postgresql-driver.properties Tue Jun 07 11:06:54 2011 +0000 +++ b/artifact-database/src/main/resources/sql/org-postgresql-driver.properties Tue Jun 07 16:27:47 2011 +0000 @@ -97,6 +97,8 @@ AND CURRENT_TIMESTAMP - c.last_access > (c.ttl || ' milliseconds')::interval \ AND a.id NOT IN ($LOCKED_IDS$) +collections.update.ttl=UPDATE collections SET ttl = ? WHERE gid = ?::uuid + collections.touch.by.gid =\ UPDATE collections SET last_access = CURRENT_TIMESTAMP \ WHERE gid = ?::uuid diff -r 2ce31a9414ff -r 22a90706d32d artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java --- a/artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java Tue Jun 07 11:06:54 2011 +0000 +++ b/artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java Tue Jun 07 16:27:47 2011 +0000 @@ -259,5 +259,8 @@ DeferredOutput outCollection(String collectionId, String type, Document format, CallMeta callMeta) throws ArtifactDatabaseException; + + Document setCollectionTTL(String collectionId, Document doc, CallMeta meta) + throws ArtifactDatabaseException; } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :