Does System.IO.File encapsulates SMB1.0/SMB2.0? - java

I am new to SMB1.0/SMB2.0 and am trying to create a C# class library to encapsulate basic File/Directory operations like Copy, ReadAllText, Delete, etc., using https://github.com/TalAloni/SMBLibrary.
My questions are:
Is System.IO.File already encapsulate SMB1.0/SMB2.0 protocol, and do I not need to use 3rd party SMBLibrary?
I have written the following code to Move a file from one SMB share to another SMB share, but it only renames and does not Move the file from a source location to the target location. How can I do that?
public bool Move(string sourceFilePath, string targetFilePath)
{
if (!CheckLoggedIn())
{
return false;
}
object fileHandle = null;
_status = _store.CreateFile(out fileHandle,
out FileStatus _,
sourceFilePath,
AccessMask.GENERIC_ALL | AccessMask.SYNCHRONIZE,
FileAttributes.Normal,
ShareAccess.Write,
CreateDisposition.FILE_OPEN,
CreateOptions.FILE_NON_DIRECTORY_FILE | CreateOptions.FILE_SYNCHRONOUS_IO_ALERT,
null);
if (_status == NTStatus.STATUS_SUCCESS)
{
_status = _store.SetFileInformation(fileHandle, new FileRenameInformationType2()
{
FileName = targetFilePath
});
_status = _store.CloseFile(fileHandle);
}
return _status == NTStatus.STATUS_SUCCESS;
}

No, System.IO does not contain an SMB 1.0 or SMB 2.0 client.
In order to move file from one share to another, the client must read the content of the file from the source share, and then write its content to the second share. it must be copied in this manner (and then deleted from the source).

I found EzSmb https://github.com/ume05rw/EzSmb library with which you could move files within same SMB share using the following code:
// ** SMB1 NOT Supported. **
//using EzSmb;
//using System;
//using System.Threading.Tasks;
// Get Node of file.
var file = await Node.GetNode(#"192.168.0.1\ShareName\FileName.txt", "userName", "password");
// Move to child folder path.
// ** Even if you don't change the file/folder name, write the name. **
var movedFile = await file.Move(#"FolderName\RenamedFileName.txt");
Console.WriteLine(movedFile.FullPath);
// -> 192.168.0.1\ShareName\FolderName\RenamedFileName.txt
// Get Node of folder.
var folder = await Node.GetNode(#"192.168.0.1\ShareName\FolderName\SubFolderName", "userName", "password");
// Move to Parent path.
var movedFolder = await folder.Move(#"..\RenamedSubFolderName");
Console.WriteLine(movedFolder.FullPath);
// -> 192.168.0.1\ShareName\RenamedSubFolderName

Related

Domino app - how to access the source document in java during custom file attachment routine

This is a non-xpages application.
I have inherited some code that I need to tweak....this code is used in a drag&drop file attachment subform. Normally, this will create a document in a separate dedicated .nsf that stores only attachments, and uses the main document's universalid as a reference to link the two....I need to change what the reference is to the value in a field already on the main document (where the subform is).
Java is challenging to me, but all I need to do is GET the value of the field from the main document (which has not necessarily been saved yet) and write that string value onto the attachment doc in that storage database, so I think I am just needing help with one line of code.
I will paste the relevant function here and hopefully someone can tell me how I get that value, or what else they need to see what is going on here.
You can see my commented-out attempt to write the field 'parentRef' in this code
...
private void storeUploadedFile( UploadedFile uploadedFile, Database dbTarget) {
File correctedFile = null;
RichTextItem rtFiles = null;
Document doc = null;
String ITEM_NAME_FILES = "file";
try {
if (uploadedFile==null) {
return;
}
doc = dbTarget.createDocument();
doc.replaceItemValue("form", "frmFileUpload");
doc.replaceItemValue("uploadedBy", dbTarget.getParent().getEffectiveUserName() );
Utils.setDate(doc, "uploadedAt", new Date() );
doc.replaceItemValue("parentUnid", parentUnid);
//doc.replaceItemValue("parentRef", ((Document) dbTarget.getParent()).getItemValue("attachmentDocKey"));
//get uploaded file and attach it to the document
fileName = uploadedFile.getClientFileName();
File tempFile = uploadedFile.getServerFile(); //the uploaded file with a cryptic name
fileSize = tempFile.length();
targetUnid = doc.getUniversalID();
correctedFile = new java.io.File( tempFile.getParentFile().getAbsolutePath() + java.io.File.separator + fileName );
//rename the file on the OS so we can embed it with the correct (original) name
boolean success = tempFile.renameTo(correctedFile);
if (success) {
//embed original file in target document
rtFiles = doc.createRichTextItem(ITEM_NAME_FILES);
rtFiles.embedObject(lotus.domino.EmbeddedObject.EMBED_ATTACHMENT, "", correctedFile.getAbsolutePath(), null);
success = doc.save();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
com.gadjj.Utils.recycle(rtFiles, doc);
try {
if (correctedFile != null) {
//rename the temporary file back to its original name so it's automatically
//removed from the os' file system.
correctedFile.renameTo(uploadedFile.getServerFile());
}
} catch(Exception ee) { ee.printStackTrace(); }
}
}
}
...
dbTarget.getParent does not do what you think it does. It returns a Session object that is the parent session containing all your objects. Casting it to (Document) won't give you your main document.
I don't see the declaration for it, but you appear to have a variable available called parentUNID. You can use it to get a handle on the main document.
You need to use the parentUNID value in a call to getDocumentByUNID() in order to retrieve the Document object representing your main document. But in order to do that, you need the Database object for the nsf file containing the main document, and if I understand you correctly, that is a different database than targetDb.
I'm going to have to assume that you already have that Database object in a variable called parentDb, or that you know the path to the NSF and can open it. In either case, your code would look like this (without error handling):
Document parentDoc = parentDb.getDocumentByUNID(parentUNID);
doc.replaceItemvalue("parentRef", parentDoc.getItemValue("attachmentDocKey"));

How to Write/ Edit a txt file using Kotlin in Android Studio?

I am in the beginning of Android App learning phase. I have a .txt file in my assets folder consisting of strings in each line, like this-
AWOL JJ AWOL
Aaronic JJ Aaronic
Aaronical JJ Aaronical
What I want to do is just replace JJ with NN and keep rest the same. My questions are-
Is there a way to edit the already existing file, because all the solutions I found are talking about creating some other file using function File(<filename>)?
If I follow the solutions described on different websites, I can not access the file USING File(), it shows the error that the file can not be found even though I have created an empty file with the same name. I can not access the file if it is inside assets folder or inside app/src. So, instead I am using Context.assets.open(<filename>) for readin the orginal file which opens the file as inputstream. Although I don't know any other way of opening the file using File() for writing.
// FOR READING FROM ORIGINAL FILE
var inputStreamLemmDict = BufferedReader(InputStreamReader(context!!.assets.open("my_file.dict"))).readLines()
// FOR WRITING TO ANOTHER FILE
File("path_to/my_file.txt").bufferedWriter().use { out ->
inputStreamLemmDict.forEach {
var eachLineSplit = it.split("\\s".toRegex())
if (eachLineSplit[1] == "NNN") {
out.write("${eachLineSplit[0]}\tNN\t${eachLineSplit[2]}\n")
}
else {
out.write("${it}\n")
}
}
Any help is appreciated. Thank you!
Hi you can use the following solution to achieve your desired result.
UPDATE : Please try this solution to read file from assets and get your result. This solution is not tested.
Make sure yourfilename.txt is under assets folder.
try {
val inputStream:InputStream = assets.open("yourfilename.txt")
val text = inputStream.bufferedReader().use{it.readText()}
println(text)
text = text.replace("JJ".toRegex(), "NN")
f.writeText(text)
}catch (e:Exception){
Log.d(TAG, e.toString())
}
fun main(args: Array<String>) {
var lineNumber = 0
var newString = ""
File("src/dataFile").forEachLine {
++lineNumber
println("$lineNumber: $it")
newString = it.replace("JJ", "NN")
println("New string : $newString")
}
File("src/dataFile").writeText(newString)
}

AWS Creating new files from an s3 object using JAVA getting error

I have a shape file and i need to read the shape file from my java code. I used below code for reading shape file.
public class App {
public static void main(String[] args) {
File file = new File("C:\\Test\\sample.shp");
Map<String, Object> map = new HashMap<>();//
try {
map.put("url", URLs.fileToUrl(file));
DataStore dataStore = DataStoreFinder.getDataStore(map);
String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
SimpleFeatureCollection collection = source.getFeatures();
try (FeatureIterator<SimpleFeature> features = collection.features()) {
while (features.hasNext()) {
SimpleFeature feature = features.next();
SimpleFeatureType schema = feature.getFeatureType();
Class<?> geomType = schema.getGeometryDescriptor().getType().getBinding();
String type = "";
if (Polygon.class.isAssignableFrom(geomType) || MultiPolygon.class.isAssignableFrom(geomType)) {
MultiPolygon geom = (MultiPolygon) feature.getDefaultGeometry();
type = "Polygon";
if (geom.getNumGeometries() > 1) {
type = "MultiPolygon";
}
} else if (LineString.class.isAssignableFrom(geomType)
|| MultiLineString.class.isAssignableFrom(geomType)) {
} else {
}
System.out.println(feature.getDefaultGeometryProperty().getValue().toString());
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
I got the desired output. But my requirement is write an aws lambda function to read shape file. For this
1. I created a Lambda java project of s3 event. I wrote the same code inside the handleRequest. I uploaded the java lambda project as a lanbda function and added one trigger. When I am uploading a .shp file to as s3 bucket lmbda function will automatically invoked. But I am getting an error like below
java.lang.RuntimeException: java.io.FileNotFoundException: /sample.shp (No such file or directory)
I have sample.shp file inside my s3 bucket. I go through below link.
How to write an S3 object to a file?
I am getting the same error. I tried to change my code like below
S3Object object = s3.getObject(new GetObjectRequest(bucket, key));
InputStream objectData = object.getObjectContent();
map.put("url", objectData );
instead of
File file = new File("C:\\Test\\sample.shp");
map.put("url", URLs.fileToUrl(file));
:-( Now i am getting an error like below
java.lang.NullPointerException
Also I tried the below code
DataStore dataStore = DataStoreFinder.getDataStore(objectData);
instead of
DataStore dataStore = DataStoreFinder.getDataStore(map);
the error was like below
java.lang.ClassCastException:
com.amazonaws.services.s3.model.S3ObjectInputStream cannot be cast to
java.util.Map
Also I tried to add key directly to the map and also as DataStore object. Everything went wrong..:-(
Is there anyone who can help me?
It will be very helpful if someone can do it for me...
The DataStoreFinder.getDataStore method in geotools requires you to provide a map containing a key/value pair with key "url". The value associated with that "url" key needs to be a file URL like "file://host/path/my.shp".
You're trying to insert a Java input stream into the map. That won't work, because it's not a file URL.
The geotools library does not accept http/https URLs (see the geotools code here and here), so you need a file:// URL. That means you will need to download the file from S3 to the local Lambda filesystem and then provide a file:// URL pointing to that local file. To do that, here's Java code that should work:
// get the shape file from S3 to local filesystem
File localshp = new File("/tmp/download.shp");
s3.getObject(new GetObjectRequest(bucket, key), localshp);
// now store file:// URL in the map
map.put("url", localshp.getURI().getURL().toString());
If the geotools library had accepted real URLs (not just file:// URLs) then you could have avoided the download and simply created a time-limited, pre-signed URL for the S3 object and put that URL into the map.
Here's an example of how to do that:
// get current time and add one hour
java.util.Date expiration = new java.util.Date();
long msec = expiration.getTime();
msec += 1000 * 60 * 60;
expiration.setTime(msec);
// request pre-signed URL that will allow bearer to GET the object
GeneratePresignedUrlRequest gpur = new GeneratePresignedUrlRequest(bucket, key);
gpur.setMethod(HttpMethod.GET);
gpur.setExpiration(expiration);
// get URL that will expire in one hour
URL url = s3.generatePresignedUrl(gpur);

how to rename a folder in Google-storage programmatically?

I have a google-storage java client.
I want to rename a folder on the cloud.
It there a way to do it?
I saw the update post but i'm not sure how to change the name meta data.
here is my try but i don't know what to fill in "entity" and there is no oac.setName()
public void renameDirectory(String oldPath, String newName) throws IOException {
final Storage gsClient = GCSlientFactory.get(PromptoConfig.s.GCP_PROJECT_ID).getGSClient();
final URI uri = URI.create(oldPath);
ObjectAccessControl oac = new ObjectAccessControl();
oac.setId("newName");
final Storage.ObjectAccessControls.Update update = gsClient.objectAccessControls().update(BUCKET_NAME, uri.toString().replace("gs://"+BUCKET_NAME+"/", ""), "", oac);
update.execute();
}
and also:
final Storage gsClient = GCSlientFactory.get(PromptoConfig.s.GCP_PROJECT_ID).getGSClient();
final URI uri = URI.create(oldPath);
ObjectAccessControl oac = new ObjectAccessControl();
oac.set("name", newName);
final Storage.ObjectAccessControls.Update update = gsClient.objectAccessControls().update(BUCKET_NAME, uri.toString().replace("gs://"+BUCKET_NAME+"/", ""), "allUsers", oac);
update.execute();
GCS doesn't support true folders -- the namespace is flat, and the meaning of "/" is really imposed by clients (and client libraries). As such, folders can't be renamed atomically - you would need to rename each of the contained files, like what the gsutil mv command does. You can see this by running a command like:
gsutil -d mv gs://my-bucket/folder1 gs://my-bucket/folder2
The -d option will cause it to output the sequence of requests gsutil generates to do the rename.

Generating a canonical path

Does any one know of any Java libraries I could use to generate canonical paths (basically remove back-references).
I need something that will do the following:
Raw Path -> Canonical Path
/../foo/ -> /foo
/foo/ -> /foo
/../../../ -> /
/./foo/./ -> /foo
//foo//bar -> /foo/bar
//foo/../bar -> /bar
etc...
At the moment I lazily rely on using:
new File("/", path).getCanonicalPath();
But this resolves the path against the actual file system, and is synchronised.
java.lang.Thread.State: BLOCKED (on object monitor)
at java.io.ExpiringCache.get(ExpiringCache.java:55)
- waiting to lock <0x93a0d180> (a java.io.ExpiringCache)
at java.io.UnixFileSystem.canonicalize(UnixFileSystem.java:137)
at java.io.File.getCanonicalPath(File.java:559)
The paths that I am canonicalising do not exist on my file system, so just the logic of the method will do me fine, thus not requiring any synchronisation. I'm hoping for a well tested library rather than having to write my own.
I think you can use the URI class to do this; e.g. if the path contains no characters that need escaping in a URI path component, you can do this.
String normalized = new URI(path).normalize().getPath();
If the path contains (or might contain) characters that need escaping, the multi-argument constructors will escape the path argument, and you can provide null for the other arguments.
Notes:
The above normalizes a file path by treating it as a relative URI. If you want to normalize an entire URI ... including the (optional) scheme, authority, and other components, don't call getPath()!
URI normalization does not involve looking at the file system as File canonicalization does. But the flip side is that normalization behaves differently to canonicalization when there are symbolic links in the path.
Using Apache Commons IO (a well-known and well-tested library)
public static String normalize(String filename)
will do exactly what you're looking for.
Example:
String result = FilenameUtils.normalize(myFile.getAbsolutePath());
If you don't need path canonization but only normalization, in Java 7 you can use java.nio.file.Path.normalize method.
According to http://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html:
This method does not access the file system; the path may not locate a file that exists.
If you work with File object you can use something like this:
file.toPath().normalize().toFile()
You could try an algorithm like this:
String collapsePath(String path) {
/* Split into directory parts */
String[] directories = path.split("/");
String[] newDirectories = new String[directories.length];
int i, j = 0;
for (i=0; i<directories.length; i++) {
/* Ignore the previous directory if it is a double dot */
if (directories[i].equals("..") && j > 0)
newDirectories[j--] = "";
/* Completely ignore single dots */
else if (! directories[i].equals("."))
newDirectories[j++] = directories[i];
}
/* Ah, what I would give for String.join() */
String newPath = new String();
for (i=0; i < j; i++)
newPath = newPath + "/" + newDirectories[i];
return newPath;
}
It isn't perfect; it's linear over the number of directories but does make a copy in memory.
Which kind of path is qualified as a Canonical Path is OS dependent.
That's why Java need to check it on the filesystem.
So there's no simple logic to test the path without knowing the OS.
So, while normalizing can do the trick, here is a procedure that exposes a little more of the Java API than would simply calling Paths.normalize()
Say I want to find a file that is not in my current directory on the file system.
My working code file is
myproject/src/JavaCode.java
Located in myproject/src/. My file is in
../../data/myfile.txt
I'm testing my program running my code from JavaCode.java
public static void main(String[] args) {
findFile("../../data","myfile.txt");
System.out.println("Found it.");
}
public static File findFile(String inputPath, String inputFile) {
File dataDir = new File("").getAbsoluteFile(); // points dataDir to working directory
String delimiters = "" + '\\' + '/'; // dealing with different system separators
StringTokenizer st = new StringTokenizer(inputPath, delimiters);
while(st.hasMoreTokens()) {
String s = st.nextToken();
if(s.trim().isEmpty() || s.equals("."))
continue;
else if(s.equals(".."))
dataDir = dataDir.getParentFile();
else {
dataDir = new File(dataDir, s);
if(!dataDir.exists())
throw new RuntimeException("Data folder does not exist.");
}
}
return new File(dataDir, inputFile);
}
Having placed a file at the specified location, this should print "Found it."
I'm assuming you have strings and you want strings, and you have Java 7 available now, and your default file system uses '/' as a path separator, so try:
String output = FileSystems.getDefault().getPath(input).normalize().toString();
You can try this out with:
/**
* Input Output
* /../foo/ -> /foo
* /foo/ -> /foo
* /../../../ -> /
* /./foo/./ -> /foo
* //foo//bar -> /foo/bar
* //foo/../bar -> /bar
*/
#Test
public void testNormalizedPath() throws URISyntaxException, IOException {
String[] in = new String[]{"/../foo/", "/foo/", "/../../../", "/./foo/./",
"//foo/bar", "//foo/../bar", "/", "/foo"};
String[] ex = new String[]{"/foo", "/foo", "/", "/foo", "/foo/bar", "/bar", "/", "/foo"};
FileSystem fs = FileSystems.getDefault();
for (int i = 0; i < in.length; i++) {
assertEquals(ex[i], fs.getPath(in[i]).normalize().toString());
}
}

Categories