PlaintextHandler.java 987 B

1234567891011121314151617181920212223242526272829303132
  1. package hello;
  2. import static io.undertow.util.Headers.CONTENT_TYPE;
  3. import static java.nio.charset.StandardCharsets.US_ASCII;
  4. import io.undertow.server.HttpHandler;
  5. import io.undertow.server.HttpServerExchange;
  6. import java.nio.ByteBuffer;
  7. /**
  8. * Handles the plaintext test.
  9. */
  10. final class PlaintextHandler implements HttpHandler {
  11. @Override
  12. public void handleRequest(HttpServerExchange exchange) {
  13. exchange.getResponseHeaders().put(CONTENT_TYPE, "text/plain");
  14. exchange.getResponseSender().send(buffer.duplicate());
  15. }
  16. // Normally, one would send the string "Hello, World!" directly. Reusing a
  17. // ByteBuffer is a micro-optimization that is explicitly permitted by the
  18. // plaintext test requirements.
  19. private static final ByteBuffer buffer;
  20. static {
  21. String message = "Hello, World!";
  22. byte[] messageBytes = message.getBytes(US_ASCII);
  23. buffer = ByteBuffer.allocateDirect(messageBytes.length);
  24. buffer.put(messageBytes);
  25. buffer.flip();
  26. }
  27. }