2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.icloud.internal.utilities;
15 import java.net.CookieManager;
16 import java.net.CookieStore;
17 import java.net.HttpCookie;
19 import java.util.List;
21 import org.eclipse.jdt.annotation.Nullable;
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.
27 * @author Simon Spielmann - Initial contribution
29 public class CustomCookieStore implements CookieStore {
31 private CookieStore cookieStore;
37 public CustomCookieStore() {
38 this.cookieStore = new CookieManager().getCookieStore();
42 public void add(@Nullable URI uri, @Nullable HttpCookie cookie) {
43 this.cookieStore.add(uri, cookie);
47 public @Nullable List<HttpCookie> get(@Nullable URI uri) {
48 List<HttpCookie> result = this.cookieStore.get(uri);
49 filterCookies(result);
54 public @Nullable List<HttpCookie> getCookies() {
55 List<HttpCookie> result = this.cookieStore.getCookies();
56 filterCookies(result);
61 public @Nullable List<URI> getURIs() {
62 return this.cookieStore.getURIs();
66 public boolean remove(@Nullable URI uri, @Nullable HttpCookie cookie) {
67 return this.cookieStore.remove(uri, cookie);
71 public boolean removeAll() {
72 return this.cookieStore.removeAll();
76 * Add quotes add beginning and end of all cookie values
78 * @param cookieList Current cookies. This list is modified in-place.
80 private void filterCookies(List<HttpCookie> cookieList) {
81 for (HttpCookie cookie : cookieList) {
82 if (!cookie.getValue().startsWith("\"")) {
83 cookie.setValue("\"" + cookie.getValue());
85 if (!cookie.getValue().endsWith("\"")) {
86 cookie.setValue(cookie.getValue() + "\"");