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

The following examples show how to use play.mvc.Results#badRequest() . 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: RegionController.java    From NationStatesPlusPlus with MIT License 6 votes vote down vote up
public Result getUpdateTime(String region, double std) throws SQLException {
	Connection conn = null;
	int regionId = this.getDatabase().getRegionId(region);
	if (regionId == -1) {
		return Results.badRequest();
	}
	try {
		conn = getConnection();
		JsonNode updateTime = getUpdateTime(conn, regionId, std);
		Result result = Utils.handleDefaultGetHeaders(request(), response(), String.valueOf(updateTime.hashCode()), "60");
		if (result != null) {
			return result;
		}
		return ok(Json.toJson(updateTime)).as("application/json");
	} finally {
		DbUtils.closeQuietly(conn);
	}
}
 
Example 3
Source File: NationController.java    From NationStatesPlusPlus with MIT License 6 votes vote down vote up
public Result retrieveForumSettings(String name) throws SQLException, ExecutionException {
	Utils.handleDefaultPostHeaders(request(), response());
	final int nationId = getDatabase().getNationId(name);
	if (nationId == -1) {
		return Results.badRequest();
	}
	NationSettings settings = getDatabase().getNationSettings(name, false);
	Map<String, Object> json = Maps.newHashMap();
	json.put("post_ids", settings.getValue("post_ids", true, Boolean.class));
	json.put("egosearch_ignore", settings.getValue("egosearch_ignore", true, Boolean.class));
	json.put("highlight_op_posts", settings.getValue("highlight_op_posts", true, Boolean.class));
	json.put("highlight_color_transparency", settings.getValue("highlight_color_transparency", 0.1, Double.class));
	json.put("highlight_color", settings.getValue("highlight_color", "#39EE00", String.class));
	json.put("floating_sidepanel", settings.getValue("floating_sidepanel", true, Boolean.class));
	return Results.ok(Json.toJson(json)).as("application/json");
}
 
Example 4
Source File: IRCController.java    From NationStatesPlusPlus with MIT License 6 votes vote down vote up
public Result getIRCNetwork(String region) throws SQLException {
	Utils.handleDefaultPostHeaders(request(), response());
	int regionId = getDatabase().getRegionId(region);
	if (regionId == -1) {
		return Results.badRequest("Invalid region");
	}
	try (Connection conn = this.getConnection()) {
		try (PreparedStatement select = conn.prepareStatement("SELECT irc_network, irc_channel, irc_port FROM assembly.irc_networks WHERE region = ?")) {
			select.setInt(1, regionId);
			try (ResultSet result = select.executeQuery()) {
				if (result.next()) {
					HashMap<String, Object> ircNetwork = new HashMap<>();
					ircNetwork.put("irc_network", result.getString(1));
					ircNetwork.put("irc_channel", result.getString(2));
					ircNetwork.put("irc_port", result.getInt(3));
					return Results.ok(Json.toJson(ircNetwork)).as("application/json");
				}
			}
		}
	}
	return Results.noContent();
}
 
Example 5
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 6
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 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: 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 9
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 10
Source File: NationController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result retrieveSettings(String name) throws SQLException, ExecutionException {
	Utils.handleDefaultPostHeaders(request(), response());
	final int nationId = getDatabase().getNationId(name);
	if (nationId == -1) {
		return Results.badRequest();
	}
	String json = getDatabase().getNationSettingsCache().get(nationId);
	if (!json.isEmpty()) {
		return Results.ok(json).as("application/json");
	}
	return Results.noContent();
}
 
Example 11
Source File: NationController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result retrieveData() 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 int nationId = getDatabase().getNationId(nation);
	if (nationId == -1) {
		return Results.badRequest();
	}
	Connection conn = null;
	PreparedStatement select = null;
	ResultSet set = null;
	try {
		conn = getConnection();
		select = conn.prepareStatement("SELECT data FROM assembly.ns_settings WHERE id = ?");
		select.setInt(1, nationId);
		set = select.executeQuery();
		if (set.next()) {
			String json = set.getString(1);
			if (!set.wasNull()) {
				return Results.ok(json).as("application/json");
			}
		}
	} finally {
		DbUtils.closeQuietly(set);
		DbUtils.closeQuietly(select);
		DbUtils.closeQuietly(conn);
	}
	return Results.noContent();
}
 
Example 12
Source File: NationController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result retrieveAllSettings(String name) throws SQLException {
	Utils.handleDefaultPostHeaders(request(), response());
	final int nationId = getDatabase().getNationId(name);
	if (nationId == -1) {
		return Results.badRequest();
	}
	String authToken = Utils.getPostValue(request(), "rss_token");
	if (authToken == null || authToken.isEmpty()) {
		return Results.badRequest();
	}
	int rssToken;
	try {
		rssToken = Integer.parseInt(authToken);
	} catch (NumberFormatException e) {
		return Results.unauthorized("Malformed rss token, expected integer");
	}
	
	Authentication auth = new Authentication(Utils.sanitizeName(name), nationId, rssToken, this.getDatabase());
	if (!auth.isValid()) {
		return Results.unauthorized("Invalid rss token");
	}
	
	NationSettings settings = getDatabase().getNationSettings(name, false);
	if (settings instanceof MongoSettings) {
		MongoSettings mongoSettings = (MongoSettings)settings;
		BasicDBObject find = new BasicDBObject("nation", Utils.sanitizeName(name));
		try (DBCursor cursor = mongoSettings.getCollection().find(find)) {
			if (cursor.hasNext()) {
				return Results.ok(Json.toJson(cursor.next().toMap())).as("application/json");
			}
		}
	}
	return Results.noContent();
}
 
Example 13
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();
}
 
Example 14
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 15
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 16
Source File: IRCController.java    From NationStatesPlusPlus with MIT License 4 votes vote down vote up
public Result setIRCNetwork(String region, boolean deleteIRC) throws SQLException {
	Utils.handleDefaultPostHeaders(request(), response());
	final int regionId = getDatabase().getRegionId(region);
	if (regionId == -1) {
		return Results.badRequest("Invalid region");
	}
	final String nation = Utils.getPostValue(request(), "nation");
	if (nation == null || nation.isEmpty()) {
		return Results.badRequest();
	}
	final int nationId = getDatabase().getNationId(nation);
	if (nationId == -1) {
		return Results.badRequest();
	}
	
	final String ircNetwork = Utils.getPostValue(request(), "irc_network");
	final String ircChannel = Utils.getPostValue(request(), "irc_channel");
	final String ircPort = Utils.getPostValue(request(), "irc_port");
	if (!deleteIRC) {
		if (ircNetwork == null || ircNetwork.isEmpty() || ircChannel == null || ircChannel.isEmpty() || ircPort == null || ircPort.isEmpty()) {
			return Results.badRequest("Missing irc network, irc channel");
		}
	}
	String authToken = Utils.getPostValue(request(), "rss_token");
	if (authToken == null || authToken.isEmpty()) {
		return Results.badRequest("Missing authentication");
	}
	int rssToken;
	try {
		rssToken = Integer.parseInt(authToken);
	} catch (NumberFormatException e) {
		return Results.unauthorized("Malformed rss token, expected integer");
	}
	
	Authentication auth = new Authentication(Utils.sanitizeName(nation), nationId, rssToken, this.getDatabase());
	if (!auth.isValid()) {
		return Results.unauthorized("Invalid rss token");
	}
	
	boolean validAdministrator = false;
	try (Connection conn = this.getConnection()) {
		try (PreparedStatement select = conn.prepareStatement("SELECT founder, delegate FROM assembly.region WHERE id = ?")) {
			select.setInt(1, regionId);
			try (ResultSet result = select.executeQuery()) {
				if (result.next()) {
					validAdministrator = nation.equalsIgnoreCase(result.getString(1)) || nation.equalsIgnoreCase(result.getString(2));
				}
			}
		}

		if (!validAdministrator) {
			return Results.unauthorized("You lack permission to edit " + region + "'s irc settings");
		}

		try (PreparedStatement delete = conn.prepareStatement("DELETE FROM assembly.irc_networks WHERE region = ?")) {
			delete.setInt(1, regionId);
			delete.executeUpdate();
		}
		if (!deleteIRC) {
			try (PreparedStatement insert = conn.prepareStatement("INSERT INTO assembly.irc_networks (region, irc_network, irc_channel, irc_port) VALUES (?, ?, ?, ?)")) {
				insert.setInt(1, regionId);
				insert.setString(2, ircNetwork);
				insert.setString(3, ircChannel);
				insert.setInt(4, Integer.parseInt(ircPort));
				insert.executeUpdate();
			}
		}
	}
	return Results.noContent();
}