@Override
  public Promise<Result> call(Context ctx) throws Throwable {
    String accessToken = ctx.request().getHeader("x-access-token");
    if (accessToken != null) {
      // Check RateLimit
      Logger.debug("Checking for token RateLimit. token:" + accessToken);
      if (!RateLimitAgent.isRateOverLimit(accessToken)) {
        return Promise.<Result>pure(badRequest("Over RateLimit. Account temporaly suspended"));
      }
    } else {
      return Promise.<Result>pure(badRequest("Authentication need"));
    }

    return delegate.call(ctx);
  }
 /**
  * Create a new SysAdminUtilsImpl
  *
  * @param lifecycle the play application lifecycle listener
  * @param configuration the play application configuration
  * @param databaseDependencyService the service which secure the availability of the database
  * @param actorSystem the Akka actor system
  */
 @Inject
 public SysAdminUtilsImpl(
     ApplicationLifecycle lifecycle,
     Configuration configuration,
     IDatabaseDependencyService databaseDependencyService,
     ActorSystem actorSystem) {
   log.info("SERVICE>>> SysAdminUtilsImpl starting...");
   this.actorSystem = actorSystem;
   this.configuration = configuration;
   initAutomatedSystemStatus();
   lifecycle.addStopHook(
       () -> {
         log.info("SERVICE>>> SysAdminUtilsImpl stopping...");
         if (automaticSystemStatus != null) {
           try {
             getAutomaticSystemStatus().cancel();
           } catch (Exception e) {
             log.error("Unable to stop the automatic system status", e);
           }
         }
         log.info("SERVICE>>> SysAdminUtilsImpl stopped");
         return Promise.pure(null);
       });
   log.info("SERVICE>>> SysAdminUtilsImpl started");
 }
Exemple #3
0
  public static Promise<Result> setNotificationResult(
      final String datasetId, final String notificationId) {
    final String[] resultString = request().body().asFormUrlEncoded().get("result");
    if (resultString == null || resultString.length != 1 || resultString[0] == null) {
      return Promise.pure((Result) redirect(controllers.routes.Datasets.show(datasetId)));
    }

    final ConfirmNotificationResult result = ConfirmNotificationResult.valueOf(resultString[0]);

    Logger.debug("Conform notification: " + notificationId + ", " + result);

    final ActorSelection database = Akka.system().actorSelection(databaseRef);

    return from(database)
        .query(new PutNotificationResult(notificationId, result))
        .execute(
            new Function<Response<?>, Result>() {
              @Override
              public Result apply(final Response<?> response) throws Throwable {
                if (response.getOperationresponse().equals(CrudResponse.OK)) {
                  flash("success", "Resultaat van de structuurwijziging is opgeslagen");
                } else {
                  flash("danger", "Resultaat van de structuurwijziging kon niet worden opgeslagen");
                }
                return redirect(controllers.routes.Datasets.show(datasetId));
              }
            });
  }
  /**
   * Generates the error json required for ajax calls calls when the user is not authorized to
   * execute the action
   *
   * @return
   */
  protected static Promise<Result> notAuthorizedResult(Http.Context ctx) {
    Http.Request req = ctx.request();
    Result result;

    if (req.accepts("text/html")) {
      result = forbidden(notAuthorizedPage(ctx));
    } else if (req.accepts("application/json")) {
      ObjectNode node = Json.newObject();
      node.put("error", "Not authorized");
      result = forbidden(node);
    } else {
      result = forbidden("Not authorized");
    }

    return Promise.pure(result);
  }
  @Override
  public Promise<SimpleResult> call(Context ctx) throws Throwable {

    Promise<SimpleResult> ret = null;

    // redirect if it's not secure
    if (!isHttpsRequest(ctx.request())) {
      String url = redirectHostHttps(ctx) + ctx.request().uri();
      ret = Promise.pure(redirect(url));
    } else {
      // Let request proceed.
      ret = delegate.call(ctx);
    }

    return ret;
  }
  /**
   * Generates the error json required for ajax calls calls when the user is not authenticated
   *
   * @return
   */
  protected static Promise<Result> notAuthenticatedResult(Http.Context ctx) {
    Http.Request req = ctx.request();
    Result result;

    if (req.accepts("text/html")) {
      ctx.flash().put("error", play.i18n.Messages.get("securesocial.loginRequired"));
      ctx.session().put(ORIGINAL_URL, ctx.request().uri());
      result = redirect(env().routes().loginPageUrl(ctx._requestHeader()));
    } else if (req.accepts("application/json")) {
      ObjectNode node = Json.newObject();
      node.put("error", "Credentials required");
      result = unauthorized(node);
    } else {
      result = unauthorized("Credentials required");
    }
    return Promise.pure(result);
  }
Exemple #7
0
 public static Promise<Response> createCacheableUrlFetchPromise(
     String getUrl, Map<String, String> getParams) {
   final String key =
       "http-cache-" + getUrl + ":" + new TreeMap<String, String>(getParams).toString();
   final Response cached = (Response) Cache.get(key);
   if (cached == null) {
     WSRequestHolder holder = WS.url(getUrl);
     for (String k : getParams.keySet()) holder.setQueryParameter(k, getParams.get(k));
     Promise<Response> promise = holder.get();
     return promise.map(
         new Function<Response, Response>() {
           @Override
           public Response apply(Response res) throws Throwable {
             Cache.set(key, res);
             synchronized (cachedSet) {
               cachedSet.insert(key);
             }
             return res;
           }
         });
   } else {
     return Promise.pure(cached);
   }
 }
Exemple #8
0
 public Promise<Result> onHandlerNotFound(RequestHeader request) {
   return Promise.<Result>pure(notFound(views.html.notFoundPage.render(request.uri())));
 }