1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.websocket.servlet;
20
21 import java.io.IOException;
22 import java.util.List;
23 import java.util.Map;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.eclipse.jetty.websocket.api.UpgradeResponse;
27 import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
28
29
30
31
32 public class ServletUpgradeResponse extends UpgradeResponse
33 {
34 private HttpServletResponse response;
35 private boolean extensionsNegotiated = false;
36 private boolean subprotocolNegotiated = false;
37
38 public ServletUpgradeResponse(HttpServletResponse response)
39 {
40 this.response = response;
41 }
42
43 @Override
44 public int getStatusCode()
45 {
46 return response.getStatus();
47 }
48
49 public void setStatus(int status)
50 {
51 response.setStatus(status);
52 }
53
54 @Override
55 public String getStatusReason()
56 {
57 throw new UnsupportedOperationException("Server cannot get Status Reason Message");
58 }
59
60 public boolean isCommitted()
61 {
62 if (response != null)
63 {
64 return response.isCommitted();
65 }
66
67 return true;
68 }
69
70 public boolean isExtensionsNegotiated()
71 {
72 return extensionsNegotiated;
73 }
74
75 public boolean isSubprotocolNegotiated()
76 {
77 return subprotocolNegotiated;
78 }
79
80 public void sendError(int statusCode, String message) throws IOException
81 {
82 setSuccess(false);
83 commitHeaders();
84 response.sendError(statusCode, message);
85 response = null;
86 }
87
88 @Override
89 public void sendForbidden(String message) throws IOException
90 {
91 setSuccess(false);
92 commitHeaders();
93 response.sendError(HttpServletResponse.SC_FORBIDDEN, message);
94 response = null;
95 }
96
97 @Override
98 public void setAcceptedSubProtocol(String protocol)
99 {
100 super.setAcceptedSubProtocol(protocol);
101 subprotocolNegotiated = true;
102 }
103
104 @Override
105 public void setExtensions(List<ExtensionConfig> extensions)
106 {
107 super.setExtensions(extensions);
108 extensionsNegotiated = true;
109 }
110
111 public void complete()
112 {
113 commitHeaders();
114 response = null;
115 }
116
117 private void commitHeaders()
118 {
119
120 for (Map.Entry<String, List<String>> entry : getHeaders().entrySet())
121 {
122 for (String value : entry.getValue())
123 {
124 response.addHeader(entry.getKey(), value);
125 }
126 }
127 }
128 }