View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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   * Servlet Specific UpgradeResponse implementation.
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          // True in all other cases
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         // Transfer all headers to the real HTTP response
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 }