- fixed POST in proxy

- prepared http connection tracking
- refactoring (mainly moving StreamTools to serverFileUtils)


git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4668 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
danielr 17 years ago
parent 14404d31a8
commit d96e2badc7

@ -36,7 +36,7 @@
</table> </table>
<h3>Outgoing Connections</h3> <h3>Outgoing Connections</h3>
<p><strong>Currently not available!</strong></p> <p><strong>Details currently not available!</strong></p>
<p>Showing #[clientActive]# active outgoing connections:</p> <p>Showing #[clientActive]# active outgoing connections:</p>
<table border="0" cellpadding="2" cellspacing="1"> <table border="0" cellpadding="2" cellspacing="1">
<tr class="TableHeader" valign="bottom"> <tr class="TableHeader" valign="bottom">

@ -51,6 +51,8 @@ import java.net.InetAddress;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Properties; import java.util.Properties;
import de.anomic.http.HttpConnectionInfo;
import de.anomic.http.JakartaCommonsHttpClient;
import de.anomic.http.httpHeader; import de.anomic.http.httpHeader;
import de.anomic.http.httpd; import de.anomic.http.httpd;
import de.anomic.plasma.plasmaSwitchboard; import de.anomic.plasma.plasmaSwitchboard;
@ -227,24 +229,21 @@ public final class Connections_p {
prop.putNum("numActivePending", numActivePending); prop.putNum("numActivePending", numActivePending);
// client sessions // client sessions
// FIXME track connections of JakartaCommons Connection Manager HttpConnectionInfo[] a = JakartaCommonsHttpClient.allConnections();
// httpc[] a = httpc.allConnections(); // TODO sorting
// Arrays.sort(a, httpc.connectionTimeComparatorInstance); // Arrays.sort(a, httpc.connectionTimeComparatorInstance);
int c = 0; int c = 0;
// for (int i = 0; i < a.length; i++) { for (HttpConnectionInfo conInfo: a) {
// httpc clientConnection = a[i]; prop.put("clientList_" + c + "_clientProtocol", conInfo.getProtocol());
// if (clientConnection != null) { prop.putNum("clientList_" + c + "_clientLifetime", conInfo.getLifetime());
// prop.put("clientList_" + c + "_clientProtocol", (clientConnection.ssl) ? "HTTPS" : "HTTP"); prop.putNum("clientList_" + c + "_clientIdletime", conInfo.getIdletime());
// prop.putNum("clientList_" + c + "_clientLifetime", System.currentTimeMillis() - clientConnection.initTime); prop.put("clientList_" + c + "_clientTargetHost", conInfo.getTargetHost());
// prop.putNum("clientList_" + c + "_clientIdletime", System.currentTimeMillis() - clientConnection.lastIO); prop.putHTML("clientList_" + c + "_clientCommand", conInfo.getCommand());
// prop.put("clientList_" + c + "_clientTargetHost", clientConnection.adressed_host + ":" + clientConnection.adressed_port); prop.put("clientList_" + c + "_clientID", conInfo.getID());
// prop.putHTML("clientList_" + c + "_clientCommand", (clientConnection.command == null) ? "-" : clientConnection.command); c++;
// prop.put("clientList_" + c + "_clientID", clientConnection.hashCode()); }
// c++;
// }
// }
prop.put("clientList", c); prop.put("clientList", c);
prop.put("clientActive", c); prop.put("clientActive", JakartaCommonsHttpClient.connectionCount());
// return rewrite values for templates // return rewrite values for templates
return prop; return prop;

@ -314,7 +314,7 @@ public final class htmlFilterWriter extends Writer {
} }
public void write(int c) throws IOException { public void write(int c) throws IOException {
// System.out.println((char) b); // System.out.println((char) c);
if ((binaryUnsuspect) && (binaryHint((char)c))) { if ((binaryUnsuspect) && (binaryHint((char)c))) {
binaryUnsuspect = false; binaryUnsuspect = false;
if (passbyIfBinarySuspect) finalize(); if (passbyIfBinarySuspect) finalize();
@ -489,10 +489,10 @@ public final class htmlFilterWriter extends Writer {
// if (transformer != null) {transformer.close(); transformer = null;} // if (transformer != null) {transformer.close(); transformer = null;}
} }
private static boolean binaryHint(char c) { private static boolean binaryHint(final char c) {
// space, punctiation and symbols, letters and digits (ASCII/latin) // space, punctiation and symbols, letters and digits (ASCII/latin)
//if (c >= 31 && c < 128) return false; //if (c >= 31 && c < 128) return false;
if(c >= 31) return false; if(c > 31) return false;
// 8 = backspace // 8 = backspace
// 9 = horizontal tab // 9 = horizontal tab
// 10 = new line (line feed) // 10 = new line (line feed)

@ -0,0 +1,114 @@
// HttpConnectionInfo.java
// (C) 2008 by Daniel Raap; danielr@users.berlios.de
// first published 07.04.2008 on http://yacy.net
//
// This is a part of YaCy, a peer-to-peer based web search engine
//
// $LastChangedDate: 2008-03-14 01:16:04 +0100 (Fr, 14 Mrz 2008) $
// $LastChangedRevision: 4558 $
// $LastChangedBy: orbiter $
//
// LICENSE
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package de.anomic.http;
import org.apache.commons.httpclient.HttpConnection;
/**
* Information about a connection
*
* @author daniel
* @since 07.04.2008
*/
public class HttpConnectionInfo {
private final String protocol;
private final String targetHost;
private final String command;
private final int id;
private final long initTime;
/**
* constructor using org.apache.commons.httpclient.HttpConnection
*
* @param connection
*/
public HttpConnectionInfo(final HttpConnection connection) {
this(connection.getProtocol().toString(), ((connection.getPort() == 80) ? connection.getHost()
: connection.getHost() + ":" + connection.getPort()), "unknown command", connection.hashCode(),
System.currentTimeMillis());
}
/**
* constructor setting all data
*
* @param protocol
* @param targetHost
* @param command
* @param id
* @param initTime
*/
public HttpConnectionInfo(final String protocol, final String targetHost, final String command, final int id,
final long initTime) {
this.protocol = protocol;
this.targetHost = targetHost;
this.command = command;
this.id = id;
this.initTime = initTime;
}
/**
* @return
*/
public String getProtocol() {
return protocol;
}
/**
* @return
*/
public long getLifetime() {
return System.currentTimeMillis() - initTime;
}
/**
* @return
*/
public int getIdletime() {
return 0;
}
/**
* @return
*/
public String getCommand() {
return command;
}
/**
* @return
*/
public String getTargetHost() {
return targetHost;
}
/**
* @return
*/
public int getID() {
return id;
}
}

@ -32,7 +32,7 @@ import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.io.Writer; import java.io.Writer;
import de.anomic.tools.StreamTools; import de.anomic.server.serverFileUtils;
/** /**
* @author daniel * @author daniel
@ -108,12 +108,12 @@ public interface HttpResponse {
if (hfos instanceof OutputStream) { if (hfos instanceof OutputStream) {
OutputStream[] streams = (byteStream == null ? new OutputStream[] { (OutputStream) hfos } OutputStream[] streams = (byteStream == null ? new OutputStream[] { (OutputStream) hfos }
: new OutputStream[] { (OutputStream) hfos, byteStream }); : new OutputStream[] { (OutputStream) hfos, byteStream });
StreamTools.copyToStreams(data, streams); serverFileUtils.copyToStreams(data, streams);
} else if (hfos instanceof Saver) { } else if (hfos instanceof Saver) {
String charSet = httpHeader.getCharSet(res.getResponseHeader()); String charSet = httpHeader.getCharSet(res.getResponseHeader());
Writer[] writers = (byteStream == null ? new Writer[] { (Writer) hfos } : new Writer[] { (Writer) hfos, Writer[] writers = (byteStream == null ? new Writer[] { (Writer) hfos } : new Writer[] { (Writer) hfos,
new OutputStreamWriter(byteStream, charSet) }); new OutputStreamWriter(byteStream, charSet) });
StreamTools.copyToWriters(data, writers, charSet); serverFileUtils.copyToWriters(data, writers, charSet);
} else { } else {
throw new IOException("cannot save data: hfos-type ("+ hfos.getClass().toString() +") not supported!"); throw new IOException("cannot save data: hfos-type ("+ hfos.getClass().toString() +") not supported!");
} }

@ -54,11 +54,12 @@ import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpClientParams; import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.commons.httpclient.util.DateUtil; import org.apache.commons.httpclient.util.DateUtil;
import de.anomic.kelondro.kelondroBase64Order; import de.anomic.kelondro.kelondroBase64Order;
import de.anomic.server.serverFileUtils;
import de.anomic.server.logging.serverLog; import de.anomic.server.logging.serverLog;
import de.anomic.tools.StreamTools;
import de.anomic.yacy.yacyVersion; import de.anomic.yacy.yacyVersion;
/** /**
@ -90,7 +91,7 @@ public class JakartaCommonsHttpClient extends de.anomic.http.HttpClient {
// TODO should this be configurable? // TODO should this be configurable?
// accept self-signed or untrusted certificates // accept self-signed or untrusted certificates
Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443)); Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory)new EasySSLProtocolSocketFactory(), 443));
} }
private final Map<HttpMethod, InputStream> openStreams = new HashMap<HttpMethod, InputStream>(); private final Map<HttpMethod, InputStream> openStreams = new HashMap<HttpMethod, InputStream>();
@ -227,7 +228,7 @@ public class JakartaCommonsHttpClient extends de.anomic.http.HttpClient {
if (file.isFile() && file.canRead()) { if (file.isFile() && file.canRead()) {
// read file // read file
final ByteArrayOutputStream fileData = new ByteArrayOutputStream(); final ByteArrayOutputStream fileData = new ByteArrayOutputStream();
StreamTools.copyToStreams(new FileInputStream(file), new OutputStream[] { fileData }); serverFileUtils.copyToStreams(new FileInputStream(file), new OutputStream[] { fileData });
value = fileData.toByteArray(); value = fileData.toByteArray();
} }
} }
@ -506,4 +507,22 @@ public class JakartaCommonsHttpClient extends de.anomic.http.HttpClient {
public static String getCurrentUserAgent() { public static String getCurrentUserAgent() {
return (String) apacheHttpClient.getParams().getParameter(HttpClientParams.USER_AGENT); return (String) apacheHttpClient.getParams().getParameter(HttpClientParams.USER_AGENT);
} }
/**
* a list of all connections (not yet implemented)
*
* @return
*/
public static HttpConnectionInfo[] allConnections() {
return new HttpConnectionInfo[0];
}
/**
* number of active connections
*
* @return
*/
public static int connectionCount() {
return conManager.getConnectionsInPool();
}
} }

@ -952,7 +952,7 @@ public final class httpHeader extends TreeMap<String, String> implements Map<Str
* @param header * @param header
* @return * @return
*/ */
static String getCharSet(httpHeader header) { static String getCharSet(final httpHeader header) {
String charSet = header.getCharacterEncoding(); String charSet = header.getCharacterEncoding();
if (charSet == null) { if (charSet == null) {
charSet = DEFAULT_CHARSET; charSet = DEFAULT_CHARSET;

@ -63,6 +63,7 @@
package de.anomic.http; package de.anomic.http;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -71,7 +72,6 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.PushbackInputStream;
import java.io.Writer; import java.io.Writer;
import java.net.BindException; import java.net.BindException;
import java.net.ConnectException; import java.net.ConnectException;
@ -514,13 +514,13 @@ public final class httpdProxyHandler {
prepareRequestHeader(requestHeader, httpVer); prepareRequestHeader(requestHeader, httpVer);
// generate request-url
final String connectHost = (yAddress == null) ? host +":"+ port : yAddress;
final String getUrl = "http://"+ connectHost + remotePath;
// setup HTTP-client // setup HTTP-client
final HttpClient client = HttpFactory.newClient(requestHeader, timeout); final HttpClient client = HttpFactory.newClient(requestHeader, timeout);
final String connectHost = hostPart(host, port, yAddress);
final String getUrl = "http://"+ connectHost + remotePath;
client.setProxy(getProxyConfig(connectHost));
// send request // send request
try { try {
res = client.GET(getUrl); res = client.GET(getUrl);
@ -585,7 +585,7 @@ public final class httpdProxyHandler {
// make a transformer // make a transformer
theLogger.logFine("create transformer for URL " + url); theLogger.logFine("create transformer for URL " + url);
//hfos = new htmlFilterOutputStream((gzippedOut != null) ? gzippedOut : ((chunkedOut != null)? chunkedOut : respond), null, transformer, (ext.length() == 0)); //hfos = new htmlFilterOutputStream((gzippedOut != null) ? gzippedOut : ((chunkedOut != null)? chunkedOut : respond), null, transformer, (ext.length() == 0));
String charSet = httpHeader.getCharSet(responseHeader); final String charSet = httpHeader.getCharSet(responseHeader);
hfos = new htmlFilterWriter((gzippedOut != null) ? gzippedOut : ((chunkedOut != null)? chunkedOut : respond),charSet, null, transformer, (ext.length() == 0)); hfos = new htmlFilterWriter((gzippedOut != null) ? gzippedOut : ((chunkedOut != null)? chunkedOut : respond),charSet, null, transformer, (ext.length() == 0));
} else { } else {
// simply pass through without parsing // simply pass through without parsing
@ -931,19 +931,15 @@ public final class httpdProxyHandler {
prepareRequestHeader(requestHeader, httpVer); prepareRequestHeader(requestHeader, httpVer);
// setup HTTP-client // setup HTTP-client
HttpClient client = HttpFactory.newClient(); HttpClient client = HttpFactory.newClient(requestHeader, timeout);
client.setTimeout(timeout);
// set header for connection
client.setHeader(requestHeader);
// send request // generate request-url
final String connectHost = (yAddress == null) ? host +":"+ port : yAddress; final String connectHost = hostPart(host, port, yAddress);
client.setProxy(getProxyConfig(connectHost));
final String getUrl = "http://"+ connectHost + remotePath; final String getUrl = "http://"+ connectHost + remotePath;
client.setProxy(getProxyConfig(connectHost));
// send request
try { try {
// sending the http-HEAD request to the server
res = client.HEAD(getUrl); res = client.HEAD(getUrl);
// determine if it's an internal error of the httpc // determine if it's an internal error of the httpc
@ -967,8 +963,22 @@ public final class httpdProxyHandler {
handleProxyException(e,conProp,respond,url); handleProxyException(e,conProp,respond,url);
} }
} }
/**
* @param host
* @param port
* @param yAddress
* @return
*/
private static String hostPart(String host, int port, String yAddress) {
final String connectHost = (yAddress == null) ? host +":"+ port : yAddress;
return connectHost;
}
public static void doPost(Properties conProp, httpHeader requestHeader, OutputStream respond, PushbackInputStream body) throws IOException { public static void doPost(Properties conProp, httpHeader requestHeader, OutputStream respond, InputStream body) throws IOException {
assert conProp != null : "precondition violated: conProp != null";
assert requestHeader != null : "precondition violated: requestHeader != null";
assert body != null : "precondition violated: body != null";
yacyURL url = null; yacyURL url = null;
try { try {
// remembering the starting time of the request // remembering the starting time of the request
@ -1029,14 +1039,27 @@ public final class httpdProxyHandler {
// set header for connection // set header for connection
client.setHeader(requestHeader); client.setHeader(requestHeader);
// send request final String connectHost = hostPart(host, port, yAddress);
final String connectHost = (yAddress == null) ? host +":"+ port : yAddress;
client.setProxy(getProxyConfig(connectHost)); client.setProxy(getProxyConfig(connectHost));
final String getUrl = "http://"+ connectHost + remotePath; final String getUrl = "http://"+ connectHost + remotePath;
// check input
if(body == null) {
theLogger.logSevere("no body to POST!");
}
// from old httpc:
// "if there is a body to the call, we would have a CONTENT-LENGTH tag in the requestHeader"
// it seems that it is a HTTP/1.1 connection which stays open (the inputStream) and endlessly waits for
// input so we have to end it to do the request
final long requestLength = requestHeader.contentLength();
if(requestLength > -1) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
serverFileUtils.copy(body, buffer, requestLength);
body = new ByteArrayInputStream(buffer.toByteArray());
}
HttpResponse res = null; HttpResponse res = null;
try { try {
// sending the request // sending the request
// TODO is the input stream required and valid HTTP-data?
res = client.POST(getUrl, body); res = client.POST(getUrl, body);
// determine if it's an internal error of the httpc // determine if it's an internal error of the httpc
@ -1133,7 +1156,7 @@ public final class httpdProxyHandler {
private static void addXForwardedForHeader(Properties conProp, httpHeader requestHeader) { private static void addXForwardedForHeader(Properties conProp, httpHeader requestHeader) {
// setting the X-Forwarded-For Header // setting the X-Forwarded-For Header
if (switchboard.getConfigBool("proxy.sendXForwardedForHeader", true)) { if (switchboard.getConfigBool("proxy.sendXForwardedForHeader", true)) {
requestHeader.put(httpHeader.X_FORWARDED_FOR,conProp.getProperty(httpHeader.CONNECTION_PROP_CLIENTIP)); requestHeader.put(httpHeader.X_FORWARDED_FOR, conProp.getProperty(httpHeader.CONNECTION_PROP_CLIENTIP));
} }
} }

@ -40,8 +40,10 @@
package de.anomic.server; package de.anomic.server;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -79,17 +81,19 @@ public final class serverFileUtils {
} }
/** /**
* Copies an InputStream to an OutputStream. * Copies an InputStream to an OutputStream.
* @param source InputStream *
* @param dest OutputStream * @param source InputStream
* @param count the total amount of bytes to copy * @param dest OutputStream
* @return Total number of bytes copied. * @param count the total amount of bytes to copy
* * @return Total number of bytes copied.
* @see #copy(InputStream source, File dest) * @throws IOException
* @see #copyRange(File source, OutputStream dest, int start) *
* @see #copy(File source, OutputStream dest) * @see #copy(InputStream source, File dest)
* @see #copy(File source, File dest) * @see #copyRange(File source, OutputStream dest, int start)
*/ * @see #copy(File source, OutputStream dest)
* @see #copy(File source, File dest)
*/
public static long copy(InputStream source, OutputStream dest, long count) throws IOException { public static long copy(InputStream source, OutputStream dest, long count) throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int chunkSize = (int) ((count > 0) ? Math.min(count, DEFAULT_BUFFER_SIZE) : DEFAULT_BUFFER_SIZE); int chunkSize = (int) ((count > 0) ? Math.min(count, DEFAULT_BUFFER_SIZE) : DEFAULT_BUFFER_SIZE);
@ -153,15 +157,17 @@ public final class serverFileUtils {
} }
/** /**
* Copies an InputStream to a File. * Copies an InputStream to a File.
* @param source InputStream *
* @param dest File * @param source InputStream
* @param the amount of bytes to copy * @param dest File
* @see #copy(InputStream source, OutputStream dest) * @param count the amount of bytes to copy
* @see #copyRange(File source, OutputStream dest, int start) * @throws IOException
* @see #copy(File source, OutputStream dest) * @see #copy(InputStream source, OutputStream dest)
* @see #copy(File source, File dest) * @see #copyRange(File source, OutputStream dest, int start)
*/ * @see #copy(File source, OutputStream dest)
* @see #copy(File source, File dest)
*/
public static void copy(InputStream source, File dest, long count) throws IOException { public static void copy(InputStream source, File dest, long count) throws IOException {
FileOutputStream fos = null; FileOutputStream fos = null;
try { try {
@ -173,15 +179,16 @@ public final class serverFileUtils {
} }
/** /**
* Copies a part of a File to an OutputStream. * Copies a part of a File to an OutputStream.
* @param source File * @param source File
* @param dest OutputStream * @param dest OutputStream
* @param start Number of bytes to skip from the beginning of the File * @param start Number of bytes to skip from the beginning of the File
* @see #copy(InputStream source, OutputStream dest) * @throws IOException
* @see #copy(InputStream source, File dest) * @see #copy(InputStream source, OutputStream dest)
* @see #copy(File source, OutputStream dest) * @see #copy(InputStream source, File dest)
* @see #copy(File source, File dest) * @see #copy(File source, OutputStream dest)
*/ * @see #copy(File source, File dest)
*/
public static void copyRange(File source, OutputStream dest, int start) throws IOException { public static void copyRange(File source, OutputStream dest, int start) throws IOException {
InputStream fis = null; InputStream fis = null;
try { try {
@ -195,14 +202,15 @@ public final class serverFileUtils {
} }
/** /**
* Copies a File to an OutputStream. * Copies a File to an OutputStream.
* @param source File * @param source File
* @param dest OutputStream * @param dest OutputStream
* @see #copy(InputStream source, OutputStream dest) * @throws IOException
* @see #copy(InputStream source, File dest) * @see #copy(InputStream source, OutputStream dest)
* @see #copyRange(File source, OutputStream dest, int start) * @see #copy(InputStream source, File dest)
* @see #copy(File source, File dest) * @see #copyRange(File source, OutputStream dest, int start)
*/ * @see #copy(File source, File dest)
*/
public static void copy(File source, OutputStream dest) throws IOException { public static void copy(File source, OutputStream dest) throws IOException {
InputStream fis = null; InputStream fis = null;
try { try {
@ -214,15 +222,16 @@ public final class serverFileUtils {
} }
/** /**
* Copies a File to a File. * Copies a File to a File.
* @param source File * @param source File
* @param dest File * @param dest File
* @param count the amount of bytes to copy * @param count the amount of bytes to copy
* @see #copy(InputStream source, OutputStream dest) * @throws IOException
* @see #copy(InputStream source, File dest) * @see #copy(InputStream source, OutputStream dest)
* @see #copyRange(File source, OutputStream dest, int start) * @see #copy(InputStream source, File dest)
* @see #copy(File source, OutputStream dest) * @see #copyRange(File source, OutputStream dest, int start)
*/ * @see #copy(File source, OutputStream dest)
*/
public static void copy(File source, File dest) throws IOException { public static void copy(File source, File dest) throws IOException {
FileInputStream fis = null; FileInputStream fis = null;
FileOutputStream fos = null; FileOutputStream fos = null;
@ -500,4 +509,75 @@ public final class serverFileUtils {
e.printStackTrace(); e.printStackTrace();
} }
} }
/**
* copies the input stream to all output streams (byte per byte)
* @param in
* @param outs
* @return
* @throws IOException
*/
public static int copyToStreams(InputStream in, final OutputStream[] outs) throws IOException {
if(!(in instanceof BufferedInputStream)) {
// add buffer
in = new BufferedInputStream(in);
}
// check if buffer is used
int i = 0;
for(final OutputStream output: outs) {
if (!(output instanceof BufferedOutputStream)) {
// add buffer
outs[i] = new BufferedOutputStream(output);
}
i++;
}
int count = 0;
// copy bytes
int b;
while((b = in.read()) != -1) {
count++;
for(final OutputStream out: outs) {
out.write(b);
}
}
return count;
}
/**
* copies the input stream to all writers (byte per byte)
* @param data
* @param writers
* @param charSet
* @return
* @throws IOException
*/
public static int copyToWriters(final InputStream data, final Writer[] writers, final String charSet) throws IOException {
// the docs say: "For top efficiency, consider wrapping an InputStreamReader within a BufferedReader."
final BufferedReader sourceReader = new BufferedReader(new InputStreamReader(data, charSet));
// check if buffer is used. From the documentation:
// "For top efficiency, consider wrapping an OutputStreamWriter within a BufferedWriter so as to avoid frequent
// converter invocations"
int i = 0;
for(final Writer writer: writers) {
if (!(writer instanceof BufferedWriter)) {
// add buffer
writers[i] = new BufferedWriter(writer);
}
i++;
}
int count = 0;
// copy bytes
int b;
while((b = sourceReader.read()) != -1) {
count++;
for(final Writer writer: writers) {
writer.write(b);
}
}
return count;
}
} }

@ -1,114 +0,0 @@
// LowLevelTools.java
// (C) 2008 by Daniel Raap; danielr@users.berlios.de
// first published 2.4.2008 on http://yacy.net
//
// This is a part of YaCy, a peer-to-peer based web search engine
//
// $LastChangedDate: 2008-03-14 01:16:04 +0100 (Fr, 14 Mrz 2008) $
// $LastChangedRevision: 4558 $
// $LastChangedBy: orbiter $
//
// LICENSE
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package de.anomic.tools;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Writer;
/**
* @author danielr
*
*/
public class StreamTools {
/**
* copies the input stream to all output streams (byte per byte)
* @param in
* @param outs
* @return
* @throws IOException
*/
public static int copyToStreams(InputStream in, final OutputStream[] outs) throws IOException {
if(!(in instanceof BufferedInputStream)) {
// add buffer
in = new BufferedInputStream(in);
}
// check if buffer is used
int i = 0;
for(final OutputStream output: outs) {
if (!(output instanceof BufferedOutputStream)) {
// add buffer
outs[i] = new BufferedOutputStream(output);
}
i++;
}
int count = 0;
// copy bytes
int b;
while((b = in.read()) != -1) {
count++;
for(final OutputStream out: outs) {
out.write(b);
}
}
return count;
}
/**
* copies the input stream to all writers (byte per byte)
* @param data
* @param writers
* @param charSet
* @return
* @throws IOException
*/
public static int copyToWriters(final InputStream data, final Writer[] writers, final String charSet) throws IOException {
// the docs say: "For top efficiency, consider wrapping an InputStreamReader within a BufferedReader."
final BufferedReader sourceReader = new BufferedReader(new InputStreamReader(data, charSet));
// check if buffer is used. From the documentation:
// "For top efficiency, consider wrapping an OutputStreamWriter within a BufferedWriter so as to avoid frequent
// converter invocations"
int i = 0;
for(final Writer writer: writers) {
if (!(writer instanceof BufferedWriter)) {
// add buffer
writers[i] = new BufferedWriter(writer);
}
i++;
}
int count = 0;
// copy bytes
int b;
while((b = sourceReader.read()) != -1) {
count++;
for(final Writer writer: writers) {
writer.write(b);
}
}
return count;
}
}

@ -83,11 +83,6 @@ import de.anomic.xml.rssReader;
public final class yacyClient { public final class yacyClient {
/**
*
*/
private static final String GZIP_POST_BODY = "GZIP_POST_BODY";
public static int publishMySeed(String address, String otherHash) { public static int publishMySeed(String address, String otherHash) {
// this is called to enrich the seed information by // this is called to enrich the seed information by
// - own address (if peer is behind a nat/router) // - own address (if peer is behind a nat/router)
@ -911,7 +906,7 @@ public final class yacyClient {
// enabling gzip compression for post request body // enabling gzip compression for post request body
if ((gzipBody) && (targetSeed.getVersion() >= yacyVersion.YACY_SUPPORTS_GZIP_POST_REQUESTS)) { if ((gzipBody) && (targetSeed.getVersion() >= yacyVersion.YACY_SUPPORTS_GZIP_POST_REQUESTS)) {
post.put(GZIP_POST_BODY,"true"); // TODO generate gzip-Header (and stream?)
} }
post.put("wordc", Integer.toString(indexes.length)); post.put("wordc", Integer.toString(indexes.length));
@ -968,7 +963,7 @@ public final class yacyClient {
// enabling gzip compression for post request body // enabling gzip compression for post request body
if ((gzipBody) && (targetSeed.getVersion() >= yacyVersion.YACY_SUPPORTS_GZIP_POST_REQUESTS)) { if ((gzipBody) && (targetSeed.getVersion() >= yacyVersion.YACY_SUPPORTS_GZIP_POST_REQUESTS)) {
post.put(GZIP_POST_BODY,"true"); // TODO generate gzip-Header (and stream?)
} }
String resource = ""; String resource = "";

Loading…
Cancel
Save