How to read a file system using Spring - java

What I need: I need a method that, given a specific directory name on my app, will return his content.
/root
/folder1
/folder11
/folder12
/folder2
/folder21
/file21.txt
/folder3
/file31.txt
/file32.txt
For example, if I call myMethod("/root"); it will return to me a certain object list containing {'folder1', 'folder2', 'folder3'}. Calling myMethod("/root/folder2"); will return {'folder21', 'file21.txt'}
Question: how can I implement this kind of function using Spring?

You can use java.io.File if it is a directory you list childs with docx.list() and then recursively you can do that for all childs that are directories

Related

Copy S3 objects with "subdirectories" using s3Client.copyObject method

I have an S3-Bucket with two files:
s3://bucketA/objectA/objectB/fileA
s3://bucketA/objectA/objectB/fileB
I want to use the s3Client in java to create a copy of objectA known as objectC using the copyObject method of s3Client.
s3://bucketA/objectA/ ---Copy-To---> s3://bucketA/objectC/
The problem is the contents of objectA are not being copied into objectC. Object C does not contain object B and fileA and fileB. How can I copy the contents of the object as well?
Here is my code: (I am using kotlin)
s3client.copyObject(CopyObjectRequest("bucketA", "objectA","bucketA", "objectC"))
I checked in the S3 console and this works (it creates a folder called objectC, but I'm unable to get the contents of objectA into object C.)
What is happening is that using the SDK you're not making a recursive copy of the objects.
So the easiest solution is to use the AWS CLI
aws s3 cp s3://source-awsexamplebucket s3://source-awsexamplebucket --recursive --storage-class STANDARD
Note that you've to take into consideration the size of the objects, the amount, etc. If its something too big a batch mechanism could be made to help your system cope with load. You can read it further on the AWS documentation.
Now, and assuming you need to be doing that programmatically. The algorithm has 2 parts listing + copying. Something along those lines will work.
ListObjectsV2Result result = s3.listObjectsV2(from_bucket);
List<S3ObjectSummary> objects = result.getObjectSummaries();
for (S3ObjectSummary os : objects) {
s3.copyObject(from_bucket, os.getKey(), to_bucket, os.getKey());
}
// exception handling and error handling ommited for brevity

AEM asset Is there any way to move a file by creating a folder in a workflow in java

I want to move few assets by creating a new folder using only the workflow in java.I dont want to create the folders manually and then move the assets as there are 10000s of assets that are to be moved to different folders.
If you are looking at creating folder using workflow - A folder in AEM is nothing but a node of jcr:primaryType either sling:Folder or sling:OrderedFolder. If you have com.day.cq.commons.jcr in your classpath, createPath method will help you create a node if it does not exist.
You could also use addNode method followed by setProperty method from javax.jcr.Node api to create this folder of appropriate primary type.
Moving assets to this newly created node(folder), can proceed after this. You could use the clone method from javax.jcr.WorkSpace which has an option to remove the existing node.
There is another straight forward way to move assets.
I would recommend you to use built-in com.adobe.granite.asset.api.AssetManager api to perform CRUD operations on DAM assets.
session = resourceResolver.adaptTo(Session.class);
String assetPath = "/content/dam/folderA/asset1.jpg";
String movePath = "/content/dam/folderB/asset1.jpg";
assetManager.moveAsset(assetPath, copyPath);
session.save()
session.logout()
Further references for AssetManager API.
HelpX Article
API Details
Moving large number of assets might cause the move operation to fail if there no appropriate indexes in place. Monitor logs for warning messages like The query read or traversed more than X nodes.. You might have to add oak based properties to the out-of-the-box /oak:index/ntBaseLucene index to fix this.
More details here.

In Java how do I create a structured tree using FileVisitor

Given a starting path I want to create a tree like object representing the filesystem using Java, showing all the folders down to level x but not ordinary files. . So using FileVisitor I know that every time just before Im going to browse a new subfolder that the preVisitDirectory() method will be called and once it had completed parsing all its children the postVisitDirectory() will be called, but my problem is knowing how to attach this directory to its parent.
i.e in my case I want to create data for jstree using ul/li/ul/li elements, and Im doing this with j2html lib. So create root using ul(), then when I go into preVisitDirectory() I would create a li() element and in postVisitDirectory() would want to attach to ul() using ul().with(li) but I cant see how to keep track of where I am in building my tree.
e.g static hard coded example not actually browsing tree
public Tag createBrowseTreeAsHtml()
{
Tag ulTag = ul(
li("ChildNode 2").withId("child_node_1"),
li("ChildNode")
);
Tag divTag= div(
ul(
li("Root Node 1").with(ulTag),
li("Root Node 2")
)
)
.withId("jstree");
return div().with(divTag);
}
I see Guava has support for Graphs, should I be utilising this somehow ?

Accessing File property folderType

I notice that the File class has a property called folderType, but I cannot see anyway to get this String at run-time. Is there a way? Edit: perhaps it's not part of File.
When I rest my mouse over file in the fileIO.open
The object you have there is a Win32ShellFolder2, which is a subclass of ShellFolder, which is a subclass of java.io.File. ShellFolder defines a getter named getFolderType() which returns the folder type as a string.
So you could get the contents of the field like this:
file = fileChooser.getSelectedFile();
if (file instanceof ShellFolder) {
ShellFolder sf = (ShellFolder)file;
String folderType = sf.getFolderType();
ShellFolder and Win32ShellFolder2 are in the package sun.awt.shell. This package isn't part of the standard Java API, so it could change from one JVM to another or from version of the JVM to another. See With what should I replace sun.awt.shell.ShellFolder so I don't get compile warnings?.
There are many ways one of them is this MimetypesFileTypeMap().getContentType(file)
and i think fileio is object of class Win32ShellFolder2,this has a public function getFolderType() as mentioned here
so you can use it like this i feel
fileIO.open(file).getFolderType()

Scala Lift - Reading a file from "/resources/toserve"

I'm attempting to provide a StreamingResponse for files stored under Lifts resources/toserve directory, in order to authorize access for different users.
I can access an image for example with:
localhost:8080/classpath/images/test.jpg
But when I try and actually read the file using scala I keep getting file not found exceptions:
val file = new java.io.FileInputStream("/classpath/images/test.jpg")
Is there a specific method to reading files located on classpath?
Thanks in advance, much appreciated :)
To read resources from the toserve-directory you need to do a call like
LiftRules.getResource("/toserve/images/test.jpg")
If you try to use 'classpath' instead of 'toserve' you will receive an empty box.
By default, Lift uses two different path-prefixes to locate resources either programmatically within the server or through a link-element from HTML. For the former, you will use the 'toserve'-prefix, for the latter the 'classpath'-prefix.
This behavior is specified in the objects net.liftweb.http.LiftRules and net.liftweb.http.ResourceServer. In particular, you can there specify (i.e. replace) the path to the resources. The relevant code is:
/** (from net.liftweb.http.ResourceServer)
* The base package for serving resources. This way, resource names can't be spoofed
*/
var baseResourceLocation = "toserve"
You might also want to look at the following method in LiftRules, which allows you to redefine the name used to serve resources through the server:
/** (from net.liftweb.http.LiftRules)
* The path to handle served resources
*/
#volatile var resourceServerPath = "classpath"
If you wish to use the same prefix to refer both to resources you can use either (or both) of these settings to achieve your purpose.
Have you tried:
LiftRules.getResource("/classpath/images/test.jpg")
That should return a java.net.URL to the item you are looking for.
This may also have more information on what you are looking to do: http://exploring.liftweb.net/master/index-9.html#lst:streaming-download

Categories

Resources