Trying to move from jcifs to jcifs-ng (the latest jar jcifs-ng-2.1.2.jar) to copy files to/from remote.
My code using old jcifs:
System.setProperty("jcifs.smb.client.responseTimeout", "10000");
System.setProperty("jcifs.smb.client.soTimeout", "2000");
if (winsIPList.trim().equals("")) {
System.setProperty("jcifs.smb.client.dfs.disabled", "true");
} else {
System.setProperty("jcifs.smb.client.dfs.disabled", "false");
System.setProperty("jcifs.netbios.wins", winsIPList.trim());
System.setProperty("resolveOrder", "DNS");
}
NtlmPasswordAuthentication auth = new
NtlmPasswordAuthentication(filesrvDomainIP, filesrvDomainUser,
filesrvDomainPassword);
smbRemoteFile = new SmbFile("smb:" + remoteFile.replace("\\", "/"), auth);
<here the code to copy file>
Found few examples in stackoverflow, but looks like they are old.
Part of them include usage of NtlmPasswordAuthentication(context, DomainIP, DomainUser,DomainPassword) which is deprecated in the last jcifs-ng package.
Others use
SmbFile smbRemoteFile = new SmbFile(remoteFile, someContext)
which is reported as undefined by compiler
Could somebody provide an example that works?
Working example:
BaseContext baseCxt = null;
Properties jcifsProperties = new Properties();
jcifsProperties.setProperty("jcifs.smb.client.enableSMB2", "true");
jcifsProperties.setProperty("jcifs.smb.client.dfs.disabled","true");
Configuration config = new PropertyConfiguration(jcifsProperties);
baseCxt = new BaseContext(config);
auth = baseCxt.withCredentials(new NtlmPasswordAuthenticator(DomainIP, DomainUser,
DomainPassword));
SmbFile smbRemoteFile = new SmbFile("smb:" + remoteFile.replace("\\", "/"), auth);
According to this issue: jcifs-ng Issue #36: Chicken/egg relationship between CIFSContext and credentials
Class NtlmPasswordAuthentication is replaced by NtlmPasswordAuthenticator.
So you might replace your NtlmPasswordAuthentication usage with:
NtlmPasswordAuthenticator auth = new NtlmPasswordAuthenticator(domain, username, password)
Besides, this answer might be helpful.
Related
I have a config properties file in a java project created in eclipse.
Below are the contents in the properties file.
adminApp= testAdminDemo
customerApp = testCustDemo
appHostIP = 172.22.XX.XX
adminAppURL = http://appHostIP:9049/adminApp
customerAppURL = http://appHostIP:9049/customerApp
When the appURL parameter is read from a Java class,
Properties prop = new Properties();
FileInputStream f = new FileInputStream(System.getProperty("user.dir") + "\\config.properties");
prop.load(f);
String url = prop.getProperty("appURL");
System.out.println("URL: " + url);
the output is the same value mentioned for parameter 'url' in config.parameters file:
http://appHostIP:9049/adminApp
Actually, I expected the output to be like:
http://172.22.XX.XX:9049/testAdminDemo
Is there anything wrong in this approach?
I don't want to read host ip and app name in the java class file and then form a string. Instead, the required URl should get formed in the properties file as need to deal with different apps - admin, customer, etc.
You cannot perform concatination in the property file, You have to handle the concatination in code where you are using it like below.
appHostIP = 172.22.XX.XX
adminAppURL = :9049/adminApp
String url = "http://" + "prop.getProperty("appHostIP")+ prop.getProperty("appURL");
I try to copy files from one network share to another.
When I run the code it says "failed to copy". But it is able to create the folder structure in the target folder.
That means this folder exists on target share after run:
\DiskStation\OpenKM Import Handled\20200728-132700\Strato\2013
But no file is created. You can see source and target folder in the first exception.
What am I doing wrong? In case it is important. The app runs on windows. The differrent shares are on a Synology Diskstation.
Any help is appreciated.
Thanks
I use jcifs.smb package for that.
The relevant lines are:
SmbFile targetFolder = getMoveToTargetFile(targetToMoveTo, newDocName, def);
SmbFile targetFile = new SmbFile(targetFolder, String.format("%s%s", targetToMoveTo, newDocName));
entry.copyTo(targetFile);
The execptions are:
jcifs.smb.SmbException: Failed to copy file from [smb://;OpenKM:xxxx#Diskstation/OpenKM Hot Folder/Strato/2013/DRP48646659.pdf] to [smb://;OpenKM:xxxx#Diskstation/OpenKM Import Handled/20200728-132700/Strato/2013/DRP4864665(2).pdf]
at jcifs.smb.SmbCopyUtil.copyFile(SmbCopyUtil.java:186)
at jcifs.smb.SmbFile.copyRecursive(SmbFile.java:1390)
at jcifs.smb.SmbFile.copyTo(SmbFile.java:1441)
at com.engst.test.java_project.App.handleFileEntry(App.java:272)
at com.engst.test.java_project.App.handleDirectoryEntry(App.java:181)
at com.engst.test.java_project.App.handleDirectoryEntry(App.java:194)
at com.engst.test.java_project.App.handleDirectoryEntry(App.java:194)
at com.engst.test.java_project.App.main(App.java:100)
Caused by: jcifs.smb.SmbException: The filename, directory name, or volume label syntax is incorrect.
at jcifs.smb.SmbTransportImpl.checkStatus2(SmbTransportImpl.java:1461)
at jcifs.smb.SmbTransportImpl.checkStatus(SmbTransportImpl.java:1572)
at jcifs.smb.SmbTransportImpl.sendrecv(SmbTransportImpl.java:1027)
at jcifs.smb.SmbTransportImpl.send(SmbTransportImpl.java:1543)
at jcifs.smb.SmbSessionImpl.send(SmbSessionImpl.java:409)
at jcifs.smb.SmbTreeImpl.send(SmbTreeImpl.java:472)
at jcifs.smb.SmbTreeConnection.send0(SmbTreeConnection.java:404)
at jcifs.smb.SmbTreeConnection.send(SmbTreeConnection.java:318)
at jcifs.smb.SmbTreeConnection.send(SmbTreeConnection.java:298)
at jcifs.smb.SmbTreeHandleImpl.send(SmbTreeHandleImpl.java:130)
at jcifs.smb.SmbTreeHandleImpl.send(SmbTreeHandleImpl.java:117)
at jcifs.smb.SmbFile.openUnshared(SmbFile.java:693)
at jcifs.smb.SmbFile.openUnshared(SmbFile.java:655)
at jcifs.smb.SmbCopyUtil.openCopyTargetFile(SmbCopyUtil.java:68)
at jcifs.smb.SmbCopyUtil.copyFile(SmbCopyUtil.java:124)
... 7 more
Refer this .
The filename, directory name or volume label syntax incorrect
Maybe directory or filename have invalid character such as white space.
You can try to use english character only.And if that works try challenge abother thing.
Thabk you.
I found now a solution that works for me. I am now getting SmbResourece for the networks shares and based on those I get SmbFile objects to do the copy operation.
I can also omit username and password in my connectString variables.
With that approach I can copy the files.
Feel free to let me know how I can make that smoother or more best practice like.
Thanks.
SmbFile srcFile = null;
try {
Properties prop = new Properties();
prop.put( "jcifs.smb.client.enableSMB2", "true");
prop.put( "jcifs.smb.client.disableSMB1", "false");
prop.put( "jcifs.traceResources", "true" );
Configuration config = new PropertyConfiguration(prop);
BaseContext bc = new BaseContext(config);
NtlmPasswordAuthenticator auth = new NtlmPasswordAuthenticator("", "OpenKM", "xxxx");
CIFSContext ct = bc.withCredentials(auth);
String connectString = "smb://192.168.10.10/OpenKM Hot Folder/";
connectString = connectString.replace('\\', '/');
SmbResource sr = ct.get(connectString);
srcFile = new SmbFile(sr, "Strato/2013/DRP46412852.pdf");
BaseContext bc2 = new BaseContext(config);
NtlmPasswordAuthenticator auth2 = new NtlmPasswordAuthenticator("", "OpenKM", "xxxx");
CIFSContext ct2 = bc2.withCredentials(auth2);
String connectString2 = "smb://192.168.10.10/OpenKM Import Handled/";
connectString2 = connectString2.replace('\\', '/');
SmbResource sr2 = ct2.get(connectString2);
SmbFile targetFile = new SmbFile(sr2, "20200728-223352/Strato/2013/paul(2).pdf");
srcFile.copyTo(targetFile);
} finally {
srcFile.close();
}
I read this documentation:
http://docs.oracle.com/javaee/6/api/javax/mail/internet/package-summary.html
so I add some properties to mimeMessage:
Properties props = new Properties();
props.put("mail.mime.decodefilename", true);
Session mailConnection = Session.getInstance(props, null);
source = new FileInputStream(emlFile);
MimeMessage message = new MimeMessage(mailConnection, source);
now I am expectaing that method bodyPart.getFileName() return correct name of file. But with this configuration it still doesn work and I need to call mimeUtils: MimeUtility.decodeText - what I dont want. I also try:
props.put("mail.mime.decodefilename", "true");
but with no success. So what I am doing wrong ?
UPDATE:
after debuging I had this sollution:
this works
Properties props = System.getProperties();
props.put("mail.mime.decodefilename", "true");
this doesnt work:
Properties props = new Properites();
props.put("mail.mime.decodefilename", "true");
so if filename is decoding depends on system property too. Does anyone know which properties ? I dont have pattion to try all system properties and solve which one it is
MimeMessage.getFileName
If the mail.mime.encodefilename System property is set to true, the
MimeUtility.decodeText method will be used to decode the filename.
Now when one looks at the implementation, this is how the MimeUtility.decodeText comes into picture during the invocation of getFileName:
if (decodeFileName && filename != null) {
try {
filename = MimeUtility.decodeText(filename);
} catch (UnsupportedEncodingException ex) {
throw new MessagingException("Can't decode filename", ex);
}
}
Where decodeFileName is initialized like this:
s = System.getProperty("mail.mime.decodefilename");
// default to false
decodeFileName = s != null && !s.equalsIgnoreCase("false");
The javadoc seems to be conflicting with the implementation.
So, try setting mail.mime.decodefilename instead of mail.mime.encodefilename, probably using System.setProperty.
I am having lots of problems with this.
I have the following code
try {
final SSHClient ssh = new SSHClient();
PKCS8KeyFile keyFile = new PKCS8KeyFile();
keyFile.init(new File(Thread.currentThread().getContextClassLoader().getResource("development.pem").toURI()));
ssh.loadKnownHosts();
ssh.addHostKeyVerifier("ec2-XX-XX-XX-XX.compute-1.amazonaws.com", 22, "ff:59:aa:24:42:b1:a0:9f:c9:4c:73:34:fb:95:53:c2:b8:37:a8:f8");
// ssh.addHostKeyVerifier("ec2-XX-XX-XX-XX.compute-1.amazonaws.com", 22, "90:1e:4d:09:42:c4:16:8a:4c:dc:ae:c2:60:14:f9:ea");
ssh.connect("ec2-XX-XX-XX-XX.compute-1.amazonaws.com");
ssh.auth("ec2-user", new AuthPublickey(keyFile));
Session session = ssh.startSession();
Command sudo = session.exec("sudo su -");
System.out.println("sudo=" +sudo.getOutputAsString());
Command whoami = session.exec("whoami");
System.out.println("whoami=" + whoami.getOutputAsString());
} catch (Exception e) {
e.printStackTrace();
}
The first addHostKeyVerifier is using the fingerprint on the AWS console, the commented out one is the one that it keeps telling me it is failing against. Where am i meant to get the correct key from.
If i use the second key it passes verification then fails afterwards.
I am using SSHJ version 0.8.1
This worked for me.
For your PEM file you need to use the OpenSSHKeyFile key provider.
SSHClient ssh = new SSHClient();
OpenSSHKeyFile keyFile = new OpenSSHKeyFile();
File file = new File("c:\\full\\path\\to\\keyfile.pem");
keyFile.init(file);
Personally, I just surpressed the host key verification to always return true. But I'm sure your way is more secure (if it works).
ssh.loadKnownHosts();
ssh.addHostKeyVerifier((a, b, c) -> true);
The username for AWS depends on your image. Very often it is "root". In my case, it was "ubuntu".
ssh.connect("ec2-54-165-233-48.compute-1.amazonaws.com");
ssh.auth("ubuntu", new AuthPublickey(keyFile));
Session session = ssh.startSession();
(Note: I'm using version 0.26.0 though.)
below is my code
DocsService client = new DocsService("testappv1");
client.setUserCredentials(username, password);
client.setProtocolVersion(DocsService.Versions.V2);
File file = new File("C:/test.jpg");
DocumentEntry newDocument = new DocumentEntry();
newDocument.setTitle(new PlainTextConstruct("test"));
String mimeType = DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType();
newDocument.setMediaSource(new MediaFileSource(file, mimeType));
newDocument = client.insert(destFolderUrl, newDocument);
the document was created successful, but it did not contain anything.
try the following
client.insert(new URL("https://docs.google.com/feeds/documents/private/full/?convert=false"), newDocument);
i think the ?convert=false bit is important, not sure how you do that without the url
client.insert(new URL(destFolderUrl+ "?convert=false"), newDocument);
would hopefully work in your case