RequestHandler.java 955B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package com.zipcoder.server;
  2. import java.io.IOException;
  3. import java.net.Socket;
  4. public class RequestHandler {
  5. private final Socket clientSocket;
  6. public RequestHandler(Socket clientSocket){
  7. this.clientSocket = clientSocket;
  8. }
  9. public void run() {
  10. try {
  11. handleRequest();
  12. } catch (Exception e) {
  13. Logger.error("Cannot handle request because " + e.getMessage(), e);
  14. } finally {
  15. closeSocket();
  16. }
  17. }
  18. private void closeSocket() {
  19. if (clientSocket != null){
  20. try {
  21. clientSocket.close();
  22. } catch (IOException e) {
  23. Logger.error("Cannot close socket because " + e.getMessage(), e);
  24. }
  25. }
  26. }
  27. private void handleRequest() throws IOException {
  28. ResponseHello response = new ResponseHello(this.clientSocket.getOutputStream());
  29. response.writeResponse();
  30. }
  31. }