Class Webserver

  • Direct Known Subclasses:
    AppletServer

    public class Webserver
    extends java.lang.Object
    A web server for running sample programs.

    This enables a Java program to instrument class files loaded by web browsers for applets. Since the (standard) security manager does not allow an applet to create and use a class loader, instrumenting class files must be done by this web server.

    Note: although this class is included in the Javassist API, it is provided as a sample implementation of the web server using Javassist. Especially, there might be security flaws in this server. Please use this with YOUR OWN RISK.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private ClassPool classPool  
      java.lang.String debugDir
      If this field is not null, the class files taken from ClassPool are written out under the directory specified by this field.
      private static byte[] endofline  
      java.lang.String htmlfileBase
      The top directory of html (and .gif, .class, ...) files.
      private java.net.ServerSocket socket  
      protected Translator translator  
      private static int typeClass  
      private static int typeGif  
      private static int typeHtml  
      private static int typeJpeg  
      private static int typeText  
    • Constructor Summary

      Constructors 
      Constructor Description
      Webserver​(int port)
      Constructs a web server.
      Webserver​(java.lang.String port)
      Constructs a web server.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addTranslator​(ClassPool cp, Translator t)
      Adds a translator, which is called whenever a client requests a class file.
      private void checkFilename​(java.lang.String filename, int len)  
      void doReply​(java.io.InputStream in, java.io.OutputStream out, java.lang.String cmd)
      Proceses a HTTP request from a client.
      void end()
      Closes the socket.
      private boolean letUsersSendClassfile​(java.io.OutputStream out, java.lang.String filename, int length)  
      void logging​(java.lang.String msg)
      Prints a log message.
      void logging​(java.lang.String msg1, java.lang.String msg2)
      Prints a log message.
      void logging​(java.lang.String msg1, java.lang.String msg2, java.lang.String msg3)
      Prints a log message.
      void logging2​(java.lang.String msg)
      Prints a log message with indentation.
      static void main​(java.lang.String[] args)
      Starts a web server.
      (package private) void process​(java.net.Socket clnt)  
      private java.lang.String readLine​(java.io.InputStream in)  
      private void replyError​(java.io.OutputStream out, BadHttpRequest e)  
      void run()
      Begins the HTTP service.
      private void sendHeader​(java.io.OutputStream out, long dataLength, int filetype)  
      void setClassPool​(ClassPool loader)
      Requests the web server to use the specified ClassPool object for obtaining a class file.
      private int skipLine​(java.io.InputStream in)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • socket

        private java.net.ServerSocket socket
      • endofline

        private static final byte[] endofline
      • debugDir

        public java.lang.String debugDir
        If this field is not null, the class files taken from ClassPool are written out under the directory specified by this field. The directory name must not end with a directory separator.
      • htmlfileBase

        public java.lang.String htmlfileBase
        The top directory of html (and .gif, .class, ...) files. It must end with the directory separator such as "/". (For portability, "/" should be used as the directory separator. Javassist automatically translates "/" into a platform-dependent character.) If this field is null, the top directory is the current one where the JVM is running.

        If the given URL indicates a class file and the class file is not found under the directory specified by this variable, then Class.getResourceAsStream() is called for searching the Java class paths.

    • Constructor Detail

      • Webserver

        public Webserver​(java.lang.String port)
                  throws java.io.IOException
        Constructs a web server.
        Parameters:
        port - port number
        Throws:
        java.io.IOException
      • Webserver

        public Webserver​(int port)
                  throws java.io.IOException
        Constructs a web server.
        Parameters:
        port - port number
        Throws:
        java.io.IOException
    • Method Detail

      • main

        public static void main​(java.lang.String[] args)
                         throws java.io.IOException
        Starts a web server. The port number is specified by the first argument.
        Throws:
        java.io.IOException
      • setClassPool

        public void setClassPool​(ClassPool loader)
        Requests the web server to use the specified ClassPool object for obtaining a class file.
      • end

        public void end()
                 throws java.io.IOException
        Closes the socket.
        Throws:
        java.io.IOException
      • logging

        public void logging​(java.lang.String msg)
        Prints a log message.
      • logging

        public void logging​(java.lang.String msg1,
                            java.lang.String msg2)
        Prints a log message.
      • logging

        public void logging​(java.lang.String msg1,
                            java.lang.String msg2,
                            java.lang.String msg3)
        Prints a log message.
      • logging2

        public void logging2​(java.lang.String msg)
        Prints a log message with indentation.
      • run

        public void run()
        Begins the HTTP service.
      • process

        final void process​(java.net.Socket clnt)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • readLine

        private java.lang.String readLine​(java.io.InputStream in)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • skipLine

        private int skipLine​(java.io.InputStream in)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • doReply

        public void doReply​(java.io.InputStream in,
                            java.io.OutputStream out,
                            java.lang.String cmd)
                     throws java.io.IOException,
                            BadHttpRequest
        Proceses a HTTP request from a client.
        Parameters:
        out - the output stream to a client
        cmd - the command received from a client
        Throws:
        java.io.IOException
        BadHttpRequest
      • letUsersSendClassfile

        private boolean letUsersSendClassfile​(java.io.OutputStream out,
                                              java.lang.String filename,
                                              int length)
                                       throws java.io.IOException,
                                              BadHttpRequest
        Throws:
        java.io.IOException
        BadHttpRequest
      • sendHeader

        private void sendHeader​(java.io.OutputStream out,
                                long dataLength,
                                int filetype)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • replyError

        private void replyError​(java.io.OutputStream out,
                                BadHttpRequest e)
                         throws java.io.IOException
        Throws:
        java.io.IOException