Java Code Examples for play.mvc.Http#RequestHeader

The following examples show how to use play.mvc.Http#RequestHeader . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ErrorHandler.java    From htwplus with MIT License 6 votes vote down vote up
protected CompletionStage<Result> onOtherClientError(Http.RequestHeader request, int errorCode, String message) {
    jpaApi.withTransaction(() -> {
        Group group = groupManager.findByTitle(configuration.getString("htwplus.admin.group"));
        if (group != null) {
            Post post = new Post();
            post.content = "Request: " + request + "\nError: " + errorCode + " - " + message;
            post.owner = accountManager.findByEmail(configuration.getString("htwplus.admin.mail"));
            post.group = group;
            postManager.createWithoutIndex(post);
        }
    });

    if (errorCode == Http.Status.REQUEST_ENTITY_TOO_LARGE) {
        return CompletableFuture.completedFuture(Results.status(errorCode, "Es sind maximal "+ MAX_FILESIZE + " MByte pro Datei möglich."));
    }

    return CompletableFuture.completedFuture(Results.status(errorCode, badRequest.render(request.method(), request.uri(), message)));
}
 
Example 2
Source File: CustomGzipFilter.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
private static boolean shouldGzipFunction(Http.RequestHeader requestHeader, Result responseHeader) {
    double responseSize = 0.0;
    boolean responseLengthKeyExist = responseHeader.headers().containsKey(HeaderParam.X_Response_Length.getName());
    if (responseLengthKeyExist) {
        if (responseHeader.headers().get(HeaderParam.X_Response_Length.getName()) != null) {
            String strValue = responseHeader.headers().get(HeaderParam.X_Response_Length.getName());
            responseSize = Double.parseDouble(strValue);
        }
    }
    if (GzipFilterEnabled && (requestHeader.header(HttpHeaders.ACCEPT_ENCODING) != null)) {
        if (requestHeader.header(HttpHeaders.ACCEPT_ENCODING).toString().toLowerCase().contains(GZIP)) {
            if (responseSize >= gzipThreshold) {
                return true;
            }
        }
    }
    return false;
}
 
Example 3
Source File: ErrorHandler.java    From activator-lagom-cargotracker with Apache License 2.0 6 votes vote down vote up
/**
 * Invoked in dev mode when a server error occurs.
 *
 * @param request The request that triggered the error.
 * @param exception The exception.
 */
protected CompletionStage<Result> onDevServerError(Http.RequestHeader request, UsefulException exception) {

    ObjectNode jsonError = Json.newObject();

    final Throwable cause = exception.cause;
    final String description = exception.description;
    final String id = exception.id;
    final String title = exception.title;

    jsonError.put("description", description);
    jsonError.put("title", title);
    jsonError.put("id", id);
    jsonError.put("message", exception.getMessage());
    jsonError.set("cause", causesToJson(cause));

    return CompletableFuture.completedFuture(Results.internalServerError(jsonError));
}
 
Example 4
Source File: HttpAuthenticationFilter.java    From commercetools-sunrise-java with Apache License 2.0 5 votes vote down vote up
@Override
public CompletionStage<Result> apply(final Function<Http.RequestHeader, CompletionStage<Result>> nextFilter,
                                     final Http.RequestHeader requestHeader) {
    if (httpAuthentication != null && httpAuthentication.isEnabled()) {
        return authenticate(nextFilter, requestHeader, httpAuthentication);
    } else {
        return nextFilter.apply(requestHeader);
    }
}
 
Example 5
Source File: ErrorHandler.java    From htwplus with MIT License 5 votes vote down vote up
protected CompletionStage<Result> onBadRequest(Http.RequestHeader request, String message) {
    jpaApi.withTransaction(() -> {
        Group group = groupManager.findByTitle(configuration.getString("htwplus.admin.group"));
        if (group != null) {
            Post post = new Post();
            post.content = "Request: " + request + "\nError: 400 - Bad Request (" + message + ")";
            post.owner = accountManager.findByEmail(configuration.getString("htwplus.admin.mail"));
            post.group = group;
            postManager.createWithoutIndex(post);
        }
    });

    return CompletableFuture.completedFuture(Results.redirect(controllers.routes.Application.index()));
}
 
Example 6
Source File: ErrorHandler.java    From htwplus with MIT License 5 votes vote down vote up
protected CompletionStage<Result> onForbidden(Http.RequestHeader request, String message) {
    jpaApi.withTransaction(() -> {
        Group group = groupManager.findByTitle(configuration.getString("htwplus.admin.group"));
        if (group != null) {
            Post post = new Post();
            post.content = "Request: " + request + "\nError: 403 - Forbidden (" + message + ")";
            post.owner = accountManager.findByEmail(configuration.getString("htwplus.admin.mail"));
            post.group = group;
            postManager.createWithoutIndex(post);
        }
    });

    return CompletableFuture.completedFuture(Results.redirect(controllers.routes.Application.index()));
}
 
Example 7
Source File: ErrorHandler.java    From htwplus with MIT License 5 votes vote down vote up
protected CompletionStage<Result> onProdServerError(Http.RequestHeader request, UsefulException exception) {
    jpaApi.withTransaction(() -> {
        Group group = groupManager.findByTitle(configuration.getString("htwplus.admin.group"));
        if (group != null) {
            Post post = new Post();
            post.content = "Request: " + request + "\nError: " + exception;
            post.owner = accountManager.findByEmail(configuration.getString("htwplus.admin.mail"));
            post.group = group;
            postManager.createWithoutIndex(post);
        }
    });

    return CompletableFuture.completedFuture(Results.redirect(controllers.routes.Application.error()));
}
 
Example 8
Source File: SunriseDefaultHttpErrorHandler.java    From commercetools-sunrise-java with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletionStage<Result> onDevServerError(final Http.RequestHeader request, final UsefulException exception) {
    return Optional.ofNullable(exception.getCause())
            .map(Throwable::getCause)
            .filter(e -> e instanceof ProvisionException)
            .map(e -> (ProvisionException) e)
            .filter(e -> e.getErrorMessages().stream()
                    .anyMatch(m -> m.getCause() instanceof SphereClientCredentialsException))
            .map(e -> renderDevErrorPage(exception))
            .orElseGet(() ->  super.onDevServerError(request, exception));
}
 
Example 9
Source File: HttpAuthenticationFilter.java    From commercetools-sunrise-java with Apache License 2.0 5 votes vote down vote up
private CompletionStage<Result> authenticate(final Function<Http.RequestHeader, CompletionStage<Result>> nextFilter,
                                             final Http.RequestHeader requestHeader, final HttpAuthentication httpAuthentication) {
    return findAuthorizationHeader(requestHeader)
            .map(authorizationHeader -> {
                if (httpAuthentication.isAuthorized(authorizationHeader)) {
                    return successfulAuthentication(nextFilter, requestHeader);
                } else {
                    return failedAuthentication();
                }
            }).orElseGet(() -> missingAuthentication(httpAuthentication));
}
 
Example 10
Source File: CustomHttpErrorHandler.java    From reactive-stock-trader with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletionStage<Result> onDevServerError(Http.RequestHeader request, UsefulException exception) {
    if (exception.cause instanceof NotFound || exception.cause instanceof CompletionException && exception.cause.getCause() instanceof NotFound) {
        return CompletableFuture.completedFuture(Results.notFound());
    } else if (exception.cause instanceof TransportException) {
        // TODO Pull out the Lagom HTTP message and return that instead of double wrapped
        return super.onDevServerError(request, exception);
    } else {
        return super.onDevServerError(request, exception);
    }
}
 
Example 11
Source File: TracingFilterTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Test
public void testStatusCodeIsNotOk() throws Exception {
    TracingFilter filter = new TracingFilter(materializer);
    Function<Http.RequestHeader, CompletionStage<Result>> next = requestHeader -> CompletableFuture.supplyAsync(() -> badRequest("Hello"));
    CompletionStage<Result> result = filter.apply(next, request);
    result.toCompletableFuture().get();
    assertThat(segmentStorage.getTraceSegments().size(), is(1));
    TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
    List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
    assertHttpSpan(spans.get(0));
    assertThat(SpanHelper.getErrorOccurred(spans.get(0)), is(true));
}
 
Example 12
Source File: TracingFilterTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Test
public void testStatusCodeIsOk() throws Exception {
    TracingFilter filter = new TracingFilter(materializer);
    Function<Http.RequestHeader, CompletionStage<Result>> next = requestHeader -> CompletableFuture.supplyAsync(() -> ok("Hello"));
    CompletionStage<Result> result = filter.apply(next, request);
    result.toCompletableFuture().get();
    assertThat(segmentStorage.getTraceSegments().size(), is(1));
    TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
    List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
    assertHttpSpan(spans.get(0));
}
 
Example 13
Source File: ErrorHandler.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
@Override
public CompletionStage<Result> onServerError(Http.RequestHeader request, Throwable t) {
  ProjectLogger.log(
      "Global: onError called for path = "
          + request.path()
          + ", headers = "
          + request.getHeaders().toMap(),
      t);
  Response response = null;
  ProjectCommonException commonException = null;
  if (t instanceof ProjectCommonException) {
    commonException = (ProjectCommonException) t;
    response =
        BaseController.createResponseOnException(
            request.path(), request.method(), (ProjectCommonException) t);
  } else if (t instanceof akka.pattern.AskTimeoutException) {
    commonException =
        new ProjectCommonException(
            ResponseCode.actorConnectionError.getErrorCode(),
            ResponseCode.actorConnectionError.getErrorMessage(),
            ResponseCode.SERVER_ERROR.getResponseCode());
  } else {
    commonException =
        new ProjectCommonException(
            ResponseCode.internalError.getErrorCode(),
            ResponseCode.internalError.getErrorMessage(),
            ResponseCode.SERVER_ERROR.getResponseCode());
  }
  response =
      BaseController.createResponseOnException(request.path(), request.method(), commonException);
  return CompletableFuture.completedFuture(Results.internalServerError(Json.toJson(response)));
}
 
Example 14
Source File: HttpAuthenticationFilter.java    From commercetools-sunrise-java with Apache License 2.0 4 votes vote down vote up
private CompletionStage<Result> successfulAuthentication(final Function<Http.RequestHeader, CompletionStage<Result>> nextFilter,
                                                         final Http.RequestHeader requestHeader) {
    LOGGER.debug("Authorized");
    return nextFilter.apply(requestHeader);
}
 
Example 15
Source File: HttpAuthenticationFilter.java    From commercetools-sunrise-java with Apache License 2.0 4 votes vote down vote up
private Optional<String> findAuthorizationHeader(final Http.RequestHeader requestHeader) {
    return Optional.ofNullable(requestHeader.getHeader(AUTHORIZATION));
}
 
Example 16
Source File: TracingFilterTest.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Override
public Http.RequestHeader removeAttr(TypedKey<?> typedKey) {
    return null;
}
 
Example 17
Source File: TracingFilterTest.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Override
public <A> Http.RequestHeader addAttr(TypedKey<A> typedKey, A a) {
    return null;
}
 
Example 18
Source File: TracingFilterTest.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Override
public Http.RequestHeader withAttrs(TypedMap typedMap) {
    return null;
}