]> git.basschouten.com Git - openhab-addons.git/blob
90fae3d3cc13035a052f87cdbf473850a8dbd5b3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.icloud.internal.utilities;
14
15 import java.net.CookieManager;
16 import java.net.CookieStore;
17 import java.net.HttpCookie;
18 import java.net.URI;
19 import java.util.List;
20
21 import org.eclipse.jdt.annotation.Nullable;
22
23 /**
24  * This class implements a customized {@link CookieStore}. Its purpose is to add hyphens at the beginning and end of
25  * each cookie value which is required by Apple iCloud API.
26  *
27  * @author Simon Spielmann - Initial contribution
28  */
29 public class CustomCookieStore implements CookieStore {
30
31     private CookieStore cookieStore;
32
33     /**
34      * The constructor.
35      *
36      */
37     public CustomCookieStore() {
38         this.cookieStore = new CookieManager().getCookieStore();
39     }
40
41     @Override
42     public void add(@Nullable URI uri, @Nullable HttpCookie cookie) {
43         this.cookieStore.add(uri, cookie);
44     }
45
46     @Override
47     public @Nullable List<HttpCookie> get(@Nullable URI uri) {
48         List<HttpCookie> result = this.cookieStore.get(uri);
49         filterCookies(result);
50         return result;
51     }
52
53     @Override
54     public @Nullable List<HttpCookie> getCookies() {
55         List<HttpCookie> result = this.cookieStore.getCookies();
56         filterCookies(result);
57         return result;
58     }
59
60     @Override
61     public @Nullable List<URI> getURIs() {
62         return this.cookieStore.getURIs();
63     }
64
65     @Override
66     public boolean remove(@Nullable URI uri, @Nullable HttpCookie cookie) {
67         return this.cookieStore.remove(uri, cookie);
68     }
69
70     @Override
71     public boolean removeAll() {
72         return this.cookieStore.removeAll();
73     }
74
75     /**
76      * Add quotes add beginning and end of all cookie values
77      *
78      * @param cookieList Current cookies. This list is modified in-place.
79      */
80     private void filterCookies(List<HttpCookie> cookieList) {
81         for (HttpCookie cookie : cookieList) {
82             if (!cookie.getValue().startsWith("\"")) {
83                 cookie.setValue("\"" + cookie.getValue());
84             }
85             if (!cookie.getValue().endsWith("\"")) {
86                 cookie.setValue(cookie.getValue() + "\"");
87             }
88         }
89     }
90 }