All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src-main.org.kawanfw.file.api.client.FileSession Maven / Gradle / Ivy

Go to download

Awake FILE is a secure Open Source framework that allows to program very easily file uploads/downloads and RPC through http. File transfers include powerful features like file chunking and automatic recovery mechanism. Security has been taken into account from the design: server side allows to specify strong security rules in order to protect the files and to secure the RPC calls.

The newest version!
/*
 * This file is part of Awake FILE. 
 * Awake file: Easy file upload & download over HTTP with Java.                                    
 * Copyright (C) 2015,  KawanSoft SAS
 * (http://www.kawansoft.com). All rights reserved.                                
 *                                                                               
 * Awake FILE is free software; you can redistribute it and/or                 
 * modify it under the terms of the GNU Lesser General Public                    
 * License as published by the Free Software Foundation; either                  
 * version 2.1 of the License, or (at your option) any later version.            
 *                                                                               
 * Awake FILE is distributed in the hope that it will be useful,               
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             
 * Lesser General Public License for more details.                               
 *                                                                               
 * You should have received a copy of the GNU Lesser General Public              
 * License along with this library; if not, write to the Free Software           
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *
 * Any modifications to this file must keep this entire header
 * intact.
 */
package org.kawanfw.file.api.client;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.FileFileFilter;
import org.kawanfw.commons.api.client.HttpProtocolParameters;
import org.kawanfw.commons.api.client.HttpProxy;
import org.kawanfw.commons.api.client.InvalidLoginException;
import org.kawanfw.commons.api.client.RemoteException;
import org.kawanfw.commons.http.HttpTransfer;
import org.kawanfw.commons.http.HttpTransferOne;
import org.kawanfw.commons.util.ClientLogger;
import org.kawanfw.commons.util.FrameworkDebug;
import org.kawanfw.commons.util.Tag;
import org.kawanfw.file.api.util.client.PathUtil;
import org.kawanfw.file.version.FileVersion;

/**
 * Main class - before version 3.0 - for executing from a client (Windows, OX S,
 * Linux/Unix and Android) all sorts of operations on remote files or remote
 * classes through an Http or Https session.
 * 

* Class includes methods to: *

    *
  • Get remote files size.
  • *
  • Delete remote files or directories.
  • *
  • TestReload if a remote file or directory exists.
  • *
  • Rename remote files.
  • *
  • Create remote directories.
  • *
  • Get the list of files of a remote directory.
  • *
  • Get the list of sub-directories of a remote directory.
  • *
  • Upload files by wrapping bytes copy from a {@code FileInputStream} to a * {@link RemoteOutputStream}.
  • *
  • Download files by wrapping bytes copy from a {@link RemoteInputStream} to * a {@code FileOtputStream}.
  • *
  • Call remote Java methods.
  • *
*

* Example:

* *
 * // Define URL of the path to the {@code ServerFileManager} servlet
 * String url = "https://www.acme.org/ServerFileManager";
 * 
 * // The login info for strong authentication on server side:
 * String username = "myUsername";
 * char[] password = { 'm', 'y', 'P', 'a', 's', 's', 'w', 'o', 'r', 'd' };
 * 
 * // Establish a session with the remote server
 * FileSession fileSession = new FileSession(url, username, password);
 * 
 * // OK: upload a file
 * fileSession.upload(new File("c:\\myFile.txt"), "/home/mylogin/myFile.txt");
 * 
 * 
* *
*

* Communication via an (authenticating) proxy server is done using a * {@link org.kawanfw.commons.api.client.HttpProxy} instance: * *

* *
 * HttpProxy httpProxy = new HttpProxy("myproxyhost", 8080);
 * String url = "https://www.acme.org/ServerFileManager";
 * 
 * // The login info for strong authentication on server side:
 * String username = "myUsername";
 * char[] password = { 'm', 'y', 'P', 'a', 's', 's', 'w', 'o', 'r', 'd' };
 * 
 * FileSession fileSession = new FileSession(url, username, password, httpProxy);
 * 
 * // Etc.
 * 
* *
* * * @see org.kawanfw.commons.api.client.HttpProxy * @see org.kawanfw.file.api.client.RemoteSession * @see org.kawanfw.file.api.client.RemoteFile * @see org.kawanfw.file.api.client.RemoteInputStream * @see org.kawanfw.file.api.client.RemoteOutputStream * * @deprecated As of version 3.0, replaced by {@link RemoteSession} for session * establishment and simple file upload/download and * {@link RemoteFile} for operations on remote files. * * @author Nicolas de Pomereu * @since 1.0 */ public class FileSession implements Cloneable { protected static final String FILE_SESSION_IS_CLOSED = "RemoteSession is closed."; /** For debug info */ private static boolean DEBUG = FrameworkDebug.isSet(FileSession.class); /** Defines 1 kilobyte */ public static final int KB = 1024; /** Defines 1 megabyte */ public static final int MB = 1024 * KB; private static final int LIST_FILES_ONLY = 1; private static final int LIST_DIRECTORIES_ONLY = 2; /** The url to use to connectet to the Awake FILE Server */ private String url = null; /** * The username is stored in static memory to be passed to upload file * servlet */ private String username = null; /** * Token is stored in static to be available during all session and contains * SHA-1(userId + ServerClientLogin.SECRET_FOR_LOGIN) computed by server. * Token is re-send and checked at each send or recv command to be sure user * is authenticated. */ private String authenticationToken = null; /** The Http Proxy instance */ private HttpProxy httpProxy = null; /** The Http Parameters instance */ private HttpProtocolParameters httpProtocolParameters = null; /** The http transfer instance */ private HttpTransfer httpTransfer = null; /** The wrapped Remote Session */ private RemoteSession remoteSession = null; /** * Says if we want to use base64 encoding for parameters passed to call() - * This is a method for legacy applications prior to v1.0. */ public static void setUseBase64EncodingForCall() { RemoteSession.setUseBase64EncodingForCall(); } /** * Private constructor for clone(). * * @param url * the URL of the path to the {@code ServerFileManager} Servlet * @param username * the username for authentication on the Awake Server (may be * null for call() or downloadUrl()) * @param authenticationToken * the actual token of the Awake FILE session to clone * @param httpProxy * the http proxy to use * @param httpProtocolParameters * the http parameters to use * @param remoteSession * the remote session to clone */ private FileSession(String url, String username, String authenticationToken, HttpProxy httpProxy, HttpProtocolParameters httpProtocolParameters, RemoteSession remoteSession) { this.url = url; this.username = username; this.authenticationToken = authenticationToken; this.httpProxy = httpProxy; this.httpProtocolParameters = httpProtocolParameters; httpTransfer = new HttpTransferOne(url, httpProxy, httpProtocolParameters); this.remoteSession = remoteSession.clone(); } /** * Creates an Awake FILE session with a proxy and protocol parameters. * * @param url * the URL of the path to the {@code ServerFileManager} Servlet * @param username * the username for authentication on the Awake Server (may be * null for call() * @param password * the user password for authentication on the Awake Server (may * be null) * @param httpProxy * the http proxy to use (null if none) * @param httpProtocolParameters * the http parameters to use (may be null) * * @throws MalformedURLException * if the url is malformed * @throws UnknownHostException * if host URL (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws InvalidLoginException * the username or password is invalid * @throws SecurityException * Scheme is required to be https (SSL/TLS) * @throws RemoteException * an exception has been thrown on the server side. This traps * an Awake product failure and should not happen. * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: * {@link RemoteSession#RemoteSession(String, String, char[])} */ public FileSession(String url, String username, char[] password, HttpProxy httpProxy, HttpProtocolParameters httpProtocolParameters) throws MalformedURLException, UnknownHostException, ConnectException, SocketException, InvalidLoginException, RemoteException, SecurityException, IOException { remoteSession = new RemoteSession(url, username, password, httpProxy, httpProtocolParameters); debug(remoteSession.toString()); // keep a copy of HttpTransfer than can not be accessed via // RemoteSession because package protected httpTransfer = new HttpTransferOne(url, httpProxy, httpProtocolParameters); this.username = username; this.url = url; this.httpProxy = httpProxy; this.httpProtocolParameters = httpProtocolParameters; this.authenticationToken = remoteSession.getAuthenticationToken(); } /** * Creates an Awake FILE session with a proxy. * * @param url * the URL of the path to the {@code ServerFileManager} Servlet * @param username * the username for authentication on the Awake Server (may be * null for call() * @param password * the user password for authentication on the Awake Server (may * be null) * @param httpProxy * the http proxy to use (null if none) * * @throws MalformedURLException * if the url is malformed * @throws UnknownHostException * if host URL (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws InvalidLoginException * the username or password is invalid * @throws SecurityException * scheme is required to be https (SSL/TLS) * @throws RemoteException * an exception has been thrown on the server side * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: * {@link RemoteSession#RemoteSession(String, String, char[], HttpProxy)} */ public FileSession(String url, String username, char[] password, HttpProxy httpProxy) throws MalformedURLException, UnknownHostException, ConnectException, SocketException, InvalidLoginException, RemoteException, SecurityException, IOException { this(url, username, password, httpProxy, null); } /** * Creates an Awake FILE session. * * @param url * the URL of the path to the {@code ServerFileManager} Servlet * @param username * the username for authentication on the Awake Server (may be * null for call() * @param password * the user password for authentication on the Awake Server (may * be null) * * @throws MalformedURLException * if the url is malformed * @throws UnknownHostException * if host URL (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws InvalidLoginException * the username or password is invalid * @throws SecurityException * scheme is required to be https (SSL/TLS) * @throws RemoteException * an exception has been thrown on the server side * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: * {@link RemoteSession#RemoteSession(String, String, char[], HttpProxy, HttpProtocolParameters)} */ public FileSession(String url, String username, char[] password) throws MalformedURLException, UnknownHostException, ConnectException, SocketException, InvalidLoginException, RemoteException, IOException, SecurityException { this(url, username, password, null, null); } /** * Returns the wrapped Remote Session * * @return the remoteSession the wrapped Remote Session * * @since 3.0 */ RemoteSession getRemoteSession() { return remoteSession; } /** * Returns the username of this Awake FILE session * * @return the username of this Awake FILE session */ public String getUsername() { return this.username; } /** * Returns the {@code HttpProtocolParameters} instance in use for the Awake * FILE session. * * @return the {@code HttpProtocolParameters} instance in use for the Awake * FILE session */ public HttpProtocolParameters getHttpProtocolParameters() { return this.httpProtocolParameters; } /** * Returns the URL of the path to the ServerFileManager Servlet * (or ServerSqlManager Servlet if session has been initiated * by a RemoteConnection). * * @return the URL of the path to the ServerFileManager Servlet */ public String getUrl() { return url; } /** * Returns the {@code HttpProxy} instance in use for this File Session. * * @return the {@code HttpProxy} instance in use for this File Session */ public HttpProxy getHttpProxy() { return this.httpProxy; } /** * Returns the http status code of the last executed verb * * @return the http status code of the last executed verb */ public int getHttpStatusCode() { if (httpTransfer != null) { return httpTransfer.getHttpStatusCode(); } else { return 0; } } /** * Returns the Authentication Token. This method is used by the Kawansoft * frameworks. * * @return the Authentication Token */ public String getAuthenticationToken() { return this.authenticationToken; } /** * Downloads a file from the remote server.
* This method simply wraps bytes copy from a {@link RemoteInputStream} to a * {@code FileOutputStream}. *

* The real path of the remote file depends on the Awake FILE configuration * on the server. See User Documentation. *

* Large files are split in chunks that are downloaded in sequence. The * default chunk length is 10Mb. You can change the default value with * {@link HttpProtocolParameters#setDownloadChunkLength(long)} before * passing {@code HttpProtocolParameters} to this class constructor. *

* Note that file chunking requires that all chunks be downloaded from to * the same web server. Thus, file chunking does not support true stateless * architecture with multiple identical web servers. If you want to set a * full stateless architecture with multiple identical web servers, you must * disable file chunking. This is done by setting a 0 download chunk length * value using {@link HttpProtocolParameters#setDownloadChunkLength(long)}. *
*
* A recovery mechanism allows - in case of failure - to start again in the * same JVM run the file download from the last non-downloaded chunk. See * User Guide for more information.
*
* Note that this method can not be used with a progress indicator/monitor * and so does not implement any increment mechanism. The reason is dual: *

    *
  • Implementing an increment mechanism would require to add cumbersome * API.
  • *
  • Wrapped classes {@link RemoteInputStream} and * {@code FileOutputStream} allow easy implementation of progress * indicators. See Tutorial and included examples.
  • *
* * @param pathname * the pathname on host * * @param file * the file to create on the client side * @throws IllegalArgumentException * if file or pathname is null * @throws InvalidLoginException * the session has been closed by a {@code logoff()} * @throws FileNotFoundException * if the remote file is not found on server * @throws UnknownHostException * if host URL (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws RemoteException * an exception has been thrown on the server side * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: * {@link RemoteSession#download(String, File)} */ public void download(String pathname, File file) throws IllegalArgumentException, InvalidLoginException, FileNotFoundException, UnknownHostException, ConnectException, SocketException, RemoteException, IOException { pathname = PathUtil.rewriteToUnixSyntax(pathname); remoteSession.download(pathname, file); } /** * Uploads a file on the server.
* This method simply wraps bytes copy from a {@code FileInputStream} to a * {@link RemoteOutputStream}. *

* The real path of the remote file depends on the Awake FILE configuration * on the server. See User Documentation. *

* Large files are split in chunks that are uploaded in sequence. The * default chunk length is 3Mb. You can change the default value with * {@link HttpProtocolParameters#setUploadChunkLength(long)} before passing * {@code HttpProtocolParameters} to this class constructor. *

* Note that file chunking requires all chunks to be sent to the same web * server that will aggregate the chunks after the last send. Thus, file * chunking does not support true stateless architecture with multiple * identical web servers. If you want to set a full stateless architecture * with multiple identical web servers, you must disable file chunking. This * is done by setting a 0 upload chunk length value using * {@link HttpProtocolParameters#setUploadChunkLength(long)}.
*
* A recovery mechanism allows - in case of failure - to start again in the * same JVM run the file upload from the last non-uploaded chunk. See User * Guide for more information.
*
* Note that this method can not be used with a progress indicator/monitor * and so does not implement any increment mechanism. The reason is dual: *

    *
  • Implementing an increment mechanism would require to add cumbersome * API.
  • *
  • Wrapped classes {@code FileInputStream} and * {@link RemoteOutputStream} allow easy implementation of progress * indicators. See Tutorial and included examples.
  • *
* * @param file * the file to upload * @param pathname * the pathname on host * @throws IllegalArgumentException * if file or pathname is null * @throws InvalidLoginException * the session has been closed by a {@code logoff()} * @throws FileNotFoundException * if the file to upload is not found * @throws UnknownHostException * if host URL (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws RemoteException * an exception has been thrown on the server side * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: * {@link RemoteSession#upload(File, String)} */ public void upload(File file, String pathname) throws IllegalArgumentException, InvalidLoginException, FileNotFoundException, UnknownHostException, ConnectException, SocketException, RemoteException, IOException { pathname = PathUtil.rewriteToUnixSyntax(pathname); remoteSession.upload(file, pathname); } /** * Returns the length of a list of files located on the remote host. *

* This convenient methods is provided for fast compute of the total length * of a list of files to download, without contacting the server for each * file result. (Case using a progress monitor). *

* The real paths of the remote files depend on the Awake FILE configuration * on the server. See User Documentation. * * @param pathnames * the list of pathnames * * @return the total length in bytes of the files located on the remote * host. * * @throws IllegalArgumentException * if pathnames is null * @throws InvalidLoginException * the session has been closed by a {@code logoff()} * * @throws UnknownHostException * if the host URL (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws RemoteException * an exception has been thrown on the server side * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: * {@link RemoteSession#length(List)} */ public long length(List pathnames) throws IllegalArgumentException, InvalidLoginException, UnknownHostException, ConnectException, SocketException, RemoteException, IOException { if (pathnames == null) { throw new IllegalArgumentException("pathnames can not be null!"); } List remoteFilesUnix = new ArrayList(); for (String remoteFile : pathnames) { remoteFile = PathUtil.rewriteToUnixSyntax(remoteFile); remoteFilesUnix.add(remoteFile); } long length = remoteSession.length(remoteFilesUnix); return length; } /** * Renames a remote pathname.
* ({@code File.renameTo(File)} is executed on remote host.)
* The method allows a previous delete of the file corresponding to the * destination file name. *

* The real path of the remote file depends on the Awake FILE configuration * on the server. See User Documentation. * * @param pathname * A pathname on the host * * @param dest * The pathname new name * * @param deleteIfExists * If true, the file or directory scorresponding to {@code dest} * will be deleted if it exists with {@code File.delete()} before * the rename * * @return {@code true} if and only if the renaming succeeded; {@code false} * otherwise * * @throws IllegalArgumentException * if pathname or dest is null * @throws InvalidLoginException * the session has been closed by a {@code logoff()} * * @throws UnknownHostException * if the host URL (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws RemoteException * an exception has been thrown on the server side * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: * {@link RemoteFile#renameTo(RemoteFile)} */ public boolean rename(String pathname, String dest, boolean deleteIfExists) throws IllegalArgumentException, InvalidLoginException, UnknownHostException, ConnectException, SocketException, RemoteException, IOException { pathname = PathUtil.rewriteToUnixSyntax(pathname); dest = PathUtil.rewriteToUnixSyntax(dest); RemoteFile remotefile = new RemoteFile(remoteSession, pathname); RemoteFile remotefileDest = new RemoteFile(remoteSession, dest); if (!remotefile.exists()) { return false; } boolean result = false; /* Delete the new file if it exists to avoid rename failure */ if (deleteIfExists && remotefileDest.exists()) { result = remotefileDest.delete(); if (!result) { return false; } } result = remotefile.renameTo(remotefileDest); return result; } /** * Returns the length of a file located on the remote host.
* ({@code File.lenth()} is executed on remote host.) * *

* The real path of the remote file depends on the Awake FILE configuration * on the server. See User Documentation. * * @param pathname * the pathname on host * * @return the length in bytes of the file located on the remote host. * * @throws IllegalArgumentException * if pathname is null * @throws InvalidLoginException * the session has been closed by a {@code logoff()} * * @throws UnknownHostException * if the Host URL (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws RemoteException * an exception has been thrown on the server side * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: {@link RemoteFile#length()} */ public long length(String pathname) throws IllegalArgumentException, InvalidLoginException, UnknownHostException, ConnectException, SocketException, RemoteException, IOException { pathname = PathUtil.rewriteToUnixSyntax(pathname); long length = new RemoteFile(remoteSession, pathname).length(); return length; } /** * Deletes a remote file or remote directory on the host. (Directory must be * empty to be deleted).
* ({@code File.delete()} is executed on remote host.) *

* The real path of the remote file depends on the Awake FILE configuration * on the server. See User Documentation. * * @param pathname * the pathname on host * * @return true if the remote file or directory has been deleted * * @throws IllegalArgumentException * if pathname is null * @throws InvalidLoginException * the session has been closed by a {@code logoff()} * * @throws UnknownHostException * if host URL (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws RemoteException * an exception has been thrown on the server side * @throws SecurityException * the url is not secured with https (SSL) * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: {@link RemoteFile#delete()} */ public boolean delete(String pathname) throws IllegalArgumentException, InvalidLoginException, UnknownHostException, ConnectException, RemoteException, IOException { pathname = PathUtil.rewriteToUnixSyntax(pathname); boolean result = new RemoteFile(remoteSession, pathname).delete(); return result; } /** * Says if a remote file or directory exists.
* ({@code File.exists()} is executed on remote host.) *

* The real path of the remote file depends on the Awake FILE configuration * on the server. See User Documentation. * * @param pathname * the pathname on host * * @return true if the remote file or directory exists * * @throws IllegalArgumentException * if pathname is null * @throws InvalidLoginException * the session has been closed by a {@code logoff()} * * @throws UnknownHostException * if host URL (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws RemoteException * an exception has been thrown on the server side * @throws SecurityException * the url is not secured with https (SSL) * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: {@link RemoteFile#exists()} */ public boolean exists(String pathname) throws IllegalArgumentException, InvalidLoginException, UnknownHostException, ConnectException, SocketException, RemoteException, IOException { pathname = PathUtil.rewriteToUnixSyntax(pathname); boolean result = new RemoteFile(remoteSession, pathname).exists(); return result; } /** * Creates a directory on the remote host.
* ({@code File.mkdir()} is executed on remote host.) *

* The real path of the remote file depends on the Awake FILE configuration * on the server. See User Documentation. * * @param pathname * the pathname on host * * @return true if and only if the directory was created; * false otherwise * * @throws IllegalArgumentException * if pathname is null * @throws InvalidLoginException * the session has been closed by a {@code logoff()} * * @throws UnknownHostException * if host url (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws RemoteException * an exception has been thrown on the server side * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: {@link RemoteFile#mkdir()} */ public boolean mkdir(String pathname) throws IllegalArgumentException, InvalidLoginException, UnknownHostException, ConnectException, SocketException, RemoteException, IOException { pathname = PathUtil.rewriteToUnixSyntax(pathname); boolean result = new RemoteFile(remoteSession, pathname).mkdir(); return result; } /** * Creates a directory on the remote host, including any necessary but * nonexistent parent directories.
* ({@code File.mkdirs()} is executed on remote host.) *

* The real path of the remote file depends on the Awake FILE configuration * on the server. See User Documentation. * * @param pathname * A pathname on the host * * @return true if and only if the directory was created, along * with all necessary parent directories; false * otherwise * * @throws IllegalArgumentException * if pathname is null * @throws InvalidLoginException * the session has been closed by a {@code logoff()} * * @throws UnknownHostException * if host url (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws RemoteException * an exception has been thrown on the server side * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: {@link RemoteFile#mkdirs()} */ public boolean mkdirs(String pathname) throws IllegalArgumentException, InvalidLoginException, UnknownHostException, ConnectException, SocketException, RemoteException, IOException { pathname = PathUtil.rewriteToUnixSyntax(pathname); boolean result = new RemoteFile(remoteSession, pathname).mkdirs(); return result; } /** * Lists the files contained in a remote directory.
* (Only remote files that return {@code true} to {@code File.isFile()} are * returned.) *

* The real path of the remote directory depends on the Awake FILE * configuration on the server. See User Documentation. * * @param pathname * the directory pathname on host * * @return the list of files in the remote directory. Will be * null if the remote directory does not exists. Will * be empty if the remote directory exists but has no files. * * @throws IllegalArgumentException * if pathname is null * @throws InvalidLoginException * the session has been closed by a {@code logoff()} * * @throws UnknownHostException * if host URL (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws RemoteException * an exception has been thrown on the server side * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: * {@link RemoteFile#listFiles(FileFilter)} */ public List listFiles(String pathname) throws IllegalArgumentException, InvalidLoginException, UnknownHostException, ConnectException, SocketException, RemoteException, IOException { int type = LIST_FILES_ONLY; return listFilesOrDirectories(pathname, type); } /** * Lists the sub-directories contained in a remote directory.
* (Only remote files that return {@code true} to {@code File.isDirectory()} * are returned.) *

* The real path of the remote directory depends on the Awake FILE * configuration on the server. See User Documentation. * * @param pathname * the directory pathname on host * * @return the list of directories in the remote directory. Will be * null if the remote directory does not exists. Will * be empty if the remote directory exists but has no * sub-directories. * * @throws IllegalArgumentException * if pathname is null * @throws InvalidLoginException * the session has been closed by a {@code logoff()} * * @throws UnknownHostException * if host URL (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws RemoteException * an exception has been thrown on the server side * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: * {@link RemoteFile#listFiles(FileFilter)} */ public List listDirectories(String pathname) throws IllegalArgumentException, InvalidLoginException, UnknownHostException, ConnectException, SocketException, RemoteException, IOException { int type = LIST_DIRECTORIES_ONLY; return listFilesOrDirectories(pathname, type); } /** * Calls a remote Java method and (eventually) pass some parameters to it. * * @param methodName * the full method name to call in the format * org.acme.config.package.MyClass.myMethod * @param params * the array of parameters passed to the method * * @return the result of the Java call as {@code String} * * @throws IllegalArgumentException * if methodName is null * @throws InvalidLoginException * the session has been closed by a {@code logoff()} * * @throws UnknownHostException * if host URL (http://www.acme.org) does not exists or no * Internet Connection. * @throws ConnectException * if the Host is correct but the {@code ServerFileManager} * Servlet is not reachable * (http://www.acme.org/ServerFileManager) and access failed * with a status != OK (200). (If the host is incorrect, or is * impossible to connect to - Tomcat down - the * {@code ConnectException} will be the sub exception * {@code HttpHostConnectException}.) * @throws SocketException * if network failure during transmission * @throws RemoteException * an exception has been thrown on the server side * @throws IOException * for all other IO / Network / System Error * * @deprecated As of version 3.0, replaced by: * {@link RemoteSession#call(String, Object...)} */ public String call(String methodName, Object... params) throws IllegalArgumentException, InvalidLoginException, UnknownHostException, ConnectException, SocketException, RemoteException, IOException { String result = remoteSession.call(methodName, params); return result; } /** * Allows to get a copy of the current RemoteSession: use it to * do some simultaneous operations in a different thread (in order to avoid * conflicts). */ @Override public FileSession clone() { FileSession fileSession = new FileSession(this.url, this.username, this.authenticationToken, this.httpProxy, this.httpProtocolParameters, remoteSession); return fileSession; } /** * Returns the Awake FILE Version. * * @return the Awake FILE Version */ public String getVersion() { return FileVersion.getVersion(); } /** * Logs off from the remote host.  This will purge the authentication * values necessary for method calls. *

* Method should be called at the closure of the Client application. */ public void logoff() { remoteSession.logoff(); username = null; authenticationToken = null; httpProxy = null; httpProtocolParameters = null; if (httpTransfer != null) { httpTransfer.close(); httpTransfer = null; } } /** * get the list of files or directories in a directory * * @param pathname * the directory pathname to list * @param type * the type of File list to return: LIST_FILES_ONLY or * LIST_DIRECTORIES_ONLY * @return the files or directories list * * @throws InvalidLoginException */ private List listFilesOrDirectories(String pathname, int type) throws InvalidLoginException { pathname = PathUtil.rewriteToUnixSyntax(pathname); RemoteFile theRemoteFile = new RemoteFile(remoteSession, pathname); FileFilter fileFilter = null; if (type == LIST_FILES_ONLY) { fileFilter = FileFileFilter.FILE; } else if (type == LIST_DIRECTORIES_ONLY) { fileFilter = DirectoryFileFilter.DIRECTORY; } else { throw new IllegalArgumentException( Tag.PRODUCT_PRODUCT_FAIL + " Invalid type. Msust be LIST_FILES_ONLY or LIST_DIRECTORIES_ONLY. Is: " + type); } RemoteFile[] remoteFiles = theRemoteFile.listFiles(fileFilter); if (remoteFiles == null) { return null; } List filenameList = new ArrayList(); for (RemoteFile remoteFileItem : remoteFiles) { filenameList.add(remoteFileItem.getName()); } return filenameList; } /** * debug tool */ private void debug(String s) { if (DEBUG) { ClientLogger.getLogger().log(Level.WARNING, s); } } } // End





© 2015 - 2024 Weber Informatics LLC | Privacy Policy