Example usage for com.squareup.okhttp HttpUrl encodedFragment

List of usage examples for com.squareup.okhttp HttpUrl encodedFragment

Introduction

In this page you can find the example usage for com.squareup.okhttp HttpUrl encodedFragment.

Prototype

public String encodedFragment() 

Source Link

Usage

From source file:org.mariotaku.twidere.util.OAuthPasswordAuthenticator.java

License:Open Source License

public OAuthPasswordAuthenticator(final TwitterOAuth oauth,
        final LoginVerificationCallback loginVerificationCallback, final String userAgent) {
    final RestClient restClient = RestAPIFactory.getRestClient(oauth);
    this.oauth = oauth;
    this.client = (OkHttpRestClient) restClient.getRestClient();
    final OkHttpClient okhttp = client.getClient();
    okhttp.setCookieHandler(new CookieManager());
    okhttp.networkInterceptors().add(new Interceptor() {
        @Override//www  . ja  va2  s  . c o  m
        public Response intercept(Chain chain) throws IOException {
            final Response response = chain.proceed(chain.request());
            if (!response.isRedirect()) {
                return response;
            }
            final String location = response.header("Location");
            final Response.Builder builder = response.newBuilder();
            if (!TextUtils.isEmpty(location) && !endpoint.checkEndpoint(location)) {
                final HttpUrl originalLocation = HttpUrl
                        .get(URI.create("https://api.twitter.com/").resolve(location));
                final HttpUrl.Builder locationBuilder = HttpUrl.parse(endpoint.getUrl()).newBuilder();
                for (String pathSegments : originalLocation.pathSegments()) {
                    locationBuilder.addPathSegment(pathSegments);
                }
                for (int i = 0, j = originalLocation.querySize(); i < j; i++) {
                    final String name = originalLocation.queryParameterName(i);
                    final String value = originalLocation.queryParameterValue(i);
                    locationBuilder.addQueryParameter(name, value);
                }
                final String encodedFragment = originalLocation.encodedFragment();
                if (encodedFragment != null) {
                    locationBuilder.encodedFragment(encodedFragment);
                }
                final HttpUrl newLocation = locationBuilder.build();
                builder.header("Location", newLocation.toString());
            }
            return builder.build();
        }
    });
    this.endpoint = restClient.getEndpoint();
    this.loginVerificationCallback = loginVerificationCallback;
    this.userAgent = userAgent;
}