use of com.firenio.codec.http11.Cookie in project MVPFrames by RockyQu.
the class SharedPrefsCookiePersistor method removeAll.
@Override
public void removeAll(Collection<Cookie> cookies) {
SharedPreferences.Editor editor = sharedPreferences.edit();
for (Cookie cookie : cookies) {
editor.remove(createCookieKey(cookie));
}
editor.commit();
}
use of com.firenio.codec.http11.Cookie in project MVPFrames by RockyQu.
the class SharedPrefsCookiePersistor method loadAll.
@Override
public List<Cookie> loadAll() {
List<Cookie> cookies = new ArrayList<>(sharedPreferences.getAll().size());
for (Map.Entry<String, ?> entry : sharedPreferences.getAll().entrySet()) {
String serializedCookie = (String) entry.getValue();
Cookie cookie = new SerializableCookie().decode(serializedCookie);
if (cookie != null) {
cookies.add(cookie);
}
}
return cookies;
}
use of com.firenio.codec.http11.Cookie in project MVPFrames by RockyQu.
the class PersistentCookieJar method loadForRequest.
@Override
public synchronized 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);
if (cookieLoadForRequest != null) {
validCookies = cookieLoadForRequest.loadForRequest(validCookies);
}
return validCookies;
}
use of com.firenio.codec.http11.Cookie in project MVPFrames by RockyQu.
the class AppConfiguration method applyOptions.
@Override
public void applyOptions(final Context context, AppConfigModule.Builder builder) {
builder.httpUrl(Api.APP_DOMAIN).cacheFile(new File(ProjectUtils.CACHE)).networkHandler(new // Http全局响应结果的处理类
NetworkHandler() {
@Override
public Request onHttpRequest(Interceptor.Chain chain, Request request) {
// return chain.request().newBuilder().header("token", tokenId).build();
return request;
}
@Override
public Response onHttpResponse(String result, Interceptor.Chain chain, Request request, Response response) {
return response;
}
}).interceptors(new Interceptor[] { new LoggingInterceptor(), new ParameterInterceptor(new ParameterInterceptor.ParameterCallback() {
/**
* 这里为接口添加类型为HashMap的统一参数,例如Token、版本号等。支持Get、Post方法,ParameterInterceptor会自动判断
*/
@Override
public HashMap<String, Object> parameters() {
User user = (User) ((App) context).getAppComponent().extras().get(LoginActivity.class.getName());
HashMap<String, Object> parameters = new HashMap<>();
if (user != null) {
// 为接口统一添加access_token参数
parameters.put("access_token", user.getToken());
}
return parameters;
}
}) }).retrofitConfiguration(new // 扩展自定义配置Retrofit参数
HttpModule.RetrofitConfiguration() {
@Override
public void configRetrofit(Context context, Retrofit.Builder builder) {
}
}).okHttpConfiguration(new // 扩展自定义配置OkHttp参数
HttpModule.OkHttpConfiguration() {
@Override
public void configOkHttp(Context context, OkHttpClient.Builder builder) {
builder.sslSocketFactory(SSL.createSSLSocketFactory(), new TrustAllX509TrustManager());
builder.hostnameVerifier(new TrustAllHostnameVerifier());
}
}).gsonConfiguration(new // 扩展自定义配置Gson参数
AppModule.GsonConfiguration() {
@Override
public void configGson(Context context, GsonBuilder builder) {
builder.serializeNulls().registerTypeAdapter(ResponseEntity.class, new GsonResponseDeserializer());
}
}).cookieLoadForRequest(new PersistentCookieJar.CookieLoadForRequest() {
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
}
@Override
public List<Cookie> loadForRequest(List<Cookie> cookies) {
return cookies;
}
});
}
use of com.firenio.codec.http11.Cookie in project edx-app-android by edx.
the class NoCacheHeaderStrippingInterceptor method intercept.
@NonNull
@Override
public Response intercept(@NonNull final Chain chain) throws IOException {
final Request request = chain.request();
final Response response = chain.proceed(request);
final Headers headers = response.headers();
Headers.Builder strippedHeadersBuilder = null;
List<String> headersToStrip = null;
for (int i = 0, headersCount = headers.size(); i < headersCount; i++) {
final String headerName = headers.name(i);
final String headerValue = headers.value(i);
if (headerName.equalsIgnoreCase("Cache-Control")) {
Matcher directiveMatcher = PATTERN_NO_CACHE_HEADER.matcher(headerValue);
if (directiveMatcher.find()) {
if (strippedHeadersBuilder == null) {
strippedHeadersBuilder = new Headers.Builder();
for (int j = 0; j < i; j++) {
strippedHeadersBuilder.add(headers.name(j), headers.value(j));
}
headersToStrip = new ArrayList<>();
}
String newHeaderValue = headerValue;
while (true) {
Collections.addAll(headersToStrip, directiveMatcher.group(GROUP_NO_CACHE_HEADERS).trim().split("\\s*,\\s*"));
final StringBuffer newHeaderValueBuffer = new StringBuffer();
directiveMatcher.appendReplacement(newHeaderValueBuffer, "$" + (directiveMatcher.group(GROUP_SEPARATOR_START).isEmpty() ? GROUP_SEPARATOR_END : GROUP_SEPARATOR_START));
directiveMatcher.appendTail(newHeaderValueBuffer);
newHeaderValue = newHeaderValueBuffer.toString();
directiveMatcher = PATTERN_NO_CACHE_HEADER.matcher(newHeaderValue);
if (!directiveMatcher.find())
break;
}
if (!newHeaderValue.isEmpty()) {
strippedHeadersBuilder.add(headerName, newHeaderValue);
}
continue;
}
}
if (strippedHeadersBuilder != null) {
strippedHeadersBuilder.add(headerName, headerValue);
}
}
if (strippedHeadersBuilder == null) {
return response;
}
final HttpUrl url = request.url();
List<Cookie> cookies = null;
for (final String headerToStrip : headersToStrip) {
strippedHeadersBuilder.removeAll(headerToStrip);
if (headerToStrip.equalsIgnoreCase("Set-Cookie")) {
if (cookieJar != CookieJar.NO_COOKIES) {
for (final String cookieString : headers.values(headerToStrip)) {
Cookie cookie = Cookie.parse(url, cookieString);
if (cookie != null) {
if (cookies == null) {
cookies = new ArrayList<>();
}
cookies.add(cookie);
}
}
}
}
}
if (cookies != null) {
cookieJar.saveFromResponse(url, cookies);
}
return response.newBuilder().headers(strippedHeadersBuilder.build()).build();
}
Aggregations