📄 httpiorserver.java
字号:
/*------------------------------------------------------------------------------Name: HttpIORServer.javaProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE fileComment: Delivering the Authentication Service IOR over HTTPVersion: $Id: HttpIORServer.java 14813 2006-03-04 23:02:48Z laghi $------------------------------------------------------------------------------*/package org.xmlBlaster.util.http;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;import java.util.Hashtable;import java.util.Iterator;import java.util.Map;import java.util.StringTokenizer;import java.util.TreeMap;import java.util.logging.Logger;import java.util.logging.Level;import org.xmlBlaster.util.Global;/** * Delivering the Authentication Service IOR over HTTP. * <p /> * This tiny HTTP server is always running in the xmlBlaster server on the * default bootstrapPort 3412.<br /> * Clients may access through this bootstrap port the AuthServer IOR if they * don't want to use a naming service * <p /> * You may specify on command line -bootstrapPort <port> and -bootstrapHostname <host> * to choose another bootstrap port or to choose a server IP address on * multi homed hosts. * <p /> * Change code to be a generic HTTP server, not only for CORBA bootstrapping * @version $Revision: 1.31 $ * @author $Author: laghi $ */public class HttpIORServer extends Thread implements I_HttpRequest{ private String ME = "HttpServer"; private final Global glob; private static Logger log = Logger.getLogger(HttpIORServer.class.getName()); private String ip_addr = null; private final int HTTP_PORT; private ServerSocket listen = null; private boolean running = true; private String icoMimeType = "image/ico"; private String icoRequestFile = "favicon.ico"; private String icoRequestUrlPath = "/"+icoRequestFile; private String fishMimeType = "image/gif"; private String fishRequestFile = "rainbowfish200.gif"; private String fishRequestUrlPath = "/"+fishRequestFile; private Hashtable knownRequests = new Hashtable(); /** * Create a little web server. * <p /> * @param ip_addr The string representation like "192.168.1.1", useful if multihomed computer * @param bootstrapPort The bootstrap port where we publish the IOR */ public HttpIORServer(Global glob, String ip_addr, int port) { super("XmlBlaster.HttpIORServer"); this.glob = glob; this.ip_addr = ip_addr; this.HTTP_PORT = port; this.ME += this.glob.getLogPrefixDashed(); if (this.HTTP_PORT <= 0) { if (log.isLoggable(Level.FINER)) log.finer("Internal HttpServer not started, as -bootstrapPort is " + this.HTTP_PORT); return; } registerRequest(icoRequestUrlPath, this); registerRequest(fishRequestUrlPath, this); if (log.isLoggable(Level.FINER)) log.finer("Creating new HttpServer on IP=" + this.ip_addr + " bootstrap port=" + this.HTTP_PORT); setDaemon(true); start(); } /** * If you want to provide some information over http, register it here. * @param urlPath The access path which the client uses to access your data, for example "/AuthenticationService.ior" * @param data The data you want to deliver to the client e.g. the CORBA IOR string */ public void registerRequest(String urlPath, String data) { if (log.isLoggable(Level.FINE)) log.fine("Registering urlPath: " + urlPath + "=" + data); knownRequests.put(urlPath.trim(), data); } /** * If you want to provide some information over http, register it here. * @param urlPath The access path which the client uses to access your data, * for example "/monitor/index.html" or "/favicon.ico" * @param data The data you want to deliver to the client e.g. the CORBA IOR string */ public void registerRequest(String urlPath, I_HttpRequest cb) { if (log.isLoggable(Level.FINE)) log.fine("Registering urlPath: " + urlPath); knownRequests.put(urlPath.trim(), cb); } /** * Unregister your http listener. * @param urlPath The access path which the client uses to access your data * for example "/monitor/index.html" or "/favicon.ico" */ public void removeRequest(String urlPath) { knownRequests.remove(urlPath.trim()); } /** * Unregister your http listener. * @param cb Remove all registered pathes of this registrar. * for example "/monitor/index.html" or "/favicon.ico" */ public void removeRequest(I_HttpRequest cb) { Iterator it = knownRequests.keySet().iterator(); ArrayList list = new ArrayList(); while (it.hasNext()) { Object obj = it.next(); if (obj instanceof I_HttpRequest) { I_HttpRequest tmp = (I_HttpRequest)obj; if (cb == tmp) { list.add(tmp); } } } for (int i=0; i<list.size(); i++) { knownRequests.remove(list.get(i)); } } /** */ public void run() { try { int backlog = glob.getProperty().get("http.backlog", 50); // queue for max 50 incoming connection request this.listen = new ServerSocket(HTTP_PORT, backlog, InetAddress.getByName(ip_addr)); while (running) { Socket accept = this.listen.accept(); log.fine("New incoming request on bootstrapPort=" + HTTP_PORT + " ..."); if (!running) { log.info("Closing http server bootstrapPort=" + HTTP_PORT + "."); break; } new HandleRequest(glob, log, accept, knownRequests); } } catch (java.net.UnknownHostException e) { log.severe("HTTP server problem, IP address '" + ip_addr + "' is invalid: " + e.toString()); } catch (java.net.BindException e) { log.severe("HTTP server problem, bootstrapPort " + ip_addr + ":" + HTTP_PORT + " is not available: " + e.toString()); } catch (java.net.SocketException e) { log.info("Socket " + ip_addr + ":" + HTTP_PORT + " closed successfully: " + e.toString()); } catch (IOException e) { log.severe("HTTP server problem on " + ip_addr + ":" + HTTP_PORT + ": " + e.toString()); } if (this.listen != null) { try { this.listen.close(); } catch (java.io.IOException e) { log.warning("this.listen.close()" + e.toString()); } this.listen = null; } } /** * Close the listener port */ public void shutdown()// throws IOException { if (log.isLoggable(Level.FINER)) log.finer("Entering shutdown"); running = false; removeRequest(icoRequestUrlPath); boolean closeHack = true; if (this.listen != null && closeHack) { // On some JDKs, listen.close() is not immediate (has a delay for about 1 sec.) // force closing by invoking server with this temporary client: try { java.net.Socket socket = new Socket(this.listen.getInetAddress(), HTTP_PORT); socket.close(); } catch (java.io.IOException e) { log.warning("shutdown problem: " + e.toString()); } } try { if (this.listen != null) { this.listen.close(); this.listen = null; } } catch (java.io.IOException e) { log.warning("shutdown problem: " + e.toString()); } } /** * A HTTP request needs to be processed * @param urlPath The url path like "/monitor" which triggered this call * @param properties The key values from the browser * @return The HTML page to return */ public HttpResponse service(String urlPath, Map properties) { if (urlPath.indexOf(icoRequestFile) != -1) { // set the application icon "favicon.ico" byte[] img = Global.getFromClasspath(icoRequestFile, this); if (log.isLoggable(Level.FINE)) log.fine("Serving urlPath '" + urlPath + "'"); return new HttpResponse(img, icoMimeType); } else if (urlPath.indexOf(fishRequestFile) != -1) { byte[] img = Global.getFromClasspath(fishRequestFile, this); if (log.isLoggable(Level.FINE)) log.fine("Serving urlPath '" + urlPath + "'"); return new HttpResponse(img, fishMimeType); } throw new IllegalArgumentException("Can't handle unknown " + urlPath); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -