View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.websocket;
16  
17  import java.net.CookieStore;
18  import java.net.HttpCookie;
19  import java.net.URI;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.htmlunit.WebClient;
24  import org.htmlunit.javascript.host.WebSocket;
25  import org.htmlunit.util.Cookie;
26  
27  /**
28   * A helper class for {@link WebSocket}.
29   *
30   * @author Ahmed Ashour
31   * @author Ronald Brill
32   */
33  class WebSocketCookieStore implements CookieStore {
34  
35      private final WebClient webClient_;
36  
37      WebSocketCookieStore(final WebClient webClient) {
38          webClient_ = webClient;
39      }
40  
41      /**
42       * {@inheritDoc}
43       */
44      @Override
45      public void add(final URI uri, final HttpCookie cookie) {
46          throw new UnsupportedOperationException();
47      }
48  
49      /**
50       * {@inheritDoc}
51       */
52      @Override
53      public List<HttpCookie> get(final URI uri) {
54          final List<HttpCookie> cookies = new ArrayList<>();
55          try {
56              final String urlString = uri.toString().replace("ws://", "http://").replace("wss://", "https://");
57              final java.net.URL url = new java.net.URL(urlString);
58              for (final Cookie cookie : webClient_.getCookies(url)) {
59                  final HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
60                  httpCookie.setVersion(0);
61                  cookies.add(httpCookie);
62              }
63          }
64          catch (final Exception e) {
65              throw new RuntimeException(e);
66          }
67          return cookies;
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public List<HttpCookie> getCookies() {
75          throw new UnsupportedOperationException();
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public List<URI> getURIs() {
83          throw new UnsupportedOperationException();
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public boolean remove(final URI uri, final HttpCookie cookie) {
91          throw new UnsupportedOperationException();
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public boolean removeAll() {
99          return false;
100     }
101 }