Author: unico
Date: Tue Jun 12 06:41:27 2012
New Revision: 1349138

URL: http://svn.apache.org/viewvc?rev=1349138&view=rev
Log:
RAVE-603 also single file resources should be able to be imported

Modified:
    
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/importing/ResourceImporter.java
    
rave/sandbox/content-services/rave-jcr-config/src/test/java/org/apache/rave/jcr/importing/ResourceImporterTest.java

Modified: 
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/importing/ResourceImporter.java
URL: 
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/importing/ResourceImporter.java?rev=1349138&r1=1349137&r2=1349138&view=diff
==============================================================================
--- 
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/importing/ResourceImporter.java
 (original)
+++ 
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/importing/ResourceImporter.java
 Tue Jun 12 06:41:27 2012
@@ -56,22 +56,28 @@ public class ResourceImporter {
      * @throws RepositoryException  when an error occurs while reading or 
writing JCR nodes
      * @throws ImportException  when there is a conflict between the resource 
to be imported and the existing content in the repository
      */
-    public void importResource(final String parentPath, final String 
resourcePath, final InputStream is, final ImportBehavior importBehavior) throws 
IOException, RepositoryException, ImportException {
+    public void importResource(final String parentPath, String resourcePath, 
final InputStream is, final ImportBehavior importBehavior) throws IOException, 
RepositoryException, ImportException {
+        resourcePath = resourcePath.equals("/") ? "" : resourcePath;
         ZipInputStream zis = new ZipInputStream(is);
         ZipEntry entry;
         while ((entry = zis.getNextEntry()) != null) {
-            if (entry.getName().startsWith(resourcePath)) {
+            if (entry.getName().startsWith(resourcePath + "/") || 
(!entry.isDirectory() && entry.getName().equals(resourcePath))) {
                 final String name = entry.getName().lastIndexOf('/') == 
entry.getName().length()-1 ? entry.getName().substring(0, 
entry.getName().length()-1) : entry.getName();
-                if (!name.equals(resourcePath)) {
-                    final String relPath = resourcePath.isEmpty() ? name : 
name.substring(resourcePath.length()+1);
-                    final String nodePath = parentPath.equals("/") ? 
parentPath + relPath : parentPath + "/" + relPath;
-                    if (entry.isDirectory()) {
-                        if (!createFolderNode(nodePath, importBehavior)) {
-                            return;
-                        }
-                    } else {
-                        createResourceNode(nodePath, entry, zis, 
importBehavior);
+                String relPath;
+                if (resourcePath.isEmpty()) {
+                    relPath = name;
+                } else if (resourcePath.equals(name)) {
+                    relPath = name.substring(name.lastIndexOf('/')+1);
+                } else {
+                    relPath = name.substring(resourcePath.length()+1);
+                }
+                final String nodePath = parentPath.equals("/") ? parentPath + 
relPath : parentPath + "/" + relPath;
+                if (entry.isDirectory()) {
+                    if (!createFolderNode(nodePath, importBehavior)) {
+                        return;
                     }
+                } else {
+                    createResourceNode(nodePath, entry, zis, importBehavior);
                 }
             }
         }

Modified: 
rave/sandbox/content-services/rave-jcr-config/src/test/java/org/apache/rave/jcr/importing/ResourceImporterTest.java
URL: 
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-jcr-config/src/test/java/org/apache/rave/jcr/importing/ResourceImporterTest.java?rev=1349138&r1=1349137&r2=1349138&view=diff
==============================================================================
--- 
rave/sandbox/content-services/rave-jcr-config/src/test/java/org/apache/rave/jcr/importing/ResourceImporterTest.java
 (original)
+++ 
rave/sandbox/content-services/rave-jcr-config/src/test/java/org/apache/rave/jcr/importing/ResourceImporterTest.java
 Tue Jun 12 06:41:27 2012
@@ -62,6 +62,16 @@ public class ResourceImporterTest extend
         checkNodes();
     }
 
+    public void testImportFileResource() throws Exception {
+        ResourceImporter resourceImporter = new ResourceImporter(session, new 
TestMimeTypeResolver());
+        InputStream is = 
Thread.currentThread().getContextClassLoader().getResource("resources.jar").openStream();
+        resourceImporter.importResource("/testroot", 
"resources/images/blank.gif", is, null);
+
+        assertTrue(session.nodeExists("/testroot/blank.gif"));
+        final Node gif = session.getNode("/testroot/blank.gif");
+        assertTrue(gif.isNodeType("nt:file"));
+    }
+
     private void checkNodes() throws RepositoryException {
         assertTrue(session.nodeExists("/testroot/empty.html"));
         final Node html = session.getNode("/testroot/empty.html");


Reply via email to