View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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  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   * Servlet Specific UpgradeResponse implementation.
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          // True in all other cases
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         // Transfer all headers to the real HTTP response
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 }