Exemplo n.º 1
0
  /* ------------------------------------------------------------ */
  @Override
  public void setServer(Server server) {
    if (server == getServer()) return;

    if (isStarted()) throw new IllegalStateException(STARTED);

    super.setServer(server);
    Handler h = getHandler();
    if (h != null) h.setServer(server);
  }
Exemplo n.º 2
0
 /* ------------------------------------------------------------ */
 @Override
 public void destroy() {
   if (!isStopped()) throw new IllegalStateException("!STOPPED");
   Handler child = getHandler();
   if (child != null) {
     setHandler(null);
     child.destroy();
   }
   super.destroy();
 }
Exemplo n.º 3
0
 /* ------------------------------------------------------------ */
 @Override
 public void handle(
     String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
     throws IOException, ServletException {
   if (_handler != null && isStarted()) {
     _handler.handle(target, baseRequest, request, response);
   }
 }
Exemplo n.º 4
0
  /** @param handler Set the {@link Handler} which should be wrapped. */
  public void setHandler(Handler handler) {
    if (isStarted()) throw new IllegalStateException(STARTED);

    if (handler != null) handler.setServer(getServer());

    updateBean(_handler, handler);
    _handler = handler;
  }
  @Override
  public void handle(
      final String target,
      final Request baseRequest,
      final HttpServletRequest request,
      final HttpServletResponse response)
      throws IOException, ServletException {
    if (!isStarted()) {
      return;
    }

    final ContextModel matched = serverModel.matchPathToContext(target);
    if (matched != null) {
      // check for nulls and start complaining
      NullArgumentException.validateNotNull(
          matched.getHttpContext(), "The http Context of " + matched.getContextName() + " is null");
      NullArgumentException.validateNotNull(getServer(), "The server is null!");

      final ContextHandler context =
          ((JettyServerWrapper) getServer()).getContext(matched.getHttpContext());

      try {
        NullArgumentException.validateNotNull(context, "Found context is Null");
        context.handle(target, baseRequest, request, response);

        // CHECKSTYLE:OFF
      } catch (EofException e) {
        throw e;
      } catch (RuntimeException e) {
        throw e;
      } catch (Exception e) {
        throw new ServletException(e);
      }
      // CHECKSTYLE:ON

    }
    // now handle all other handlers
    for (Handler handler : getHandlers()) {
      if (matched != null && matchedContextEqualsHandler(matched, handler)) {
        continue;
      }
      handler.handle(target, baseRequest, request, response);
    }
  }
Exemplo n.º 6
0
  @Test
  public void testHandleGet() throws IOException, ServletException {
    Request baseRequest = createMock(Request.class);
    expect(baseRequest.getPathInfo()).andReturn("/abcdef");
    baseRequest.setHandled(true);
    HttpServletRequest request = createMock(HttpServletRequest.class);

    HttpServletResponse response = createMock(HttpServletResponse.class);
    response.setStatus(200);
    response.setContentType("text/html; charset=utf-8");
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    expect(response.getWriter()).andReturn(printWriter);
    response.flushBuffer();

    Handler traceHandler = new TemplateHandler(new TraceHandlerDelegate());

    replayAll();
    traceHandler.handle("/trace/abcdef", baseRequest, request, response);
    verifyAll();

    String expectedScriptTag = "<script src=\"/tracedata/abcdef?callback=onTraceLoaded\">";
    assertThat(stringWriter.toString(), containsString(expectedScriptTag));
  }
  /*
   * @see org.eclipse.jetty.server.Handler#handle(java.lang.String,
   *      javax.servlet.http.HttpServletRequest,
   *      javax.servlet.http.HttpServletResponse, int)
   */
  @Override
  public void handle(
      String pathInContext,
      Request baseRequest,
      HttpServletRequest request,
      HttpServletResponse response)
      throws IOException, ServletException {
    final Response base_response = baseRequest.getResponse();
    final Handler handler = getHandler();

    if (handler == null) return;

    final Authenticator authenticator = _authenticator;

    if (checkSecurity(baseRequest)) {
      // See Servlet Spec 3.1 sec 13.6.3
      if (authenticator != null) authenticator.prepareRequest(baseRequest);

      RoleInfo roleInfo = prepareConstraintInfo(pathInContext, baseRequest);

      // Check data constraints
      if (!checkUserDataPermissions(pathInContext, baseRequest, base_response, roleInfo)) {
        if (!baseRequest.isHandled()) {
          response.sendError(HttpServletResponse.SC_FORBIDDEN);
          baseRequest.setHandled(true);
        }
        return;
      }

      // is Auth mandatory?
      boolean isAuthMandatory = isAuthMandatory(baseRequest, base_response, roleInfo);

      if (isAuthMandatory && authenticator == null) {
        LOG.warn("No authenticator for: " + roleInfo);
        if (!baseRequest.isHandled()) {
          response.sendError(HttpServletResponse.SC_FORBIDDEN);
          baseRequest.setHandled(true);
        }
        return;
      }

      // check authentication
      Object previousIdentity = null;
      try {
        Authentication authentication = baseRequest.getAuthentication();
        if (authentication == null || authentication == Authentication.NOT_CHECKED)
          authentication =
              authenticator == null
                  ? Authentication.UNAUTHENTICATED
                  : authenticator.validateRequest(request, response, isAuthMandatory);

        if (authentication instanceof Authentication.Wrapped) {
          request = ((Authentication.Wrapped) authentication).getHttpServletRequest();
          response = ((Authentication.Wrapped) authentication).getHttpServletResponse();
        }

        if (authentication instanceof Authentication.ResponseSent) {
          baseRequest.setHandled(true);
        } else if (authentication instanceof Authentication.User) {
          Authentication.User userAuth = (Authentication.User) authentication;
          baseRequest.setAuthentication(authentication);
          if (_identityService != null)
            previousIdentity = _identityService.associate(userAuth.getUserIdentity());

          if (isAuthMandatory) {
            boolean authorized =
                checkWebResourcePermissions(
                    pathInContext,
                    baseRequest,
                    base_response,
                    roleInfo,
                    userAuth.getUserIdentity());
            if (!authorized) {
              response.sendError(HttpServletResponse.SC_FORBIDDEN, "!role");
              baseRequest.setHandled(true);
              return;
            }
          }

          handler.handle(pathInContext, baseRequest, request, response);
          if (authenticator != null)
            authenticator.secureResponse(request, response, isAuthMandatory, userAuth);
        } else if (authentication instanceof Authentication.Deferred) {
          DeferredAuthentication deferred = (DeferredAuthentication) authentication;
          baseRequest.setAuthentication(authentication);

          try {
            handler.handle(pathInContext, baseRequest, request, response);
          } finally {
            previousIdentity = deferred.getPreviousAssociation();
          }

          if (authenticator != null) {
            Authentication auth = baseRequest.getAuthentication();
            if (auth instanceof Authentication.User) {
              Authentication.User userAuth = (Authentication.User) auth;
              authenticator.secureResponse(request, response, isAuthMandatory, userAuth);
            } else authenticator.secureResponse(request, response, isAuthMandatory, null);
          }
        } else {
          baseRequest.setAuthentication(authentication);
          if (_identityService != null) previousIdentity = _identityService.associate(null);
          handler.handle(pathInContext, baseRequest, request, response);
          if (authenticator != null)
            authenticator.secureResponse(request, response, isAuthMandatory, null);
        }
      } catch (ServerAuthException e) {
        // jaspi 3.8.3 send HTTP 500 internal server error, with message
        // from AuthException
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
      } finally {
        if (_identityService != null) _identityService.disassociate(previousIdentity);
      }
    } else handler.handle(pathInContext, baseRequest, request, response);
  }