private static final String CONTENT_TYPE = "application/json";
private static final ObjectMapper mapper = new ObjectMapper();
protected ResourceResponse newResourceResponse(Attributes attributes)
{
final StringValue queriesParam = attributes.getRequest().getQueryParameters().getParameterValue("queries");
int qs = queriesParam.toInt(1);
if (qs < 1)
{
qs = 1;
}
else if (qs > 500)
{
qs = 500;
}
final int queries = qs;
final ResourceResponse response = new ResourceResponse();
response.setContentType(CONTENT_TYPE);
response.setWriteCallback(new WriteCallback()
{
public void writeData(Attributes attributes)
{
try
{
final ThreadLocalRandom random = ThreadLocalRandom.current();
DataSource dataSource = WicketApplication.get().getDataSource();
World[] worlds = new World[queries];
try (Connection connection = dataSource.getConnection())
{
try (PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM World WHERE id = ?",
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY))
{
for (int i = 0; i < queries; i++)
{
statement.setInt(1, random.nextInt(DB_ROWS) + 1);
try (ResultSet resultSet = statement.executeQuery())
{
resultSet.next();
worlds[i] = new World(
resultSet.getInt("id"),
resultSet.getInt("randomNumber"));
}
}
}
}
String data;
if (queriesParam.isNull())
{
// request to /db should return JSON object
data = HelloDbResponse.mapper.writeValueAsString(worlds[0]);
}
else