Exemplo n.º 1
0
  /**
   * Standard way of initializing this filter. Map config parameters onto bean properties of this
   * filter, and invoke subclass initialization.
   *
   * @param filterConfig the configuration for this filter
   * @throws ServletException if bean properties are invalid (or required properties are missing),
   *     or if subclass initialization fails.
   * @see #initFilterBean
   */
  public final void init(FilterConfig filterConfig) throws ServletException {
    Assert.notNull(filterConfig, "FilterConfig must not be null");
    if (logger.isDebugEnabled()) {
      logger.debug("Initializing filter '" + filterConfig.getFilterName() + "'");
    }

    this.filterConfig = filterConfig;

    // Set bean properties from init parameters.
    try {
      PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
      BeanWrapper bw = new BeanWrapperImpl(this);
      ResourceLoader resourceLoader =
          new ServletContextResourceLoader(filterConfig.getServletContext());
      bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader));
      initBeanWrapper(bw);
      bw.setPropertyValues(pvs, true);
    } catch (BeansException ex) {
      String msg =
          "Failed to set bean properties on filter '"
              + filterConfig.getFilterName()
              + "': "
              + ex.getMessage();
      logger.error(msg, ex);
      throw new NestedServletException(msg, ex);
    }

    // Let subclasses do whatever initialization they like.
    initFilterBean();

    if (logger.isDebugEnabled()) {
      logger.debug("Filter '" + filterConfig.getFilterName() + "' configured successfully");
    }
  }
Exemplo n.º 2
0
 /**
  * Gets the filter name during the initialization
  *
  * @param joinPoint the join point to be proceed
  * @return the proceeded outcome of the join point
  * @throws Throwable if something went wrong
  */
 @Around("execution(* org.apache.wicket.protocol.http.WicketFilter.init(..))")
 public Object aroundInit(ProceedingJoinPoint joinPoint) throws Throwable {
   Object[] args = joinPoint.getArgs();
   String filterName = null;
   if (args.length == 2) {
     FilterConfig filterConfig = (FilterConfig) args[1];
     filterName = filterConfig.getFilterName();
   } else {
     FilterConfig filterConfig = (FilterConfig) args[0];
     filterName = filterConfig.getFilterName();
   }
   WicketMetrics.setFilterName(filterName);
   return joinPoint.proceed();
 }
Exemplo n.º 3
0
  public void init(FilterConfig filterConfig) {
    int max_priority = __DEFAULT_MAX_PRIORITY;
    if (filterConfig.getInitParameter(MAX_PRIORITY_INIT_PARAM) != null)
      max_priority = Integer.parseInt(filterConfig.getInitParameter(MAX_PRIORITY_INIT_PARAM));
    _queues = new Queue[max_priority + 1];
    _listeners = new AsyncListener[_queues.length];
    for (int p = 0; p < _queues.length; ++p) {
      _queues[p] = new ConcurrentLinkedQueue<>();
      _listeners[p] = new QoSAsyncListener(p);
    }

    int maxRequests = __DEFAULT_PASSES;
    if (filterConfig.getInitParameter(MAX_REQUESTS_INIT_PARAM) != null)
      maxRequests = Integer.parseInt(filterConfig.getInitParameter(MAX_REQUESTS_INIT_PARAM));
    _passes = new Semaphore(maxRequests, true);
    _maxRequests = maxRequests;

    long wait = __DEFAULT_WAIT_MS;
    if (filterConfig.getInitParameter(MAX_WAIT_INIT_PARAM) != null)
      wait = Integer.parseInt(filterConfig.getInitParameter(MAX_WAIT_INIT_PARAM));
    _waitMs = wait;

    long suspend = __DEFAULT_TIMEOUT_MS;
    if (filterConfig.getInitParameter(SUSPEND_INIT_PARAM) != null)
      suspend = Integer.parseInt(filterConfig.getInitParameter(SUSPEND_INIT_PARAM));
    _suspendMs = suspend;

    ServletContext context = filterConfig.getServletContext();
    if (context != null
        && Boolean.parseBoolean(filterConfig.getInitParameter(MANAGED_ATTR_INIT_PARAM)))
      context.setAttribute(filterConfig.getFilterName(), this);
  }
Exemplo n.º 4
0
    /**
     * Create new FilterConfigPropertyValues.
     *
     * @param config FilterConfig we'll use to take PropertyValues from
     * @param requiredProperties set of property names we need, where we can't accept default values
     * @throws ServletException if any required properties are missing
     */
    public FilterConfigPropertyValues(FilterConfig config, Set requiredProperties)
        throws ServletException {

      Set missingProps =
          (requiredProperties != null && !requiredProperties.isEmpty())
              ? new HashSet(requiredProperties)
              : null;

      Enumeration en = config.getInitParameterNames();
      while (en.hasMoreElements()) {
        String property = (String) en.nextElement();
        Object value = config.getInitParameter(property);
        addPropertyValue(new PropertyValue(property, value));
        if (missingProps != null) {
          missingProps.remove(property);
        }
      }

      // Fail if we are still missing properties.
      if (missingProps != null && missingProps.size() > 0) {
        throw new ServletException(
            "Initialization from FilterConfig for filter '"
                + config.getFilterName()
                + "' failed; the following required properties were missing: "
                + StringUtils.collectionToDelimitedString(missingProps, ", "));
      }
    }
Exemplo n.º 5
0
 public String getFilterName() {
   if (servletConfig != null) {
     return servletConfig.getServletName();
   } else {
     return config.getFilterName();
   }
 }
Exemplo n.º 6
0
  private void initFilterPathFromWebXml(FilterConfig filterConfig) {
    String filterName = filterConfig.getFilterName();
    FilterRegistration filterRegistration =
        filterConfig.getServletContext().getFilterRegistration(filterName);
    Collection<String> mappings = filterRegistration.getUrlPatternMappings();
    int size = mappings.size();

    if (size > 1) {
      throw new PippoRuntimeException(
          "Expected one filter path for '{}' but found multiple", filterName);
    }

    if (size == 1) {
      String urlPattern = mappings.iterator().next();
      initFilterPath(urlPattern);
    }
  }
  @Override
  public HttpServletResponse getWrappedHttpServletResponse(
      HttpServletRequest request, HttpServletResponse response) {

    if (!ServerDetector.isWebLogic()) {
      ServletContext servletContext = request.getServletContext();

      InvokerFilterHelper invokerFilterHelper =
          (InvokerFilterHelper) servletContext.getAttribute(InvokerFilterHelper.class.getName());

      FilterConfig filterConfig = getFilterConfig();

      invokerFilterHelper.unregisterFilterMappings(filterConfig.getFilterName());
    }

    if (isWrap(response)) {
      return new WebLogicIncludeServletResponse(response);
    }

    return response;
  }
Exemplo n.º 8
0
  /**
   * Servlets and Filters are treated essentially the same with Wicket. This is the entry point for
   * both of them.
   *
   * @see #init(FilterConfig)
   * @param isServlet True if Servlet, false if Filter
   * @param filterConfig
   * @throws ServletException
   */
  public void init(final boolean isServlet, final FilterConfig filterConfig)
      throws ServletException {
    this.filterConfig = filterConfig;
    this.isServlet = isServlet;
    initIgnorePaths(filterConfig);

    final ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader();
    final ClassLoader newClassLoader = getClassLoader();
    try {
      if (previousClassLoader != newClassLoader) {
        Thread.currentThread().setContextClassLoader(newClassLoader);
      }

      // locate application instance unless it was already specified during construction
      if (application == null) {
        applicationFactory = getApplicationFactory();
        application = applicationFactory.createApplication(this);
      }

      application.setName(filterConfig.getFilterName());
      application.setWicketFilter(this);

      // Allow the filterPath to be preset via setFilterPath()
      String configureFilterPath = getFilterPath();

      if (configureFilterPath == null) {
        configureFilterPath = getFilterPathFromConfig(filterConfig);

        if (configureFilterPath == null) {
          configureFilterPath = getFilterPathFromWebXml(isServlet, filterConfig);

          if (configureFilterPath == null) {
            configureFilterPath = getFilterPathFromAnnotation(isServlet);
          }
        }

        if (configureFilterPath != null) {
          setFilterPath(configureFilterPath);
        }
      }

      if (getFilterPath() == null) {
        log.warn(
            "Unable to determine filter path from filter init-param, web.xml, "
                + "or servlet 3.0 annotations. Assuming user will set filter path "
                + "manually by calling setFilterPath(String)");
      }

      ThreadContext.setApplication(application);
      try {
        application.initApplication();

        // Give the application the option to log that it is started
        application.logStarted();
      } finally {
        ThreadContext.detach();
      }
    } finally {
      if (newClassLoader != previousClassLoader) {
        Thread.currentThread().setContextClassLoader(previousClassLoader);
      }
    }
  }
Exemplo n.º 9
0
 public String getServletName() {
   return filterConfig.getFilterName();
 }
Exemplo n.º 10
0
 @Override
 public void init(FilterConfig filterConfig) throws ServletException {
   LOGGER.warning("init() " + filterConfig.getFilterName());
 }
Exemplo n.º 11
0
 public String getFilterName() {
   return initialConfig.getFilterName();
 }
Exemplo n.º 12
0
 @Override
 public void init(FilterConfig filterConfig) throws ServletException {
   this.filterConfig = filterConfig;
   if (logger.isDebugEnabled())
     logger.debug(filterConfig.getFilterName() + " filter loaded successfully.");
 }
Exemplo n.º 13
0
  public void init(FilterConfig filterConfig) throws ServletException {
    _queues = new Queue[getMaxPriority() + 1];
    _listeners = new AsyncListener[_queues.length];
    for (int p = 0; p < _queues.length; p++) {
      _queues[p] = new ConcurrentLinkedQueue<>();
      _listeners[p] = new DoSAsyncListener(p);
    }

    _rateTrackers.clear();

    int maxRequests = __DEFAULT_MAX_REQUESTS_PER_SEC;
    String parameter = filterConfig.getInitParameter(MAX_REQUESTS_PER_S_INIT_PARAM);
    if (parameter != null) maxRequests = Integer.parseInt(parameter);
    setMaxRequestsPerSec(maxRequests);

    long delay = __DEFAULT_DELAY_MS;
    parameter = filterConfig.getInitParameter(DELAY_MS_INIT_PARAM);
    if (parameter != null) delay = Long.parseLong(parameter);
    setDelayMs(delay);

    int throttledRequests = __DEFAULT_THROTTLE;
    parameter = filterConfig.getInitParameter(THROTTLED_REQUESTS_INIT_PARAM);
    if (parameter != null) throttledRequests = Integer.parseInt(parameter);
    setThrottledRequests(throttledRequests);

    long maxWait = __DEFAULT_MAX_WAIT_MS;
    parameter = filterConfig.getInitParameter(MAX_WAIT_INIT_PARAM);
    if (parameter != null) maxWait = Long.parseLong(parameter);
    setMaxWaitMs(maxWait);

    long throttle = __DEFAULT_THROTTLE_MS;
    parameter = filterConfig.getInitParameter(THROTTLE_MS_INIT_PARAM);
    if (parameter != null) throttle = Long.parseLong(parameter);
    setThrottleMs(throttle);

    long maxRequestMs = __DEFAULT_MAX_REQUEST_MS_INIT_PARAM;
    parameter = filterConfig.getInitParameter(MAX_REQUEST_MS_INIT_PARAM);
    if (parameter != null) maxRequestMs = Long.parseLong(parameter);
    setMaxRequestMs(maxRequestMs);

    long maxIdleTrackerMs = __DEFAULT_MAX_IDLE_TRACKER_MS_INIT_PARAM;
    parameter = filterConfig.getInitParameter(MAX_IDLE_TRACKER_MS_INIT_PARAM);
    if (parameter != null) maxIdleTrackerMs = Long.parseLong(parameter);
    setMaxIdleTrackerMs(maxIdleTrackerMs);

    String whiteList = "";
    parameter = filterConfig.getInitParameter(IP_WHITELIST_INIT_PARAM);
    if (parameter != null) whiteList = parameter;
    setWhitelist(whiteList);

    parameter = filterConfig.getInitParameter(INSERT_HEADERS_INIT_PARAM);
    setInsertHeaders(parameter == null || Boolean.parseBoolean(parameter));

    parameter = filterConfig.getInitParameter(TRACK_SESSIONS_INIT_PARAM);
    setTrackSessions(parameter == null || Boolean.parseBoolean(parameter));

    parameter = filterConfig.getInitParameter(REMOTE_PORT_INIT_PARAM);
    setRemotePort(parameter != null && Boolean.parseBoolean(parameter));

    parameter = filterConfig.getInitParameter(ENABLED_INIT_PARAM);
    setEnabled(parameter == null || Boolean.parseBoolean(parameter));

    parameter = filterConfig.getInitParameter(TOO_MANY_CODE);
    setTooManyCode(parameter == null ? 429 : Integer.parseInt(parameter));

    _scheduler = startScheduler();

    ServletContext context = filterConfig.getServletContext();
    if (context != null
        && Boolean.parseBoolean(filterConfig.getInitParameter(MANAGED_ATTR_INIT_PARAM)))
      context.setAttribute(filterConfig.getFilterName(), this);
  }
Exemplo n.º 14
0
 /*
  * (non-Javadoc)
  *
  * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
  */
 public void init(final FilterConfig config) throws ServletException {
   log.info("init: " + config.getFilterName());
 }