Class NetServlet

java.lang.Object
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
org.apache.derby.drda.NetServlet
All Implemented Interfaces:
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:
  • Field Details

  • Constructor Details

    • NetServlet

      public NetServlet()
  • Method Details

    • 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
      Parameters:
      config - the ServletConfig object that contains configutation information for this servlet
      Throws:
      javax.servlet.ServletException - if an exception occurs that interrupts the servlet's normal operation
      See Also:
      • UnavailableException
    • doGet

      public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException
      Get the form of NetServlet. Provides buttons and forms to control the Network server.
      Overrides:
      doGet in class javax.servlet.http.HttpServlet
      Parameters:
      request - an HttpServletRequest object that contains the request the client has made of the servlet
      response - an HttpServletResponse object that contains the response the servlet sends to the client
      Throws:
      javax.servlet.ServletException - if the request for the GET could not be handled
      IOException - if an input or output error is detected when the servlet handles the GET request
      See Also:
      • ServletResponse.setContentType(java.lang.String)
    • doPost

      public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException
      Get the form of NetServlet. Provides a buttons and form to control the Network server
      Overrides:
      doPost in class javax.servlet.http.HttpServlet
      Parameters:
      request - an HttpServletRequest object that contains the request the client has made of the servlet
      response - an HttpServletResponse object that contains the response the servlet sends to the client
      Throws:
      javax.servlet.ServletException - if the request for the POST could not be handled
      IOException - if an input or output error is detected when the servlet handles the request
      See Also:
      • ServletOutputStream
      • ServletResponse.setContentType(java.lang.String)
    • getForm

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

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

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

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

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

      private void runServer(LocalizedResource localUtil, String returnMessage, 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, Exception e, String returnMessage, 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, String msg, String returnMessage, PrintWriter out)
      Display an error form
      Parameters:
      localUtil - LocalizedResource to use to translate messages
      msg - String to be displayed
      returnMessage - localized continue message for continue button on error form
      out - Form PrintWriter
    • displayCurrentStatus

      private void displayCurrentStatus(LocalizedResource localUtil, String returnMessage, 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, String returnMessage, 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, String returnMessage, 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, String returnMessage, 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, String returnMessage, 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, String traceDirectory, String returnMessage, 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, String returnMessage, 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, String name, String fieldKey, LocalizedResource localUtil, String returnMessage, 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, PrintWriter out)
      Print Derby Network Server banner
    • getCurrentAppUI

      private LocalizedResource getCurrentAppUI(javax.servlet.http.HttpServletRequest request, 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 String getLocStringFromLanguage(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(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 String fixLanguageCode(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 String fixLanguageCode(String lang, int index)
    • getHtmlLabelledMessageInstance

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

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

      private String escapeSingleQuotes(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 String escapeHTML(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)