3 files changed +31
-8
lines changed Original file line number Diff line number Diff line change 12
12
<packaging >war</packaging >
13
13
14
14
<name >Java EE 7 Sample: servlet - protocol-handler</name >
15
+
16
+ <build >
17
+ <plugins >
18
+ <plugin >
19
+ <groupId >org.apache.maven.plugins</groupId >
20
+ <artifactId >maven-surefire-plugin</artifactId >
21
+ <configuration >
22
+ <systemPropertyVariables combine.children=" append" >
23
+ <!-- to request protocol upgrade, the client must send the Connection and Upgrade headers -->
24
+ <sun .net.http.allowRestrictedHeaders>true</sun .net.http.allowRestrictedHeaders>
25
+ </systemPropertyVariables >
26
+ </configuration >
27
+ </plugin >
28
+ </plugins >
29
+ </build >
15
30
</project >
Original file line number Diff line number Diff line change 39
39
*/
40
40
package org .javaee7 .servlet .protocolhandler ;
41
41
42
+ import static javax .servlet .http .HttpServletResponse .SC_BAD_REQUEST ;
42
43
import static javax .servlet .http .HttpServletResponse .SC_SWITCHING_PROTOCOLS ;
43
44
44
45
import java .io .IOException ;
@@ -66,11 +67,16 @@ public class UpgradeServlet extends HttpServlet {
66
67
* @throws IOException if an I/O error occurs
67
68
*/
68
69
protected void doGet (HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
69
- response .setStatus (SC_SWITCHING_PROTOCOLS );
70
- response .setHeader ("Connection" , "Upgrade" );
71
- response .setHeader ("Upgrade" , "echo" );
72
- request .upgrade (MyProtocolHandler .class );
73
-
74
- System .out .println ("Request upgraded to MyProtocolHandler" );
70
+ String requestedUpgrade = request .getHeader ("Upgrade" );
71
+ if ("echo" .equals (requestedUpgrade )) {
72
+ response .setStatus (SC_SWITCHING_PROTOCOLS );
73
+ response .setHeader ("Connection" , "Upgrade" );
74
+ response .setHeader ("Upgrade" , "echo" );
75
+ request .upgrade (MyProtocolHandler .class );
76
+
77
+ System .out .println ("Request upgraded to MyProtocolHandler" );
78
+ } else {
79
+ response .sendError (SC_BAD_REQUEST , "unknown upgrade " + requestedUpgrade );
80
+ }
75
81
}
76
82
}
Original file line number Diff line number Diff line change 1
1
package org .javaee7 .servlet .protocolhandler ;
2
2
3
- import static org .junit .Assert .assertTrue ;
3
+ import static org .junit .Assert .assertEquals ;
4
4
5
5
import java .io .IOException ;
6
6
import java .io .InputStream ;
@@ -44,6 +44,8 @@ public void testUpgradeProtocol() throws IOException, URISyntaxException {
44
44
// typically hang when reading.
45
45
46
46
URLConnection connection = new URL (base , "UpgradeServlet" ).openConnection ();
47
+ connection .setRequestProperty ("Connection" , "Upgrade" );
48
+ connection .setRequestProperty ("Upgrade" , "echo" );
47
49
connection .setConnectTimeout (2000 );
48
50
connection .setReadTimeout (2000 );
49
51
@@ -71,7 +73,7 @@ public void testUpgradeProtocol() throws IOException, URISyntaxException {
71
73
}
72
74
}
73
75
74
- assertTrue ("In protocol handler" . equals ( response .toString () ));
76
+ assertEquals ("In protocol handler" , response .toString ());
75
77
}
76
78
77
79
}
0 commit comments