< prev index next >
   1 package java.net.http;
   2 
   3 import java.net.http.WebSocket.CloseCode;
   4 
   5 import static java.net.http.WebSocket.CloseCode.PROTOCOL_ERROR;
   6 import static java.util.Objects.requireNonNull;
   7 
   8 //
   9 // Special kind of exception closed from the outside world.
  10 //
  11 // Used as a "marker exception" for protocol issues in the incoming data, so the
  12 // implementation could close the connection and specify an appropriate status
  13 // code.
  14 //
  15 // A separate 'section' argument makes it more uncomfortable to be lazy and to
  16 // leave a relevant spec reference empty :-) As a bonus all messages have the
  17 // same style.
  18 //
  19 final class WebSocketProtocolException extends RuntimeException {
  20 
  21     private static final long serialVersionUID = 1L;
  22     private final CloseCode closeCode;
  23     private final String section;
  24 
  25     WebSocketProtocolException(String section, String detail) {
  26         this(section, detail, PROTOCOL_ERROR);
  27     }
  28 
  29     WebSocketProtocolException(String section, String detail, Throwable cause) {
  30         this(section, detail, PROTOCOL_ERROR, cause);
  31     }
  32 
  33     private WebSocketProtocolException(String section, String detail, CloseCode code) {
  34         super(formatMessage(section, detail));
  35         this.closeCode = requireNonNull(code);
  36         this.section = section;
  37     }
  38 
  39     WebSocketProtocolException(String section, String detail, CloseCode code,
  40                                Throwable cause) {
  41         super(formatMessage(section, detail), cause);
  42         this.closeCode = requireNonNull(code);
  43         this.section = section;
  44     }
  45 
  46     private static String formatMessage(String section, String detail) {
  47         if (requireNonNull(section).isEmpty()) {
  48             throw new IllegalArgumentException();
  49         }
  50         if (requireNonNull(detail).isEmpty()) {
  51             throw new IllegalArgumentException();
  52         }
  53         return Utils.webSocketSpecViolation(section, detail);
  54     }
  55 
  56     CloseCode getCloseCode() {
  57         return closeCode;
  58     }
  59 
  60     public String getSection() {
  61         return section;
  62     }
  63 
  64     @Override
  65     public String toString() {
  66         return super.toString() + "[" + closeCode + "]";
  67     }
  68 }
< prev index next >