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   */
32  class WebSocketCookieStore implements CookieStore {
33  
34      private final WebClient webClient_;
35  
36      WebSocketCookieStore(final WebClient webClient) {
37          webClient_ = webClient;
38      }
39  
40      /**
41       * {@inheritDoc}
42       */
43      @Override
44      public void add(final URI uri, final HttpCookie cookie) {
45          throw new UnsupportedOperationException();
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      @Override
52      public List<HttpCookie> get(final URI uri) {
53          final List<HttpCookie> cookies = new ArrayList<>();
54          try {
55              final String urlString = uri.toString().replace("ws://", "http://").replace("wss://", "https://");
56              final java.net.URL url = new java.net.URL(urlString);
57              for (final Cookie cookie : webClient_.getCookies(url)) {
58                  final HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
59                  httpCookie.setVersion(0);
60                  cookies.add(httpCookie);
61              }
62          }
63          catch (final Exception e) {
64              throw new RuntimeException(e);
65          }
66          return cookies;
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public List<HttpCookie> getCookies() {
74          throw new UnsupportedOperationException();
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public List<URI> getURIs() {
82          throw new UnsupportedOperationException();
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public boolean remove(final URI uri, final HttpCookie cookie) {
90          throw new UnsupportedOperationException();
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public boolean removeAll() {
98          return false;
99      }
100 }