All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.contentful.java.cda.interceptor.UserAgentHeaderInterceptor Maven / Gradle / Ivy

There is a newer version: 9.1.0
Show newest version
package com.contentful.java.cda.interceptor;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Interceptor to add user agent header to requests
 */
public class UserAgentHeaderInterceptor implements Interceptor {
  public static final String HEADER_NAME = "User-Agent";
  private final String userAgent;

  /**
   * Create Header interceptor, saving parameters.
   *
   * @param userAgent user agent header to be send with _every_ request.
   */
  public UserAgentHeaderInterceptor(String userAgent) {
    this.userAgent = userAgent;
  }

  /**
   * Method called by framework, to enrich current request chain with the header information requested.
   *
   * @param chain the execution chain for the request.
   * @return the response received
   * @throws IOException
   */
  @Override public Response intercept(Chain chain) throws IOException {
    final Request request = chain.request();

    return chain.proceed(request.newBuilder()
        .addHeader(HEADER_NAME, userAgent)
        .build());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy