NetworkClient.java revision 28
0N/A/*
2362N/A * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2362N/A * CA 95054 USA or visit www.sun.com if you need additional information or
2362N/A * have any questions.
0N/A */
0N/Apackage sun.net;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.Socket;
0N/Aimport java.net.InetAddress;
0N/Aimport java.net.InetSocketAddress;
0N/Aimport java.net.UnknownHostException;
0N/Aimport java.net.URL;
0N/Aimport java.net.Proxy;
0N/Aimport java.util.Arrays;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/A
0N/A/**
0N/A * This is the base class for network clients.
0N/A *
0N/A * @author Jonathan Payne
0N/A */
0N/Apublic class NetworkClient {
0N/A protected Proxy proxy = Proxy.NO_PROXY;
0N/A /** Socket for communicating with server. */
0N/A protected Socket serverSocket = null;
0N/A
0N/A /** Stream for printing to the server. */
0N/A public PrintStream serverOutput;
0N/A
0N/A /** Buffered stream for reading replies from server. */
0N/A public InputStream serverInput;
0N/A
0N/A protected static int defaultSoTimeout;
0N/A protected static int defaultConnectTimeout;
0N/A
0N/A protected int readTimeout = -1;
0N/A protected int connectTimeout = -1;
0N/A /* Name of encoding to use for output */
0N/A protected static String encoding;
0N/A
0N/A static {
0N/A final int vals[] = {0, 0};
0N/A final String encs[] = { null };
0N/A
0N/A AccessController.doPrivileged(
0N/A new PrivilegedAction<Void>() {
0N/A public Void run() {
0N/A vals[0] = Integer.getInteger("sun.net.client.defaultReadTimeout", 0).intValue();
0N/A vals[1] = Integer.getInteger("sun.net.client.defaultConnectTimeout", 0).intValue();
0N/A encs[0] = System.getProperty("file.encoding", "ISO8859_1");
0N/A return null;
0N/A }
0N/A });
0N/A if (vals[0] == 0)
0N/A defaultSoTimeout = -1;
0N/A else
0N/A defaultSoTimeout = vals[0];
0N/A
0N/A if (vals[1] == 0)
0N/A defaultConnectTimeout = -1;
0N/A else
0N/A defaultConnectTimeout = vals[1];
0N/A
0N/A
0N/A encoding = encs[0];
0N/A try {
0N/A if (!isASCIISuperset (encoding)) {
0N/A encoding = "ISO8859_1";
0N/A }
0N/A } catch (Exception e) {
0N/A encoding = "ISO8859_1";
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Test the named character encoding to verify that it converts ASCII
0N/A * characters correctly. We have to use an ASCII based encoding, or else
0N/A * the NetworkClients will not work correctly in EBCDIC based systems.
0N/A * However, we cannot just use ASCII or ISO8859_1 universally, because in
0N/A * Asian locales, non-ASCII characters may be embedded in otherwise
0N/A * ASCII based protocols (eg. HTTP). The specifications (RFC2616, 2398)
0N/A * are a little ambiguous in this matter. For instance, RFC2398 [part 2.1]
0N/A * says that the HTTP request URI should be escaped using a defined
0N/A * mechanism, but there is no way to specify in the escaped string what
0N/A * the original character set is. It is not correct to assume that
0N/A * UTF-8 is always used (as in URLs in HTML 4.0). For this reason,
0N/A * until the specifications are updated to deal with this issue more
0N/A * comprehensively, and more importantly, HTTP servers are known to
0N/A * support these mechanisms, we will maintain the current behavior
0N/A * where it is possible to send non-ASCII characters in their original
0N/A * unescaped form.
0N/A */
0N/A private static boolean isASCIISuperset (String encoding) throws Exception {
0N/A String chkS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
0N/A "abcdefghijklmnopqrstuvwxyz-_.!~*'();/?:@&=+$,";
0N/A
0N/A // Expected byte sequence for string above
0N/A byte[] chkB = { 48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70,71,72,
0N/A 73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,
0N/A 100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,
0N/A 115,116,117,118,119,120,121,122,45,95,46,33,126,42,39,40,41,59,
0N/A 47,63,58,64,38,61,43,36,44};
0N/A
0N/A byte[] b = chkS.getBytes (encoding);
0N/A return Arrays.equals (b, chkB);
0N/A }
0N/A
0N/A /** Open a connection to the server. */
0N/A public void openServer(String server, int port)
0N/A throws IOException, UnknownHostException {
0N/A if (serverSocket != null)
0N/A closeServer();
0N/A serverSocket = doConnect (server, port);
0N/A try {
0N/A serverOutput = new PrintStream(new BufferedOutputStream(
0N/A serverSocket.getOutputStream()),
0N/A true, encoding);
0N/A } catch (UnsupportedEncodingException e) {
0N/A throw new InternalError(encoding +"encoding not found");
0N/A }
0N/A serverInput = new BufferedInputStream(serverSocket.getInputStream());
0N/A }
0N/A
0N/A /**
0N/A * Return a socket connected to the server, with any
0N/A * appropriate options pre-established
0N/A */
0N/A protected Socket doConnect (String server, int port)
0N/A throws IOException, UnknownHostException {
0N/A Socket s;
0N/A if (proxy != null) {
0N/A if (proxy.type() == Proxy.Type.SOCKS) {
0N/A s = AccessController.doPrivileged(
0N/A new PrivilegedAction<Socket>() {
0N/A public Socket run() {
0N/A return new Socket(proxy);
0N/A }});
0N/A } else
0N/A s = new Socket(Proxy.NO_PROXY);
0N/A } else
0N/A s = new Socket();
0N/A // Instance specific timeouts do have priority, that means
0N/A // connectTimeout & readTimeout (-1 means not set)
0N/A // Then global default timeouts
0N/A // Then no timeout.
0N/A if (connectTimeout >= 0) {
0N/A s.connect(new InetSocketAddress(server, port), connectTimeout);
0N/A } else {
0N/A if (defaultConnectTimeout > 0) {
0N/A s.connect(new InetSocketAddress(server, port), defaultConnectTimeout);
0N/A } else {
0N/A s.connect(new InetSocketAddress(server, port));
0N/A }
0N/A }
0N/A if (readTimeout >= 0)
0N/A s.setSoTimeout(readTimeout);
0N/A else if (defaultSoTimeout > 0) {
0N/A s.setSoTimeout(defaultSoTimeout);
0N/A }
0N/A return s;
0N/A }
0N/A
0N/A protected InetAddress getLocalAddress() throws IOException {
0N/A if (serverSocket == null)
0N/A throw new IOException("not connected");
0N/A return serverSocket.getLocalAddress();
0N/A }
0N/A
0N/A /** Close an open connection to the server. */
0N/A public void closeServer() throws IOException {
0N/A if (! serverIsOpen()) {
0N/A return;
0N/A }
0N/A serverSocket.close();
0N/A serverSocket = null;
0N/A serverInput = null;
0N/A serverOutput = null;
0N/A }
0N/A
0N/A /** Return server connection status */
0N/A public boolean serverIsOpen() {
0N/A return serverSocket != null;
0N/A }
0N/A
0N/A /** Create connection with host <i>host</i> on port <i>port</i> */
0N/A public NetworkClient(String host, int port) throws IOException {
0N/A openServer(host, port);
0N/A }
0N/A
0N/A public NetworkClient() {}
0N/A
0N/A public void setConnectTimeout(int timeout) {
0N/A connectTimeout = timeout;
0N/A }
0N/A
0N/A public int getConnectTimeout() {
0N/A return connectTimeout;
0N/A }
0N/A
0N/A public void setReadTimeout(int timeout) {
0N/A if (serverSocket != null && timeout >= 0) {
0N/A try {
0N/A serverSocket.setSoTimeout(timeout);
0N/A } catch(IOException e) {
0N/A // We tried...
0N/A }
0N/A }
0N/A readTimeout = timeout;
0N/A }
0N/A
0N/A public int getReadTimeout() {
0N/A return readTimeout;
0N/A }
0N/A}
0N/A