Skip to content

Commit d76795d

Browse files
author
hideki
committed
Applied feedbacks from @pasin
1 parent 64fbf0c commit d76795d

5 files changed

Lines changed: 101 additions & 51 deletions

File tree

src/main/java/com/couchbase/lite/replicator/Replication.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
/**
2-
* Copyright (c) 2016 Couchbase, Inc. All rights reserved.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5-
* except in compliance with the License. You may obtain a copy of the License at
6-
*
7-
* http://www.apache.org/licenses/LICENSE-2.0
8-
*
9-
* Unless required by applicable law or agreed to in writing, software distributed under the
10-
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11-
* either express or implied. See the License for the specific language governing permissions
12-
* and limitations under the License.
13-
*/
1+
//
2+
// Copyright (c) 2016 Couchbase, Inc. All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
// except in compliance with the License. You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software distributed under the
10+
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
// either express or implied. See the License for the specific language governing permissions
12+
// and limitations under the License.
13+
//
1414
package com.couchbase.lite.replicator;
1515

1616
import com.couchbase.lite.AsyncTask;
@@ -696,9 +696,9 @@ public void setFilterParams(Map<String, Object> filterParams) {
696696
replicationInternal.setFilterParams(filterParams);
697697
}
698698

699-
/** The server user name that the authenticator has logged in as, if known. Observable. */
699+
/** The server user name that the authenticator has logged in as, if known. */
700700
@InterfaceAudience.Public
701-
public String getUsername(){
701+
public String getUsername() {
702702
return replicationInternal.getUsername();
703703
}
704704

@@ -751,15 +751,15 @@ public void deleteCookie(String name) {
751751
* replication's CBLAuthenticator. Also removes session cookies from the cookie store.
752752
*/
753753
@InterfaceAudience.Public
754-
public boolean removeStoredCredentials(){
754+
public boolean removeStoredCredentials() {
755755
if (getAuthenticator() != null) {
756756
if (!(getAuthenticator() instanceof Authorizer) ||
757757
!((Authorizer) getAuthenticator()).removeStoredCredentials())
758758
return false;
759-
}else {
759+
} else {
760760
// CBL Java does not use credential. No thing to do
761761
}
762-
replicationInternal.resetCookieStore();
762+
replicationInternal.deleteCookie(remote);
763763
return true;
764764
}
765765

src/main/java/com/couchbase/lite/replicator/ReplicationInternal.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected enum ChangeListenerNotifyStyle {
105105
protected String lastSequence;
106106
protected Authenticator authenticator;
107107
protected boolean authenticating = false;
108-
protected String username;
108+
private String username;
109109
protected String filterName;
110110
protected Map<String, Object> filterParams;
111111
protected List<String> documentIDs;
@@ -1799,7 +1799,11 @@ public void deleteCookie(String name) {
17991799
this.clientFactory.deleteCookie(name);
18001800
}
18011801

1802-
/* package */ void resetCookieStore(){
1802+
/* package */ void deleteCookie(URL url) {
1803+
this.clientFactory.deleteCookie(url);
1804+
}
1805+
1806+
/* package */ void resetCookieStore() {
18031807
this.clientFactory.resetCookieStore();
18041808
}
18051809

src/main/java/com/couchbase/lite/support/CouchbaseLiteHttpClientFactory.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import com.couchbase.lite.internal.InterfaceAudience;
1717

18+
import java.net.URL;
1819
import java.security.GeneralSecurityException;
1920
import java.security.cert.CertificateException;
2021
import java.security.cert.X509Certificate;
@@ -29,6 +30,7 @@
2930

3031
import okhttp3.Cookie;
3132
import okhttp3.CookieJar;
33+
import okhttp3.HttpUrl;
3234
import okhttp3.OkHttpClient;
3335

3436
public class CouchbaseLiteHttpClientFactory implements HttpClientFactory {
@@ -40,11 +42,11 @@ public class CouchbaseLiteHttpClientFactory implements HttpClientFactory {
4042

4143
// deprecated
4244
public static int DEFAULT_SO_TIMEOUT_SECONDS = 40; // 40 sec (previously it was 5 min)
43-
// heartbeat value 30sec + 10 sec
45+
// heartbeat value 30sec + 10 sec
4446

4547
// OkHttp Default Timeout is 10 sec for all timeout settings
4648
public static int DEFAULT_CONNECTION_TIMEOUT_SECONDS = 10;
47-
public static int DEFAULT_READ_TIMEOUT = DEFAULT_SO_TIMEOUT_SECONDS;
49+
public static int DEFAULT_READ_TIMEOUT = DEFAULT_SO_TIMEOUT_SECONDS;
4850
public static int DEFAULT_WRITE_TIMEOUT = 10;
4951

5052
/**
@@ -128,9 +130,38 @@ synchronized public void deleteCookie(String name) {
128130
cookieJar.saveFromResponse(null, retainedCookies);
129131
}
130132

133+
static private boolean isMatch(Cookie cookie, URL url) {
134+
return cookie.matches(HttpUrl.get(url));
135+
}
136+
137+
@Override
138+
@InterfaceAudience.Private
139+
synchronized public void deleteCookie(URL url) {
140+
// since CookieStore does not have a way to delete an individual cookie, do workaround:
141+
// 1. get all cookies
142+
// 2. filter list to strip out the one we want to delete
143+
// 3. clear cookie store
144+
// 4. re-add all cookies except the one we want to delete
145+
if (cookieJar == null)
146+
return;
147+
148+
List<Cookie> cookies = cookieJar.loadForRequest(null);
149+
List<Cookie> retainedCookies = new ArrayList<Cookie>();
150+
for (Cookie cookie : cookies) {
151+
// matching rely on OkHttp's matching logic
152+
// https://square.github.io/okhttp/3.x/okhttp/okhttp3/Cookie.html#matches-okhttp3.HttpUrl-
153+
if (!cookie.matches(HttpUrl.get(url)))
154+
retainedCookies.add(cookie);
155+
}
156+
cookieJar.clear();
157+
158+
// TODO: HttpUrl parameter should be revisited.
159+
cookieJar.saveFromResponse(null, retainedCookies);
160+
}
161+
131162
@Override
132163
@InterfaceAudience.Private
133-
synchronized public void resetCookieStore(){
164+
synchronized public void resetCookieStore() {
134165
if (cookieJar == null)
135166
return;
136167
cookieJar.clear();

src/main/java/com/couchbase/lite/support/HttpClientFactory.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
/**
2-
* Copyright (c) 2016 Couchbase, Inc. All rights reserved.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5-
* except in compliance with the License. You may obtain a copy of the License at
6-
*
7-
* http://www.apache.org/licenses/LICENSE-2.0
8-
*
9-
* Unless required by applicable law or agreed to in writing, software distributed under the
10-
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11-
* either express or implied. See the License for the specific language governing permissions
12-
* and limitations under the License.
13-
*/
1+
//
2+
// Copyright (c) 2016 Couchbase, Inc. All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
// except in compliance with the License. You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software distributed under the
10+
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
// either express or implied. See the License for the specific language governing permissions
12+
// and limitations under the License.
13+
//
1414
package com.couchbase.lite.support;
1515

16+
import java.net.URL;
1617
import java.util.List;
1718

1819
import okhttp3.Cookie;
@@ -26,6 +27,8 @@ public interface HttpClientFactory {
2627

2728
void deleteCookie(String name);
2829

30+
void deleteCookie(URL url);
31+
2932
void resetCookieStore();
3033

3134
CookieJar getCookieStore();

src/main/java/com/couchbase/lite/support/PersistentCookieJar.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
/**
2-
* Copyright (c) 2011 James Smith <james@loopj.com>
3-
* Copyright (c) 2016 Couchbase, Inc.
4-
*
5-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
6-
* except in compliance with the License. You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software distributed under the
11-
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12-
* either express or implied. See the License for the specific language governing permissions
13-
* and limitations under the License.
14-
*/
1+
//
2+
// Copyright (c) 2016 Couchbase, Inc. All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
// except in compliance with the License. You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software distributed under the
10+
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
// either express or implied. See the License for the specific language governing permissions
12+
// and limitations under the License.
13+
//
1514
package com.couchbase.lite.support;
1615

1716
import com.couchbase.lite.CouchbaseLiteException;
1817
import com.couchbase.lite.Database;
1918
import com.couchbase.lite.util.Log;
2019

2120
import java.lang.ref.WeakReference;
21+
import java.net.URL;
2222
import java.util.ArrayList;
2323
import java.util.Date;
2424
import java.util.HashMap;
@@ -136,9 +136,21 @@ public void saveFromResponse(HttpUrl httpUrl, List<okhttp3.Cookie> list) {
136136
}
137137
}
138138

139+
/**
140+
* @return return all cookies if httpUrl is null, otherwise return matched cookies.
141+
*/
139142
@Override
140143
public List<okhttp3.Cookie> loadForRequest(HttpUrl httpUrl) {
141-
return new ArrayList(cookies.values());
144+
List<Cookie> list = new ArrayList<Cookie>();
145+
if (httpUrl == null) {
146+
list.addAll(cookies.values());
147+
} else {
148+
for (Cookie cookie : cookies.values()) {
149+
if (cookie.matches(httpUrl))
150+
list.add(cookie);
151+
}
152+
}
153+
return list;
142154
}
143155

144156
////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)