You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2015/07/15 22:34:55 UTC

[2/2] jena git commit: JENA-979: Admin operation to list files in the backup area

JENA-979: Admin operation to list files in the backup area

This closes #82


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/2ab1ca87
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/2ab1ca87
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/2ab1ca87

Branch: refs/heads/master
Commit: 2ab1ca877c477aed0069c50300067b61f1bac6e5
Parents: 4228e91
Author: Andy Seaborne <an...@apache.org>
Authored: Wed Jul 15 21:26:13 2015 +0100
Committer: Andy Seaborne <an...@apache.org>
Committed: Wed Jul 15 21:29:51 2015 +0100

----------------------------------------------------------------------
 .../jena/fuseki/mgt/ActionBackupList.java       | 94 ++++++++++++++++++++
 .../org/apache/jena/fuseki/mgt/MgtConst.java    | 11 +--
 .../src/main/webapp/WEB-INF/web.xml             | 12 ++-
 .../java/org/apache/jena/fuseki/TestAdmin.java  | 12 ++-
 4 files changed, 121 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/2ab1ca87/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/mgt/ActionBackupList.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/mgt/ActionBackupList.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/mgt/ActionBackupList.java
new file mode 100644
index 0000000..01de0ce
--- /dev/null
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/mgt/ActionBackupList.java
@@ -0,0 +1,94 @@
+/**
+ * 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.
+ */
+
+package org.apache.jena.fuseki.mgt;
+
+import static java.lang.String.format ;
+
+import java.io.File ;
+import java.io.IOException ;
+import java.nio.file.DirectoryStream ;
+import java.nio.file.Files ;
+import java.nio.file.Path ;
+import java.util.ArrayList ;
+import java.util.List ;
+import java.util.stream.Collectors ;
+
+import javax.servlet.http.HttpServletRequest ;
+import javax.servlet.http.HttpServletResponse ;
+
+import org.apache.jena.atlas.json.JsonBuilder ;
+import org.apache.jena.atlas.json.JsonValue ;
+import org.apache.jena.fuseki.server.FusekiServer ;
+import org.apache.jena.fuseki.servlets.HttpAction ;
+import org.apache.jena.fuseki.servlets.ServletOps ;
+
+/**
+ * A JSON API to list all the backups in the backup directory
+ */
+public class ActionBackupList extends ActionCtl {
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
+        doCommon(req, resp);
+    }
+
+    @Override
+    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
+        doCommon(req, resp);
+    }
+
+    @Override
+    protected void perform(HttpAction action) {
+        JsonValue result = description(action) ;
+        ServletOps.setNoCache(action.response) ;
+        ServletOps.sendJsonReponse(action, result);
+    }
+        
+    private static DirectoryStream.Filter<Path> filterVisibleFiles = (entry) -> {
+        File f = entry.toFile() ;
+        return f.isFile() && !f.isHidden() ;
+    } ;
+
+    private JsonValue description(HttpAction action) {
+        if ( ! Files.isDirectory(FusekiServer.dirBackups) )
+            ServletOps.errorOccurred(format("[%d] Backup area '%s' is not a directory", action.id, FusekiServer.dirBackups)) ;
+        
+        List<Path> paths = new ArrayList<>() ;
+        try (DirectoryStream<Path> stream = Files.newDirectoryStream(FusekiServer.dirBackups, filterVisibleFiles)) {
+            stream.forEach(paths::add) ;
+        } catch (IOException ex) {
+            action.log.error(format("[%d] Backup file list :: IOException :: %s", action.id, ex.getMessage())) ;
+            ServletOps.errorOccurred(ex);
+        }
+
+        List<String> fileNames = paths.stream().map((p)->p.getFileName().toString()).sorted().collect(Collectors.toList()) ;
+
+        JsonBuilder builder = new JsonBuilder() ;
+        builder.startObject("top") ;
+        builder.key("backups") ;
+
+        builder.startArray() ;
+        fileNames.forEach(builder::value) ;
+        builder.finishArray() ;
+
+        builder.finishObject("top") ;
+        return builder.build() ; 
+        
+    }
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/2ab1ca87/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/mgt/MgtConst.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/mgt/MgtConst.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/mgt/MgtConst.java
index e398894..7302f16 100644
--- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/mgt/MgtConst.java
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/mgt/MgtConst.java
@@ -20,11 +20,12 @@ package org.apache.jena.fuseki.mgt;
 
 /** Various contants used in the admin functions */ 
 public class MgtConst {
-    public static final String  opDump      = "dump" ;  
-    public static final String  opPing      = "ping" ;
+    public static final String  opDump          = "dump" ;  
+    public static final String  opPing          = "ping" ;
     
-    public static final String  opStats     = "stats" ;  
-    public static final String  opDatasets  = "datasets" ;
-    public static final String  opServer    = "server" ;
+    public static final String  opStats         = "stats" ;  
+    public static final String  opDatasets      = "datasets" ;
+    public static final String  opListBackups   = "backups-list" ;
+    public static final String  opServer        = "server" ;
 }
 

http://git-wip-us.apache.org/repos/asf/jena/blob/2ab1ca87/jena-fuseki2/jena-fuseki-core/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/webapp/WEB-INF/web.xml b/jena-fuseki2/jena-fuseki-core/src/main/webapp/WEB-INF/web.xml
index be68117..4c3c415 100644
--- a/jena-fuseki2/jena-fuseki-core/src/main/webapp/WEB-INF/web.xml
+++ b/jena-fuseki2/jena-fuseki-core/src/main/webapp/WEB-INF/web.xml
@@ -135,6 +135,16 @@
     <servlet-name>PingServlet</servlet-name>
     <servlet-class>org.apache.jena.fuseki.mgt.ActionPing</servlet-class>
   </servlet>
+  
+  <servlet>
+    <servlet-name>BackupListServlet</servlet-name>
+    <servlet-class>org.apache.jena.fuseki.mgt.ActionBackupList</servlet-class>
+  </servlet>
+
+  <servlet-mapping>
+    <servlet-name>BackupListServlet</servlet-name>
+    <url-pattern>/$/backups-list</url-pattern>
+  </servlet-mapping>
 
   <servlet-mapping>
    <servlet-name>DumpServlet</servlet-name>
@@ -266,4 +276,4 @@
   </filter-mapping>
   -->
 
-</web-app>
\ No newline at end of file
+</web-app>

http://git-wip-us.apache.org/repos/asf/jena/blob/2ab1ca87/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestAdmin.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestAdmin.java b/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestAdmin.java
index 29f74c3..8b836b7 100644
--- a/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestAdmin.java
+++ b/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestAdmin.java
@@ -21,6 +21,7 @@ package org.apache.jena.fuseki;
 import static org.apache.jena.fuseki.ServerTest.datasetPath ;
 import static org.apache.jena.fuseki.ServerTest.urlRoot ;
 import static org.apache.jena.fuseki.mgt.MgtConst.opDatasets ;
+import static org.apache.jena.fuseki.mgt.MgtConst.opListBackups ;
 import static org.apache.jena.fuseki.mgt.MgtConst.opPing ;
 import static org.apache.jena.fuseki.mgt.MgtConst.opServer ;
 import static org.apache.jena.fuseki.mgt.MgtConst.opStats ;
@@ -315,16 +316,23 @@ public class TestAdmin extends BaseTest {
     }
     
     @Test public void task_5() {
-        // Short ruuning task - still in info API call.
+        // Short running task - still in info API call.
         String x = execSleepTask(null, 1) ;
         checkInTasks(x) ;
     }
 
+    @Test public void list_backups_1() {
+        try ( TypedInputStream in = execHttpGet(urlRoot+"$/"+opListBackups) ) {
+            assertEqualsIgnoreCase(WebContent.contentTypeJSON, in.getContentType()) ;
+            JsonValue v = JSON.parseAny(in) ;
+            assertNotNull(v.getAsObject().get("backups")) ; 
+        }
+    }
+
     private JsonValue getTask(String taskId) {
         String url = urlRoot+"$/tasks/"+taskId ;
         return httpGetJson(url) ;
     }
-    
 
     private static JsonValue getDatasetDescription(String dsName) {
     try ( TypedInputStream in = execHttpGet(urlRoot+"$/"+opDatasets+"/"+dsName) ) {