forked from LJYcoder/DevRing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
添加框架Cookie支持(支持非持久化Cookie和持久化Cookie)
- Loading branch information
谢杨
committed
Jan 24, 2019
1 parent
80b9bb1
commit 5d61fdc
Showing
12 changed files
with
662 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
devring/src/main/java/com/ljy/devring/persistentcookiejar/ClearableCookieJar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (C) 2016 Francisco José Montiel Navarro. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.ljy.devring.persistentcookiejar; | ||
|
||
import okhttp3.CookieJar; | ||
|
||
/** | ||
* This interface extends {@link CookieJar} and adds methods to clear the cookies. | ||
*/ | ||
public interface ClearableCookieJar extends CookieJar { | ||
|
||
/** | ||
* Clear all the session cookies while maintaining the persisted ones. | ||
*/ | ||
void clearSession(); | ||
|
||
/** | ||
* Clear all the cookies from persistence and from the cache. | ||
*/ | ||
void clear(); | ||
} |
95 changes: 95 additions & 0 deletions
95
devring/src/main/java/com/ljy/devring/persistentcookiejar/PersistentCookieJar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright (C) 2016 Francisco José Montiel Navarro. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.ljy.devring.persistentcookiejar; | ||
|
||
import com.ljy.devring.persistentcookiejar.cache.CookieCache; | ||
import com.ljy.devring.persistentcookiejar.persistence.CookiePersistor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import okhttp3.Cookie; | ||
import okhttp3.HttpUrl; | ||
|
||
public class PersistentCookieJar implements ClearableCookieJar { | ||
|
||
private CookieCache cache; | ||
private CookiePersistor persistor; | ||
|
||
public PersistentCookieJar(CookieCache cache, CookiePersistor persistor) { | ||
this.cache = cache; | ||
this.persistor = persistor; | ||
|
||
this.cache.addAll(persistor.loadAll()); | ||
} | ||
|
||
@Override | ||
synchronized public void saveFromResponse(HttpUrl url, List<Cookie> cookies) { | ||
cache.addAll(cookies); | ||
persistor.saveAll(filterPersistentCookies(cookies)); | ||
} | ||
|
||
private static List<Cookie> filterPersistentCookies(List<Cookie> cookies) { | ||
List<Cookie> persistentCookies = new ArrayList<>(); | ||
|
||
for (Cookie cookie : cookies) { | ||
if (cookie.persistent()) { | ||
persistentCookies.add(cookie); | ||
} | ||
} | ||
return persistentCookies; | ||
} | ||
|
||
@Override | ||
synchronized public List<Cookie> loadForRequest(HttpUrl url) { | ||
List<Cookie> cookiesToRemove = new ArrayList<>(); | ||
List<Cookie> validCookies = new ArrayList<>(); | ||
|
||
for (Iterator<Cookie> it = cache.iterator(); it.hasNext(); ) { | ||
Cookie currentCookie = it.next(); | ||
|
||
if (isCookieExpired(currentCookie)) { | ||
cookiesToRemove.add(currentCookie); | ||
it.remove(); | ||
|
||
} else if (currentCookie.matches(url)) { | ||
validCookies.add(currentCookie); | ||
} | ||
} | ||
|
||
persistor.removeAll(cookiesToRemove); | ||
|
||
return validCookies; | ||
} | ||
|
||
private static boolean isCookieExpired(Cookie cookie) { | ||
return cookie.expiresAt() < System.currentTimeMillis(); | ||
} | ||
|
||
@Override | ||
synchronized public void clearSession() { | ||
cache.clear(); | ||
cache.addAll(persistor.loadAll()); | ||
} | ||
|
||
@Override | ||
synchronized public void clear() { | ||
cache.clear(); | ||
persistor.clear(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
devring/src/main/java/com/ljy/devring/persistentcookiejar/cache/CookieCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2016 Francisco José Montiel Navarro. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.ljy.devring.persistentcookiejar.cache; | ||
|
||
import java.util.Collection; | ||
|
||
import okhttp3.Cookie; | ||
|
||
/** | ||
* A CookieCache handles the volatile cookie session storage. | ||
*/ | ||
public interface CookieCache extends Iterable<Cookie> { | ||
|
||
/** | ||
* Add all the new cookies to the session, existing cookies will be overwritten. | ||
* | ||
* @param cookies | ||
*/ | ||
void addAll(Collection<Cookie> cookies); | ||
|
||
/** | ||
* Clear all the cookies from the session. | ||
*/ | ||
void clear(); | ||
} |
72 changes: 72 additions & 0 deletions
72
devring/src/main/java/com/ljy/devring/persistentcookiejar/cache/IdentifiableCookie.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (C) 2016 Francisco José Montiel Navarro. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.ljy.devring.persistentcookiejar.cache; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import okhttp3.Cookie; | ||
|
||
/** | ||
* This class decorates a Cookie to re-implements equals() and hashcode() methods in order to identify | ||
* the cookie by the following attributes: name, domain, path, secure & hostOnly.<p> | ||
* | ||
* This new behaviour will be useful in determining when an already existing cookie in session must be overwritten. | ||
*/ | ||
class IdentifiableCookie { | ||
|
||
private Cookie cookie; | ||
|
||
static List<IdentifiableCookie> decorateAll(Collection<Cookie> cookies) { | ||
List<IdentifiableCookie> identifiableCookies = new ArrayList<>(cookies.size()); | ||
for (Cookie cookie : cookies) { | ||
identifiableCookies.add(new IdentifiableCookie(cookie)); | ||
} | ||
return identifiableCookies; | ||
} | ||
|
||
IdentifiableCookie(Cookie cookie) { | ||
this.cookie = cookie; | ||
} | ||
|
||
Cookie getCookie() { | ||
return cookie; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (!(other instanceof IdentifiableCookie)) return false; | ||
IdentifiableCookie that = (IdentifiableCookie) other; | ||
return that.cookie.name().equals(this.cookie.name()) | ||
&& that.cookie.domain().equals(this.cookie.domain()) | ||
&& that.cookie.path().equals(this.cookie.path()) | ||
&& that.cookie.secure() == this.cookie.secure() | ||
&& that.cookie.hostOnly() == this.cookie.hostOnly(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int hash = 17; | ||
hash = 31 * hash + cookie.name().hashCode(); | ||
hash = 31 * hash + cookie.domain().hashCode(); | ||
hash = 31 * hash + cookie.path().hashCode(); | ||
hash = 31 * hash + (cookie.secure() ? 0 : 1); | ||
hash = 31 * hash + (cookie.hostOnly() ? 0 : 1); | ||
return hash; | ||
} | ||
} |
Oops, something went wrong.