package org.docserv;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Component;

@Component
@Path("/directory")
@Produces(MediaType.APPLICATION_JSON)
public class DirectoryResource {
	
	private List<DocumentItem> generateDocumentList(String path) {

		File directory = Application.DOCUMENT_FOLDER;
		List<DocumentItem> documentItems = new ArrayList<>();
		URI root = Application.DOCUMENT_FOLDER.toURI();
		if (path != null && !path.trim().isEmpty()) {
			directory = new File(directory, path);
		}
		if (directory.isDirectory()) {
			for (File file : directory.listFiles()) {
				String itemPath = root.relativize(file.toURI()).toString();
				documentItems.add(new DocumentItem(itemPath, file.isDirectory()));
			}
		} else {
			throw new WebApplicationException(404);
		}
		return documentItems;
	}
	
	@GET
	public List<DocumentItem> getDirectory() throws IOException {
		return generateDocumentList(null);
	}
	
	@GET
	@Path("/{path:.*}")
	public List<DocumentItem> getDirectory(@PathParam("path") String path) throws IOException {
		return generateDocumentList(path);
	}
}

@@ -325,16 +325,79 @@
 aType;%0A%0A
+import org.springframework.beans.factory.annotation.Autowired;%0A
 import o
@@ -437,16 +437,16 @@
 ponent;%0A
-
 %0A@Compon
@@ -542,16 +542,72 @@
 rce %7B%0A%09%0A
+%09@Autowired%0A%09private DocumentFactory documentFactory;%0A%09%0A
 %09private
@@ -989,16 +989,155 @@
 es()) %7B%0A
+%09%09%09%09boolean addFile = file.isDirectory() %7C%7C documentFactory.isSupported(file); %0A%09%09%09%09if (addFile) %7B%0A%09%09%09%09%09String itemName = file.getName();%0A%09
 %09%09%09%09Stri
@@ -1196,16 +1196,17 @@
 ();%0A%09%09%09%09
+%09
 document
@@ -1232,16 +1232,26 @@
 entItem(
+itemName, 
 itemPath
@@ -1274,16 +1274,22 @@
 ry()));%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Use document factory to check support for file in folder list.