Skip to content

Instantly share code, notes, and snippets.

@goofyahead
Forked from alex-shpak/Interceptor.java
Created June 7, 2017 04:08
Show Gist options
  • Save goofyahead/c638716f3736d3d9316784533497a03a to your computer and use it in GitHub Desktop.
Save goofyahead/c638716f3736d3d9316784533497a03a to your computer and use it in GitHub Desktop.

Revisions

  1. @alex-shpak alex-shpak revised this gist Jun 26, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Interceptor.java
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ public Response intercept(Chain chain) throws IOException {
    }
    }

    if(settings.hasAccessToken()) { //retry requires new auth token,
    if(settings.getAccessToken() != null) { //retry requires new auth token,
    setAuthHeader(builder, settings.getAccessToken()); //set auth token to updated
    request = builder.build();
    return chain.proceed(request); //repeat request with new token
  2. @alex-shpak alex-shpak revised this gist Apr 9, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Interceptor.java
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ private void setAuthHeader(Request.Builder builder, String token) {
    }

    private int refreshToken() {
    //Refresh token, synchronously, and return result code
    //Refresh token, synchronously, save it, and return result code
    //you might use retrofit here
    }

  3. @alex-shpak alex-shpak revised this gist Apr 9, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Interceptor.java
    Original file line number Diff line number Diff line change
    @@ -38,14 +38,17 @@ public Response intercept(Chain chain) throws IOException {

    return response;
    }

    private void setAuthHeader(Request.Builder builder, String token) {
    if (token != null) //Add Auth token to each request if authorized
    builder.header("Authorization", String.format("Bearer %s", token));
    }

    private int refreshToken() {
    //Refresh token, synchronously, and return result code
    //you might use retrofit here
    }

    private int logout() {
    //logout your user
    }
  4. @alex-shpak alex-shpak created this gist Apr 9, 2015.
    52 changes: 52 additions & 0 deletions Interceptor.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    private class HttpInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    //Build new request
    Request.Builder builder = request.newBuilder();
    builder.header("Accept", "application/json"); //if necessary, say to consume JSON

    String token = settings.getAccessToken(); //save token of this request for future
    setAuthHeader(builder, token); //write current token to request

    request = builder.build(); //overwrite old request
    Response response = chain.proceed(request); //perform request, here original request will be executed

    if (response.code() == 401) { //if unauthorized
    synchronized (httpClient) { //perform all 401 in sync blocks, to avoid multiply token updates
    String currentToken = settings.getAccessToken(); //get currently stored token

    if(currentToken != null && currentToken.equals(token)) { //compare current token with token that was stored before, if it was not updated - do update

    int code = refreshToken() / 100; //refresh token
    if(code != 2) { //if refresh token failed for some reason
    if(code == 4) //only if response is 400, 500 might mean that token was not updated
    logout(); //go to login screen
    return response; //if token refresh failed - show error to user
    }
    }

    if(settings.hasAccessToken()) { //retry requires new auth token,
    setAuthHeader(builder, settings.getAccessToken()); //set auth token to updated
    request = builder.build();
    return chain.proceed(request); //repeat request with new token
    }
    }
    }

    return response;
    }
    private void setAuthHeader(Request.Builder builder, String token) {
    if (token != null) //Add Auth token to each request if authorized
    builder.header("Authorization", String.format("Bearer %s", token));
    }
    private int refreshToken() {
    //Refresh token, synchronously, and return result code
    //you might use retrofit here
    }
    private int logout() {
    //logout your user
    }
    }