Esempio n. 1
0
  @BodyParser.Of(BodyParser.Json.class)
  public F.Promise<Result> addPoint() {

    JsonNode json = request().body().asJson();
    play.Logger.debug("AddPoint (Json): " + request().body().asJson().toString());

    String name = json.findPath("name").textValue();
    double latitude = json.get("location").get("lat").asDouble();
    double longitude = json.get("location").get("lon").asDouble();
    float rating = json.get("rating").floatValue();
    String id_token = json.findPath("access_token").textValue();
    String user_name = json.findPath("user_name").textValue();
    String user_id = json.findPath("user_id").textValue();

    latitude = getSixDecimalFormatNumber(latitude);
    longitude = getSixDecimalFormatNumber(longitude);

    ObjectNode newPoint = getNewPointJson(name, "", latitude, longitude);
    ObjectNode cafeInfo = getCafeInfoJson(rating, user_id, user_name);

    play.Logger.debug(
        String.format("\n Json newPoint: %s \n Json CafeInfo: %s", newPoint, cafeInfo));

    String id = generateId(name + latitude + longitude);

    WSRequest requestNewPoint = ws.url(getAddPointUrl(id)).setContentType("application/json");
    F.Promise<WSResponse> responsePromise;
    if (isValidToken(id_token)) {
      responsePromise = requestNewPoint.post(newPoint);
      return responsePromise.map(
          response -> {
            if (response.getStatus() == Http.Status.CREATED && addCafeInfo(id, cafeInfo)) {
              return ok(response.asJson());
            } else {
              return badRequest(response.asJson());
            }
          });

    } else {
      responsePromise = null;
      ObjectNode status = Json.newObject();
      status.put("status", "error");
      status.put("point", newPoint);
      status.put("reason", "invalid token");
      return responsePromise.map(wsResponse -> unauthorized(status));
    }
  }
Esempio n. 2
0
  public F.Promise<Result> points(String n, String s, String w, String e) {
    WSRequest request = ws.url(getSearchUrl());
    F.Promise<WSResponse> responsePromise = request.post(getRequestBody(n, s, w, e));

    play.Logger.debug(String.format("N - : %s, E - : %s, W - : %s, E - : %s", n, s, w, e));

    return responsePromise.map(response -> ok(getJsonPoints(response.asJson())));
  }
Esempio n. 3
0
  public F.Promise<Result> testValidate(String token) {
    WSRequest request = ws.url(getValidateTokenUrl(token));

    F.Promise<WSResponse> responsePromise = request.get();
    return responsePromise.map(
        response -> {
          play.Logger.debug(response.getBody());
          if (response.getStatus() == 200) {
            return ok(response.getBody());
          } else {
            return unauthorized("fail");
          }
        });
  }
  /** Protects an action with SecureSocial */
  public static class Secured extends Action<SecuredAction> {
    private RuntimeEnvironment env;

    public Secured(RuntimeEnvironment env) {
      this.env = env;
    }

    private play.Logger.ALogger logger = play.Logger.of("securesocial.core.java.Secured");

    @Override
    public Promise<Result> call(final Http.Context ctx) throws Throwable {
      initEnv(env);
      return F.Promise.wrap(env.authenticatorService().fromRequest(ctx._requestHeader()))
          .flatMap(
              new F.Function<Option<Authenticator>, Promise<Result>>() {
                @Override
                public Promise<Result> apply(Option<Authenticator> authenticatorOption)
                    throws Throwable {
                  if (authenticatorOption.isDefined() && authenticatorOption.get().isValid()) {
                    final Authenticator authenticator = authenticatorOption.get();
                    Object user = authenticator.user();
                    Authorization authorization = configuration.authorization().newInstance();
                    if (authorization.isAuthorized(user, configuration.params())) {
                      return F.Promise.wrap(authenticator.touch())
                          .flatMap(
                              new F.Function<Authenticator, Promise<Result>>() {
                                @Override
                                public Promise<Result> apply(Authenticator touched)
                                    throws Throwable {
                                  ctx.args.put(USER_KEY, touched.user());
                                  return F.Promise.wrap(touched.touching(ctx))
                                      .flatMap(
                                          new F.Function<
                                              scala.runtime.BoxedUnit, Promise<Result>>() {
                                            @Override
                                            public Promise<Result> apply(
                                                scala.runtime.BoxedUnit unit) throws Throwable {
                                              return delegate.call(ctx);
                                            }
                                          });
                                }
                              });
                    } else {
                      return notAuthorizedResult(ctx);
                    }
                  } else {
                    if (authenticatorOption.isDefined()) {
                      return F.Promise.wrap(authenticatorOption.get().discarding(ctx))
                          .flatMap(
                              new F.Function<Authenticator, Promise<Result>>() {
                                @Override
                                public Promise<Result> apply(Authenticator authenticator)
                                    throws Throwable {
                                  return notAuthenticatedResult(ctx);
                                }
                              });
                    }
                    return notAuthenticatedResult(ctx);
                  }
                }
              });
    }
  }