Search in sources :

Example 6 with Cookie

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();
}
Also used : Cookie(okhttp3.Cookie) SharedPreferences(android.content.SharedPreferences)

Example 7 with Cookie

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;
}
Also used : Cookie(okhttp3.Cookie) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 8 with Cookie

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;
}
Also used : Cookie(okhttp3.Cookie) ArrayList(java.util.ArrayList)

Example 9 with Cookie

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;
        }
    });
}
Also used : User(com.frame.mvp.entity.User) OkHttpClient(okhttp3.OkHttpClient) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) GsonResponseDeserializer(com.frame.mvp.app.utils.gson.GsonResponseDeserializer) TrustAllX509TrustManager(com.tool.common.http.ssl.TrustAllX509TrustManager) NetworkHandler(com.tool.common.http.NetworkHandler) List(java.util.List) ParameterInterceptor(com.tool.common.http.interceptor.ParameterInterceptor) Context(android.content.Context) Cookie(okhttp3.Cookie) LoggingInterceptor(com.tool.common.http.interceptor.LoggingInterceptor) GsonBuilder(com.google.gson.GsonBuilder) PersistentCookieJar(com.tool.common.http.cookie.PersistentCookieJar) Request(okhttp3.Request) HttpModule(com.tool.common.di.module.HttpModule) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) TrustAllHostnameVerifier(com.tool.common.http.ssl.TrustAllHostnameVerifier) File(java.io.File)

Example 10 with Cookie

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();
}
Also used : Cookie(okhttp3.Cookie) Matcher(java.util.regex.Matcher) Headers(okhttp3.Headers) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) NonNull(android.support.annotation.NonNull)

Aggregations

Cookie (okhttp3.Cookie)102 ArrayList (java.util.ArrayList)31 IOException (java.io.IOException)20 HttpUrl (okhttp3.HttpUrl)18 ByteArrayInputStream (java.io.ByteArrayInputStream)16 ObjectInputStream (java.io.ObjectInputStream)16 SharedPreferences (android.content.SharedPreferences)12 Context (android.content.Context)8 HashMap (java.util.HashMap)8 List (java.util.List)8 Map (java.util.Map)8 Test (org.junit.Test)8 Response (okhttp3.Response)7 SerializableCookie (com.lzy.okgo.cookie.SerializableCookie)6 Request (okhttp3.Request)6 SuppressLint (android.annotation.SuppressLint)4 CookieManager (android.webkit.CookieManager)4 EhCookieStore (com.hippo.ehviewer.client.EhCookieStore)4 OnClick (butterknife.OnClick)3 SerializableCookie (com.franmontiel.persistentcookiejar.persistence.SerializableCookie)3