/** * @param request * @return * @throws IOException */ public InputStream sendRequest(byte[] request) throws IOException { output.write(("POST " + uri + " HTTP/1.0\r\n").getBytes()); output.write(("User-Agent: " + XmlRpc.version + "\r\n").getBytes()); output.write(("Host: " + host + "\r\n").getBytes()); if (XmlRpc.getKeepAlive()) { output.write("Connection: Keep-Alive\r\n".getBytes()); } output.write("Content-Type: text/xml\r\n".getBytes()); if (auth != null) { output.write(("Authorization: Basic " + auth + "\r\n").getBytes()); } output.write(("Content-Length: " + request.length).getBytes()); output.write("\r\n\r\n".getBytes()); output.write(request); output.flush(); // start reading server response headers String line = readLine(); if (XmlRpc.debug) { System.out.println(line); } int contentLength = -1; try { StringTokenizer tokens = new StringTokenizer(line); final String httpversion = tokens.nextToken(); String statusCode = tokens.nextToken(); String statusMsg = tokens.nextToken("\n\r"); keepalive = XmlRpc.getKeepAlive() && "HTTP/1.1".equals(httpversion); if (!"200".equals(statusCode)) { throw new IOException("Unexpected Response from Server: " + statusMsg); } } catch (IOException iox) { throw iox; } catch (Exception x) { // x.printStackTrace (); throw new IOException("Server returned invalid Response."); } do { line = readLine(); if (line != null) { if (XmlRpc.debug) { System.out.println(line); } line = line.toLowerCase(); if (line.startsWith("content-length:")) { contentLength = Integer.parseInt(line.substring(15).trim()); } if (line.startsWith("connection:")) { keepalive = XmlRpc.getKeepAlive() && line.indexOf("keep-alive") > -1; } } } while (line != null && !line.equals("")); return new ServerInputStream(input, contentLength); }
/** @return */ protected Runner getRunner() { try { return (Runner) threadpool.pop(); } catch (EmptyStackException empty) { int maxRequests = XmlRpc.getMaxThreads(); if (runners.activeCount() > XmlRpc.getMaxThreads()) { throw new RuntimeException( "System overload: Maximum number " + "of concurrent requests (" + maxRequests + ") exceeded"); } return new Runner(); } }
/** * This <em>can</em> be called from command line, but you'll have to edit and recompile to change * the server port or handler objects. By default, it sets up the following responders: * * <ul> * <li>A java.lang.String object * <li>The java.lang.Math class (making its static methods callable via XML-RPC) * <li>An Echo handler that returns the argument array * </ul> */ public static void main(String args[]) { System.err.println("Usage: java helma.xmlrpc.WebServer [port]"); int p = 8080; if (args.length > 0) try { p = Integer.parseInt(args[0]); } catch (NumberFormatException nfx) { System.err.println("Error parsing port number: " + args[0]); } // XmlRpc.setDebug (true); XmlRpc.setKeepAlive(true); try { WebServer webserver = new WebServer(p); // webserver.setParanoid (true); // webserver.acceptClient ("192.168.*.*"); webserver.addHandler("string", "Welcome to XML-RPC!"); webserver.addHandler("math", Math.class); webserver.addHandler("auth", new AuthDemo()); webserver.addHandler("$default", new Echo()); // XmlRpcClients can be used as Proxies in XmlRpcServers which is a cool feature for applets. webserver.addHandler("mttf", new XmlRpcClient("http://www.mailtothefuture.com:80/RPC2")); System.err.println("started web server on port " + p); } catch (IOException x) { System.err.println("Error creating web server: " + x); } }
/** * This <em>can</em> be called from command line, but you'll have to edit and recompile to change * the server port or handler objects. By default, it sets up the following responders: * * <ul> * <li>A java.lang.String object * <li>The java.lang.Math class (making its static methods callable via XML-RPC) * <li>An Echo handler that returns the argument array * </ul> * * @see #addDefaultHandlers() */ public static void main(String[] argv) { int p = determinePort(argv, 8080); // XmlRpc.setDebug (true); XmlRpc.setKeepAlive(true); WebServer webserver = new WebServer(p); try { webserver.addDefaultHandlers(); webserver.start(); } catch (Exception e) { System.err.println("Error running web server"); e.printStackTrace(); System.exit(1); } }
public void run() { try { boolean keepalive = false; do { // reset user authentication user = password = null; String line = readLine(); // Netscape sends an extra \n\r after bodypart, swallow it if ("".equals(line)) line = readLine(); if (XmlRpc.debug) System.err.println(line); // get time of last request lastRequest = System.currentTimeMillis(); int contentLength = -1; // tokenize first line of HTTP request StringTokenizer tokens = new StringTokenizer(line); String method = tokens.nextToken(); String uri = tokens.nextToken(); String httpversion = tokens.nextToken(); keepalive = XmlRpc.getKeepAlive() && "HTTP/1.1".equals(httpversion); do { line = readLine(); if (line != null) { if (XmlRpc.debug) System.err.println(line); String lineLower = line.toLowerCase(); if (lineLower.startsWith("content-length:")) contentLength = Integer.parseInt(line.substring(15).trim()); if (lineLower.startsWith("connection:")) keepalive = XmlRpc.getKeepAlive() && lineLower.indexOf("keep-alive") > -1; if (lineLower.startsWith("authorization: basic ")) parseAuth(line); } } while (line != null && !line.equals("")); if ("POST".equalsIgnoreCase(method)) { ServerInputStream sin = new ServerInputStream(input, contentLength); byte result[] = xmlrpc.execute(sin, user, password); output.write(httpversion.getBytes()); output.write(ok); output.write(server); if (keepalive) output.write(conkeep); else output.write(conclose); output.write(ctype); output.write(clength); output.write(Integer.toString(result.length).getBytes()); output.write(doubleNewline); output.write(result); output.flush(); } else { output.write(httpversion.getBytes()); output.write(" 400 Bad Request\r\n".getBytes()); output.write("Server: helma.XML-RPC\r\n\r\n".getBytes()); output.write(("Method " + method + " not implemented (try POST)").getBytes()); output.flush(); keepalive = false; } } while (keepalive); } catch (Exception exception) { if (XmlRpc.debug) { System.err.println(exception); exception.printStackTrace(); } } finally { try { socket.close(); } catch (IOException ignore) { } } }
public void run() { try { boolean keepAlive = false; do { // reset user authentication user = null; password = null; String line = readLine(); // Netscape sends an extra \n\r after bodypart, swallow it if (line != null && line.length() == 0) { line = readLine(); } if (XmlRpc.debug) { System.out.println(line); } int contentLength = -1; // tokenize first line of HTTP request StringTokenizer tokens = new StringTokenizer(line); String method = tokens.nextToken(); String uri = tokens.nextToken(); String httpVersion = tokens.nextToken(); keepAlive = XmlRpc.getKeepAlive() && HTTP_11.equals(httpVersion); do { line = readLine(); if (line != null) { if (XmlRpc.debug) { System.out.println(line); } String lineLower = line.toLowerCase(); if (lineLower.startsWith("content-length:")) { contentLength = Integer.parseInt(line.substring(15).trim()); } if (lineLower.startsWith("connection:")) { keepAlive = XmlRpc.getKeepAlive() && lineLower.indexOf("keep-alive") > -1; } if (lineLower.startsWith("authorization: basic ")) { parseAuth(line); } } } while (line != null && line.length() != 0); if ("POST".equalsIgnoreCase(method)) { ServerInputStream sin = new ServerInputStream(input, contentLength); try { byte[] result = xmlrpc.execute(sin, user, password); writeResponse(result, httpVersion, keepAlive); } catch (AuthenticationFailed unauthorized) { keepAlive = false; writeUnauthorized(httpVersion, method); } } else { keepAlive = false; writeBadRequest(httpVersion, method); } output.flush(); } while (keepAlive); } catch (Exception exception) { if (XmlRpc.debug) { exception.printStackTrace(); } else { System.err.println(exception); } } finally { try { if (socket != null) { socket.close(); } } catch (IOException ignore) { } } }