From caece04f267b3d3f3476564ec7810e12f5b39169 Mon Sep 17 00:00:00 2001 From: orbiter Date: Sun, 22 Aug 2010 22:51:31 +0000 Subject: [PATCH] removed System.err and System.out usage from FTPClient; changed logging to log4j (preferred in yacy.cora) git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7064 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- .../anomic/crawler/retrieval/FTPLoader.java | 3 +- source/net/yacy/cora/protocol/Client.java | 644 ------------------ .../protocol/GzipDecompressingEntity.java | 73 -- .../cora/protocol/GzipRequestInterceptor.java | 46 -- .../protocol/GzipResponseInterceptor.java | 62 -- .../net/yacy/cora/protocol/ProxySettings.java | 97 --- .../net/yacy/cora/protocol/ftp/FTPClient.java | 597 +++++++--------- 7 files changed, 260 insertions(+), 1262 deletions(-) delete mode 100644 source/net/yacy/cora/protocol/Client.java delete mode 100644 source/net/yacy/cora/protocol/GzipDecompressingEntity.java delete mode 100644 source/net/yacy/cora/protocol/GzipRequestInterceptor.java delete mode 100644 source/net/yacy/cora/protocol/GzipResponseInterceptor.java delete mode 100644 source/net/yacy/cora/protocol/ProxySettings.java diff --git a/source/de/anomic/crawler/retrieval/FTPLoader.java b/source/de/anomic/crawler/retrieval/FTPLoader.java index 507e45123..8e316b25f 100644 --- a/source/de/anomic/crawler/retrieval/FTPLoader.java +++ b/source/de/anomic/crawler/retrieval/FTPLoader.java @@ -94,8 +94,7 @@ public class FTPLoader { final ByteArrayOutputStream berr = new ByteArrayOutputStream(); // create new ftp client - final PrintStream err = new PrintStream(berr); - final FTPClient ftpClient = new FTPClient(System.in, null, err); + final FTPClient ftpClient = new FTPClient(); ftpClient.setDataTimeoutByMaxFilesize(maxFileSize); // get a connection diff --git a/source/net/yacy/cora/protocol/Client.java b/source/net/yacy/cora/protocol/Client.java deleted file mode 100644 index e8814e396..000000000 --- a/source/net/yacy/cora/protocol/Client.java +++ /dev/null @@ -1,644 +0,0 @@ -/** - * Client - * Copyright 2010 by Sebastian Gaebel - * First released 01.07.2010 at http://yacy.net - * - * $LastChangedDate: 2010-06-16 17:11:21 +0200 (Mi, 16 Jun 2010) $ - * $LastChangedRevision: 7020 $ - * $LastChangedBy: sixcooler $ - * - * This library 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. - * - * This library 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 program in the file lgpl21.txt - * If not, see . - */ - - -package net.yacy.cora.protocol; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.util.LinkedHashMap; -import java.util.Set; -import java.util.Map.Entry; -import java.util.concurrent.TimeUnit; - -import org.apache.http.Header; -import org.apache.http.HttpEntity; -import org.apache.http.HttpHost; -import org.apache.http.HttpResponse; -import org.apache.http.HttpVersion; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpHead; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.client.params.HttpClientParams; -import org.apache.http.conn.ClientConnectionManager; -import org.apache.http.conn.params.ConnManagerParams; -import org.apache.http.conn.params.ConnPerRouteBean; -import org.apache.http.conn.params.ConnRouteParams; -import org.apache.http.conn.routing.HttpRoute; -import org.apache.http.conn.scheme.PlainSocketFactory; -import org.apache.http.conn.scheme.Scheme; -import org.apache.http.conn.scheme.SchemeRegistry; -import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.entity.InputStreamEntity; -import org.apache.http.entity.mime.MultipartEntity; -import org.apache.http.entity.mime.content.ContentBody; -import org.apache.http.entity.mime.content.StringBody; -import org.apache.http.impl.client.AbstractHttpClient; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; -import org.apache.http.message.BasicHeader; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.params.HttpConnectionParams; -import org.apache.http.params.HttpParams; -import org.apache.http.params.HttpProtocolParams; -import org.apache.http.protocol.BasicHttpContext; -import org.apache.http.protocol.HTTP; -import org.apache.http.protocol.HttpContext; -import org.apache.http.util.EntityUtils; - - -/** - * HttpClient implementation which uses HttpComponents Client {@link http://hc.apache.org/} - * - * @author sixcooler - * - */ -public class Client { - - private final static int maxcon = 20; - private static IdledConnectionEvictor idledConnectionEvictor = null; - private static HttpClient httpClient = null; - private Header[] headers = null; - private HttpResponse httpResponse = null; - private HttpUriRequest currentRequest = null; - private long upbytes = 0L; - private int timeout = 10000; - private String userAgent = null; - private String host = null; - private boolean redirecting = true; - private String realm = null; - - public Client() { - super(); - if (httpClient == null) { - initConnectionManager(); - } - } - - public static void setDefaultUserAgent(final String defaultAgent) { - HttpProtocolParams.setUserAgent(httpClient.getParams(), defaultAgent); - } - - private static void initConnectionManager() { - // Create and initialize HTTP parameters - final HttpParams httpParams = new BasicHttpParams(); - /** - * ConnectionManager settings - */ - // TODO: how much connections do we need? - default: 20 - ConnManagerParams.setMaxTotalConnections(httpParams, maxcon); - // for statistics same value should also be set here - ConnectionInfo.setMaxcount(maxcon); - // connections per host (2 default) - final ConnPerRouteBean connPerRoute = new ConnPerRouteBean(2); - // Increase max connections for localhost - HttpHost localhost = new HttpHost("locahost"); - connPerRoute.setMaxForRoute(new HttpRoute(localhost), maxcon); - ConnManagerParams.setMaxConnectionsPerRoute(httpParams, connPerRoute); - // how long to wait for getting a connection from manager in milliseconds - ConnManagerParams.setTimeout(httpParams, 3000L); - /** - * HTTP protocol settings - */ - HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); - // UserAgent - HttpProtocolParams.setUserAgent(httpParams, "yacy (" + systemOST +") yacy.net"); - HttpProtocolParams.setUseExpectContinue(httpParams, true); - /** - * HTTP connection settings - */ - // timeout in milliseconds until a connection is established in milliseconds - HttpConnectionParams.setConnectionTimeout(httpParams, 10000); - // SO_LINGER affects the socket close operation in seconds - // HttpConnectionParams.setLinger(httpParams, 6); - // TODO: is default ok? - // HttpConnectionParams.setSocketBufferSize(httpParams, 8192); - // SO_TIMEOUT: maximum period inactivity between two consecutive data packets in milliseconds - HttpConnectionParams.setSoTimeout(httpParams, 5000); - // getting an I/O error when executing a request over a connection that has been closed at the server side - HttpConnectionParams.setStaleCheckingEnabled(httpParams, true); - // conserve bandwidth by minimizing the number of segments that are sent - HttpConnectionParams.setTcpNoDelay(httpParams, false); - // TODO: testing noreuse - there will be HttpConnectionParams.setSoReuseaddr(HttpParams params, boolean reuseaddr) in core-4.1 - - // Create and initialize scheme registry - final SchemeRegistry schemeRegistry = new SchemeRegistry(); - schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); - schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); - - ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry); - - httpClient = new DefaultHttpClient(clientConnectionManager, httpParams); - // ask for gzip - ((AbstractHttpClient) httpClient).addRequestInterceptor(new GzipRequestInterceptor()); - // uncompress gzip - ((AbstractHttpClient) httpClient).addResponseInterceptor(new GzipResponseInterceptor()); - - idledConnectionEvictor = new IdledConnectionEvictor(clientConnectionManager); - idledConnectionEvictor.start(); - - } - - /** - * This method should be called just before shutdown - * to stop the ConnectionManager and idledConnectionEvictor - * - * @throws InterruptedException - */ - public static void closeConnectionManager() throws InterruptedException { - if (idledConnectionEvictor != null) { - // Shut down the evictor thread - idledConnectionEvictor.shutdown(); - idledConnectionEvictor.join(); - } - if (httpClient != null) { - // Shut down the connection manager - httpClient.getConnectionManager().shutdown(); - } - - } - - /** - * This method sets the Header used for the request - * - * @param entrys to be set as request header - */ - public void setHeader(final Set> entrys) { - if (entrys != null) { - int i = 0; - headers = new Header[entrys.size()]; - for (final Entry entry : entrys) { - headers[i++] = new BasicHeader(entry.getKey(),entry.getValue()); - } - } - } - - /** - * This method sets the timeout of the Connection and Socket - * - * @param timeout in milliseconds - */ - public void setTimout(final int timeout) { - this.timeout = timeout; - } - - /** - * This method sets the UserAgent to be used for the request - * - * @param userAgent - */ - public void setUserAgent(final String userAgent) { - this.userAgent = userAgent; - } - - /** - * This method sets the host to be called at the request - * - * @param host - */ - public void setHost(final String host) { - this.host = host; - } - - /** - * This method sets if requests should follow redirects - * - * @param redirecting - */ - public void setRedirecting(final boolean redirecting) { - this.redirecting = redirecting; - } - - /** - * This method sets the authorization realm for the request - * - * @param realm - */ - public void setRealm(final String realm) { - this.realm = realm; - } - - /** - * This method GETs a page from the server. - * - * @param uri the url to get - * @return content bytes - * @throws IOException - */ - public byte[] GETbytes(final String uri) throws IOException { - return GETbytes(uri, Long.MAX_VALUE); - } - - /** - * This method GETs a page from the server. - * - * @param uri the url to get - * @param maxBytes to get - * @return content bytes - * @throws IOException - */ - public byte[] GETbytes(final String uri, long maxBytes) throws IOException { - final HttpGet httpGet = new HttpGet(uri); - return getContentBytes(httpGet, maxBytes); - } - - /** - * This method GETs a page from the server. - * to be used for streaming out - * Please take care to call finish()! - * - * @param uri the url to get - * @throws IOException - */ - public void GET(final String uri) throws IOException { - if (currentRequest != null) throw new IOException("Client is in use!"); - final HttpGet httpGet = new HttpGet(uri); - currentRequest = httpGet; - execute(httpGet); - } - - /** - * This method gets HEAD response - * - * @param uri the url to Response from - * @return the HttpResponse - * @throws IOException - */ - public HttpResponse HEADResponse(final String uri) throws IOException { - final HttpHead httpHead = new HttpHead(uri); - execute(httpHead); - finish(); - ConnectionInfo.removeConnection(httpHead.hashCode()); - return httpResponse; - } - - /** - * This method POSTs a page from the server. - * to be used for streaming out - * Please take care to call finish()! - * - * @param uri the url to post - * @param instream the input to post - * @param length the contentlength - * @throws IOException - */ - public void POST(final String uri, final InputStream instream, long length) throws IOException { - if (currentRequest != null) throw new IOException("Client is in use!"); - final HttpPost httpPost = new HttpPost(uri); - final InputStreamEntity inputStreamEntity = new InputStreamEntity(instream, length); - // statistics - upbytes = length; - httpPost.setEntity(inputStreamEntity); - currentRequest = httpPost; - execute(httpPost); - } - - /** - * This method POSTs a page from the server. - * - * @param uri the url to post - * @param parts to post - * @return content bytes - * @throws IOException - */ - public byte[] POSTbytes(final String uri, final LinkedHashMap parts) throws IOException { - final HttpPost httpPost = new HttpPost(uri); - - final MultipartEntity multipartEntity = new MultipartEntity(); - for (Entry part : parts.entrySet()) - multipartEntity.addPart(part.getKey(), part.getValue()); - // statistics - upbytes = multipartEntity.getContentLength(); - - httpPost.setEntity(multipartEntity); - - return getContentBytes(httpPost, Long.MAX_VALUE); - } - - /** - * - * @return HttpResponse from call - */ - public HttpResponse getHttpResponse() { - return httpResponse; - } - - /** - * - * @return status code from http request - */ - public int getStatusCode() { - return httpResponse.getStatusLine().getStatusCode(); - } - - /** - * This method gets direct access to the content-stream - * Since this way is uncontrolled by the Client think of using 'writeTo' instead! - * Please take care to call finish()! - * - * @return the content as InputStream - * @throws IOException - */ - public InputStream getContentstream() throws IOException { - if (httpResponse != null && currentRequest != null) { - final HttpEntity httpEntity = httpResponse.getEntity(); - if (httpEntity != null) try { - return httpEntity.getContent(); - } catch (final IOException e) { - ConnectionInfo.removeConnection(currentRequest.hashCode()); - currentRequest.abort(); - currentRequest = null; - throw e; - } - } - return null; - } - - /** - * This method streams the content to the outputStream - * Please take care to call finish()! - * - * @param outputStream - * @throws IOException - */ - public void writeTo(final OutputStream outputStream) throws IOException { - if (httpResponse != null && currentRequest != null) { - final HttpEntity httpEntity = httpResponse.getEntity(); - if (httpEntity != null) try { - httpEntity.writeTo(outputStream); - outputStream.flush(); - // TODO: The name of this method is misnomer. - // It will be renamed to #finish() in the next major release of httpcore - httpEntity.consumeContent(); - ConnectionInfo.removeConnection(currentRequest.hashCode()); - currentRequest = null; - } catch (final IOException e) { - ConnectionInfo.removeConnection(currentRequest.hashCode()); - currentRequest.abort(); - currentRequest = null; - throw e; - } - } - } - - /** - * This method ensures correct finish of client-connections - * This method should be used after every use of GET or POST and writeTo or getContentstream! - * - * @throws IOException - */ - public void finish() throws IOException { - if (httpResponse != null) { - final HttpEntity httpEntity = httpResponse.getEntity(); - if (httpEntity != null && httpEntity.isStreaming()) { - // TODO: The name of this method is misnomer. - // It will be renamed to #finish() in the next major release of httpcore - httpEntity.consumeContent(); - } - } - if (currentRequest != null) { - ConnectionInfo.removeConnection(currentRequest.hashCode()); - currentRequest.abort(); - currentRequest = null; - } - } - - private byte[] getContentBytes(final HttpUriRequest httpUriRequest, final long maxBytes) throws IOException { - byte[] content = null; - try { - execute(httpUriRequest); - if (httpResponse == null) return null; - // get the response body - final HttpEntity httpEntity = httpResponse.getEntity(); - if (httpEntity != null) { - if (httpEntity.getContentLength() < maxBytes) { - content = EntityUtils.toByteArray(httpEntity); - } - // TODO: The name of this method is misnomer. - // It will be renamed to #finish() in the next major release of httpcore - httpEntity.consumeContent(); - } - } catch (final IOException e) { - ConnectionInfo.removeConnection(httpUriRequest.hashCode()); - httpUriRequest.abort(); - throw e; - } - ConnectionInfo.removeConnection(httpUriRequest.hashCode()); - return content; - } - - private void execute(final HttpUriRequest httpUriRequest) throws IOException { - final HttpContext httpContext = new BasicHttpContext(); - setHeaders(httpUriRequest); - setParams(httpUriRequest.getParams()); - setProxy(httpUriRequest.getParams()); - // statistics - storeConnectionInfo(httpUriRequest); - try { - // execute the method - httpResponse = httpClient.execute(httpUriRequest, httpContext); - } catch (ClientProtocolException e) { - ConnectionInfo.removeConnection(httpUriRequest.hashCode()); - httpUriRequest.abort(); - throw new IOException("Client can't execute: " + e.getMessage()); - } - } - - private void setHeaders(final HttpUriRequest httpUriRequest) { - if (headers != null) { - for (Header header : headers) { - httpUriRequest.addHeader(header); - } - } - if (realm != null) - httpUriRequest.setHeader("Authorization", "realm=" + realm); - } - - private void setParams(final HttpParams httpParams) { - HttpClientParams.setRedirecting(httpParams, redirecting); - HttpConnectionParams.setConnectionTimeout(httpParams, timeout); - HttpConnectionParams.setSoTimeout(httpParams, timeout); - if (userAgent != null) - HttpProtocolParams.setUserAgent(httpParams, userAgent); - if (host != null) - httpParams.setParameter(HTTP.TARGET_HOST, host); - } - - private void setProxy(final HttpParams httpParams) { - if (ProxySettings.use) - ConnRouteParams.setDefaultProxy(httpParams, ProxySettings.getProxyHost()); - // TODO find a better way for this - ProxySettings.setProxyCreds((AbstractHttpClient) httpClient); - } - - private void storeConnectionInfo(final HttpUriRequest httpUriRequest) { - final int port = httpUriRequest.getURI().getPort(); - final String thost = httpUriRequest.getURI().getHost(); - ConnectionInfo.addConnection(new ConnectionInfo( - httpUriRequest.getURI().getScheme(), - port == 80 ? thost : thost + ":" + port, - httpUriRequest.getMethod() + " " + httpUriRequest.getURI().getPath(), - httpUriRequest.hashCode(), - System.currentTimeMillis(), - upbytes)); - } - - /** - * provide system information for client identification - */ - private static final String systemOST = System.getProperty("os.arch", "no-os-arch") + " " + - System.getProperty("os.name", "no-os-name") + " " + System.getProperty("os.version", "no-os-version") + - "; " + "java " + System.getProperty("java.version", "no-java-version") + "; " + generateLocation(); - - /** - * generating the location string - * - * @return - */ - public static String generateLocation() { - String loc = System.getProperty("user.timezone", "nowhere"); - final int p = loc.indexOf('/'); - if (p > 0) { - loc = loc.substring(0, p); - } - loc = loc + "/" + System.getProperty("user.language", "dumb"); - return loc; - } - - /** - * @return the systemOST - */ - public static String getSystemOST() { - return systemOST; - } - - /** - * testing - * - * @param args urls to test - */ - public static void main(final String[] args) { - String url = null; - // prepare Parts - final LinkedHashMap newparts = new LinkedHashMap(); - try { - newparts.put("foo", new StringBody("FooBar")); - newparts.put("bar", new StringBody("BarFoo")); - } catch (UnsupportedEncodingException e) { - System.out.println(e.getStackTrace()); - } - Client client = new Client(); - client.setUserAgent("foobar"); - client.setRedirecting(false); - // Get some - for (int i = 0; i < args.length; i++) { - url = args[i]; - if (!url.toUpperCase().startsWith("HTTP://")) { - url = "http://" + url; - } - try { - System.out.println(new String(client.GETbytes(url))); - } catch (IOException e) { - e.printStackTrace(); - } - } - // Head some -// try { -// client.HEADResponse(url); -// } catch (IOException e) { -// e.printStackTrace(); -// } - for (Header header: client.getHttpResponse().getAllHeaders()) { - System.out.println("Header " + header.getName() + " : " + header.getValue()); -// for (HeaderElement element: header.getElements()) -// System.out.println("Element " + element.getName() + " : " + element.getValue()); - } - System.out.println(client.getHttpResponse().getLocale()); - System.out.println(client.getHttpResponse().getProtocolVersion()); - System.out.println(client.getHttpResponse().getStatusLine()); - // Post some -// try { -// System.out.println(new String(client.POSTbytes(url, newparts))); -// } catch (IOException e1) { -// e1.printStackTrace(); -// } - // Close out connection manager - try { - Client.closeConnectionManager(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - - /** - * - * @see: http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/connmgmt.html#d4e638 - * - */ - public static class IdledConnectionEvictor extends Thread { - - private final ClientConnectionManager clientConnectionManager; - - private volatile boolean shutdown; - - public IdledConnectionEvictor(ClientConnectionManager clientConnectionManager) { - super(); - this.clientConnectionManager = clientConnectionManager; - } - - @Override - public void run() { - try { - while (!shutdown) { - synchronized (this) { - wait(5000); - // Close expired connections - clientConnectionManager.closeExpiredConnections(); - // Optionally, close connections - // that have been idle longer than 5 sec - // (some SOHO router act strange on >5sec idled connections) - clientConnectionManager.closeIdleConnections(5, TimeUnit.SECONDS); - } - } - } catch (InterruptedException ex) { - // terminate - } - } - - public void shutdown() { - shutdown = true; - synchronized (this) { - notifyAll(); - } - } - - } - -} diff --git a/source/net/yacy/cora/protocol/GzipDecompressingEntity.java b/source/net/yacy/cora/protocol/GzipDecompressingEntity.java deleted file mode 100644 index d3bc1f1d9..000000000 --- a/source/net/yacy/cora/protocol/GzipDecompressingEntity.java +++ /dev/null @@ -1,73 +0,0 @@ -/** - * GzipDecompressingEntity - * Copyright 2010 by Sebastian Gaebel - * First released 01.07.2010 at http://yacy.net - * - * $LastChangedDate: 2010-06-16 17:11:21 +0200 (Mi, 16 Jun 2010) $ - * $LastChangedRevision: 7011 $ - * $LastChangedBy: sixcooler $ - * - * This library 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. - * - * This library 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 program in the file lgpl21.txt - * If not, see . - */ - - -package net.yacy.cora.protocol; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.zip.GZIPInputStream; - -import org.apache.http.HttpEntity; -import org.apache.http.entity.HttpEntityWrapper; - -public class GzipDecompressingEntity extends HttpEntityWrapper { - - private static final int DEFAULT_BUFFER_SIZE = 1024; // this is also the maximum chunk size - - public GzipDecompressingEntity(final HttpEntity entity) { - super(entity); - } - - public InputStream getContent() throws IOException, IllegalStateException { - - // the wrapped entity's getContent() decides about repeatability - InputStream wrappedin = wrappedEntity.getContent(); - - return new GZIPInputStream(wrappedin); - } - - public void writeTo(OutputStream outstream) throws IOException { - if (outstream == null) { - throw new IllegalArgumentException("Output stream may not be null"); - } - InputStream instream = this.getContent(); - int l; - byte[] tmp = new byte[DEFAULT_BUFFER_SIZE]; - while ((l = instream.read(tmp)) != -1) { - outstream.write(tmp, 0, l); - } - } - - public boolean isChunked() { - return true; - } - - public long getContentLength() { - // length of ungzipped content not known in advance - return -1; - } - -} diff --git a/source/net/yacy/cora/protocol/GzipRequestInterceptor.java b/source/net/yacy/cora/protocol/GzipRequestInterceptor.java deleted file mode 100644 index 2c5ab2752..000000000 --- a/source/net/yacy/cora/protocol/GzipRequestInterceptor.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * GzipRequestInterceptor - * Copyright 2010 by Sebastian Gaebel - * First released 01.07.2010 at http://yacy.net - * - * $LastChangedDate: 2010-06-16 17:11:21 +0200 (Mi, 16 Jun 2010) $ - * $LastChangedRevision: 7001 $ - * $LastChangedBy: sixcooler $ - * - * This library 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. - * - * This library 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 program in the file lgpl21.txt - * If not, see . - */ - - -package net.yacy.cora.protocol; - -import java.io.IOException; - -import org.apache.http.HttpException; -import org.apache.http.HttpRequest; -import org.apache.http.HttpRequestInterceptor; -import org.apache.http.protocol.HttpContext; - -public class GzipRequestInterceptor implements HttpRequestInterceptor { - - private static final String ACCEPT_ENCODING = "Accept-Encoding"; - private static final String GZIP_CODEC = "gzip"; - - public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { - if (!request.containsHeader(ACCEPT_ENCODING)) { - request.addHeader(ACCEPT_ENCODING, GZIP_CODEC); - } - } - -} diff --git a/source/net/yacy/cora/protocol/GzipResponseInterceptor.java b/source/net/yacy/cora/protocol/GzipResponseInterceptor.java deleted file mode 100644 index 5e5f6caa6..000000000 --- a/source/net/yacy/cora/protocol/GzipResponseInterceptor.java +++ /dev/null @@ -1,62 +0,0 @@ -/** - * GzipResponseInterceptor - * Copyright 2010 by Sebastian Gaebel - * First released 01.07.2010 at http://yacy.net - * - * $LastChangedDate: 2010-06-16 17:11:21 +0200 (Mi, 16 Jun 2010) $ - * $LastChangedRevision: 7001 $ - * $LastChangedBy: sixcooler $ - * - * This library 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. - * - * This library 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 program in the file lgpl21.txt - * If not, see . - */ - - -package net.yacy.cora.protocol; - -import java.io.IOException; - -import org.apache.http.Header; -import org.apache.http.HeaderElement; -import org.apache.http.HttpEntity; -import org.apache.http.HttpException; -import org.apache.http.HttpResponse; -import org.apache.http.HttpResponseInterceptor; -import org.apache.http.protocol.HttpContext; - -public class GzipResponseInterceptor implements HttpResponseInterceptor { - - private static final String GZIP_CODEC = "gzip"; - - public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { - if (context == null) { - throw new IllegalArgumentException("HTTP context may not be null"); - } - HttpEntity entity = response.getEntity(); - if (entity != null) { - Header ceheader = entity.getContentEncoding(); - if (ceheader != null) { - HeaderElement[] codecs = ceheader.getElements(); - for (int i = 0; i < codecs.length; i++) { - if (codecs[i].getName().equalsIgnoreCase(GZIP_CODEC)) { -// response.removeHeader(ceheader); - response.setEntity(new GzipDecompressingEntity(response.getEntity())); - return; - } - } - } - } - } - -} diff --git a/source/net/yacy/cora/protocol/ProxySettings.java b/source/net/yacy/cora/protocol/ProxySettings.java deleted file mode 100644 index 87244b285..000000000 --- a/source/net/yacy/cora/protocol/ProxySettings.java +++ /dev/null @@ -1,97 +0,0 @@ -/** - * ProxySettings - * Copyright 2010 by Michael Peter Christen - * First released 25.05.2010 at http://yacy.net - * - * This library 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. - * - * This library 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 program in the file lgpl21.txt - * If not, see . - */ - -package net.yacy.cora.protocol; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.commons.httpclient.HostConfiguration; -import org.apache.commons.httpclient.HttpClient; -import org.apache.http.HttpHost; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.impl.client.AbstractHttpClient; - -/** - * settings for a remote proxy - * - */ -public final class ProxySettings { - - // Dummy value to associate with an Object in the backing Map - private static final Object PRESENT = new Object(); - - public static boolean use = false, use4YaCy = false, use4ssl = false; - public static String host = null, user = "", password = ""; - public static int port = 0; - public static String[] noProxy = null; - public static final Map allowProxy = new ConcurrentHashMap(); - public static final Map disallowProxy = new ConcurrentHashMap(); - - /** - * produce a HostConfiguration (apache object) with the proxy access information included - * @param apacheHttpClient - * @return a host configuration with proxy config if the proxy shall be used; a cloned configuration otherwise - */ - public static HostConfiguration getProxyHostConfig(HttpClient apacheHttpClient) { - final HostConfiguration hostConfig; - if (!use) return null; - hostConfig = new HostConfiguration(apacheHttpClient.getHostConfiguration()); - hostConfig.setProxy(host, port); - return hostConfig; - } - - /** - * - * @return the HttpHost to be used as proxy - */ - public static HttpHost getProxyHost() { - if (!use) return null; - return new HttpHost(host, port); - } - - public static void setProxyCreds(AbstractHttpClient httpClient) { - if (!use) return; - httpClient.getCredentialsProvider().setCredentials( - new AuthScope(host, port), - new UsernamePasswordCredentials(user, password)); - } - - /** - * tell if a remote proxy will be used for the given host - * @param host - * @return true, if the proxy shall be used for the given host - */ - public static boolean useForHost(final String host) { - if (!use) return false; - if (allowProxy.containsKey(host)) return true; - if (disallowProxy.containsKey(host)) return false; - for (String pattern: noProxy) { - if (host.matches(pattern)) { - disallowProxy.put(host, PRESENT); - return false; - } - } - allowProxy.put(host, PRESENT); - return true; - } - -} diff --git a/source/net/yacy/cora/protocol/ftp/FTPClient.java b/source/net/yacy/cora/protocol/ftp/FTPClient.java index b63af2eb9..69f6e6d83 100644 --- a/source/net/yacy/cora/protocol/ftp/FTPClient.java +++ b/source/net/yacy/cora/protocol/ftp/FTPClient.java @@ -62,17 +62,15 @@ import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; -import net.yacy.kelondro.logging.Log; import net.yacy.kelondro.util.Domains; +import org.apache.log4j.Logger; + public class FTPClient { - private static final String vDATE = "20040506"; - private static final String logPrefix = "FTPC: "; + private static Logger log = Logger.getLogger(FTPClient.class); - private final InputStream in; - private final PrintStream out; - private final PrintStream err; + private static final String vDATE = "20100823"; private boolean glob = true; // glob = false -> filenames are taken // literally for mget, .. @@ -123,19 +121,6 @@ public class FTPClient { // TODO: implement RFC 2640 Internationalization public FTPClient() { - this(System.in, System.out, System.err); - } - - public FTPClient(final java.io.InputStream ins, final java.io.PrintStream outs, final java.io.PrintStream errs) { - - // try { - // System.setSecurityManager(new sm()); - // } catch (java.security.AccessControlException e) { - // } - - in = ins; - out = outs; - err = errs; currentLocalPath = new File(System.getProperty("user.dir")); try { @@ -152,59 +137,6 @@ public class FTPClient { remotesystem = null; } - public void shell(final String server) { - String command; - - java.io.PrintWriter pw = null; - if (out != null) { - pw = new java.io.PrintWriter(out); - } - - try { - final java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(in)); - if (server != null) { - exec("open " + server, true); - } - while (true) { - - // prompt - if (pw != null) { - pw.print(prompt); - pw.flush(); - } - - // read a line - while ((command = stdin.readLine()) == null) { - if (pw != null) { - pw.print(prompt); - pw.flush(); - } - } - - // execute - if (!exec(command, false)) { - break; - } - - } - } catch (final Exception e) { - errPrintln("Error - ftp exception: " + e); - Log.logException(e); - } - } - - private void errPrintln(final String msg) { - if (err != null) { - err.println(logPrefix + "---- " + msg); - } - } - - private void outPrintln(final String msg) { - if (out != null) { - out.println(logPrefix + msg); - } - } - public boolean exec(String command, final boolean promptIt) { if ((command == null) || (command.length() == 0)) { return true; @@ -225,7 +157,7 @@ public class FTPClient { command = command.substring(pos + 1); } if (promptIt) { - outPrintln(prompt + com); + log.info(prompt + com); } cmd = line2args(com); try { @@ -236,15 +168,14 @@ public class FTPClient { if (notConnected()) { // the error was probably caused because there is no // connection - errPrintln("not connected. no effect."); - Log.logException(e); + log.error("not connected. no effect.", e); } else { - errPrintln("ftp internal exception: target exception " + e); + log.error("ftp internal exception: target exception " + e); } return ret; } } catch (final IllegalAccessException e) { - errPrintln("ftp internal exception: wrong access " + e); + log.error("ftp internal exception: wrong access " + e); return ret; } catch (final NoSuchMethodException e) { // consider first that the user attempted to execute a java @@ -255,7 +186,7 @@ public class FTPClient { try { javaexec(cmd); } catch (final Exception ee) { - errPrintln("Command '" + cmd[0] + "' not supported. Try 'HELP'."); + log.error("Command '" + cmd[0] + "' not supported. Try 'HELP'."); } } else { // try a remote exec @@ -354,7 +285,7 @@ public class FTPClient { // pr.put("java.class.path", "" + pr.get("user.dir") + // pr.get("path.separator") + origPath); - // errPrintln(logPrefix + "System Properties: " + pr.toString()); + // log.error("System Properties: " + pr.toString()); System.setProperties(pr); @@ -374,41 +305,38 @@ public class FTPClient { // handle result if (result != null) { - outPrintln("returns " + result); + log.info("returns " + result); } // set the local path to the user.dir (which may have changed) currentLocalPath = new File((String) pr.get("user.dir")); } catch (final ClassNotFoundException e) { - // errPrintln("cannot find class file " + obj + + // log.error("cannot find class file " + obj + // ".class"); // class file does not exist, go silently over it to not show // everybody that the // system attempted to load a class file - errPrintln("Command '" + obj + "' not supported. Try 'HELP'."); + log.error("Command '" + obj + "' not supported. Try 'HELP'."); } catch (final NoSuchMethodException e) { - errPrintln("no \"public static main(String args[])\" in " + obj); + log.error("no \"public static main(String args[])\" in " + obj); } catch (final InvocationTargetException e) { final Throwable orig = e.getTargetException(); if (orig.getMessage() != null) { - errPrintln("Exception from " + obj + ": " + orig.getMessage()); - Log.logException(orig); + log.error("Exception from " + obj + ": " + orig.getMessage(), orig); } } catch (final IllegalAccessException e) { - errPrintln("Illegal access for " + obj + ": class is probably not declared as public"); - Log.logException(e); + log.error("Illegal access for " + obj + ": class is probably not declared as public", e); } catch (final NullPointerException e) { - errPrintln("main(String args[]) is not defined as static for " + obj); + log.error("main(String args[]) is not defined as static for " + obj); /* * } catch (IOException e) { // class file does not exist, go * silently over it to not show everybody that the // system - * attempted to load a class file errPrintln("Command '" + obj + "' + * attempted to load a class file log.error("Command '" + obj + "' * not supported. Try 'HELP'."); */ } catch (final Exception e) { - errPrintln("Exception caught: " + e); - Log.logException(e); + log.error("Exception caught: ", e); } // set the classpath to its original definition @@ -420,26 +348,26 @@ public class FTPClient { public boolean ASCII() { if (cmd.length != 1) { - errPrintln("Syntax: ASCII (no parameter)"); + log.error("Syntax: ASCII (no parameter)"); return true; } try { literal("TYPE A"); } catch (final IOException e) { - errPrintln("Error: ASCII transfer type not supported by server."); + log.error("Error: ASCII transfer type not supported by server."); } return true; } public boolean BINARY() { if (cmd.length != 1) { - errPrintln("Syntax: BINARY (no parameter)"); + log.error("Syntax: BINARY (no parameter)"); return true; } try { literal("TYPE I"); } catch (final IOException e) { - errPrintln("Error: BINARY transfer type not supported by server."); + log.error("Error: BINARY transfer type not supported by server."); } return true; } @@ -450,7 +378,7 @@ public class FTPClient { public boolean CD() { if (cmd.length != 2) { - errPrintln("Syntax: CD "); + log.error("Syntax: CD "); return true; } if (notConnected()) { @@ -465,7 +393,7 @@ public class FTPClient { throw new IOException(reply); } } catch (final IOException e) { - errPrintln("Error: change of working directory to path " + cmd[1] + " failed."); + log.error("Error: change of working directory to path " + cmd[1] + " failed."); } return true; } @@ -511,7 +439,7 @@ public class FTPClient { public boolean DEL() { if (cmd.length != 2) { - errPrintln("Syntax: DEL "); + log.error("Syntax: DEL "); return true; } if (notConnected()) { @@ -520,7 +448,7 @@ public class FTPClient { try { rmForced(cmd[1]); } catch (final IOException e) { - errPrintln("Error: deletion of file " + cmd[1] + " failed."); + log.error("Error: deletion of file " + cmd[1] + " failed."); } return true; } @@ -531,7 +459,7 @@ public class FTPClient { public boolean DIR() { if (cmd.length > 2) { - errPrintln("Syntax: DIR [|]"); + log.error("Syntax: DIR [|]"); return true; } if (notConnected()) { @@ -546,7 +474,7 @@ public class FTPClient { } printElements(l); } catch (final IOException e) { - errPrintln("Error: remote list not available (1): " + e.getMessage()); + log.error("Error: remote list not available (1): " + e.getMessage()); } return true; } @@ -554,7 +482,7 @@ public class FTPClient { public boolean DISCONNECT() { try { quit(); - outPrintln("---- Connection closed."); + log.info("---- Connection closed."); } catch (final IOException e) { // Connection to server lost // do not append any error to errPrintln because we can silently go over this error @@ -594,7 +522,7 @@ public class FTPClient { public boolean GET() { if ((cmd.length < 2) || (cmd.length > 3)) { - errPrintln("Syntax: GET []"); + log.error("Syntax: GET []"); return true; } final String remote = cmd[1]; // (new File(cmd[1])).getName(); @@ -604,7 +532,7 @@ public class FTPClient { final File local = absoluteLocalFile(localFilename); if (local.exists()) { - errPrintln("Error: local file " + local.toString() + " already exists.\n" + " File " + remote + log.error("Error: local file " + local.toString() + " already exists.\n" + " File " + remote + " not retrieved. Local file unchanged."); } else { if (withoutLocalFile) { @@ -613,7 +541,7 @@ public class FTPClient { try { get(local.getAbsolutePath(), remote); } catch (final IOException e) { - errPrintln("Error: retrieving file " + remote + " failed. (" + e.getMessage() + ")"); + log.error("Error: retrieving file " + remote + " failed. (" + e.getMessage() + ")"); } } } @@ -644,7 +572,7 @@ public class FTPClient { rmForced(remote); } } catch (final IOException eee) { - errPrintln("Warning: remote file or path " + remote + " cannot be removed."); + log.error("Warning: remote file or path " + remote + " cannot be removed."); } } catch (final IOException e) { if (e.getMessage().startsWith("550")) { @@ -667,13 +595,13 @@ public class FTPClient { rmForced(remote); } } catch (final IOException eee) { - errPrintln("Warning: remote file or path " + remote + " cannot be removed."); + log.error("Warning: remote file or path " + remote + " cannot be removed."); } } else { - errPrintln("Error: remote file or path " + remote + " does not exist."); + log.error("Error: remote file or path " + remote + " does not exist."); } } else { - errPrintln("Error: retrieving file " + remote + " failed. (" + e.getMessage() + ")"); + log.error("Error: retrieving file " + remote + " failed. (" + e.getMessage() + ")"); } } } @@ -718,16 +646,16 @@ public class FTPClient { public boolean GLOB() { if (cmd.length != 1) { - errPrintln("Syntax: GLOB (no parameter)"); + log.error("Syntax: GLOB (no parameter)"); return true; } glob = !glob; - outPrintln("---- globbing is now turned " + ((glob) ? "ON" : "OFF")); + log.info("---- globbing is now turned " + ((glob) ? "ON" : "OFF")); return true; } public boolean HASH() { - errPrintln("no games implemented"); + log.error("no games implemented"); return true; } @@ -742,7 +670,7 @@ public class FTPClient { public boolean JJENCODE() { if (cmd.length != 2) { - errPrintln("Syntax: JJENCODE "); + log.error("Syntax: JJENCODE "); return true; } final String path = cmd[1]; @@ -767,17 +695,17 @@ public class FTPClient { exec("cd ..;jar -cfM \"" + path + ".jj\" \"" + path + ".jar\"", true); exec("rm \"" + path + ".jar\"", true); } else { - errPrintln("Error: local path " + newPath.toString() + " denotes not to a directory."); + log.error("Error: local path " + newPath.toString() + " denotes not to a directory."); } } else { - errPrintln("Error: local path " + newPath.toString() + " does not exist."); + log.error("Error: local path " + newPath.toString() + " does not exist."); } return true; } public boolean JJDECODE() { if (cmd.length != 2) { - errPrintln("Syntax: JJENCODE "); + log.error("Syntax: JJENCODE "); return true; } final String path = cmd[1]; @@ -796,13 +724,13 @@ public class FTPClient { exec("mkdir \"" + path + ".dir\"", true); } else { - errPrintln("Error: target dir " + newFolder.toString() + " cannot be created"); + log.error("Error: target dir " + newFolder.toString() + " cannot be created"); } } else { - errPrintln("Error: local path " + newPath.toString() + " must denote to jar/jar file"); + log.error("Error: local path " + newPath.toString() + " must denote to jar/jar file"); } } else { - errPrintln("Error: local path " + newPath.toString() + " does not exist."); + log.error("Error: local path " + newPath.toString() + " does not exist."); } return true; } @@ -817,7 +745,7 @@ public class FTPClient { final String dest_name = args[1]; final File dest_file = new File(dest_name); if (dest_file.exists()) { - errPrintln(logPrefix + "join: destination file " + dest_name + " already exists"); + log.error("join: destination file " + dest_name + " already exists"); return true; } @@ -883,10 +811,10 @@ public class FTPClient { for (pc = 0; pc < args.length; pc++) { try { if (!(new File(args[pc])).delete()) { - errPrintln(logPrefix + "join: unable to delete file " + args[pc]); + log.error("join: unable to delete file " + args[pc]); } } catch (final SecurityException e) { - errPrintln(logPrefix + "join: no permission to delete file " + args[pc]); + log.error("join: no permission to delete file " + args[pc]); } } } catch (final FileNotFoundException e) { @@ -910,7 +838,7 @@ public class FTPClient { } // print appropriate message - errPrintln(logPrefix + "join created output from " + args.length + " source files"); + log.error("join created output from " + args.length + " source files"); } return true; } @@ -918,7 +846,7 @@ public class FTPClient { public boolean COPY(final String[] args) { final File dest_file = new File(args[2]); if (dest_file.exists()) { - errPrintln(logPrefix + "copy: destination file " + args[2] + " already exists"); + log.error("copy: destination file " + args[2] + " already exists"); return true; } int bytes_read = 0; @@ -979,7 +907,7 @@ public class FTPClient { public boolean LCD() { if (cmd.length != 2) { - errPrintln("Syntax: LCD "); + log.error("Syntax: LCD "); return true; } final String path = cmd[1]; @@ -992,12 +920,12 @@ public class FTPClient { if (newPath.exists()) { if (newPath.isDirectory()) { currentLocalPath = newPath; - outPrintln("---- New local path: " + currentLocalPath.toString()); + log.info("---- New local path: " + currentLocalPath.toString()); } else { - errPrintln("Error: local path " + newPath.toString() + " denotes not a directory."); + log.error("Error: local path " + newPath.toString() + " denotes not a directory."); } } else { - errPrintln("Error: local path " + newPath.toString() + " does not exist."); + log.error("Error: local path " + newPath.toString() + " does not exist."); } return true; } @@ -1008,12 +936,12 @@ public class FTPClient { public boolean LDIR() { if (cmd.length != 1) { - errPrintln("Syntax: LDIR (no parameter)"); + log.error("Syntax: LDIR (no parameter)"); return true; } final String[] name = currentLocalPath.list(); for (int n = 0; n < name.length; ++n) { - outPrintln(ls(new File(currentLocalPath, name[n]))); + log.info(ls(new File(currentLocalPath, name[n]))); } return true; } @@ -1123,7 +1051,7 @@ public class FTPClient { try { size = Integer.parseInt(tokens.group(2)); } catch (final NumberFormatException e) { - Log.logWarning("FTPC", "Error: not a number in list-entry: " + e.getMessage()); + log.warn("not a number in list-entry: ", e); return null; } String time; @@ -1146,7 +1074,7 @@ public class FTPClient { date = lsDateFormat.parse(dateString); } } catch (final ParseException e) { - Log.logWarning("FTPC", "---- Error: not ls date-format '" + dateString + "': " + e.getMessage()); + log.warn("---- Error: not ls date-format '" + dateString, e); date = new Date(); } return new entryInfo(isDir, size, date, tokens.group(6)); @@ -1256,7 +1184,7 @@ public class FTPClient { public boolean LITERAL() { if (cmd.length == 1) { - errPrintln("Syntax: LITERAL [] (see RFC959)"); + log.error("Syntax: LITERAL [] (see RFC959)"); return true; } String s = ""; @@ -1266,7 +1194,7 @@ public class FTPClient { try { literal(s.substring(1)); } catch (final IOException e) { - errPrintln("Error: Syntax of FTP-command wrong. See RFC959 for details."); + log.error("Error: Syntax of FTP-command wrong. See RFC959 for details."); } return true; } @@ -1281,15 +1209,15 @@ public class FTPClient { public boolean LMKDIR() { if (cmd.length != 2) { - errPrintln("Syntax: LMKDIR "); + log.error("Syntax: LMKDIR "); return true; } final File f = new File(currentLocalPath, cmd[1]); if (f.exists()) { - errPrintln("Error: local file/folder " + cmd[1] + " already exists"); + log.error("Error: local file/folder " + cmd[1] + " already exists"); } else { if (!f.mkdir()) { - errPrintln("Error: creation of local folder " + cmd[1] + " failed"); + log.error("Error: creation of local folder " + cmd[1] + " failed"); } } return true; @@ -1297,29 +1225,29 @@ public class FTPClient { public boolean LMV() { if (cmd.length != 3) { - errPrintln("Syntax: LMV "); + log.error("Syntax: LMV "); return true; } final File from = new File(cmd[1]); final File to = new File(cmd[2]); if (!to.exists()) { if (from.renameTo(to)) { - outPrintln("---- \"" + from.toString() + "\" renamed to \"" + to.toString() + "\""); + log.info("---- \"" + from.toString() + "\" renamed to \"" + to.toString() + "\""); } else { - errPrintln(logPrefix + "rename failed"); + log.error("rename failed"); } } else { - errPrintln(logPrefix + "\"" + to.toString() + "\" already exists"); + log.error("\"" + to.toString() + "\" already exists"); } return true; } public boolean LPWD() { if (cmd.length != 1) { - errPrintln("Syntax: LPWD (no parameter)"); + log.error("Syntax: LPWD (no parameter)"); return true; } - outPrintln("---- Local path: " + currentLocalPath.toString()); + log.info("---- Local path: " + currentLocalPath.toString()); return true; } @@ -1329,15 +1257,15 @@ public class FTPClient { public boolean LRMDIR() { if (cmd.length != 2) { - errPrintln("Syntax: LRMDIR "); + log.error("Syntax: LRMDIR "); return true; } final File f = new File(currentLocalPath, cmd[1]); if (!f.exists()) { - errPrintln("Error: local folder " + cmd[1] + " does not exist"); + log.error("Error: local folder " + cmd[1] + " does not exist"); } else { if (!f.delete()) { - errPrintln("Error: deletion of local folder " + cmd[1] + " failed"); + log.error("Error: deletion of local folder " + cmd[1] + " failed"); } } return true; @@ -1345,15 +1273,15 @@ public class FTPClient { public boolean LRM() { if (cmd.length != 2) { - errPrintln("Syntax: LRM "); + log.error("Syntax: LRM "); return true; } final File f = new File(currentLocalPath, cmd[1]); if (!f.exists()) { - errPrintln("Error: local file " + cmd[1] + " does not exist"); + log.error("Error: local file " + cmd[1] + " does not exist"); } else { if (!f.delete()) { - errPrintln("Error: deletion of file " + cmd[1] + " failed"); + log.error("Error: deletion of file " + cmd[1] + " failed"); } } return true; @@ -1361,7 +1289,7 @@ public class FTPClient { public boolean LS() { if (cmd.length > 2) { - errPrintln("Syntax: LS [|]"); + log.error("Syntax: LS [|]"); return true; } if (notConnected()) { @@ -1376,7 +1304,7 @@ public class FTPClient { } printElements(l); } catch (final IOException e) { - errPrintln("Error: remote list not available (2): " + e.getMessage()); + log.error("Error: remote list not available (2): " + e.getMessage()); } return true; } @@ -1385,11 +1313,11 @@ public class FTPClient { * @param list */ private void printElements(final List list) { - outPrintln("---- v---v---v---v---v---v---v---v---v---v---v---v---v---v---v---v---v---v---v"); + log.info("---- v---v---v---v---v---v---v---v---v---v---v---v---v---v---v---v---v---v---v"); for (final String element : list) { - outPrintln(element); + log.info(element); } - outPrintln("---- ^---^---^---^---^---^---^---^---^---^---^---^---^---^---^---^---^---^---^"); + log.info("---- ^---^---^---^---^---^---^---^---^---^---^---^---^---^---^---^---^---^---^"); } private List list(final String path, final boolean extended) throws IOException { @@ -1445,7 +1373,7 @@ public class FTPClient { public boolean MKDIR() { if (cmd.length != 2) { - errPrintln("Syntax: MKDIR "); + log.error("Syntax: MKDIR "); return true; } if (notConnected()) { @@ -1460,20 +1388,20 @@ public class FTPClient { throw new IOException(reply); } } catch (final IOException e) { - errPrintln("Error: creation of folder " + cmd[1] + " failed"); + log.error("Error: creation of folder " + cmd[1] + " failed"); } return true; } public boolean MGET() { if (cmd.length != 2) { - errPrintln("Syntax: MGET "); + log.error("Syntax: MGET "); return true; } try { mget(cmd[1], false); } catch (final IOException e) { - errPrintln("Error: mget failed (" + e.getMessage() + ")"); + log.error("Error: mget failed (" + e.getMessage() + ")"); } return true; } @@ -1485,9 +1413,9 @@ public class FTPClient { if (matches(remote, pattern)) { local = new File(currentLocalPath, remote); if (local.exists()) { - errPrintln("Warning: local file " + local.toString() + " overwritten."); + log.error("Warning: local file " + local.toString() + " overwritten."); if(!local.delete()) - errPrintln("Warning: local file " + local.toString() + " could not be deleted."); + log.error("Warning: local file " + local.toString() + " could not be deleted."); } retrieveFilesRecursively(remote, remove); } @@ -1496,13 +1424,13 @@ public class FTPClient { public boolean MOVEDOWN() { if (cmd.length != 2) { - errPrintln("Syntax: MOVEDOWN "); + log.error("Syntax: MOVEDOWN "); return true; } try { mget(cmd[1], true); } catch (final IOException e) { - errPrintln("Error: movedown failed (" + e.getMessage() + ")"); + log.error("Error: movedown failed (" + e.getMessage() + ")"); } return true; } @@ -1514,7 +1442,7 @@ public class FTPClient { */ public boolean MV() { if (cmd.length != 3) { - errPrintln("Syntax: MV "); + log.error("Syntax: MV "); return true; } if (notConnected()) { @@ -1535,27 +1463,27 @@ public class FTPClient { throw new IOException(reply); } } catch (final IOException e) { - errPrintln("Error: rename of " + cmd[1] + " to " + cmd[2] + " failed."); + log.error("Error: rename of " + cmd[1] + " to " + cmd[2] + " failed."); } return true; } public boolean NOOP() { if (cmd.length != 1) { - errPrintln("Syntax: NOOP (no parameter)"); + log.error("Syntax: NOOP (no parameter)"); return true; } try { literal("NOOP"); } catch (final IOException e) { - errPrintln("Error: server does not know how to do nothing"); + log.error("Error: server does not know how to do nothing"); } return true; } public boolean OPEN() { if ((cmd.length < 2) || (cmd.length > 3)) { - errPrintln("Syntax: OPEN []"); + log.error("Syntax: OPEN []"); return true; } int port = 21; @@ -1573,10 +1501,10 @@ public class FTPClient { } try { open(cmd[1], port); - outPrintln("---- Connection to " + cmd[1] + " established."); + log.info("---- Connection to " + cmd[1] + " established."); prompt = "ftp [" + cmd[1] + "]>"; } catch (final IOException e) { - errPrintln("Error: connecting " + cmd[1] + " on port " + port + " failed: " + e.getMessage()); + log.error("Error: connecting " + cmd[1] + " on port " + port + " failed: " + e.getMessage()); } return true; } @@ -1645,25 +1573,25 @@ public class FTPClient { } public boolean PROMPT() { - errPrintln("prompt is always off"); + log.error("prompt is always off"); return true; } public boolean PUT() { if ((cmd.length < 2) || (cmd.length > 3)) { - errPrintln("Syntax: PUT []"); + log.error("Syntax: PUT []"); return true; } final File local = new File(currentLocalPath, cmd[1]); final String remote = (cmd.length == 2) ? local.getName() : cmd[2]; if (!local.exists()) { - errPrintln("Error: local file " + local.toString() + " does not exist."); - errPrintln(logPrefix + " Remote file " + remote + " not overwritten."); + log.error("Error: local file " + local.toString() + " does not exist."); + log.error(" Remote file " + remote + " not overwritten."); } else { try { put(local.getAbsolutePath(), remote); } catch (final IOException e) { - errPrintln("Error: transmitting file " + local.toString() + " failed."); + log.error("Error: transmitting file " + local.toString() + " failed."); } } return true; @@ -1671,16 +1599,16 @@ public class FTPClient { public boolean PWD() { if (cmd.length > 1) { - errPrintln("Syntax: PWD (no parameter)"); + log.error("Syntax: PWD (no parameter)"); return true; } if (notConnected()) { return LPWD(); } try { - outPrintln("---- Current remote path is: " + pwd()); + log.info("---- Current remote path is: " + pwd()); } catch (final IOException e) { - errPrintln("Error: remote path not available"); + log.error("Error: remote path not available"); } return true; } @@ -1701,20 +1629,20 @@ public class FTPClient { public boolean REMOTEHELP() { if (cmd.length != 1) { - errPrintln("Syntax: REMOTEHELP (no parameter)"); + log.error("Syntax: REMOTEHELP (no parameter)"); return true; } try { literal("HELP"); } catch (final IOException e) { - errPrintln("Error: remote help not supported by server."); + log.error("Error: remote help not supported by server."); } return true; } public boolean RMDIR() { if (cmd.length != 2) { - errPrintln("Syntax: RMDIR "); + log.error("Syntax: RMDIR "); return true; } if (notConnected()) { @@ -1723,7 +1651,7 @@ public class FTPClient { try { rmForced(cmd[1]); } catch (final IOException e) { - errPrintln("Error: deletion of folder " + cmd[1] + " failed."); + log.error("Error: deletion of folder " + cmd[1] + " failed."); } return true; } @@ -1788,180 +1716,180 @@ public class FTPClient { public boolean USER() { if (cmd.length != 3) { - errPrintln("Syntax: USER "); + log.error("Syntax: USER "); return true; } try { login(cmd[1], cmd[2]); - outPrintln("---- Granted access for user " + cmd[1] + "."); + log.info("---- Granted access for user " + cmd[1] + "."); } catch (final IOException e) { - errPrintln("Error: authorization of user " + cmd[1] + " failed: " + e.getMessage()); + log.error("Error: authorization of user " + cmd[1] + " failed: " + e.getMessage()); } return true; } public boolean APPEND() { - errPrintln("not yet supported"); + log.error("not yet supported"); return true; } public boolean HELP() { - outPrintln("---- ftp HELP ----"); - outPrintln(""); - outPrintln("This ftp client shell can act as command shell for the local host as well for the"); - outPrintln("remote host. Commands that point to the local host are preceded by 'L'."); - outPrintln(""); - outPrintln("Supported Commands:"); - outPrintln("ASCII"); - outPrintln(" switch remote server to ASCII transfer mode"); - outPrintln("BINARY"); - outPrintln(" switch remote server to BINARY transfer mode"); - outPrintln("BYE"); - outPrintln(" quit the command shell (same as EXIT)"); - outPrintln("CD "); - outPrintln(" change remote path"); - outPrintln("CLOSE"); - outPrintln(" close connection to remote host (same as DISCONNECT)"); - outPrintln("DEL "); - outPrintln(" delete file on remote server (same as RM)"); - outPrintln("RM "); - outPrintln(" remove file from remote server (same as DEL)"); - outPrintln("DIR [|] "); - outPrintln(" print file information for remote directory or file"); - outPrintln("DISCONNECT"); - outPrintln(" disconnect from remote server (same as CLOSE)"); - outPrintln("EXIT"); - outPrintln(" quit the command shell (same as BYE)"); - outPrintln("GET []"); - outPrintln(" load from remote server and store it locally,"); - outPrintln(" optionally to . if the is a directory,"); - outPrintln(" then all files in that directory are retrieved,"); - outPrintln(" including recursively all subdirectories."); - outPrintln("GLOB"); - outPrintln(" toggles globbing: matching with wild cards or not"); - outPrintln("COPY"); - outPrintln(" copies local files"); - outPrintln("LCD "); - outPrintln(" local directory change"); - outPrintln("LDEL "); - outPrintln(" local file delete"); - outPrintln("LDIR"); - outPrintln(" shows local directory content"); - outPrintln("LITERAL []"); - outPrintln(" Sends FTP commands as documented in RFC959"); - outPrintln("LLS"); - outPrintln(" as LDIR"); - outPrintln("LMD"); - outPrintln(" as LMKDIR"); - outPrintln("LMV "); - outPrintln(" copies local files"); - outPrintln("LPWD"); - outPrintln(" prints local path"); - outPrintln("LRD"); - outPrintln(" as LMKDIR"); - outPrintln("LRMD "); - outPrintln(" deletes local directory "); - outPrintln("LRM "); - outPrintln(" deletes local file "); - outPrintln("LS [|]"); - outPrintln(" prints list of remote directory or information of file "); - outPrintln("MDIR"); - outPrintln(" as MKDIR"); - outPrintln("MGET "); - outPrintln(" copies files from remote server that fits into the"); - outPrintln(" pattern to the local path."); - outPrintln("MOVEDOWN "); - outPrintln(" copies files from remote server as with MGET"); - outPrintln(" and deletes them afterwards on the remote server"); - outPrintln("MV "); - outPrintln(" moves or renames files on the local host"); - outPrintln("NOOP"); - outPrintln(" sends the NOOP command to the remote server (which does nothing)"); - outPrintln(" This command is usually used to measure the speed of the remote server."); - outPrintln("OPEN []"); - outPrintln(" connects the ftp shell to the remote server . Optionally,"); - outPrintln(" a port number can be given, the default port number is 21."); - outPrintln(" Example: OPEN localhost:2121 or OPEN 192.168.0.1 2121"); - outPrintln("PROMPT"); - outPrintln(" compatibility command, that usually toggles beween prompting on or off."); - outPrintln(" ftp has prompting switched off by default and cannot switched on."); - outPrintln("PUT []"); - outPrintln(" copies the to the remote server to the current remote path or"); - outPrintln(" optionally to the given path."); - outPrintln("PWD"); - outPrintln(" prints current path on the remote server."); - outPrintln("REMOTEHELP"); - outPrintln(" asks the remote server to print the help text of the remote server"); - outPrintln("RMDIR "); - outPrintln(" removes the directory on the remote server"); - outPrintln("QUIT"); - outPrintln(" exits the ftp application"); - outPrintln("RECV"); - outPrintln(" as GET"); - outPrintln("USER "); - outPrintln(" logs into the remote server with the user "); - outPrintln(" and the password "); - outPrintln(""); - outPrintln(""); - outPrintln("EXAMPLE:"); - outPrintln("a standard sessions looks like this"); - outPrintln(">open 192.168.0.1:2121"); - outPrintln(">user anonymous bob"); - outPrintln(">pwd"); - outPrintln(">ls"); - outPrintln(">....."); - outPrintln(""); - outPrintln(""); + log.info("---- ftp HELP ----"); + log.info(""); + log.info("This ftp client shell can act as command shell for the local host as well for the"); + log.info("remote host. Commands that point to the local host are preceded by 'L'."); + log.info(""); + log.info("Supported Commands:"); + log.info("ASCII"); + log.info(" switch remote server to ASCII transfer mode"); + log.info("BINARY"); + log.info(" switch remote server to BINARY transfer mode"); + log.info("BYE"); + log.info(" quit the command shell (same as EXIT)"); + log.info("CD "); + log.info(" change remote path"); + log.info("CLOSE"); + log.info(" close connection to remote host (same as DISCONNECT)"); + log.info("DEL "); + log.info(" delete file on remote server (same as RM)"); + log.info("RM "); + log.info(" remove file from remote server (same as DEL)"); + log.info("DIR [|] "); + log.info(" print file information for remote directory or file"); + log.info("DISCONNECT"); + log.info(" disconnect from remote server (same as CLOSE)"); + log.info("EXIT"); + log.info(" quit the command shell (same as BYE)"); + log.info("GET []"); + log.info(" load from remote server and store it locally,"); + log.info(" optionally to . if the is a directory,"); + log.info(" then all files in that directory are retrieved,"); + log.info(" including recursively all subdirectories."); + log.info("GLOB"); + log.info(" toggles globbing: matching with wild cards or not"); + log.info("COPY"); + log.info(" copies local files"); + log.info("LCD "); + log.info(" local directory change"); + log.info("LDEL "); + log.info(" local file delete"); + log.info("LDIR"); + log.info(" shows local directory content"); + log.info("LITERAL []"); + log.info(" Sends FTP commands as documented in RFC959"); + log.info("LLS"); + log.info(" as LDIR"); + log.info("LMD"); + log.info(" as LMKDIR"); + log.info("LMV "); + log.info(" copies local files"); + log.info("LPWD"); + log.info(" prints local path"); + log.info("LRD"); + log.info(" as LMKDIR"); + log.info("LRMD "); + log.info(" deletes local directory "); + log.info("LRM "); + log.info(" deletes local file "); + log.info("LS [|]"); + log.info(" prints list of remote directory or information of file "); + log.info("MDIR"); + log.info(" as MKDIR"); + log.info("MGET "); + log.info(" copies files from remote server that fits into the"); + log.info(" pattern to the local path."); + log.info("MOVEDOWN "); + log.info(" copies files from remote server as with MGET"); + log.info(" and deletes them afterwards on the remote server"); + log.info("MV "); + log.info(" moves or renames files on the local host"); + log.info("NOOP"); + log.info(" sends the NOOP command to the remote server (which does nothing)"); + log.info(" This command is usually used to measure the speed of the remote server."); + log.info("OPEN []"); + log.info(" connects the ftp shell to the remote server . Optionally,"); + log.info(" a port number can be given, the default port number is 21."); + log.info(" Example: OPEN localhost:2121 or OPEN 192.168.0.1 2121"); + log.info("PROMPT"); + log.info(" compatibility command, that usually toggles beween prompting on or off."); + log.info(" ftp has prompting switched off by default and cannot switched on."); + log.info("PUT []"); + log.info(" copies the to the remote server to the current remote path or"); + log.info(" optionally to the given path."); + log.info("PWD"); + log.info(" prints current path on the remote server."); + log.info("REMOTEHELP"); + log.info(" asks the remote server to print the help text of the remote server"); + log.info("RMDIR "); + log.info(" removes the directory on the remote server"); + log.info("QUIT"); + log.info(" exits the ftp application"); + log.info("RECV"); + log.info(" as GET"); + log.info("USER "); + log.info(" logs into the remote server with the user "); + log.info(" and the password "); + log.info(""); + log.info(""); + log.info("EXAMPLE:"); + log.info("a standard sessions looks like this"); + log.info(">open 192.168.0.1:2121"); + log.info(">user anonymous bob"); + log.info(">pwd"); + log.info(">ls"); + log.info(">....."); + log.info(""); + log.info(""); return true; } public boolean QUOTE() { - errPrintln("not yet supported"); + log.error("not yet supported"); return true; } public boolean BELL() { - errPrintln("not yet supported"); + log.error("not yet supported"); return true; } public boolean MDELETE() { - errPrintln("not yet supported"); + log.error("not yet supported"); return true; } public boolean SEND() { - errPrintln("not yet supported"); + log.error("not yet supported"); return true; } public boolean DEBUG() { - errPrintln("not yet supported"); + log.error("not yet supported"); return true; } public boolean MLS() { - errPrintln("not yet supported"); + log.error("not yet supported"); return true; } public boolean TRACE() { - errPrintln("not yet supported"); + log.error("not yet supported"); return true; } public boolean MPUT() { - errPrintln("not yet supported"); + log.error("not yet supported"); return true; } public boolean TYPE() { - errPrintln("not yet supported"); + log.error("not yet supported"); return true; } public boolean CREATE() { - errPrintln("not yet supported"); + log.error("not yet supported"); return true; } @@ -1971,7 +1899,7 @@ public class FTPClient { // checks whether the string name matches with the pattern // the pattern may contain characters '*' as wildcard for several // characters (also none) and '?' to match exactly one characters - // outPrintln("MATCH " + name + " " + pattern); + // log.info("MATCH " + name + " " + pattern); if (!glob) { return name.equals(pattern); } @@ -2022,9 +1950,9 @@ public class FTPClient { clientOutput.write('\n'); clientOutput.flush(); if (buf.startsWith("PASS")) { - outPrintln("> PASS ********"); + log.info("> PASS ********"); } else { - outPrintln("> " + buf); + log.info("> " + buf); } } @@ -2040,7 +1968,7 @@ public class FTPClient { throw new IOException("Server has presumably shut down the connection."); } - outPrintln("< " + reply); + log.info("< " + reply); // serverResponse.addElement(reply); if (reply.length() >= 4 && Character.isDigit(reply.charAt(0)) && Character.isDigit(reply.charAt(1)) @@ -2301,16 +2229,15 @@ public class FTPClient { // write statistics final long stop = System.currentTimeMillis(); - outPrintln(logPrefix - + " ---- downloaded " + log.info(" ---- downloaded " + ((length < 2048) ? length + " bytes" : (length / 1024) + " kbytes") + " in " + (((stop - start) < 2000) ? (stop - start) + " milliseconds" : (((int) ((stop - start) / 100)) / 10) + " seconds")); if (start == stop) { - errPrintln(logPrefix); + log.error("start == stop"); } else { - outPrintln(" (" + (length * 1000 / 1024 / (stop - start)) + " kbytes/second)"); + log.info(" (" + (length * 1000 / 1024 / (stop - start)) + " kbytes/second)"); } } else { @@ -2550,7 +2477,7 @@ public class FTPClient { try { applyDataSocketTimeout(); } catch (final SocketException e) { - errPrintln(logPrefix + " setDataSocketTimeout: " + e.getMessage()); + log.error("setDataSocketTimeout: " + e.getMessage()); } } @@ -2616,7 +2543,7 @@ public class FTPClient { final String account, final String password) { // opens a new connection and returns a directory listing as html try { - final FTPClient c = new FTPClient(System.in, null, System.err); + final FTPClient c = new FTPClient(); c.open(host, port); c.login(account, password); c.sys(); @@ -2700,9 +2627,9 @@ public class FTPClient { fos.write(page.toString().getBytes()); fos.close(); } catch (final FileNotFoundException e) { - Log.logException(e); + log.error(e); } catch (final IOException e) { - Log.logException(e); + log.error(e); } } @@ -2716,7 +2643,7 @@ public class FTPClient { final ByteArrayOutputStream berr = new ByteArrayOutputStream(); final PrintStream err = new PrintStream(berr); - final FTPClient c = new FTPClient(System.in, out, err); + final FTPClient c = new FTPClient(); c.exec("open " + host, false); c.exec("user " + account + " " + password, false); if (remotePath != null) { @@ -2797,7 +2724,7 @@ public class FTPClient { try { put(host, localFile, remotePath, remoteName, account, password); } catch (final IOException e) { - Log.logException(e); + log.error(e, e); } } } @@ -2811,33 +2738,27 @@ public class FTPClient { } private static void printHelp() { - System.out.println(logPrefix + "ftp help"); - System.out.println(logPrefix + "----------"); - System.out.println(logPrefix); - System.out.println(logPrefix + "The following commands are supported"); - System.out.println(logPrefix - + "java ftp -- (without arguments) starts the shell. Thy 'help' then for shell commands."); - System.out.println(logPrefix + "java ftp [':'] -- starts shell and connects to specified host"); - System.out.println(logPrefix + "java ftp -h -- prints this help"); - System.out.println(logPrefix + "java ftp -dir [':'] [ ]"); - System.out.println(logPrefix - + "java ftp -get [':'] [ ]"); - System.out.println(logPrefix + "java ftp -put [':'] "); - System.out.println(logPrefix); + System.out.println("ftp help"); + System.out.println("----------"); + System.out.println(); + System.out.println("The following commands are supported"); + System.out.println("java ftp -- (without arguments) starts the shell. Thy 'help' then for shell commands."); + System.out.println("java ftp [':'] -- starts shell and connects to specified host"); + System.out.println("java ftp -h -- prints this help"); + System.out.println("java ftp -dir [':'] [ ]"); + System.out.println("java ftp -get [':'] [ ]"); + System.out.println("java ftp -put [':'] "); + System.out.println(); } public static void main(final String[] args) { - System.out.println(logPrefix + "WELCOME TO THE ANOMIC FTP CLIENT v" + vDATE); - System.out.println(logPrefix + "Visit http://www.anomic.de and support shareware!"); - System.out.println(logPrefix + "try -h for command line options"); - System.out.println(logPrefix); - if (args.length == 0) { - (new FTPClient()).shell(null); - } else if (args.length == 1) { + System.out.println("WELCOME TO THE ANOMIC FTP CLIENT v" + vDATE); + System.out.println("Visit http://www.anomic.de and support shareware!"); + System.out.println("try -h for command line options"); + System.out.println(); + if (args.length == 1) { if (args[0].equals("-h")) { printHelp(); - } else { - (new FTPClient()).shell(args[0]); } } else if (args.length == 2) { printHelp(); @@ -2869,7 +2790,7 @@ public class FTPClient { put(args[1], new File(args[2]), args[3], "", args[4], args[5]); } catch (final IOException e) { // TODO Auto-generated catch block - Log.logException(e); + log.error(e, e); } } else { printHelp();