Beispiel #1
0
 public static Result route(Application app, FakeRequest fakeRequest, byte[] body, long timeout) {
   return wrapScalaResult(
       Scala.orNull(
           play.api.test.Helpers.jRoute(
               app.getWrappedApplication(), fakeRequest.getWrappedRequest(), body)),
       timeout);
 }
Beispiel #2
0
 @SuppressWarnings("unchecked")
 public static Result route(Application app, RequestBuilder requestBuilder, long timeout) {
   final scala.Option<scala.concurrent.Future<play.api.mvc.Result>> opt =
       play.api.test.Helpers.jRoute(
           app.getWrappedApplication(),
           requestBuilder.build()._underlyingRequest(),
           requestBuilder.body());
   return wrapScalaResult(Scala.orNull(opt), timeout);
 }
  public static Result createNewUser() {
    Form<User> nu = userForm.bindFromRequest();

    ObjectNode jsonData = Json.newObject();
    String userName = null;

    try {
      userName =
          nu.field("firstName").value()
              + " "
              + (nu.field("middleInitial")).value()
              + " "
              + (nu.field("lastName")).value();
      jsonData.put("userName", userName);
      jsonData.put("firstName", nu.get().getFirstName());
      jsonData.put("middleInitial", nu.get().getMiddleInitial());
      jsonData.put("lastName", nu.get().getLastName());
      jsonData.put("password", nu.get().getPassword());
      jsonData.put("affiliation", nu.get().getAffiliation());
      jsonData.put("title", nu.get().getTitle());
      jsonData.put("email", nu.get().getEmail());
      jsonData.put("mailingAddress", nu.get().getMailingAddress());
      jsonData.put("phoneNumber", nu.get().getPhoneNumber());
      jsonData.put("faxNumber", nu.get().getFaxNumber());
      jsonData.put("researchFields", nu.get().getResearchFields());
      jsonData.put("highestDegree", nu.get().getHighestDegree());

      JsonNode response =
          RESTfulCalls.postAPI(
              Constants.URL_HOST + Constants.CMU_BACKEND_PORT + Constants.ADD_USER, jsonData);

      // flash the response message
      Application.flashMsg(response);
      return redirect(routes.Application.createSuccess());

    } catch (IllegalStateException e) {
      e.printStackTrace();
      Application.flashMsg(RESTfulCalls.createResponse(ResponseType.CONVERSIONERROR));
    } catch (Exception e) {
      e.printStackTrace();
      Application.flashMsg(RESTfulCalls.createResponse(ResponseType.UNKNOWN));
    }
    return ok(signup.render(nu));
  }
Beispiel #4
0
 @Before
 static void setConnectedUser() {
   if (Security.isConnected()) {
     User user = User.find("byUsername", Security.connected()).first();
     if (!user.isAdmin) {
       flash.error(Messages.get("UserIsNotAuthorized"));
       Application.index();
     }
   }
 }
  public static Result isEmailExisted() {
    JsonNode json = request().body().asJson();
    String email = json.path("email").asText();

    ObjectNode jsonData = Json.newObject();
    JsonNode response = null;
    try {
      jsonData.put("email", email);
      response =
          RESTfulCalls.postAPI(
              Constants.URL_HOST + Constants.CMU_BACKEND_PORT + Constants.IS_EMAIL_EXISTED,
              jsonData);
      Application.flashMsg(response);
    } catch (IllegalStateException e) {
      e.printStackTrace();
      Application.flashMsg(RESTfulCalls.createResponse(ResponseType.CONVERSIONERROR));
    } catch (Exception e) {
      e.printStackTrace();
      Application.flashMsg(RESTfulCalls.createResponse(ResponseType.UNKNOWN));
    }
    return ok(response);
  }
Beispiel #6
0
  /**
   * REST endpoint returns a json array containing metadata about the currently logged in user's
   * uploaded files.
   *
   * @return json containing file upload ids and filenames
   */
  public Result listUploads() {
    List<UserUpload> uploadList = UserUpload.findUploadsByUserId(Application.getCurrentUserId());

    ObjectNode response = Json.newObject();
    ArrayNode uploads = response.putArray("uploads");

    for (UserUpload userUpload : uploadList) {
      ObjectNode upload = Json.newObject();
      upload.put("id", userUpload.id);
      upload.put("filename", userUpload.fileName);
      uploads.add(upload);
    }

    return ok(uploads);
  }
Beispiel #7
0
  private static void startStream(List<String> terms) throws TwitterException {
    if (twitter != null) {
      twitter.cleanUp();
    }
    if (esClient != null) {
      esClient.close();
      esClient = null;
    }

    play.Configuration pconf = Play.application().configuration();
    String elasticSearchCluster = pconf.getString("tweet.elasticsearch.cluster.name");
    if (elasticSearchCluster != null) {
      Logger.info("Configuring ElasticSearch...");
      Settings settings =
          ImmutableSettings.settingsBuilder().put("cluster.name", elasticSearchCluster).build();

      esClient =
          new TransportClient(settings)
              .addTransportAddress(
                  new InetSocketTransportAddress(
                      pconf.getString("tweet.elasticsearch.transport.host"),
                      pconf.getInt("tweet.elasticsearch.transport.port")));
    } else {
      esClient = null;
    }

    twitter4j.conf.Configuration tconf = Application.getTwitterConfiguration();
    TwitterStreamFactory tf = new TwitterStreamFactory(tconf);
    twitter = tf.getInstance();
    StatusListener l =
        new TweetListener(
            terms,
            esClient,
            pconf.getString("tweet.elasticsearch.index"),
            pconf.getString("tweet.elasticsearch.type"));
    twitter.addListener(l);

    String[] tracks = new String[terms.size()];
    StringBuffer termsString = new StringBuffer();
    for (int i = 0; i < terms.size(); i++) {
      tracks[i] = terms.get(i);
      if (i != 0) termsString.append(",");
      termsString.append(terms.get(i));
    }
    FilterQuery q = new FilterQuery().track(tracks);
    twitter.filter(q);
    Logger.info("Starting listening for tweets using terms " + termsString.toString() + "...");
  }
Beispiel #8
0
  public static void activate(String uuid) {

    Logger.info("用户开始进行激活,uuid" + uuid);
    User user = User.find("uuid", uuid).first();
    Logger.info("start activate:" + user.activated + "id:" + user.id);
    if (user != null) {
      // 激活成功
      user.activated = true;
      user.save();
      Logger.info("user id:" + user.id);
      render("@activatesuccess");

    } else {
      Application.index();
    }
  }
Beispiel #9
0
 public static void at(String location, String name, String address) {
   if (location != null
       && name != null
       && address != null
       && !location.equals("")
       && !name.equals("")
       && !address.equals("")) {
     new Post(
             user(),
             new Date().toString(),
             HTML.htmlEscape("Checked in at: " + name + "\n" + address))
         .save();
     Application.news(null);
   } else {
     redirect("/checkin");
   }
 }
Beispiel #10
0
  /**
   * Helper method creates a temp file from the multipart form data and persists the upload file
   * metadata to the database
   *
   * @param filePart data from the form submission
   * @return an instance of UploadFile that has been persisted to the db
   */
  private static UserUpload uploadFile(Http.MultipartFormData.FilePart filePart) {
    File src = (File) filePart.getFile();
    File file = null;
    try {
      file = File.createTempFile(filePart.getFilename() + "-", ".csv");
      FileUtils.copyFile(src, file);
    } catch (IOException e) {
      throw new RuntimeException("Could not create temp file for upload", e);
    }

    UserUpload uploadFile = new UserUpload();
    uploadFile.absolutePath = file.getAbsolutePath();
    uploadFile.fileName = filePart.getFilename();
    uploadFile.user = Application.getCurrentUser();
    uploadFile.save();

    return uploadFile;
  }
Beispiel #11
0
  public static void isLogin(String email, String password) {
    User user = User.find("byEmailAndPasswordAndDeleted", email, password, false).first();
    // 用户名or 密码错误
    if (user == null) {
      render("@login");
    } else {

      // 未激活
      if (user.activated == false) {
        session.put("user", user.email);
        // 跳至激活页面
        render("@notactivate", email);
      }

      session.put("user", user.email);
      Application.index();
    }
  }
Beispiel #12
0
 public static Result route(Application app, FakeRequest fakeRequest, long timeout) {
   final scala.Option<scala.concurrent.Future<play.api.mvc.Result>> opt =
       play.api.test.Helpers.jRoute(app.getWrappedApplication(), fakeRequest.fake);
   return wrapScalaResult(Scala.orNull(opt), timeout);
 }
Beispiel #13
0
 /** Stops an application. */
 public static void stop(Application application) {
   play.api.Play.stop(application.getWrappedApplication());
 }
Beispiel #14
0
 public static void logout() {
   session.clear();
   Application.index();
 }