Java Code Examples for org.apache.wicket.request.http.WebResponse#setContentLength()

The following examples show how to use org.apache.wicket.request.http.WebResponse#setContentLength() . 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: HelloJsonResponse.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
 public void respond(Attributes attributes) 
 {
try
  {
    final WebResponse webResponse = (WebResponse) attributes.getResponse();
    webResponse.setContentLength(27);
    webResponse.setContentType(APPLICATION_JSON);
    JsonMessage message = new JsonMessage(HELLO_WORLD);
    byte[] json = MAPPER.writeValueAsBytes(message);
    webResponse.write(json);
  }
  catch (IOException ex)
  {
   // do nothing
  }
}
 
Example 2
Source File: HelloTextResource.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void respond(Attributes attributes)
{

  final WebResponse webResponse = (WebResponse) attributes.getResponse();
  webResponse.setContentType(CONTENT_TYPE);
  webResponse.setContentLength(DATA.length);
  webResponse.write(DATA);
}
 
Example 3
Source File: HelloDbResponse.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void respond(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;

 try 
 {
byte[] data = getDataFromDatabase(queriesParam, queries);

final WebResponse webResponse = (WebResponse) attributes.getResponse();
webResponse.setContentLength(data.length);
webResponse.setContentType(HelloJsonResponse.APPLICATION_JSON);
webResponse.write(data);
 } 
 catch (Exception ex)
 {
WebResponse response = (WebResponse) attributes.getResponse();

response.setContentType(TEXT_PLAIN);
response.setStatus(500);
response.write(ex.getClass().getSimpleName() + ": " + ex.getMessage());

ex.printStackTrace();
 }
}
 
Example 4
Source File: HelloDbUpdatesResource.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void respond(Attributes attributes)
{
  int _queries = attributes.getRequest().getQueryParameters().getParameterValue("queries").toInt(1);
  if (_queries < 1)
  {
    _queries = 1;
  }
  else if (_queries > 500)
  {
    _queries = 500;
  }
  final int queries = _queries;

  try
  {
    final ThreadLocalRandom random = ThreadLocalRandom.current();
    DataSource dataSource = WicketApplication.get().getDataSource();

    World[] worlds = new World[queries];
    try (Connection connection = dataSource.getConnection();
         PreparedStatement query = connection.prepareStatement(
                 "SELECT * FROM World WHERE id = ?",
                 ResultSet.TYPE_FORWARD_ONLY,
                 ResultSet.CONCUR_READ_ONLY);
         PreparedStatement update = connection.prepareStatement(
                 "UPDATE World SET randomNumber = ? WHERE id= ?"))
    {
      for (int i = 0; i < queries; i++)
      {
        query.setInt(1, random.nextInt(DB_ROWS) + 1);
        World world;
        try (ResultSet resultSet = query.executeQuery())
        {
          resultSet.next();
          world = new World(
              resultSet.getInt("id"),
              resultSet.getInt("randomNumber"));
        }
        world.randomNumber = random.nextInt(DB_ROWS) + 1;
        update.setInt(1, world.randomNumber);
        update.setInt(2, world.id);
        update.executeUpdate();
        worlds[i] = world;
      }
    }

    byte[] data = HelloJsonResponse.MAPPER.writeValueAsBytes(worlds);
    final WebResponse webResponse = (WebResponse) attributes.getResponse();
    webResponse.setContentLength(data.length);
    webResponse.setContentType(HelloJsonResponse.APPLICATION_JSON);
    webResponse.write(data);
  }
  catch (Exception ex)
  {
    // do nothing
  }
}