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

The following examples show how to use play.mvc.Results#noContent() . 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: 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 2
Source File: HappeningsController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
private Result doParseHappenings(String region, String xml) throws SQLException {
	NationStates api = new NationStates();
	Connection conn = null;
	ArrayList<HappeningData> happenings = new ArrayList<HappeningData>();
	try {
		RegionData regionData = api.getRegionInfo(api.getInfo(IOUtils.toInputStream(xml)), region);
		conn = getConnection();
		for (RegionHappening happening : regionData.happenings) {
			String text = Utils.formatHappeningText(happening.text, conn, "");
			HappeningData data = new HappeningData();
			data.happening = text;
			data.timestamp = happening.timestamp;
			happenings.add(data);
		}
	} catch (Exception e) {
		Logger.error("Unable to parse happenings for region [" + region + "]. Data: " + xml, e);
		return Results.status(INTERNAL_SERVER_ERROR);
	} finally {
		DbUtils.closeQuietly(conn);
	}
	
	String calculatedEtag = String.valueOf(happenings.hashCode());
	Result result = Utils.handleDefaultGetHeaders(request(), response(), calculatedEtag);

	if (happenings.isEmpty()) {
		return Results.noContent();
	} else	if (result != null) {
		return result;
	}

	return ok(Json.toJson(happenings)).as("application/json");
}
 
Example 3
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 4
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 5
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 6
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();
}