Java Code Examples for play.mvc.Results#ok()

The following examples show how to use play.mvc.Results#ok() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RMBController.java    From NationStatesPlusPlus with MIT License 6 votes vote down vote up
public Result ratePost(int rmbPost, int rating) throws SQLException {
	Result ret = Utils.validateRequest(request(), response(), getAPI(), getDatabase());
	if (ret != null) {
		return ret;
	}
	if (rmbPost < 12 || rating > 1) {
		return Results.badRequest();
	}
	final String nation = Utils.sanitizeName(Utils.getPostValue(request(), "nation"));
	final int nationId = getDatabase().getNationId(nation);
	if (nationId == -1) {
		return Results.badRequest();
	}
	try (Connection conn = getConnection()) {
		JsonNode ratings = rateRMBPost(getDatabase(), conn, nation, nationId, rmbPost, rating);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("rmb_post_id", rmbPost);
		getDatabase().getWebsocketManager().onUpdate(PageType.REGION, RequestType.RMB_RATINGS, new DataRequest(RequestType.RMB_RATINGS, data), ratings);
	} 
	Utils.handleDefaultPostHeaders(request(), response());
	return Results.ok();
}
 
Example 2
Source File: LessonControllerUtils.java    From judgels with GNU General Public License v2.0 5 votes vote down vote up
public static Result downloadFile(File file) {
    if (!file.exists()) {
        return Results.notFound();
    }
    Controller.response().setContentType("application/x-download");
    Controller.response().setHeader("Content-disposition", "attachment; filename=" + file.getName());
    return Results.ok(file);
}
 
Example 3
Source File: RMBController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result addComment(int rmbPost) throws SQLException {
	Result invalid = Utils.validateRequest(request(), response(), getAPI(), getDatabase());
	if (invalid != null) {
		return invalid;
	}
	final String comment = Utils.getPostValue(request(), "comment");
	if (comment == null) {
		Utils.handleDefaultGetHeaders(request(), response(), null, "0");
		return Results.badRequest();
	}
	final String nation = Utils.getPostValue(request(), "nation");
	final int nationId = getDatabase().getNationId(nation);
	Connection conn = getConnection();
	try {
		PreparedStatement select = conn.prepareStatement("SELECT timestamp FROM assembly.rmb_comments WHERE rmb_message_id = ? AND nation_id = ? AND timestamp > ?");
		select.setInt(1, rmbPost);
		select.setInt(2, nationId);
		select.setLong(3, System.currentTimeMillis() - Duration.standardSeconds(10).getMillis());
		ResultSet result = select.executeQuery();
		if (result.next()) {
			Utils.handleDefaultGetHeaders(request(), response(), null, "0");
			return Results.status(429);
		}
		DbUtils.closeQuietly(result);
		DbUtils.closeQuietly(select);
		
		PreparedStatement insert = conn.prepareStatement("INSERT INTO assembly.rmb_comments (rmb_message_id, timestamp, comment, nation_id) VALUES (?, ?, ?, ?)");
		insert.setInt(1, rmbPost);
		insert.setLong(2, System.currentTimeMillis());
		insert.setString(3, comment);
		insert.setInt(4, nationId);
		DbUtils.closeQuietly(insert);
	} finally {
		DbUtils.closeQuietly(conn);
	}
	Utils.handleDefaultGetHeaders(request(), response(), null, "0");
	return Results.ok();
}
 
Example 4
Source File: RecruitmentController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result hideRecruitmentCampaign(String region, int id) throws SQLException {
	Result ret = Utils.validateRequest(request(), response(), getAPI(), getDatabase());
	if (ret != null) {
		return ret;
	}
	String nation = Utils.sanitizeName(Utils.getPostValue(request(), "nation"));
	Utils.handleDefaultPostHeaders(request(), response());
	try (Connection conn = getConnection()) {
		if (!hideRecruitmentCampaign(conn, region, id, nation, getDatabase().getNationId(nation))) {
			return Results.unauthorized();
		}
	}
	return Results.ok();
}
 
Example 5
Source File: RecruitmentController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result retireRecruitmentCampaign(String region, int id) throws SQLException {
	Result ret = Utils.validateRequest(request(), response(), getAPI(), getDatabase());
	if (ret != null) {
		return ret;
	}
	String nation = Utils.sanitizeName(Utils.getPostValue(request(), "nation"));
	Utils.handleDefaultPostHeaders(request(), response());
	try (Connection conn = getConnection()) {
		if (!retireRecruitmentCampaign(conn, region, id, nation, getDatabase().getNationId(nation))) {
			return Results.unauthorized();
		}
	}
	return Results.ok();
}
 
Example 6
Source File: RecruitmentController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result confirmRecruitmentSent(String region, String target, String accessKey) throws SQLException {
	final boolean validScriptAccess = isValidAccessKey(region, accessKey);
	//Bypass standard nation authentication if we are a valid script
	if (!validScriptAccess) {
		Result ret = Utils.validateRequest(request(), response(), getAPI(), getDatabase());
		if (ret != null) {
			return ret;
		}
	}
	Utils.handleDefaultPostHeaders(request(), response());
	Connection conn = null;
	try {
		conn = getConnection();
		final int regionId;
		//Bypass region officer authentication if we are a valid script
		if (validScriptAccess) {
			regionId = getDatabase().getRegionId(region);
		} else {
			String nation = Utils.sanitizeName(Utils.getPostValue(request(), "nation"));
			regionId = getRecruitmentAdministrator(conn, nation, getDatabase().getNationId(nation), region);
		}
		if (regionId == -1) {
			Utils.handleDefaultPostHeaders(request(), response());
			return Results.unauthorized();
		}
		confirmRecruitment(getDatabase(), conn, regionId, target);
	} finally {
		DbUtils.closeQuietly(conn);
	}
	return Results.ok();
}
 
Example 7
Source File: RecruitmentController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result markPuppetNation(String nation) {
	Utils.handleDefaultPostHeaders(request(), response());
	nation = Utils.sanitizeName(nation);
	if (getDatabase().getNationId(nation) == -1) {
		HappeningsTask.markNationAsPuppet(nation);
		return Results.ok();
	}
	return Results.badRequest();
}
 
Example 8
Source File: AbstractJudgelsController.java    From judgels with GNU General Public License v2.0 5 votes vote down vote up
protected static Result getResult(LazyHtml content, int statusCode) {
    HtmlCompressor htmlCompressor = new HtmlCompressor();
    Html compressedContent = new Html(htmlCompressor.compress(content.render().body()));
    switch (statusCode) {
        case Http.Status.OK:
            return Results.ok(compressedContent);
        case Http.Status.NOT_FOUND:
            return Results.notFound(compressedContent);
        default:
            return Results.badRequest(compressedContent);
    }
}
 
Example 9
Source File: NSWikiController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result verifyNationLogin() throws IOException, SQLException {
	Utils.handleDefaultPostHeaders(request(), response());
	Result ret = Utils.validateRequest(request(), response(), getAPI(), getDatabase(), false);
	if (ret != null) {
		return ret;
	}
	String nation = Utils.getPostValue(request(), "nation");
	String password = Utils.getPostValue(request(), "password");
	if (password == null || password.isEmpty() || password.length() < 8) {
		Logger.warn("NSWiki User [" + nation + "] attempted an invalid password: [" + password + "]");
		return Results.badRequest("Invalid password");
	}
	Logger.info("Attempting NSWiki login for " + nation);
	final String title;
	Connection conn = null;
	PreparedStatement select = null;
	ResultSet set = null;
	try {
		conn = getConnection();
		select = conn.prepareStatement("SELECT title FROM assembly.nation WHERE name = ?");
		select.setString(1, Utils.sanitizeName(nation));
		set = select.executeQuery();
		set.next();
		title = set.getString(1);
		
		if (doesNSWikiUserExist(title)) {
			Logger.info("NSWiki Updating password for " + title);
			if (changePassword(conn, title, password)) {
				return Results.ok();
			}
			return Results.internalServerError("Unable to change password for " + title);
		}
	} finally {
		DbUtils.closeQuietly(conn);
		DbUtils.closeQuietly(select);
		DbUtils.closeQuietly(set);
	}
	return createNSWikiUser(title, password);
}
 
Example 10
Source File: NSWikiController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
private Result createNSWikiUser(String nation, String password) throws IOException {
	MediaWikiBot wikibot = new MediaWikiBot("http://nswiki.org/");
	wikibot.login(nswikiAdmin, nswikiPass);
	String result = wikibot.performAction(new CreateUser(nation, password));
	if (result.toLowerCase().contains("success")) {
		return Results.ok();
	} else {
		Logger.warn("Unable to create NSWiki user: " + result);
		return Results.internalServerError();
	}
}
 
Example 11
Source File: NewspaperController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result canEdit(int newspaper) throws SQLException {
	Result result = canEditImpl(newspaper, true, Utils.getPostValue(request(), "nation"));
	if (result != null) {
		return result;
	}
	Utils.handleDefaultPostHeaders(request(), response());
	return Results.ok();
}
 
Example 12
Source File: NewspaperController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result isEditor(int newspaper, String nation) throws SQLException {
	Result result = canEditImpl(newspaper, false, nation);
	if (result != null) {
		return result;
	}
	Utils.handleDefaultGetHeaders(request(), response(), null, "120");
	return Results.ok();
}
 
Example 13
Source File: NewspaperController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result administrateNewspaper(int newspaper) throws SQLException {
	Result result = Utils.validateRequest(request(), response(), getAPI(), getDatabase());
	if (result != null) {
		return result;
	}
	String nation = Utils.getPostValue(request(), "nation");
	String title = Utils.getPostValue(request(), "title");
	String byline = Utils.getPostValue(request(), "byline");
	String columns = Utils.getPostValue(request(), "columns");

	Utils.handleDefaultPostHeaders(request(), response());
	if (title == null || title.length() > 255 || byline == null || byline.length() > 255) {
		return Results.badRequest();
	}
	Connection conn = null;
	try {
		conn = getConnection();

		if (!isEditorInChief(newspaper, nation, conn)) {
			Utils.handleDefaultPostHeaders(request(), response());
			return Results.unauthorized();
		}

		try (PreparedStatement update = conn.prepareStatement("UPDATE assembly.newspapers SET title = ?, byline = ?" + (columns != null ? ", newspapers.columns = ?" : "") + " WHERE id = ?")) {
			update.setString(1, title);
			update.setString(2, byline);
			if (columns != null) {
				update.setInt(3, Math.max(1, Math.min(3, Integer.parseInt(columns))));
				update.setInt(4, newspaper);
			} else {
				update.setInt(3, newspaper);
			}
			update.executeUpdate();
		}
	} finally {
		DbUtils.closeQuietly(conn);
	}
	return Results.ok();
}
 
Example 14
Source File: NationController.java    From NationStatesPlusPlus with MIT License 4 votes vote down vote up
public Result updateData() throws SQLException {
	Result result = Utils.validateRequest(request(), response(), getAPI(), getDatabase());
	if (result != null) {
		return result;
	}
	Utils.handleDefaultPostHeaders(request(), response());
	final String nation = Utils.getPostValue(request(), "nation");
	final String data = Utils.getPostValue(request(), "data");
	final int nationId = getDatabase().getNationId(nation);
	if (nationId == -1 || data == null) {
		return Results.badRequest();
	}
	Connection conn = null;
	PreparedStatement select = null;
	ResultSet set = null;
	try {
		conn = getConnection();
		select = conn.prepareStatement("SELECT last_data_update FROM assembly.ns_settings WHERE id = ?");
		select.setInt(1, nationId);
		set = select.executeQuery();
		if (set.next()) {
			PreparedStatement update = conn.prepareStatement("UPDATE assembly.ns_settings SET data = ?, last_data_update = ? WHERE id = ?");
			update.setString(1, data);
			update.setLong(2, System.currentTimeMillis());
			update.setInt(3, nationId);
			update.executeUpdate();
			DbUtils.closeQuietly(update);
		} else {
			PreparedStatement insert = conn.prepareStatement("INSERT INTO assembly.ns_settings (id, data, last_data_update) VALUES (?, ?, ?)");
			insert.setInt(1, nationId);
			insert.setString(2, data);
			insert.setLong(3, System.currentTimeMillis());
			insert.executeUpdate();
			DbUtils.closeQuietly(insert);
		}
		return Results.ok();
	} finally {
		DbUtils.closeQuietly(set);
		DbUtils.closeQuietly(select);
		DbUtils.closeQuietly(conn);
	}
}
 
Example 15
Source File: NationController.java    From NationStatesPlusPlus with MIT License 4 votes vote down vote up
public Result updateSettings() throws SQLException {
	Result result = Utils.validateRequest(request(), response(), getAPI(), getDatabase());
	if (result != null) {
		return result;
	}
	Utils.handleDefaultPostHeaders(request(), response());
	final String nation = Utils.getPostValue(request(), "nation");
	final String settings = Utils.getPostValue(request(), "settings");
	final int nationId = getDatabase().getNationId(nation);
	if (nationId == -1 || settings == null) {
		return Results.badRequest();
	}
	Connection conn = null;
	PreparedStatement select = null;
	ResultSet set = null;
	try {
		conn = getConnection();
		select = conn.prepareStatement("SELECT last_settings_update FROM assembly.ns_settings WHERE id = ?");
		select.setInt(1, nationId);
		set = select.executeQuery();
		if (set.next()) {
			PreparedStatement update = conn.prepareStatement("UPDATE assembly.ns_settings SET settings = ?, last_settings_update = ? WHERE id = ?");
			update.setString(1, settings);
			update.setLong(2, System.currentTimeMillis());
			update.setInt(3, nationId);
			update.executeUpdate();
			DbUtils.closeQuietly(update);
		} else {
			PreparedStatement insert = conn.prepareStatement("INSERT INTO assembly.ns_settings (id, settings, last_settings_update) VALUES (?, ?, ?)");
			insert.setInt(1, nationId);
			insert.setString(2, settings);
			insert.setLong(3, System.currentTimeMillis());
			insert.executeUpdate();
			DbUtils.closeQuietly(insert);
		}
		getDatabase().getNationSettingsCache().put(nationId, settings);
		return Results.ok();
	} finally {
		DbUtils.closeQuietly(set);
		DbUtils.closeQuietly(select);
		DbUtils.closeQuietly(conn);
	}
}
 
Example 16
Source File: NewspaperController.java    From NationStatesPlusPlus with MIT License 4 votes vote down vote up
public Result disbandNewspaper(String region) throws SQLException {
	Result ret = Utils.validateRequest(request(), response(), getAPI(), getDatabase());
	if (ret != null) {
		return ret;
	}
	final String nation = Utils.getPostValue(request(), "nation");
	final int regionId = getDatabase().getRegionId(region);
	
	Utils.handleDefaultPostHeaders(request(), response());
	try (Connection conn = getConnection()) {
		try (PreparedStatement newspaper = conn.prepareStatement("SELECT id FROM assembly.newspapers WHERE region = ? AND disbanded = 0")) {
			newspaper.setInt(1, regionId);
			try (ResultSet result = newspaper.executeQuery()) {
				if (!result.next()) {
					return Results.forbidden();
				}
			}
		}

		try (PreparedStatement select = conn.prepareStatement("SELECT delegate, founder FROM assembly.region WHERE id = ?")) {
			select.setInt(1, regionId);
			try (ResultSet result = select.executeQuery()) {
				boolean regionAdministrator = false;
				if (result.next()) {
					if (nation.equals(result.getString("delegate")) || nation.equals(result.getString("founder"))) {
						regionAdministrator = true;
					}
				}
				if (!regionAdministrator) {
					return Results.unauthorized();
				}
			}
		}

		try (PreparedStatement newspaper = conn.prepareStatement("UPDATE assembly.newspapers SET disbanded = 1 WHERE region = ?")) {
			newspaper.setInt(1, regionId);
			newspaper.executeUpdate();
		}
	}
	return Results.ok();
}
 
Example 17
Source File: RegionController.java    From NationStatesPlusPlus with MIT License 4 votes vote down vote up
public Result setRegionalTitle(String region, boolean disband) throws SQLException {
	Result ret = Utils.validateRequest(request(), response(), getAPI(), getDatabase());
	if (ret != null) {
		return ret;
	}
	String nation = Utils.sanitizeName(Utils.getPostValue(request(), "nation"));
	String delegateTitle = Utils.getPostValue(request(), "delegate_title");
	String founderTitle = Utils.getPostValue(request(), "founder_title");
	Utils.handleDefaultPostHeaders(request(), response());

	//Must have valid title
	if (!disband) {
		if (delegateTitle == null || founderTitle == null || delegateTitle.isEmpty() || founderTitle.isEmpty()) {
			return Results.badRequest("Missing title");
		} else if (delegateTitle.length() > 40 || founderTitle.length() > 40) {
			return Results.badRequest("Maximum title length is 40 characters");
		}
	}

	Connection conn = null;
	try {
		conn = getConnection();
		PreparedStatement select = conn.prepareStatement("SELECT id, delegate, founder FROM assembly.region WHERE name = ?");
		select.setString(1, Utils.sanitizeName(region));
		ResultSet result = select.executeQuery();
		boolean regionAdministrator = true;
		int regionId = -1;
		if (result.next()) {
			regionId = result.getInt(1);
			final String delegate = result.getString(2);
			final String founder = result.getString(3);
			Logger.info("Attempting to set regional titles for " + region + ", nation: " + nation);
			Logger.info("Delegate: " + delegate + " | Founder: " + founder);
			if (!nation.equals(delegate) && !nation.equals(founder)) {
				regionAdministrator = false;
			}
		} else {
			Logger.info("Attempting to set regional titles for " + region + ", no region found!");
			regionAdministrator = false;
		}
		if (regionAdministrator) {
			PreparedStatement update = conn.prepareStatement("UPDATE assembly.region SET delegate_title = ?, founder_title = ? WHERE id = ?");
			if (!disband) {
				update.setString(1, delegateTitle);
				update.setString(2, founderTitle);
			} else {
				update.setNull(1, Types.VARCHAR);
				update.setNull(2, Types.VARCHAR);
			}
			update.setInt(3, regionId);
			update.executeUpdate();
			return Results.ok();
		}
	} finally {
		DbUtils.closeQuietly(conn);
	}
	return Results.unauthorized();
}