コード例 #1
0
ファイル: RESTfulCalls.java プロジェクト: QiHaoBao/localCs
 public static JsonNode postAPI(String apiString, JsonNode jsonData) {
   System.out.println("apiString: " + apiString);
   System.out.println("jsonData: " + jsonData.get("purpose"));
   Promise<WSResponse> responsePromise = WS.url(apiString).post(jsonData);
   final Promise<JsonNode> bodyPromise =
       responsePromise.map(
           new Function<WSResponse, JsonNode>() {
             public JsonNode apply(WSResponse response) throws Throwable {
               if ((response.getStatus() == 201 || response.getStatus() == 200)) {
                 try {
                   return response.asJson();
                 } catch (Exception e) {
                   // If response is in Json format, return as json, otherwise just plain success
                   return createResponse(ResponseType.SUCCESS);
                 }
               } else { // other response status from the server
                 return createResponse(ResponseType.SAVEERROR);
               }
             }
           });
   try {
     return bodyPromise.get(10000L);
   } catch (Exception e) {
     return createResponse(ResponseType.TIMEOUT);
   }
 }
コード例 #2
0
 // #async
 public static Promise<SimpleResult> index() {
   Promise<Integer> promiseOfInt =
       Promise.promise(
           new Function0<Integer>() {
             public Integer apply() {
               return intensiveComputation();
             }
           });
   return promiseOfInt.map(
       new Function<Integer, SimpleResult>() {
         public SimpleResult apply(Integer i) {
           return ok("Got result: " + i);
         }
       });
 }
コード例 #3
0
ファイル: RESTfulCalls.java プロジェクト: QiHaoBao/localCs
 public static JsonNode putAPI(String apiString, JsonNode jsonData) {
   Promise<WSResponse> responsePromise = WS.url(apiString).put(jsonData);
   final Promise<JsonNode> bodyPromise =
       responsePromise.map(
           new Function<WSResponse, JsonNode>() {
             public JsonNode apply(WSResponse response) throws Throwable {
               if ((response.getStatus() == 201 || response.getStatus() == 200)
                   && !response.getBody().contains("not")) {
                 return createResponse(ResponseType.SUCCESS);
               } else { // other response status from the server
                 return createResponse(ResponseType.SAVEERROR);
               }
             }
           });
   try {
     return bodyPromise.get(10000L);
   } catch (Exception e) {
     return createResponse(ResponseType.TIMEOUT);
   }
 }
コード例 #4
0
ファイル: RESTfulCalls.java プロジェクト: QiHaoBao/localCs
 public static JsonNode deleteAPI(String apiString) {
   Promise<WSResponse> responsePromise =
       WS.url(apiString.replace("+", "%20")).setContentType("text/html").delete();
   final Promise<JsonNode> bodyPromise =
       responsePromise.map(
           new Function<WSResponse, JsonNode>() {
             public JsonNode apply(WSResponse response) throws Throwable {
               if ((response.getStatus() == 200 || response.getStatus() == 201)
                   && !response.getBody().contains("not")) {
                 return createResponse(ResponseType.SUCCESS);
               } else { // no response from the server
                 return createResponse(ResponseType.DELETEERROR);
               }
             }
           });
   try {
     return bodyPromise.get(10000L);
   } catch (Exception e) {
     return createResponse(ResponseType.TIMEOUT);
   }
 }
コード例 #5
0
ファイル: RESTfulCalls.java プロジェクト: QiHaoBao/localCs
  public static JsonNode getAPI(String apiString) {
    Logger.info(apiString);
    Promise<WSResponse> responsePromise = WS.url(apiString).get();
    final Promise<JsonNode> bodyPromise =
        responsePromise.map(
            new Function<WSResponse, JsonNode>() {
              public JsonNode apply(WSResponse response) {
                if (response.getStatus() == 200 || response.getStatus() == 201) {
                  return response.asJson();
                } else { // no response from the server
                  Logger.info("" + response.getStatus());
                  return createResponse(ResponseType.GETERROR);
                }
              }
            });

    try {
      return bodyPromise.get(10000L);
    } catch (Exception e) {
      return createResponse(ResponseType.TIMEOUT);
    }
  }
コード例 #6
0
ファイル: HttpUtil.java プロジェクト: psjava/solutions-site
 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);
   }
 }