Exemplo n.º 1
0
  protected void doError(Request request, Response response, OAuthProblemException exception) {
    if (OAuth2Utils.ParameterLocation.HTTP_HEADER.equals(parameterLocation)) {
      if (!isOptional()) {
        if (isRechallenging()) {
          boolean loggable =
              response.getRequest().isLoggable() && getLogger().isLoggable(Level.FINE);

          if (loggable) {
            getLogger().log(Level.FINE, "An authentication challenge was requested.");
          }

          response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
          response
              .getChallengeRequests()
              .add(
                  getTokenVerifier()
                      .getTokenExtractor()
                      .createChallengeRequest(getRealm(), exception));
          response.setEntity(new JacksonRepresentation<Map>(exception.getErrorMessage()));
        } else {
          forbid(response);
        }
      }
    } else {
      response.setStatus(exception.getStatus());
      response.setEntity(new JacksonRepresentation<Map>(exception.getErrorMessage()));
    }
  }
  @Override
  public void challenge(Response response, boolean stale) {
    // Load the FreeMarker template
    Representation ftl =
        new ClientResource(
                LocalReference.createClapReference(getClass().getPackage()) + "/Login.ftl")
            .get();

    // Wraps the bean with a FreeMarker representation
    response.setEntity(
        new TemplateRepresentation(
            ftl, response.getRequest().getResourceRef(), MediaType.TEXT_HTML));
    response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
  }
  @Override
  public void onMessageCompleted(boolean endDetected) throws IOException {
    Response message = getMessage();

    if (message != null) {
      Request request = message.getRequest();

      if (request.getOnSent() != null) {
        request.getOnSent().handle(request, message);
      }
    }

    super.onMessageCompleted(endDetected);
  }
  public boolean write(final Object result, final Response response) throws ResourceException {
    MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
    if (MediaType.APPLICATION_JSON.equals(type)) {
      if (result instanceof String || result instanceof Number || result instanceof Boolean) {
        StringRepresentation representation =
            new StringRepresentation(result.toString(), MediaType.APPLICATION_JSON);

        response.setEntity(representation);

        return true;
      }
    }

    return false;
  }
  @Override
  public void onHeadersCompleted() throws IOException {
    Response message = getMessage();

    if (message != null) {
      Request request = message.getRequest();

      if (request.isExpectingResponse()) {
        Queue<Response> inboundMessages =
            ((HttpClientInboundWay) getConnection().getInboundWay()).getMessages();
        inboundMessages.add(message);
        getConnection().getInboundWay().setMessageState(MessageState.START);
      }
    }

    super.onHeadersCompleted();
  }
  private Request createChallengeResponse(Response resourceNeedsAuth) {
    ChallengeResponse cResponse = null;

    for (ChallengeRequest challengeRequest : resourceNeedsAuth.getChallengeRequests()) {
      ChallengeScheme cs = challengeRequest.getScheme();

      if (ChallengeScheme.HTTP_BASIC.equals(cs)) {
        if (log.isLoggable(Level.INFO)) {
          log.info("Received 401 Error -> retrying request with HTTP_BASIC AUTH now!");
        }
        cResponse = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, getUsername(), getPassword());
        break;
      }
    }

    if (cResponse != null) {
      Request authenticatedRequest = resourceNeedsAuth.getRequest();
      authenticatedRequest.setChallengeResponse(cResponse);
      return authenticatedRequest;
    }

    throw new IllegalStateException(
        new AuthenticationException("Unable to determine required authentication scheme!"));
  }