Commit 94bac90d authored by Greg Messner's avatar Greg Messner
Browse files

Added support to log requests/responses when communicating with the GitLab API (#228).

parent 8281b079
......@@ -4,6 +4,7 @@ import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.MediaType;
......@@ -23,7 +24,7 @@ import org.gitlab4j.api.utils.SecretString;
*/
public class GitLabApi {
private final static Logger LOG = Logger.getLogger(GitLabApi.class.getName());
private final static Logger LOGGER = Logger.getLogger(GitLabApi.class.getName());
/** GitLab4J default per page. GitLab will ignore anything over 100. */
public static final int DEFAULT_PER_PAGE = 100;
......@@ -85,7 +86,7 @@ public class GitLabApi {
* @return the GitLab4J shared Logger instance
*/
public static final Logger getLogger() {
return (LOG);
return (LOGGER);
}
/**
......@@ -638,6 +639,69 @@ public class GitLabApi {
apiClient = new GitLabApiClient(apiVersion, hostUrl, tokenType, authToken, secretToken, clientConfigProperties);
}
/**
* Enable the logging of the requests to and the responses from the GitLab server API
* using the GitLab4J shared Logger instance and Level.FINE as the level.
*
* @return this GitLabApi instance
*/
public GitLabApi withRequestResponseLogging() {
enableRequestResponseLogging();
return (this);
}
/**
* Enable the logging of the requests to and the responses from the GitLab server API
* using the GitLab4J shared Logger instance.
*
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
* @return this GitLabApi instance
*/
public GitLabApi withRequestResponseLogging(Level level) {
enableRequestResponseLogging(level);
return (this);
}
/**
* Enable the logging of the requests to and the responses from the GitLab server API.
*
* @param logger the Logger instance to log to
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
* @return this GitLabApi instance
*/
public GitLabApi withRequestResponseLogging(Logger logger, Level level) {
enableRequestResponseLogging(logger, level);
return (this);
}
/**
* Enable the logging of the requests to and the responses from the GitLab server API
* using the GitLab4J shared Logger instance and Level.FINE as the level.
*/
public void enableRequestResponseLogging() {
enableRequestResponseLogging(LOGGER, Level.FINE);
}
/**
* Enable the logging of the requests to and the responses from the GitLab server API
* using the GitLab4J shared Logger instance.
*
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
*/
public void enableRequestResponseLogging(Level level) {
enableRequestResponseLogging(LOGGER, level);
}
/**
* Enable the logging of the requests to and the responses from the GitLab server API.
*
* @param logger the Logger instance to log to
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
*/
public void enableRequestResponseLogging(Logger logger, Level level) {
this.apiClient.enableRequestResponseLogging(logger, level);
}
/**
* Sets up all future calls to the GitLab API to be done as another user specified by sudoAsUsername.
* To revert back to normal non-sudo operation you must call unsudo(), or pass null as the username.
......@@ -1413,5 +1477,4 @@ public class GitLabApi {
return wikisApi;
}
}
......@@ -12,6 +12,8 @@ import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
......@@ -36,6 +38,7 @@ import org.gitlab4j.api.utils.JacksonJson;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
......@@ -237,6 +240,24 @@ public class GitLabApiClient {
clientConfig.register(MultiPartFeature.class);
}
/**
* Enable the logging of the requests to and the responses from the GitLab server API.
*
* @param logger the Logger instance to log to
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
*/
void enableRequestResponseLogging(Logger logger, Level level) {
LoggingFeature loggingFeature = new LoggingFeature(
logger, level, LoggingFeature.Verbosity.PAYLOAD_TEXT, LoggingFeature.DEFAULT_MAX_ENTITY_SIZE);
clientConfig.register(loggingFeature);
// Recreate the Client instance if already created.
if (apiClient != null) {
createApiClient();
}
}
/**
* Get the auth token being used by this client.
*
......@@ -629,18 +650,22 @@ public class GitLabApiClient {
return (invocation(url, queryParams, MediaType.APPLICATION_JSON));
}
protected Client createApiClient() {
ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig(clientConfig);
if (ignoreCertificateErrors) {
clientBuilder.sslContext(openSslContext).hostnameVerifier(openHostnameVerifier);
}
apiClient = clientBuilder.build();
return (apiClient);
}
protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String> queryParams, String accept) {
if (apiClient == null) {
if (ignoreCertificateErrors) {
apiClient = ClientBuilder.newBuilder()
.withConfig(clientConfig)
.sslContext(openSslContext)
.hostnameVerifier(openHostnameVerifier)
.build();
} else {
apiClient = ClientBuilder.newBuilder().withConfig(clientConfig).build();
}
createApiClient();
}
WebTarget target = apiClient.target(url.toExternalForm()).property(ClientProperties.FOLLOW_REDIRECTS, true);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment