コード例 #1
0
ファイル: EnergyAtHome.java プロジェクト: elifesy/jemma
  protected synchronized void setHttpService(HttpService s) {
    ahHttpAdapter = new HttpAhBinder();

    this.setRootUrl(applicationWebAlias);

    Servlet customJsonServlet = new CustomJsonServlet(ahHttpAdapter, "");
    Servlet jsonRPC = new JsonRPC(ahHttpAdapter, "");

    this.registerResource("/", "webapp/ehdemo");
    this.registerResource("/gh", "webapp/gh");
    this.registerResource("/post-json", customJsonServlet);
    this.registerResource("/json-rpc", jsonRPC);
    this.registerResource(
        "/JSON-RPC",
        new JSONRPCServlet() {
          public void init(ServletConfig config) throws ServletException {
            super.init(new ServletConfigDisableGZIPWrapper(config));
          }
        });

    this.setHttpContext(this);

    this.update(null);
    super.bindHttpService(s);
  }
コード例 #2
0
 /**
  * Map a web application exception to a response.
  *
  * @param e the web application exception.
  */
 public void mapWebApplicationException(WebApplicationException e) {
   if (e.getResponse().getEntity() != null) {
     wa.getResponseListener().onError(Thread.currentThread().getId(), e);
     onException(e, e.getResponse(), false);
   } else {
     if (!mapException(e)) {
       onException(e, e.getResponse(), false);
     }
   }
 }
コード例 #3
0
  /**
   * Map an exception to a response.
   *
   * @param e the exception.
   * @return true if the exception was mapped, otherwise false.
   */
  public boolean mapException(Throwable e) {
    ExceptionMapper em = wa.getExceptionMapperContext().find(e.getClass());
    if (em == null) {
      wa.getResponseListener().onError(Thread.currentThread().getId(), e);

      return false;
    }

    wa.getResponseListener().onMappedException(Thread.currentThread().getId(), e, em);

    if (request.isTracingEnabled()) {
      request.trace(
          String.format(
              "matched exception mapper: %s -> %s",
              ReflectionHelper.objectToString(e), ReflectionHelper.objectToString(em)));
    }

    try {
      Response r = em.toResponse(e);
      if (r == null) r = Response.noContent().build();
      onException(e, r, true);
    } catch (MappableContainerException ex) {
      // If the exception mapper throws a MappableContainerException then
      // rethrow it to the HTTP container
      throw ex;
    } catch (RuntimeException ex) {
      LOGGER.severe(
          "Exception mapper "
              + em
              + " for Throwable "
              + e
              + " threw a RuntimeException when "
              + "attempting to obtain the response");
      Response r = Response.serverError().build();
      onException(ex, r, false);
    }
    return true;
  }
コード例 #4
0
  /** @see javax.servlet.Filter#destroy() */
  @Override
  public void destroy() {
    if (application != null) {
      try {
        ThreadContext.setApplication(application);
        application.internalDestroy();
      } finally {
        ThreadContext.detach();
        application = null;
      }
    }

    if (applicationFactory != null) {
      applicationFactory.destroy(this);
    }
  }
コード例 #5
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);
      }
    }
  }
コード例 #6
0
  /**
   * This is Wicket's main method to execute a request
   *
   * @param request
   * @param response
   * @param chain
   * @return false, if the request could not be processed
   * @throws IOException
   * @throws ServletException
   */
  boolean processRequest(
      ServletRequest request, final ServletResponse response, final FilterChain chain)
      throws IOException, ServletException {
    final ThreadContext previousThreadContext = ThreadContext.detach();

    // Assume we are able to handle the request
    boolean res = true;

    final ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader();
    final ClassLoader newClassLoader = getClassLoader();

    try {
      if (previousClassLoader != newClassLoader) {
        Thread.currentThread().setContextClassLoader(newClassLoader);
      }

      HttpServletRequest httpServletRequest = (HttpServletRequest) request;
      HttpServletResponse httpServletResponse = (HttpServletResponse) response;

      // Make sure getFilterPath() gets called before checkIfRedirectRequired()
      String filterPath = getFilterPath(httpServletRequest);

      if (filterPath == null) {
        throw new IllegalStateException("filter path was not configured");
      }

      if (shouldIgnorePath(httpServletRequest)) {
        log.debug("Ignoring request {}", httpServletRequest.getRequestURL());
        if (chain != null) {
          chain.doFilter(request, response);
        }
        return false;
      }

      String redirectURL = checkIfRedirectRequired(httpServletRequest);
      if (redirectURL == null) {
        // No redirect; process the request
        ThreadContext.setApplication(application);

        WebRequest webRequest = application.createWebRequest(httpServletRequest, filterPath);
        WebResponse webResponse = application.createWebResponse(webRequest, httpServletResponse);

        RequestCycle requestCycle = application.createRequestCycle(webRequest, webResponse);
        if (!requestCycle.processRequestAndDetach()) {
          if (chain != null) {
            chain.doFilter(request, response);
          }
          res = false;
        } else {
          webResponse.flush();
        }
      } else {
        if (Strings.isEmpty(httpServletRequest.getQueryString()) == false) {
          redirectURL += "?" + httpServletRequest.getQueryString();
        }

        try {
          // send redirect - this will discard POST parameters if the request is POST
          // - still better than getting an error because of lacking trailing slash
          httpServletResponse.sendRedirect(httpServletResponse.encodeRedirectURL(redirectURL));
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    } finally {
      ThreadContext.restore(previousThreadContext);

      if (newClassLoader != previousClassLoader) {
        Thread.currentThread().setContextClassLoader(previousClassLoader);
      }

      if (response.isCommitted()) {
        response.flushBuffer();
      }
    }
    return res;
  }
コード例 #7
0
 /**
  * Get the message body workers.
  *
  * @return the message body workers.
  */
 public MessageBodyWorkers getMessageBodyWorkers() {
   return wa.getMessageBodyWorkers();
 }