⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionhandler.java

📁 aglet的部分源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			// ClassNames			int len = in.readInt();			ClassName class_names[] = new ClassName[len];			for (int i = 0; i < len; i++) {				String name = in.readUTF();				byte desc[] = new byte[in.readInt()];				in.readFully(desc, 0, desc.length);				class_names[i] = new ClassName(name, desc);			} 			// Agent			byte agent[] = new byte[in.readInt()];			in.readFully(agent, 0, agent.length);			if (_auth != null && _authenticated) {				byte[] mic = getMIC(request);				SharedSecret secret = _auth.getSelectedSecret();				if (mic != null && secret != null && agent != null) {					if (!verifyMIC(mic, secret, agent)) {						throw new IOException("Incorrect MIC of transfered aglet.");					} 					verboseOut("MIC is CORRECT.");				} 			} 			_maf.receive_agent(request.getAgentName(), 							   request.getAgentProfile(), agent, 							   request.getPlaceName(), class_names, codebase, 							   class_sender);			response.getOutputStream();			response.setStatusCode(OKAY);			response.sendResponse();			sent = true;		} catch (SecurityException ex) {			response.sendError(FORBIDDEN);			sent = true;		} catch (ClassUnknown ex) {			response.sendError(NOT_FOUND);			sent = true;		} catch (DeserializationFailed ex) {			response.sendError(NOT_FOUND);			sent = true;		} catch (MAFExtendedException ex) {			response.sendError(INTERNAL_ERROR);			sent = true;		} 		finally {			if (sent == false) {				response.sendError(INTERNAL_ERROR);			} 		} 	}	/**	 * Handles fetch requests.	 * @param request	 * @param response	 */	protected void handleFetchRequest(AtpRequest request, AtpResponse response) 			throws IOException {		byte[] bytecode = null;		response.setContentType("application/x-aglets");		boolean sent = false;		try {			byte b[][] = _maf.fetch_class(null, request.getFetchClassFile(), 										  request.getAgentProfile());			OutputStream out = response.getOutputStream();			verboseOut("fetch_class(" + request.getFetchClassFile() + ") : " 					   + b[0].length + "bytes");			// if(Daemon.isVerbose()) {			// dumpBytes(b[0]);			// }			out.write(b[0]);			response.setStatusCode(OKAY);			response.sendResponse();			sent = true;		} catch (ClassUnknown ex) {			response.sendError(NOT_FOUND);			sent = true;		} catch (MAFExtendedException ex) {			response.sendError(INTERNAL_ERROR);			sent = true;		} 		finally {			if (sent == false) {				response.sendError(NOT_FOUND);			} 		} 	}	// - 	    String filename = request.getRequestURI().getFile();	// - 	    int index = filename.lastIndexOf('/');	// -	// - 	    String dirname = filename.substring(0,index);	// -	// - 	    filename = filename.substring(index+1,filename.length());	// -	// - 	    if (AgletsSystem.getSecurityManager() != null) {	// - 		//	// - 		// REMIND: this part should be moved to SecurityManager	// - 		//	// - 		if (FileUtils.checkFile(dirname, agletsExportPath) == false) {	// - 		    throw new AgletsSecurityException("Illegal Fetch Request "	// - 						      + dirname + ' '	// - 						      + filename);	// - 		}	// - 	    }	// -	// - 	    bytecode = getClassData(dirname,filename);	// -	// - 	    response.setStatusCode(OKAY);	// -	// - 	    OutputStream out = response.getOutputStream();	// - 	    out.write(bytecode);	// -	// - 	    response.sendResponse();	// - 	} catch (SecurityException ce) {	// - 	    response.sendError(FORBIDDEN);	// - 	} catch (ClassNotFoundException ce) {	// - 	    response.sendError(NOT_FOUND);	// - 	} catch (IOException ie) {	// - 	    response.sendError(INTERNAL_ERROR);	// - 	}	protected void handleMessageRequest(AtpRequest request, AtpResponse response) 			throws IOException {		response.setContentType("application/x-aglets");		boolean sent = false;		Name name = request.getAgentName();		InputStream in = request.getInputStream();		byte type = (byte)in.read();		int len = request.getContentLength();		byte content[] = new byte[len - 1];		new DataInputStream(in).readFully(content);		try {			switch (type) {			case SYNC:				try {					byte ret[] = _maf.receive_message(name, content);					OutputStream out = response.getOutputStream();					out.write(HANDLED);					out.write(ret, 0, ret.length);					// DataOutput out = new DataOutputStream(...);					// out.writeByte(HANDLED);					response.setStatusCode(OKAY);					response.sendResponse();				} catch (NotHandled ex) {					response.getOutputStream().write(NOT_HANDLED);					// DataOutput out = new DataOutputStream(					// out.writeByte(NOT_HANDLED);					response.setStatusCode(OKAY);					response.sendResponse();				} catch (MessageEx ex) {					DataOutput out = 						new DataOutputStream(response.getOutputStream());					out.writeByte(EXCEPTION);					ex.write(out);					response.setStatusCode(OKAY);					response.sendResponse();				} 				break;			case FUTURE:				String sender_address = request.getSender();				MAFAgentSystem sender = 					MAFAgentSystem.getMAFAgentSystem(sender_address);				long id = _maf.receive_future_message(name, content, sender);				if (sender instanceof MAFAgentSystem_ATPClient) {					((MAFAgentSystem_ATPClient)sender)						.registerFutureReply(this, id);				} 				OutputStream out = response.getOutputStream();				response.setStatusCode(OKAY);				response.sendResponse();				synchronized (this) {					while (future_reply == null) {						try {							wait();						} catch (InterruptedException ex) {}					} 					out.write(future_reply);					out.flush();					out.close();					future_reply = null;				} 				// - 	try {				// - 	    DataOutput out = new DataOutputStream(response.getOutputStream());				// - 	    out.write(reply);				// - 	    response.setStatusCode(OKAY);				// - 	    response.sendResponse();				// - 	} catch (IOException ex) {				// - 	    throw new MAFExtendedException("UnexpectedException " +ex);				// - 	}				break;			case ONEWAY:				_maf.receive_oneway_message(name, content);				response.getOutputStream();				response.setStatusCode(OKAY);				response.sendResponse();				break;			}			sent = true;		} catch (AgentNotFound ex) {			response.sendError(NOT_FOUND);			sent = true;		} catch (ClassUnknown ex) {			response.sendError(NOT_FOUND);			sent = true;		} catch (DeserializationFailed ex) {			response.sendError(NOT_FOUND);			sent = true;		} catch (MAFExtendedException ex) {			response.sendError(INTERNAL_ERROR);			sent = true;		} 		finally {			if (sent == false) {				response.sendError(INTERNAL_ERROR);			} 		} 	}	/**	 * Handle ATP Requests	 */	void handleRequest(AtpRequest request, 					   AtpResponse response) throws IOException {		switch (request.getMethod()) {		case DISPATCH:			handleDispatchRequest(request, response);			break;		case RETRACT:			handleRetractRequest(request, response);			break;		case FETCH:			handleFetchRequest(request, response);			break;		case MESSAGE:			handleMessageRequest(request, response);			break;		default:			response.sendError(BAD_REQUEST);			break;		}	}	// - 	    AgletContextImpl context = getAgletContext(request);	// - 	    if (context == null) {	// - 		response.sendError(NOT_FOUND);	// - 		return;	// - 	    }	// - 	    AgletID aid =	// - 		    context.receiveAglet(request.getInputStream());	// - 	    response.setStatusCode(OKAY);	// - 	    response.getOutputStream().write(aid.toByteArray());	// - 	    response.sendResponse();	// - 	} catch (ShuttingDownException ex) {	// - 	    response.sendError(SERVICE_UNAVAILABLE);	// - 	} catch (AgletException ae) {	// - 	    response.sendError(INTERNAL_ERROR);	// - 	} catch (ClassNotFoundException cnfe) {	// - 	    response.sendError(NOT_FOUND);	// - 	} catch (SecurityException ex) {	// - 	    response.sendError(FORBIDDEN);	// - 	}	// -     }	/**	 * Handles retract requests.	 * @param request	 * @param response	 */	protected void handleRetractRequest(AtpRequest request, AtpResponse response) 			throws IOException {		response.setContentType("application/x-aglets");		boolean sent = false;		try {			byte b[] = _maf.retract_agent(request.getAgentName());			OutputStream out = response.getOutputStream();			out.write(b);			// - 	    String idstr = request.getAgentId();			// - 	    int len = idstr.length();			// - 	    byte[] b = new byte[len/2];			// - 	    for(int i = 0, j=0; j<len; i++, j++) {			// - 		b[i] = (byte)(Character.digit(idstr.charAt(j++), 16) << 4);			// - 		b[i] += (byte)Character.digit(idstr.charAt(j), 16);			// - 	    }			// - 	    AgletID aid = new AgletID(b);			// -			// -			// -			// - 	    // REMIND: This is supposed to be replaced with			// - 	    // other information. The remote URL is always null for the time			// - 	    // being			// - 	    URL remote = null;			// - 	    AgletContextImpl context = getAgletContext(request);			// - 	    if (context == null) {			// - 		response.sendError(NOT_FOUND);			// - 		return;			// - 	    }			// - 	    context.revertAglet(aid, remote, response.getOutputStream());			response.setStatusCode(OKAY);			response.sendResponse();			sent = true;		} catch (SecurityException ex) {			response.sendError(FORBIDDEN);			sent = true;			// - 	} catch (DeserializationFailed ae) {			// - 	    response.sendError(NOT_FOUND);		} catch (AgentNotFound ex) {			response.sendError(NOT_FOUND);			sent = true;		} catch (MAFExtendedException ex) {			ex.printStackTrace();			response.sendError(INTERNAL_ERROR);			sent = true;		} 		finally {			if (sent == false) {				response.sendError(INTERNAL_ERROR);			} 		} 	}	synchronized public void run() {		try {			while (true) {				try {					idle_handlers++;					try {						final ServerSocket fServerSocket = _serversocket;						_connection = 							(Socket)AccessController								.doPrivileged(new PrivilegedExceptionAction() {							public Object run() throws IOException {								return fServerSocket.accept();							} 						});					} catch (Exception ex) {						ex.printStackTrace();					} 					idle_handlers--;					// synchronized (this.class) {					if (idle_handlers == 0 && num_handlers < max_handlers) {						new ConnectionHandler(_maf, _serversocket);					} 					// }					handle();					_connection = null;					headers.clear();				} catch (Exception ex) {					ex.printStackTrace();				} 			} 		} 		finally {			num_handlers--;		} 	}	synchronized void sendFutureReply(byte b[]) {		future_reply = b;		notify();	}	private void sendHttpResponse() throws IOException {		PrintStream p = new PrintStream(_connection.getOutputStream());		// auto flush must be false.		p.print("HTTP/1.0 200 OKAY" + CRLF);		p.print("Content-type: text/html" + CRLF);		String s = "<HTTP><HEAD> ATP DAEMON/0.1 </HEAD><BODY>" 				   + "<H1> IBM ATP DAEMON/0.1 </H1><BR>" + "</BODY></HTTP>";		p.print("Content-length: " + s.length() + CRLF + CRLF);		p.println(s);		_connection.getOutputStream().flush();		_connection.close();		// _connection.getOutputStream().close();		// p.flush();		// p.close();	}	public String toString() {		return super.toString() + ", handling = " + (_connection != null);	}	static public void update() {		Resource res = Resource.getResourceFor("atp");		BUFFSIZE = res.getInteger("atp.buffersize", 2048);		authentication = res.getBoolean("atp.authentication", false);		http_tunneling = res.getBoolean("atp.http.tunneling", false);		http_messaging = res.getBoolean("atp.http.messaging", false);		max_handlers = res.getInteger("atp.maxHandlerThread", 32);	}	static private void verboseOut(String msg) {		Daemon.verboseOut(msg);	}	static boolean verifyMIC(byte[] mic, SharedSecret secret, byte[] agent) {		if (mic == null) {			// No MIC			System.err.println("No MIC");			return false;		} 		if (secret == null) {			// No shared secret			System.err.println("No authenticated security domain");			return false;		} 		if (agent == null) {			// No aglet byte sequence			System.err.println("No Aglet");			return false;		} 		byte[] digest = calculateMIC(secret, agent);		verboseOut("MIC=" + Hexadecimal.valueOf(mic));		verboseOut("digest=" + Hexadecimal.valueOf(digest));		return equalsSeq(mic, digest);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -