View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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.client.internal;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.eclipse.jetty.util.MultiMap;
29  import org.eclipse.jetty.websocket.core.api.UpgradeException;
30  import org.eclipse.jetty.websocket.core.api.UpgradeResponse;
31  import org.eclipse.jetty.websocket.core.protocol.ExtensionConfig;
32  
33  public class ClientUpgradeResponse implements UpgradeResponse
34  {
35      public static final String SEC_WEBSOCKET_PROTOCOL = "Sec-WebSocket-Protocol";
36      private int statusCode;
37      private String statusReason;
38      private MultiMap<String> headers;
39      private List<ExtensionConfig> extensions;
40      private boolean success = false;
41  
42      public ClientUpgradeResponse()
43      {
44          headers = new MultiMap<>();
45      }
46  
47      @Override
48      public void addHeader(String name, String value)
49      {
50          headers.add(name.toLowerCase(),value);
51      }
52  
53      @Override
54      public String getAcceptedSubProtocol()
55      {
56          return headers.getValue(SEC_WEBSOCKET_PROTOCOL,0);
57      }
58  
59      @Override
60      public List<ExtensionConfig> getExtensions()
61      {
62          return extensions;
63      }
64  
65      @Override
66      public Set<String> getHeaderNamesSet()
67      {
68          return headers.keySet();
69      }
70  
71      @Override
72      public String getHeaderValue(String name)
73      {
74          return headers.getValue(name.toLowerCase(),0);
75      }
76  
77      @Override
78      public Iterator<String> getHeaderValues(String name)
79      {
80          List<String> values = headers.getValues(name.toLowerCase());
81          if (values == null)
82          {
83              return Collections.emptyIterator();
84          }
85          return values.iterator();
86      }
87  
88      @Override
89      public int getStatusCode()
90      {
91          return statusCode;
92      }
93  
94      @Override
95      public String getStatusReason()
96      {
97          return statusReason;
98      }
99  
100     @Override
101     public boolean isSuccess()
102     {
103         return success;
104     }
105 
106     @Override
107     public void sendForbidden(String message) throws IOException
108     {
109         throw new UnsupportedOperationException("Not supported on client implementation");
110     }
111 
112     @Override
113     public void setAcceptedSubProtocol(String protocol)
114     {
115         headers.put(SEC_WEBSOCKET_PROTOCOL,protocol);
116     }
117 
118     @Override
119     public void setExtensions(List<ExtensionConfig> extensions)
120     {
121         if (this.extensions == null)
122         {
123             this.extensions = new ArrayList<>();
124         }
125         else
126         {
127             this.extensions.clear();
128         }
129         this.extensions.addAll(extensions);
130     }
131 
132     @Override
133     public void setHeader(String name, String value)
134     {
135         headers.putValues(name,value);
136     }
137 
138     public void setStatusCode(int statusCode)
139     {
140         this.statusCode = statusCode;
141     }
142 
143     public void setStatusReason(String statusReason)
144     {
145         this.statusReason = statusReason;
146     }
147 
148     @Override
149     public void validateWebSocketHash(String expectedHash) throws UpgradeException
150     {
151         String respHash = getHeaderValue("Sec-WebSocket-Accept");
152 
153         success = true;
154         if (expectedHash.equals(respHash) == false)
155         {
156             success = false;
157             throw new UpgradeException("Invalid Sec-WebSocket-Accept hash");
158         }
159     }
160 }