i wrote programmatically a startup code of the apache Server like this:
public void _start()
{
String Path = "C:\\Dokumente und Einstellungen\\andjock\\Desktop\\ab";
File ftpDirectory = new File(Path);
ftpDirectory.mkdirs();
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
factory.setPort(2221);
try {
serverFactory.addListener("default", factory.createListener());
PropertiesUserManagerFactory userFactory = new PropertiesUserManagerFactory();
File userFile = new File("C:\\Dokumente und Einstellungen\\andjock\\Desktop\\ftpusers.properties");
userFactory.setFile(userFile);
UserManager um = userFactory.createUserManager();
BaseUser user = new BaseUser();
user.setName("myNewUser");
user.setPassword("secret");
user.setHomeDirectory(Path);
um.save(user);
serverFactory.setUserManager(um);
FtpServer ftpServer = serverFactory.createServer();
ftpServer.start();
} catch (Exception e) {
Logger LOGGER = Logger.getLogger(TestapacheFtpServer.class);
LOGGER.log(Level.FATAL, "Unable to start test ftpserver", e);
}
How do i know that the server is really working ?
how can i access this apache Server , from the "outside"?
i tried with a telnet and ftp (ftp 127.0.0.1) on my machine but i received:
FTP: connect : unknown error code
does someone got any idea ? i just don't want to rely on the jvm log, but rather test it , and accesing the started it
i figure it out!! i wrote a client using the FTP client library (the apache commons library) to test connectivity and list the files ; something like that
FTPClient ftp = new FTPClient();
ftp.connect(InetAddress.getLocalHost(), 2221);// or "localhost" in your case
String loging_success = ftp.login("myNewUser", "secret") == true ? "success" : "failed";
System.out.println("login: "+ loging_success);
FTPFile[] files = ftp.listFiles();
System.out.println("Listed "+files.length+" files.");
for(FTPFile file : files) {
System.out.println(file.getName());
}
Related
I am trying to use an ftps client with proxy.
I am using this library by apache: org.apache.commons.net.ftp;
The problem is, that it looks like when I set proxy to the ftps client, it is connecting and also doing login, but gets connect timed out when trying to transfer the file.
though if I don't set proxy on the ftps client, the file is being written succesfully.
just to mention, if I use normal FTP client (with proxy, i.e. new FTPClient()), and not FTPS client (i.e. new FTPSClient()), it also works.
This is how I create the ftps client:
FTPClient ftpClient = new FTPSClient();
ftpClient.setConnectTimeout(timeout);
ftpClient.setCopyStreamListener(new FileCopyListener(context, "Transfer file using ftp."));
// setting the proxy:
ftpClient.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(OUTBOUND_HTTP_PROXY_HOST, OUTBOUND_HTTP_PROXY_PORT)));
ftpClient.setRemoteVerificationEnabled(false);
// connecting:
ftpClient.connect(host, port);
// login
ftpClient.login(username, password)
// some file configuration
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.setBufferSize(DataSourceProperties.BUFFER_SIZE);
// has encryption
((FTPSClient) ftpClient).execPBSZ(0);
((FTPSClient) ftpClient).execPROT("P");
// now trying to transfer file
for (File file : files) {
try (InputStream fileInputStream = new FileInputStream(file)) {
String remoteFileName = file.getName();
if (ftpClient.storeFile(remoteFileName, fileInputStream)) { // *** THIS IS WHERE I GET THE TIMEOUT
logger.info("File uploaded successfully");
} else {
throw new Exception(errorMessage);
}
traceLogger.info(LogFactory.createLog(stepId, "Complete uploading file", parameters));
}
}
any idea?
I have the following code to start my software:
public static void main(String[] args) throws Exception {
// set system property for exit on failure
System.setProperty("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE", "true");
// create tomcat
Tomcat tomcat = new Tomcat();
// create connector, configure and add to tomcat
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setMaxPostSize(-1);
connector.setPort(8080);
connector.setURIEncoding("UTF-8");
((Http11NioProtocol)connector.getProtocolHandler()).setConnectionUploadTimeout(36000000);
((Http11NioProtocol)connector.getProtocolHandler()).setDisableUploadTimeout(false);
((Http11NioProtocol)connector.getProtocolHandler()).setConnectionTimeout(3600000);
((Http11NioProtocol)connector.getProtocolHandler()).setCompression("on");
((Http11NioProtocol)connector.getProtocolHandler()).setCompressibleMimeType("text/html,text/xml,text/plain,application/javascript");
tomcat.setConnector(connector);
// add web app with jsps and servlets
StandardContext standardContext = (StandardContext)tomcat.addWebapp("", new File(".").getAbsolutePath()+"/src/webroot");
standardContext.getJarScanner().setJarScanFilter(new JarScanFilter() { #Override public boolean check(JarScanType jarScanType, String s) {
if(s != null){
if(s.startsWith("mchange-commons-java")){
return false;
}
}
return true;
}});
standardContext.setParentClassLoader(Run.class.getClassLoader());
WebResourceRoot webResourceRoot = new StandardRoot(standardContext);
File additionWebInfClassesFolder = new File(new File(".").getAbsolutePath(), "target/classes");
WebResourceSet webResourceSet = new DirResourceSet(webResourceRoot, "/WEB-INF/classes", additionWebInfClassesFolder.getAbsolutePath(), "/");
webResourceRoot.addPreResources(webResourceSet);
standardContext.setResources(webResourceRoot);
// start tomcat
tomcat.start();
// stay in this method as long as tomcat is running
tomcat.getServer().await();
}
Now I have my certificate files (private key, certificate) and I want to add SSL functionality to this Tomcat Server. I know that this might not be best practice, but I am looking for a very simple way to do that. I know I can create a keystore file and add the properties to the connector but what I basically want is to have a string with my certificate content and apply that.
My solution winds up looking a lot like the code I finally stumbled upon here to help me fix my issues: https://github.com/OryxProject/oryx/blob/master/framework/oryx-lambda-serving/src/main/java/com/cloudera/oryx/lambda/serving/ServingLayer.java#L202
Note: I believe I am using Tomcat 10.
private Connector createSslConnector(){
Connector httpsConnector = new Connector();
httpsConnector.setPort(443);
httpsConnector.setSecure(true);
httpsConnector.setScheme("https");
httpsConnector.setAttribute("SSLEnabled", "true");
SSLHostConfig sslConfig = new SSLHostConfig();
SSLHostConfigCertificate certConfig = new SSLHostConfigCertificate(sslConfig, SSLHostConfigCertificate.Type.RSA);
certConfig.setCertificateKeystoreFile("/root/.keystore");
certConfig.setCertificateKeystorePassword("changeit");
certConfig.setCertificateKeyAlias("mykeyalias");
sslConfig.addCertificate(certConfig);
httpsConnector.addSslHostConfig(sslConfig);
return httpsConnector;
}
I am developing a code for xcel generation and download using apache poi. LocalHost server and app server is jboss. When i run the code on localhost, a temp folder is generated in jboss's deployment folder and in that the xcel is generated and then downloaded through frontend. I am using java spring angularjs and html. This runs fine on localhost but after deploying on app server the xcel is not downloaded and it gives 500:internal server error.
angularjs controller code:
$scope.generateExcel=function(sDate,eDate,doc,search)
{
console.log("hello");
var sDate = document.getElementById('sD').value
var eDate = document.getElementById('eD').value
$scope.obj.sDate = sDate;
$scope.obj.eDate = eDate;
$scope.obj.iou = doc;
$scope.obj.du = search;
console.log($scope.obj);
$http.post('abc/generateExcel',$scope.obj).then(function()
{
//console.log(path);
$window.location.href="/ProjectName/file_name.xls";
})
.error(function()
{
console.log("Error!!");
});
};
java code:
//Method
public HttpServletResponse generateExcel ( HttpServletRequest request , HttpServletResponse response, String sD, String eD, String doc, String search)
{
//EXCEL GENERATION HERE
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=filename.xls");
//Path Specification
String path = request.getRealPath("/file_name.xls");
//System.out.println("Here...");
System.out.println(path);
FileOutputStream fileOut2 = new FileOutputStream(path);
workbook.write(fileOut2);
/*returning response*/
}
It's difficult to answer this unless one knows what's the error you are getting on the server side. Put your server code in a try-catch block. Rerun the code, and check the server logs. Paste them here.
try{
String path = request.getRealPath("/file_name.xls");
//System.out.println("Here...");
System.out.println(path);
FileOutputStream fileOut2 = new FileOutputStream(path);
workbook.write(fileOut2);
} catch(Exception e){
e.printStackTrace(); // this should print some error in server logs
}
I'm trying to download a file from FTP in my test.
When I'm running this from my local PC or on BrowserStack it works perfectly but when I upload it to jenkins it gets stuck in the line.
I can't understand what is the difference why doesn't it run on Jenkins?
I managed to create a connection to the FTP. the code shown below is the method that downloads the file.
boolean success = ftpClient.retrieveFile(remoteFile, outputStream);
public static File downloadFileFromFtp(String fileName, String ftpFilePath, String downloadDirectory, String fileExtension, ExtentTest test) throws Exception {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(AutomationPropeties.ftpHost, Integer.valueOf(AutomationPropeties.ftpPort));
ftpClient.login(AutomationPropeties.ftpUsername, AutomationPropeties.ftpPassword);
ftpClient.enterLocalPassiveMode();
System.out.println("loged in ftp");
if (ftpClient.isConnected()) {
test.log(LogStatus.INFO, "Connected Succesfuly to ftp server.");
System.out.println("loged in ftp");
} else {
test.log(LogStatus.INFO, "Failed connecting to ftp.");
System.out.println("not loged in ftp");
}
String remoteFile = ftpFilePath + fileName + ".xlsx";
System.out.println(remoteFile);
// File downloadFile = new File(downloadDirectory+fileName+".xlsx");
File downloadFile = File.createTempFile(fileName, ".xlsx");
System.out.println("reached the try");
try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile))) {
System.out.println("finished with the output");
boolean success = ftpClient.retrieveFile(remoteFile, outputStream);
System.out.println("retrive the file & conection closed");
if (success) {
test.log(LogStatus.PASS, "File was downloaded succesfuly");
} else {
test.log(LogStatus.FAIL, "Failed to download file");
}
} finally {
ftpClient.logout();
ftpClient.disconnect();
}
return downloadFile;
}
Perhaps the Jenkins FTP plugin is what you need. Jenkins can be defined with worker bee (so much better than slave, don't you think?) servers so your job may run on different physical servers.
If you look into the plugin please report back if that helped.
Update
Use:
curl -O ftp://server/path/to/file
This is Mukul from BrowserStack. We have recently released a Jenkins plugin that might help you out.
I am using org.apache.commons.net.ftp.FTPClient in one of my applications to work with a FTP server. I am able to connect, login, pwd and cwd. However, when I try to list the files it doesn't return the list of files in that directory, where I know for sure that there are files. I am using the method FTPFile[] listFiles(), it returns an empty array of FTPFile.
Please find below the code snippet where I am trying this:
String hostname = properties.getProperty("FTP_SERVER");
String user = properties.getProperty("FTP_USER");
String passwd = properties.getProperty("FTP_PASSWD");
FTPClient client = new FTPClient();
client.connect(hostname);
client.login(user, passwd);
String reply = client.getStatus();
System.out.println(reply);
client.enterRemotePassiveMode();
client.changeWorkingDirectory("/uploads");
FTPFile[] files = client.listFiles();
System.out.println(files.length);
for (FTPFile file : files) {
System.out.println(file.getName());
}
String[] fileNames = client.listNames();
if (fileNames != null) {
for (String file : fileNames) {
System.out.println(file);
}
}
client.disconnect();
This seems like the same issue I had (and solved), see this answer:
Apache Commons Net FTPClient and listFiles()
After I set the mode as PASV it is working fine now!
Thanks for all your efforts and suggestions!
I added client.enterLocalPassiveMode() and it works:
client.connect("xxx.com");
boolean login = client.login("xxx", "xxx");
client.enterLocalPassiveMode();
Just a silly suggestion... can you do a listing on the /uploads folder using a normal FTP client. I ask this because some FTP servers are setup to not display the listing of an upload folder.
First, make sure the listing works in other programs. If so, one possibility is that the file listing isn't being parsed correctly. You can try explicitly specifying the parser to use with initiateListParsing.
I had to same problem and it turned out to be that it couldn't parse what the server was returning for a file listing. I this line after connecting to the ftp server ftpClient.setParserFactory(new MyFTPFileEntryParserFactory());
public class MyFTPFileEntryParserFactory implements FTPFileEntryParserFactory {
private final static FTPFileEntryParser parser = new UnixFTPEntryParser() {
#Override public FTPFile parseFTPEntry(String entry) {
FTPFile ftpFile = new FTPFile();
ftpFile.setTimestamp(getCalendar(entry));
ftpFile.setSize(get(entry));
ftpFile.setName(getName(entry));
return ftpFile;
}
};
#Override public FTPFileEntryParser createFileEntryParser(FTPClientConfig config) throws ParserInitializationException {
return parser;
}
#Override public FTPFileEntryParser createFileEntryParser(String key) throws ParserInitializationException {
return parser;
}
}
In my case, on top of applying enterLocalPassiveMode and indicating correct operation system, I also need to set UnparseableEntries to true to make the listFile method work.
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
conf.setUnparseableEntries(true);
f.configure(conf);
boolean isLoginSuccess = client.login(username, password);