001: /*
002: * @(#)NetworkClient.java 1.31 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: package sun.net;
028:
029: import java.io.*;
030: import java.net.Socket;
031: import java.net.InetAddress;
032: import java.net.InetSocketAddress;
033: import java.net.UnknownHostException;
034: import java.net.URL;
035: import sun.io.CharToByteConverter;
036: import java.util.Arrays;
037:
038: /**
039: * This is the base class for network clients.
040: *
041: * @version 1.31 10/10/06
042: * @author Jonathan Payne
043: */
044: public class NetworkClient {
045: /** Socket for communicating with server. */
046: protected Socket serverSocket = null;
047:
048: /** Stream for printing to the server. */
049: public PrintStream serverOutput;
050:
051: /** Buffered stream for reading replies from server. */
052: public InputStream serverInput;
053:
054: protected static int defaultSoTimeout;
055: protected static int defaultConnectTimeout;
056:
057: /* Name of encoding to use for output */
058: protected static String encoding;
059:
060: static {
061: Integer tm = (Integer) java.security.AccessController
062: .doPrivileged(new java.security.PrivilegedAction() {
063: public Object run() {
064: return (Integer
065: .getInteger("sun.net.client.defaultReadTimeout"));
066: }
067: });
068: if (tm == null)
069: defaultSoTimeout = -1;
070: else
071: defaultSoTimeout = tm.intValue();
072:
073: tm = (Integer) java.security.AccessController
074: .doPrivileged(new java.security.PrivilegedAction() {
075: public Object run() {
076: return (Integer
077: .getInteger("sun.net.client.defaultConnectTimeout"));
078: }
079: });
080: if (tm == null)
081: defaultConnectTimeout = -1;
082: else
083: defaultConnectTimeout = tm.intValue();
084:
085: encoding = (String) java.security.AccessController
086: .doPrivileged(new sun.security.action.GetPropertyAction(
087: "file.encoding", "ISO8859_1"));
088: try {
089: if (!isASCIISuperset(encoding)) {
090: encoding = "ISO8859_1";
091: }
092: } catch (Exception e) {
093: encoding = "ISO8859_1";
094: }
095: }
096:
097: /**
098: * Test the named character encoding to verify that it converts ASCII
099: * characters correctly. We have to use an ASCII based encoding, or else
100: * the NetworkClients will not work correctly in EBCDIC based systems.
101: * However, we cannot just use ASCII or ISO8859_1 universally, because in
102: * Asian locales, non-ASCII characters may be embedded in otherwise
103: * ASCII based protocols (eg. HTTP). The specifications (RFC2616, 2398)
104: * are a little ambiguous in this matter. For instance, RFC2398 [part 2.1]
105: * says that the HTTP request URI should be escaped using a defined
106: * mechanism, but there is no way to specify in the escaped string what
107: * the original character set is. It is not correct to assume that
108: * UTF-8 is always used (as in URLs in HTML 4.0). For this reason,
109: * until the specifications are updated to deal with this issue more
110: * comprehensively, and more importantly, HTTP servers are known to
111: * support these mechanisms, we will maintain the current behavior
112: * where it is possible to send non-ASCII characters in their original
113: * unescaped form.
114: */
115: private static boolean isASCIISuperset(String encoding)
116: throws Exception {
117: String chkS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
118: + "abcdefghijklmnopqrstuvwxyz-_.!~*'();/?:@&=+$,";
119:
120: // Expected byte sequence for string above
121: byte[] chkB = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66,
122: 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
123: 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99,
124: 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
125: 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
126: 122, 45, 95, 46, 33, 126, 42, 39, 40, 41, 59, 47, 63,
127: 58, 64, 38, 61, 43, 36, 44 };
128:
129: CharToByteConverter ctob = CharToByteConverter
130: .getConverter(encoding);
131: byte[] b = ctob.convertAll(chkS.toCharArray());
132: return Arrays.equals(b, chkB);
133: }
134:
135: /** Open a connection to the server. */
136: public void openServer(String server, int port) throws IOException,
137: UnknownHostException {
138: if (serverSocket != null)
139: closeServer();
140: serverSocket = doConnect(server, port);
141: try {
142: serverOutput = new PrintStream(new BufferedOutputStream(
143: serverSocket.getOutputStream()), true, encoding);
144: } catch (UnsupportedEncodingException e) {
145: throw new InternalError(encoding + "encoding not found");
146: }
147: serverInput = new BufferedInputStream(serverSocket
148: .getInputStream());
149: }
150:
151: /**
152: * Return a socket connected to the server, with any
153: * appropriate options pre-established
154: */
155: protected Socket doConnect(String server, int port)
156: throws IOException, UnknownHostException {
157: Socket s = new Socket();
158: if (defaultConnectTimeout > 0) {
159: s.connect(new InetSocketAddress(server, port),
160: defaultConnectTimeout);
161: } else {
162: s.connect(new InetSocketAddress(server, port));
163: }
164: if (defaultSoTimeout > 0) {
165: s.setSoTimeout(defaultSoTimeout);
166: }
167: return s;
168: }
169:
170: protected InetAddress getLocalAddress() throws IOException {
171: if (serverSocket == null)
172: throw new IOException("not connected");
173: return serverSocket.getLocalAddress();
174: }
175:
176: /** Close an open connection to the server. */
177: public void closeServer() throws IOException {
178: if (!serverIsOpen()) {
179: return;
180: }
181: serverSocket.close();
182: serverSocket = null;
183: serverInput = null;
184: serverOutput = null;
185: }
186:
187: /** Return server connection status */
188: public boolean serverIsOpen() {
189: return serverSocket != null;
190: }
191:
192: /** Create connection with host <i>host</i> on port <i>port</i> */
193: public NetworkClient(String host, int port) throws IOException {
194: openServer(host, port);
195: }
196:
197: public NetworkClient() {
198: }
199: }
|