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