Class NetServlet

  • All Implemented Interfaces:
    java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

    public class NetServlet
    extends javax.servlet.http.HttpServlet
    This servlet can be used to start Derby Network Server from a remote location.

    These servlet configuration parameters are understood by this servlet.

    • portNumber
      - Port number to use. The default is 1527.
    • startNetworkServerOnInit
      - Starts the Derby Network Server at servlet initialization if 'true'.
    • tracingDirectory
      - Directory for trace files
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      NetServlet()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void displayCurrentStatus​(LocalizedResource localUtil, java.lang.String returnMessage, java.io.PrintWriter out)
      Display the current Network server status
      void doGet​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
      Get the form of NetServlet.
      void doPost​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
      Get the form of NetServlet.
      private java.lang.String escapeHTML​(java.lang.String str)
      Escapes potentially dangerous characters in data written to the browser.
      private java.lang.String escapeSingleQuotes​(java.lang.String str)
      If the received string has one or more single quotes in it, replace each one with the HTML escape-code for a single quote (apostrophe) so that the string can be properly displayed on a submit button.
      private java.lang.String fixLanguageCode​(java.lang.String lang)
      Fix the language code, as some browsers send then in a bad format (for instance, Firefox sends en-us instead of en_US).
      private java.lang.String fixLanguageCode​(java.lang.String lang, int index)  
      private LocalizedResource getCurrentAppUI​(javax.servlet.http.HttpServletRequest request, java.lang.String[] locale)
      Determine the locale file needed for this browsers preferences Defaults to the settings for derby.locale and derby.codeset if set English otherwise if browsers preferences can't be found
      private java.lang.String getDoAction​(javax.servlet.http.HttpServletRequest request)  
      private java.lang.String getForm​(javax.servlet.http.HttpServletRequest request)  
      private java.lang.String getHtmlLabelledMessageInstance​(LocalizedResource localUtil, java.lang.String key, java.lang.String id)
      get an HTML labelled message from the resource bundle file, according to the given key.
      private int getIntParameter​(javax.servlet.http.HttpServletRequest request, java.lang.String name, java.lang.String fieldKey, LocalizedResource localUtil, java.lang.String returnMessage, java.io.PrintWriter out)
      Get an integer parameter
      private java.lang.String getLocStringFromLanguage​(java.lang.String lang)
      Get locale string from language which may have qvalue set
      private java.lang.String getLogging​(javax.servlet.http.HttpServletRequest request)  
      private java.lang.String getParam​(javax.servlet.http.HttpServletRequest request, java.lang.String paramName)
      get UTF8 parameter value and decode international characters
      private boolean getServerStatus()
      Get the currrent server status by using test connection
      private java.lang.String getTrace​(javax.servlet.http.HttpServletRequest request)  
      void init​(javax.servlet.ServletConfig config)
      Initialize the servlet.
      private static boolean isServerStarted​(NetworkServerControl server, int ntries)  
      private boolean logging​(LocalizedResource localUtil, boolean val, java.lang.String returnMessage, java.io.PrintWriter out)
      Turn logging of connections on
      private void printAsContentHeader​(java.lang.String str, java.io.PrintWriter out)
      Print the received string as a header.
      private void printBanner​(LocalizedResource localUtil, java.io.PrintWriter out)
      Print Derby Network Server banner
      private void printErrorForm​(LocalizedResource localUtil, java.lang.Exception e, java.lang.String returnMessage, java.io.PrintWriter out)
      Display an error form
      private void printErrorForm​(LocalizedResource localUtil, java.lang.String msg, java.lang.String returnMessage, java.io.PrintWriter out)
      Display an error form
      private void runServer​(LocalizedResource localUtil, java.lang.String returnMessage, java.io.PrintWriter out)
      Start the network server and attempt to connect to it before returning
      private void setDefaults()
      Set defaults for logging and tracing (both off)
      private boolean setNetParam​(LocalizedResource localUtil, int max, int slice, java.lang.String returnMessage, java.io.PrintWriter out)
      Set Network server parameters
      private boolean shutdownServer​(LocalizedResource localUtil, java.lang.String returnMessage, java.io.PrintWriter out)
      Shutdown the network server
      private boolean traceAll​(LocalizedResource localUtil, boolean val, java.lang.String returnMessage, java.io.PrintWriter out)
      Change tracing for all sessions
      private boolean traceDirectory​(LocalizedResource localUtil, java.lang.String traceDirectory, java.lang.String returnMessage, java.io.PrintWriter out)
      Set trace directory
      private boolean traceSession​(LocalizedResource localUtil, boolean val, int session, java.lang.String returnMessage, java.io.PrintWriter out)
      Change tracing for a given session
      private int translationAvailable​(java.lang.String lang)
      Check if the required translation is available
      • Methods inherited from class javax.servlet.http.HttpServlet

        doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
      • Methods inherited from class javax.servlet.GenericServlet

        destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
      • Methods inherited from class java.lang.Object

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

      • SERVLET_PROP_MESSAGES

        private static final java.lang.String SERVLET_PROP_MESSAGES
        See Also:
        Constant Field Values
      • knownLang

        private static final java.lang.String[] knownLang
      • host

        private java.lang.String host
      • portNumber

        private int portNumber
      • tracingDirectory

        private volatile java.lang.String tracingDirectory
      • logStatus

        private volatile boolean logStatus
      • traceStatus

        private volatile boolean traceStatus
    • Constructor Detail

      • NetServlet

        public NetServlet()
    • Method Detail

      • init

        public void init​(javax.servlet.ServletConfig config)
                  throws javax.servlet.ServletException
        Initialize the servlet. Configuration parameters:
        • portNumber
          - Port number
        • host
          - Host name
        • traceDirectory
          - location of trace directory
        • startNetworkServerOnInit
          - start the server on initialization
        Specified by:
        init in interface javax.servlet.Servlet
        Overrides:
        init in class javax.servlet.GenericServlet
        Throws:
        javax.servlet.ServletException
      • doGet

        public void doGet​(javax.servlet.http.HttpServletRequest request,
                          javax.servlet.http.HttpServletResponse response)
                   throws javax.servlet.ServletException,
                          java.io.IOException
        Get the form of NetServlet. Provides buttons and forms to control the Network server.
        Overrides:
        doGet in class javax.servlet.http.HttpServlet
        Throws:
        javax.servlet.ServletException
        java.io.IOException
      • doPost

        public void doPost​(javax.servlet.http.HttpServletRequest request,
                           javax.servlet.http.HttpServletResponse response)
                    throws javax.servlet.ServletException,
                           java.io.IOException
        Get the form of NetServlet. Provides a buttons and form to control the Network server
        Overrides:
        doPost in class javax.servlet.http.HttpServlet
        Throws:
        javax.servlet.ServletException
        java.io.IOException
      • getForm

        private java.lang.String getForm​(javax.servlet.http.HttpServletRequest request)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • getDoAction

        private java.lang.String getDoAction​(javax.servlet.http.HttpServletRequest request)
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • getLogging

        private java.lang.String getLogging​(javax.servlet.http.HttpServletRequest request)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • getTrace

        private java.lang.String getTrace​(javax.servlet.http.HttpServletRequest request)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • getParam

        private java.lang.String getParam​(javax.servlet.http.HttpServletRequest request,
                                          java.lang.String paramName)
                                   throws java.io.IOException
        get UTF8 parameter value and decode international characters
        Parameters:
        request - HttpServletRequest
        paramName - Parameter name
        Returns:
        decoded String
        Throws:
        java.io.IOException
      • runServer

        private void runServer​(LocalizedResource localUtil,
                               java.lang.String returnMessage,
                               java.io.PrintWriter out)
                        throws javax.servlet.ServletException
        Start the network server and attempt to connect to it before returning
        Parameters:
        localUtil - LocalizedResource to use to translate messages
        returnMessage - localized continue message for continue button on error form
        out - Form PrintWriter
        Throws:
        javax.servlet.ServletException - throws an exception if error in starting the Network Server during initialization
      • printErrorForm

        private void printErrorForm​(LocalizedResource localUtil,
                                    java.lang.Exception e,
                                    java.lang.String returnMessage,
                                    java.io.PrintWriter out)
        Display an error form
        Parameters:
        localUtil - LocalizedResource to use to translate messages
        e - Exception to be displayed
        returnMessage - localized continue message for continue button on error form
        out - Form PrintWriter
      • printErrorForm

        private void printErrorForm​(LocalizedResource localUtil,
                                    java.lang.String msg,
                                    java.lang.String returnMessage,
                                    java.io.PrintWriter out)
        Display an error form
        Parameters:
        localUtil - LocalizedResource to use to translate messages
        msg - String to be displayed
        out - Form PrintWriter
        returnMessage - localized continue message for continue button on error form
      • displayCurrentStatus

        private void displayCurrentStatus​(LocalizedResource localUtil,
                                          java.lang.String returnMessage,
                                          java.io.PrintWriter out)
        Display the current Network server status
        Parameters:
        localUtil - LocalizedResource to use for localizing messages
        returnMessage - localized continue message for continue button on error form
        out - Form PrintWriter
      • getServerStatus

        private boolean getServerStatus()
        Get the currrent server status by using test connection
        Returns:
        true if server is up and reachable; false; otherwise
      • shutdownServer

        private boolean shutdownServer​(LocalizedResource localUtil,
                                       java.lang.String returnMessage,
                                       java.io.PrintWriter out)
        Shutdown the network server
        Parameters:
        localUtil - LocalizedResource to use to translate messages
        returnMessage - localized continue message for continue button on error form
        out - Form PrintWriter
        Returns:
        true if succeeded; false; otherwise
      • logging

        private boolean logging​(LocalizedResource localUtil,
                                boolean val,
                                java.lang.String returnMessage,
                                java.io.PrintWriter out)
        Turn logging of connections on
        Parameters:
        localUtil - LocalizedResource to use to translate messages
        returnMessage - localized continue message for continue button on error form
        out - Form PrintWriter
        Returns:
        true if succeeded; false; otherwise
      • traceAll

        private boolean traceAll​(LocalizedResource localUtil,
                                 boolean val,
                                 java.lang.String returnMessage,
                                 java.io.PrintWriter out)
        Change tracing for all sessions
        Parameters:
        localUtil - LocalizedResource to use to translate messages
        val - if true, turn tracing on, if false turn it off
        returnMessage - localized continue message for continue button on error form
        out - Form PrintWriter
        Returns:
        true if succeeded; false; otherwise
      • traceSession

        private boolean traceSession​(LocalizedResource localUtil,
                                     boolean val,
                                     int session,
                                     java.lang.String returnMessage,
                                     java.io.PrintWriter out)
        Change tracing for a given session
        Parameters:
        localUtil - LocalizedResource to use to translate messages
        val - if true, turn tracing on, if false turn it off
        session - session to trace
        returnMessage - localized continue message for continue button on error form
        out - Form PrintWriter
        Returns:
        true if succeeded; false; otherwise
      • traceDirectory

        private boolean traceDirectory​(LocalizedResource localUtil,
                                       java.lang.String traceDirectory,
                                       java.lang.String returnMessage,
                                       java.io.PrintWriter out)
        Set trace directory
        Parameters:
        localUtil - LocalizedResource to use to translate messages
        traceDirectory - directory for trace files
        returnMessage - localized continue message for continue button on error form
        out - Form PrintWriter
        Returns:
        true if succeeded; false; otherwise
      • setNetParam

        private boolean setNetParam​(LocalizedResource localUtil,
                                    int max,
                                    int slice,
                                    java.lang.String returnMessage,
                                    java.io.PrintWriter out)
        Set Network server parameters
        Parameters:
        localUtil - LocalizedResource to use to translate messages
        max - maximum number of threads
        slice - time slice for each connection
        returnMessage - localized continue message for continue button on error form
        out - Form PrintWriter
        Returns:
        true if succeeded; false; otherwise
      • setDefaults

        private void setDefaults()
        Set defaults for logging and tracing (both off)
      • getIntParameter

        private int getIntParameter​(javax.servlet.http.HttpServletRequest request,
                                    java.lang.String name,
                                    java.lang.String fieldKey,
                                    LocalizedResource localUtil,
                                    java.lang.String returnMessage,
                                    java.io.PrintWriter out)
        Get an integer parameter
        Parameters:
        request - HttpServetRequest for forms
        name - parameter name
        fieldKey - Key for the name of the field we're reading.
        localUtil - LocalizedResource to use in localizing messages
        returnMessage - localized continue message for continue button on error form
        out - Form PrintWriter
      • printBanner

        private void printBanner​(LocalizedResource localUtil,
                                 java.io.PrintWriter out)
        Print Derby Network Server banner
      • getCurrentAppUI

        private LocalizedResource getCurrentAppUI​(javax.servlet.http.HttpServletRequest request,
                                                  java.lang.String[] locale)
        Determine the locale file needed for this browsers preferences Defaults to the settings for derby.locale and derby.codeset if set English otherwise if browsers preferences can't be found
        Parameters:
        request - HttpServetRequest for forms
        locale - Name of locale (return arg)
        Returns:
        the appUI which fits the browsers preferences
      • getLocStringFromLanguage

        private java.lang.String getLocStringFromLanguage​(java.lang.String lang)
        Get locale string from language which may have qvalue set
        Parameters:
        lang - language string to parse
        Returns:
        stripped language string to use in matching
      • translationAvailable

        private int translationAvailable​(java.lang.String lang)
        Check if the required translation is available
        Parameters:
        lang - language we are looking for
        Returns:
        index into language array if found, -1 otherwise;
      • fixLanguageCode

        private java.lang.String fixLanguageCode​(java.lang.String lang)
        Fix the language code, as some browsers send then in a bad format (for instance, Firefox sends en-us instead of en_US).
        Parameters:
        lang - language to be fixed
        Returns:
        fixed version of the language, with _ separating parts and country in upper case
      • fixLanguageCode

        private java.lang.String fixLanguageCode​(java.lang.String lang,
                                                 int index)
      • getHtmlLabelledMessageInstance

        private java.lang.String getHtmlLabelledMessageInstance​(LocalizedResource localUtil,
                                                                java.lang.String key,
                                                                java.lang.String id)
        get an HTML labelled message from the resource bundle file, according to the given key.
      • printAsContentHeader

        private void printAsContentHeader​(java.lang.String str,
                                          java.io.PrintWriter out)
        Print the received string as a header.
        Parameters:
        str - The string to be printed as a header.
        out - Form PrintWriter
      • escapeSingleQuotes

        private java.lang.String escapeSingleQuotes​(java.lang.String str)
        If the received string has one or more single quotes in it, replace each one with the HTML escape-code for a single quote (apostrophe) so that the string can be properly displayed on a submit button.
        Parameters:
        str - The string in which we want to escape single quotes.
      • escapeHTML

        private java.lang.String escapeHTML​(java.lang.String str)
        Escapes potentially dangerous characters in data written to the browser.

        NOTE: This is a poor mans implementation - it doesn't protect against all kinds of attacks, and it cannot be used in all contexts.

        Parameters:
        str - the string to escape
        Returns:
        A sanitized string.
      • isServerStarted

        private static boolean isServerStarted​(NetworkServerControl server,
                                               int ntries)