📄 connectionhandler.java
字号:
package com.ibm.maf.atp;/* * @(#)ConnectionHandler.java * * IBM Confidential-Restricted * * OCO Source Materials * * 03L7246 (c) Copyright IBM Corp. 1996, 1998 * * The source code for this program is not published or otherwise * divested of its trade secrets, irrespective of what has been * deposited with the U.S. Copyright Office. */import com.ibm.maf.ClassName;import com.ibm.maf.MAFAgentSystem;import com.ibm.maf.Name;import com.ibm.maf.AgentNotFound;import com.ibm.maf.ClassUnknown;import com.ibm.maf.DeserializationFailed;import com.ibm.maf.MAFExtendedException;import com.ibm.maf.MessageEx;import com.ibm.maf.NotHandled;import com.ibm.atp.AtpConstants;import com.ibm.atp.auth.Auth;import com.ibm.atp.auth.AuthPacket;import com.ibm.atp.auth.Authentication;// - import com.ibm.atp.auth.AuthenticationManager;import com.ibm.atp.auth.AuthenticationProtocolException;import com.ibm.atp.auth.SharedSecrets;import com.ibm.atp.auth.SharedSecret;// - import com.ibm.aglets.AgletsSystem;import com.ibm.awb.misc.Resource;import com.ibm.awb.misc.Hexadecimal;import java.security.AccessController;import java.security.PrivilegedExceptionAction;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.security.InvalidKeyException;import java.net.InetAddress;import java.net.Socket;import java.net.ServerSocket;import java.net.URL;import java.io.InputStream;import java.io.BufferedInputStream;import java.io.DataInput;import java.io.DataInputStream;import java.io.OutputStream;import java.io.BufferedOutputStream;import java.io.DataOutput;import java.io.DataOutputStream;import java.io.PrintStream;import java.io.IOException;/** * @version 1.10 $Date: 2001/07/28 06:31:34 $ * @author Danny D. Langue * @author Gaku Yamamoto * @author Mitsuru Oshima */final class ConnectionHandler extends Thread implements AtpConstants { /** * message digest algorithm. */ final private static String MESSAGE_DIGEST_ALGORITHM = "SHA"; private static MessageDigest _mdigest = null; private MAFAgentSystem _maf = null; private ServerSocket _serversocket = null; private Socket _connection = null; private Authentication _auth = null; private boolean _authenticated = false; private static ThreadGroup group = new ThreadGroup("ConnectionHandler"); private static int BUFFSIZE = 2048; private static int number = 0; private static boolean authentication = false; private static int max_handlers = 32; private static int num_handlers = 0; private static int idle_handlers = 0; static { try { _mdigest = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM); } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); } update(); } static boolean http_tunneling = false; static boolean http_messaging = false; private java.util.Hashtable headers = new java.util.Hashtable(); // - AgletID aid = null; // - if (obj instanceof AgletID) { // - aid = (AgletID)obj; // - } else { // - int len = ref.length(); // - byte[] b = new byte[len/2]; // - for(int i = 0, j=0; j<len; i++, j++) { // - b[i] = (byte)(Character.digit(ref.charAt(j++), 16) << 4); // - b[i] += (byte)Character.digit(ref.charAt(j), 16); // - } // - aid = new AgletID(b); // - } // - // - // AgletID aid = new AgletID(b); // - // String t = // - context.handleMessageRequest(aid, // - request.getAgentLanguage(), // - request.getInputStream(), // - response); // - // response.setContentType(t); // - // response.setStatusCode(OKAY); // - // response.sendResponse(); // - } catch (InvalidAgletException ex) { // - response.sendError(NOT_FOUND); // - } catch (Exception ex) { // - response.sendError(INTERNAL_ERROR); // - } // - } byte future_reply[] = null; /* * */ private static final String CRLF = "\r\n"; public ConnectionHandler(MAFAgentSystem maf, ServerSocket s) { super(group, "handler:" + (number++)); _serversocket = s; _maf = maf; num_handlers++; start(); } static byte[] calculateMIC(SharedSecret secret, byte[] agent) { if (secret == null) { // No shared secret return null; } if (agent == null) { // No aglet byte sequence return null; } _mdigest.reset(); _mdigest.update(agent); _mdigest.update(secret.secret()); return _mdigest.digest(); } private static void dumpBytes(byte[] bytes) { if (bytes != null) { for (int i = 0; i < bytes.length; i++) { System.out.print(Hexadecimal.valueOf(bytes[i])); if (i % 16 == 15) { System.out.println(); } else { System.out.print(" "); } } if (bytes.length % 16 != 0) { System.out.println(); } } } private static boolean equalsSeq(byte[] seqa, byte[] seqb) { if (seqa == null && seqb == null) { return true; } if (seqa == null || seqb == null || seqa.length != seqb.length) { return false; } for (int i = 0; i < seqa.length; i++) { if (seqa[i] != seqb[i]) { return false; } } return true; } private static byte[] getMIC(AtpRequest request) { String micstr = request.getRequestParameter("mic"); if (micstr == null) { return null; } byte[] mic = null; try { mic = Hexadecimal.parseSeq(micstr); } catch (NumberFormatException excpt) { System.err.println("Illegal MIC in ATP request header : " + micstr); } return mic; } private void handle() throws IOException { AtpRequest request = null; AtpResponse response = null; InetAddress remoteHost = null; long time = System.currentTimeMillis(); remoteHost = _connection.getInetAddress(); verboseOut("[Connected from " + remoteHost + ']'); InputStream in = new BufferedInputStream(_connection.getInputStream(), BUFFSIZE); in.mark(128); DataInput di = new DataInputStream(in); String topLine = di.readLine(); in.reset(); if (topLine == null) { try { _connection.close(); } catch (IOException exx) { System.err.println(exx.toString()); } return; } _auth = null; _authenticated = false; if (AuthPacket.isTopLine(topLine)) { if (SharedSecrets.getSharedSecrets() == null) { throw new IOException("Authentication failed : no shared secrets."); } // authentication protocol // _auth = new Authentication(Auth.SECOND_TURN, _connection); _auth = new Authentication(Auth.SECOND_TURN, di, _connection); try { _authenticated = _auth.authenticate(); } catch (AuthenticationProtocolException excpt) { // protocol error System.err.println(excpt.toString()); try { _connection.close(); } catch (IOException exx) { System.err.println(exx.toString()); } throw new IOException("Authentication failed : " + excpt.getMessage()); } catch (IOException excpt) { // protocol error System.err.println(excpt.toString()); try { _connection.close(); } catch (IOException exx) { System.err.println(exx.toString()); } throw new IOException("Authentication failed : " + excpt.getMessage()); } if (!_authenticated) { response = new AtpResponseImpl(new BufferedOutputStream(_connection .getOutputStream(), BUFFSIZE)); response.sendError(NOT_AUTHENTICATED); try { _connection.close(); } catch (IOException exx) { System.err.println(exx.toString()); } return; } in.mark(128); topLine = di.readLine(); in.reset(); if (topLine == null) { try { _connection.close(); } catch (IOException exx) { System.err.println(exx.toString()); } return; } } String protocol = topLine.trim(); protocol = protocol.substring(protocol.lastIndexOf(' ') + 1, protocol.lastIndexOf('/')); if (protocol.equalsIgnoreCase("ATP")) { if (authentication && (_auth == null ||!_authenticated)) { System.err .println("ATP connection from unauthenticated host is closed."); response = new AtpResponseImpl(new BufferedOutputStream(_connection .getOutputStream(), BUFFSIZE)); response.sendError(NOT_AUTHENTICATED); try { _connection.close(); } catch (IOException exx) { System.err.println(exx.toString()); } return; } // verboseOut("creating ATP Request & Response"); request = new AtpRequestImpl(in); response = new AtpResponseImpl(new BufferedOutputStream(_connection .getOutputStream(), BUFFSIZE)); } else if (protocol.equalsIgnoreCase("HTTP")) { verboseOut("[Accepting HTTP..]"); // Trim http headers HttpFilter.readHttpHeaders(in, headers); String r = (String)headers.get("method"); String type = (String)headers.get("content-type"); if ("GET".equalsIgnoreCase(r) && http_messaging) { // may be HTTP browser... verboseOut("[Http/GET request received.]"); request = new HttpCGIRequestImpl(in, headers); response = new HttpCGIResponseImpl(_connection.getOutputStream()); } else if ("POST".equalsIgnoreCase(r) && "application/x-atp".equalsIgnoreCase(type) && http_tunneling) { // http tunneling verboseOut("[Http/POST request received.]"); request = new AtpRequestImpl(in); response = new AtpResponseImpl(new HttpResponseOutputStream(_connection .getOutputStream())); } else if ("POST".equalsIgnoreCase(r) && "application/x-www-url-form" .equalsIgnoreCase(type)) { verboseOut("[POST request received.]"); sendHttpResponse(); verboseOut("[Sending responser.]"); return; } else { throw new IOException("Unknown Content-Type:" + type); } } else { throw new IOException("unknown protocol " + protocol); } try { request.parseHeaders(); } catch (IOException ex) { try { _connection.close(); } catch (IOException exx) { System.err.println(exx.toString()); } Daemon.error(remoteHost, System.currentTimeMillis(), "", ex.toString()); Daemon.access(remoteHost, System.currentTimeMillis(), request.getRequestLine(), response.getStatusCode(), String.valueOf('-')); return; } try { handleRequest(request, response); // - String agentSystem = request.getAgentSystem(); // - AgentRequestHandler handler = // - _daemon.getRequestHandler(agentSystem); // - // - verboseOut("[Start handling : method = "+request.getMethod()); // - // - if (handler != null) { // - // com.ibm.awb.misc.Debug.check(); // - handler.handleRequest(request, response); // - } else { // - throw new ClassNotFoundException("[Agent System '" + // - agentSystem + // - "' not found]"); // - } } catch (IOException ioe) { if (Daemon.isVerbose()) { ioe.printStackTrace(); } Daemon.error(remoteHost, System.currentTimeMillis(), "", ioe.toString()); ioe.printStackTrace(); try { response.sendError(INTERNAL_ERROR); } catch (IOException ex) { System.err.println(ex.toString()); } // - } catch (ClassNotFoundException cnfe) { // - _daemon.error(remoteHost, // - System.currentTimeMillis(), // - "Error: SERVICE_UNAVAILABLE", // - cnfe.getMessage()); // - cnfe.printStackTrace(); // - try { // - response.sendError(NOT_FOUND); // - } catch (Exception ex) { // - ex.printStackTrace(); // - } } finally { // com.ibm.awb.misc.Debug.check(); try { _connection.close(); } catch (IOException e) { System.err.println(e.toString()); } Daemon.access(remoteHost, System.currentTimeMillis(), request.getRequestLine(), response.getStatusCode(), String.valueOf('-')); } } /** * Handles Dispatch Requests */ protected void handleDispatchRequest(AtpRequest request, AtpResponse response) throws IOException { response.setContentType("application/x-aglets"); boolean sent = false; try { MAFAgentSystem class_sender = MAFAgentSystem.getMAFAgentSystem(request.getSender()); DataInputStream in = new DataInputStream(request.getInputStream()); // CodeBase String codebase = in.readUTF();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -