예제 #1
0
  /** Add Tomcat-style valve. */
  public synchronized void addValve(Valve valve) {

    /*
     * Check if this is a GlassFish-style valve that was compiled
     * against the old org.apache.catalina.Valve interface (from
     * GlassFish releases prior to V3), which has since been renamed
     * to org.glassfish.web.valve.GlassFishValve (in V3)
     */
    if (isGlassFishValve(valve)) {
      try {
        addValve(new GlassFishValveAdapter(valve));
      } catch (Exception e) {
        String msg = MessageFormat.format(rb.getString(ADD_TOMCAT_STYLE_VALVE_EXCEPTION), valve);
        log.log(Level.SEVERE, msg, e);
      }
      return;
    }

    if (valve instanceof Contained) ((Contained) valve).setContainer(this.container);

    // Start the new Valve if necessary
    if (started) {
      if (valve instanceof Lifecycle) {
        try {
          ((Lifecycle) valve).start();
        } catch (LifecycleException e) {
          log.log(Level.SEVERE, ADD_VALVE_EXCEPTION, e);
        }
      }
    }

    if (firstTcValve == null) {
      firstTcValve = lastTcValve = valve;
    } else {
      lastTcValve.setNext(valve);
      lastTcValve = valve;
    }

    if (basic != null) {
      valve.setNext((Valve) basic);
    }
  }
예제 #2
0
 /*
  * Checks if the give valve is a GlassFish-style valve that was compiled
  * against the old org.apache.catalina.Valve interface (from
  * GlassFish releases prior to V3), which has since been renamed
  * to org.glassfish.web.valve.GlassFishValve (in V3).
  *
  * The check is done by checking that it is not an abstract method with
  * return type int. Note that invoke method in the original Tomcat-based
  * Valve interface is declared to be void.
  *
  * @param valve the valve to check
  *
  * @return true if the given valve is a GlassFish-style valve, false
  * otherwise
  */
 private boolean isGlassFishValve(Valve valve) {
   try {
     Method m =
         valve
             .getClass()
             .getMethod(
                 "invoke", org.apache.catalina.Request.class, org.apache.catalina.Response.class);
     return (m != null
         && int.class.equals(m.getReturnType())
         && (!Modifier.isAbstract(m.getModifiers())));
   } catch (Exception e) {
     return false;
   }
 }
예제 #3
0
  private void doInvoke(Request request, Response response, boolean chaining)
      throws IOException, ServletException {

    if ((valves.length > 0) || (basic != null)) {
      // Set the status so that if there are no valves (other than the
      // basic one), the basic valve's request processing logic will
      // be invoked
      int status = GlassFishValve.INVOKE_NEXT;

      // Iterate over all the valves in the pipeline and invoke
      // each valve's processing logic and then move onto to the
      // next valve in the pipeline only if the previous valve indicated
      // that the pipeline should proceed.
      int i;
      for (i = 0; i < valves.length; i++) {
        Request req = request;
        Response resp = response;
        if (chaining) {
          req = getRequest(request);
          resp = getResponse(request, response);
        }
        status = valves[i].invoke(req, resp);
        if (status != GlassFishValve.INVOKE_NEXT) break;
      }

      // Save a reference to the valve[], to ensure that postInvoke()
      // is invoked on the original valve[], in case a valve gets added
      // or removed during the invocation of the basic valve (e.g.,
      // in case access logging is enabled or disabled by some kind of
      // admin servlet), in which case the indices used for postInvoke
      // invocations below would be off
      GlassFishValve[] savedValves = valves;

      // Invoke the basic valve's request processing and post-request
      // logic only if the pipeline was not aborted (i.e. no valve
      // returned END_PIPELINE).
      // In addition, the basic valve needs to be invoked by the
      // pipeline only if no Tomcat-style valves have been added.
      // Otherwise, it will be invoked by the last Tomcat-style valve
      // directly.
      if (status == GlassFishValve.INVOKE_NEXT) {
        if (firstTcValve != null) {
          firstTcValve.invoke(
              (org.apache.catalina.connector.Request) request,
              (org.apache.catalina.connector.Response) response);
        } else if (basic != null) {
          Request req = request;
          Response resp = response;
          if (chaining) {
            req = getRequest(request);
            resp = getResponse(request, response);
          }
          basic.invoke(req, resp);
          basic.postInvoke(req, resp);
        }
      }

      // Invoke the post-request processing logic only on those valves
      // that returned a status of INVOKE_NEXT
      for (int j = i - 1; j >= 0; j--) {
        Request req = request;
        Response resp = response;
        if (chaining) {
          req = getRequest(request);
          resp = getResponse(request, response);
        }

        savedValves[j].postInvoke(req, resp);
      }

      savedValves = null;

    } else {
      throw new ServletException(rb.getString(NO_VALVES_IN_PIPELINE_EXCEPTION));
    }

    // Calls the protocol handler's init method if the request is marked to be upgraded
    if (request instanceof org.apache.catalina.connector.Request) {
      org.apache.catalina.connector.Request req = (org.apache.catalina.connector.Request) request;
      if (req.isUpgrade()) {
        HttpUpgradeHandler handler = req.getHttpUpgradeHandler();
        if (handler != null) {
          WebConnectionImpl wc =
              new WebConnectionImpl(
                  req.getInputStream(),
                  ((org.apache.catalina.connector.Response) req.getResponse()).getOutputStream());
          wc.setRequest(req);
          req.setWebConnection(wc);
          if (response instanceof org.apache.catalina.connector.Response) {
            wc.setResponse((org.apache.catalina.connector.Response) response);
          }
          Context context = req.getContext();
          try {
            context.fireContainerEvent(ContainerEvent.BEFORE_UPGRADE_HANDLER_INITIALIZED, handler);
            handler.init(wc);
          } finally {
            context.fireContainerEvent(ContainerEvent.AFTER_UPGRADE_HANDLER_INITIALIZED, handler);
          }
        } else {
          log.log(Level.SEVERE, PROTOCOL_HANDLER_REQUIRED_EXCEPTION);
        }
        req.setUpgrade(false);
      }
    }
  }