Ejemplo n.º 1
0
 /**
  * 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);
   }
 }
Ejemplo n.º 2
0
    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) {
        }
      }
    }