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.jsr356;
20  
21  import java.net.HttpCookie;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.websocket.ClientEndpointConfig.Configurator;
26  
27  import org.eclipse.jetty.websocket.api.UpgradeRequest;
28  import org.eclipse.jetty.websocket.api.UpgradeResponse;
29  import org.eclipse.jetty.websocket.client.io.UpgradeListener;
30  
31  public class JsrUpgradeListener implements UpgradeListener
32  {
33      private Configurator configurator;
34  
35      public JsrUpgradeListener(Configurator configurator)
36      {
37          this.configurator = configurator;
38      }
39  
40      @Override
41      public void onHandshakeRequest(UpgradeRequest request)
42      {
43          if (configurator == null)
44          {
45              return;
46          }
47  
48          Map<String, List<String>> headers = request.getHeaders();
49          configurator.beforeRequest(headers);
50  
51          // Handle cookies
52          for (String name : headers.keySet())
53          {
54              if ("cookie".equalsIgnoreCase(name))
55              {
56                  List<String> values = headers.get(name);
57                  if (values != null)
58                  {
59                      for (String cookie : values)
60                      {
61                          List<HttpCookie> cookies = HttpCookie.parse(cookie);
62                          request.getCookies().addAll(cookies);
63                      }
64                  }
65              }
66          }
67      }
68  
69      @Override
70      public void onHandshakeResponse(UpgradeResponse response)
71      {
72          if (configurator == null)
73          {
74              return;
75          }
76  
77          JsrHandshakeResponse hr = new JsrHandshakeResponse(response);
78          configurator.afterResponse(hr);
79      }
80  }