示例#1
0
文件: UserApp.java 项目: bluemir/hive
  /**
   * ajax 를 이용한 사용자 검색 요청 헤더의 accept 파라미터에 application/json 값이 없으면 406 응답 응답에 포함되는 데이터 수는
   * MAX_FETCH_USERS 로 제한된다 입력 파라미터 query 가 부분매칭 되는 loginId 목록을 json 형태로 응답
   *
   * @param query 검색어
   * @return
   */
  public static Result users(String query) {
    if (!request().accepts("application/json")) {
      return status(Http.Status.NOT_ACCEPTABLE);
    }

    ExpressionList<User> el = User.find.select("loginId, name").where().disjunction();
    el.icontains("loginId", query);
    el.icontains("name", query);
    el.endJunction();

    int total = el.findRowCount();
    if (total > MAX_FETCH_USERS) {
      el.setMaxRows(MAX_FETCH_USERS);
      response().setHeader("Content-Range", "items " + MAX_FETCH_USERS + "/" + total);
    }

    List<Map<String, String>> users = new ArrayList<>();
    for (User user : el.findList()) {
      StringBuilder sb = new StringBuilder();
      sb.append(String.format("<img class='mention_image' src='%s'>", user.avatarUrl()));
      sb.append(String.format("<b class='mention_name'>%s</b>", user.name));
      sb.append(String.format("<span class='mention_username'> @%s</span>", user.loginId));

      Map<String, String> userMap = new HashMap<>();
      userMap.put("info", sb.toString());
      userMap.put("loginId", user.loginId);
      users.add(userMap);
    }

    return ok(toJson(users));
  }
示例#2
0
 public static String hex(byte[] array) {
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < array.length; ++i) {
     sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
   }
   return sb.toString();
 }
 @Test
 public void maxLength() {
   StringBuilder body = new StringBuilder();
   for (int i = 0; i < 1100; i++) {
     body.append("1234567890");
   }
   assertThat(
       status(
           callWithStringBody(
               new MockJavaAction() {
                 // #max-length
                 // Accept only 10KB of data.
                 @BodyParser.Of(value = BodyParser.Text.class, maxLength = 10 * 1024)
                 public Result index() {
                   if (request().body().isMaxSizeExceeded()) {
                     return badRequest("Too much data!");
                   } else {
                     return ok("Got body: " + request().body().asText());
                   }
                 }
                 // #max-length
               },
               fakeRequest(),
               body.toString())),
       equalTo(400));
 }
示例#4
0
 public TweetListener(List<String> terms, Client esClient, String esIndex, String esType) {
   this.terms = terms;
   this.esClient = esClient;
   StringBuilder query = new StringBuilder();
   for (int i = 0; i < terms.size(); i++) {
     if (i > 0) query.append("|");
     // query.append("(").append(Pattern.quote(terms.get(i))).append(")");
     // query.append("(").append(terms.get(i)).append(")");
     query.append(terms.get(i));
   }
   Logger.info("Query match pattern: " + query.toString());
   this.matchPattern = Pattern.compile(query.toString(), Pattern.CASE_INSENSITIVE);
   Logger.info("Query match pattern: " + matchPattern.toString());
 }
示例#5
0
  public static Result start() {
    java.util.Map<String, String[]> map = request().body().asFormUrlEncoded();

    List<String> terms = new ArrayList<>(map.size());
    for (int i = 0; i < map.size(); i++) {
      String key = "terms[" + i + "]";
      if (map.containsKey(key)) {
        String[] values = map.get(key);
        if ((values != null) && (values.length >= 1)) {
          terms.add(values[0]);
        }
      }
    }

    StreamConfig config = getConfig();
    config.putTerms(terms);
    config.update();

    StringBuilder sb = new StringBuilder();
    for (String t : terms) {
      sb.append(t);
      sb.append(", ");
    }
    sb.delete(sb.length() - 2, sb.length());

    try {
      startStream(terms);
      flash("success", "Twitter stream started (" + sb.toString() + ")");
    } catch (TwitterException e) {
      Logger.info("Error starting twitter stream", e);
      flash("error", "Error starting Twitter stream" + e.getMessage());
    }
    return redirect(routes.Streams.listAll());
  }
示例#6
0
  public F.Promise<Result> call(Http.Context ctx) throws Throwable {
    try {
      return delegate.call(ctx);
    } catch (Exception e) {
      e.printStackTrace();
      StringBuilder sb = new StringBuilder();

      sb.append("Error for request at " + ctx.request().uri() + "\n");
      sb.append("Headers: \n");
      Map<String, String[]> headers = ctx.request().headers();
      for (String key : headers.keySet()) {
        sb.append("  " + key + " --> ");
        for (String val : headers.get(key)) {
          sb.append(val + "|||");
        }
        sb.append("\n");
      }

      sb.append("Cookies: \n");
      for (Http.Cookie cookie : ctx.request().cookies()) {
        sb.append("  " + cookie.name() + " --> " + cookie.value() + "\n");
      }

      Http.RequestBody body = ctx.request().body();
      Map<String, String[]> body_vals = body.asFormUrlEncoded();
      if (body_vals != null) {
        sb.append("Body (as form URL encoded): \n");
        for (String key : body_vals.keySet()) {
          sb.append("  " + key + " --> ");
          for (String val : body_vals.get(key)) {
            sb.append(val + "|||");
          }
          sb.append("\n");
        }
      }

      Logger.error(sb.toString());
      throw e;
    }
  }