Skip to content

Commit

Permalink
supdev 26OUm5kk - add logging functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
braintreeps committed Feb 26, 2016
1 parent 97d6e5f commit aa96478
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/braintreegateway/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.braintreegateway;

import com.braintreegateway.util.ClientLibraryProperties;

import java.net.Proxy;
import java.net.InetSocketAddress;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Configuration {
private Environment environment;
Expand All @@ -13,6 +17,13 @@ public class Configuration {
private String clientSecret;
private String accessToken;
private Proxy proxy;
private static Logger logger;

static {
logger = Logger.getLogger("Braintree");
logger.addHandler(new ConsoleHandler());
logger.setLevel(Level.INFO);
}

public static final String VERSION = new ClientLibraryProperties().version();

Expand Down Expand Up @@ -99,4 +110,12 @@ public void setProxy(String url, Integer port) {
public Proxy getProxy() {
return proxy;
}

public Logger getLogger() {
return logger;
}

public void setLogger(Logger log) {
logger = log;
}
}
72 changes: 65 additions & 7 deletions src/main/java/com/braintreegateway/util/Http.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.braintreegateway.util;

import com.braintreegateway.Configuration;
import com.braintreegateway.Request;
import com.braintreegateway.exceptions.*;
import com.braintreegateway.org.apache.commons.codec.binary.Base64;

import javax.net.ssl.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -19,10 +13,33 @@
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

import com.braintreegateway.Configuration;
import com.braintreegateway.Request;
import com.braintreegateway.exceptions.AuthenticationException;
import com.braintreegateway.exceptions.AuthorizationException;
import com.braintreegateway.exceptions.DownForMaintenanceException;
import com.braintreegateway.exceptions.NotFoundException;
import com.braintreegateway.exceptions.ServerException;
import com.braintreegateway.exceptions.UnexpectedException;
import com.braintreegateway.exceptions.UpgradeRequiredException;
import com.braintreegateway.org.apache.commons.codec.binary.Base64;

public class Http {

enum RequestMethod {
Expand Down Expand Up @@ -70,6 +87,11 @@ private NodeWrapper httpRequest(RequestMethod requestMethod, String url, String
try {
connection = buildConnection(requestMethod, url);

Logger logger = configuration.getLogger();
if (logger.getLevel().intValue() <= Level.FINE.intValue() && postBody != null) {
logger.log(Level.FINE, formatSanitizeBodyForLog(postBody));
}

if (connection instanceof HttpsURLConnection) {
((HttpsURLConnection) connection).setSSLSocketFactory(getSSLSocketFactory());
}
Expand Down Expand Up @@ -100,6 +122,14 @@ private NodeWrapper httpRequest(RequestMethod requestMethod, String url, String
}

String xml = StringUtils.inputStreamToString(responseStream);

logger.log(Level.INFO, "[Braintree] [{0}]] {1} {2}", new Object[] { getCurrentTime(), requestMethod.toString(), url });
logger.log(Level.FINE, "[Braintree] [{0}] {1} {2} {3}", new Object[] { getCurrentTime(), requestMethod.toString(), url, connection.getResponseCode() });

if (logger.getLevel().intValue() <= Level.FINE.intValue() && xml != null) {
logger.log(Level.FINE, formatSanitizeBodyForLog(xml));
}

nodeWrapper = NodeWrapperFactory.instance.create(xml);
} finally {
if (responseStream != null) {
Expand All @@ -117,6 +147,32 @@ private NodeWrapper httpRequest(RequestMethod requestMethod, String url, String
return nodeWrapper;
}

private String formatSanitizeBodyForLog(String body) {
if (body == null) {
return body;
}

Pattern regex = Pattern.compile("(^)", Pattern.MULTILINE);
Matcher regexMatcher = regex.matcher(body);
if (regexMatcher.find()) {
body = regexMatcher.replaceAll("[Braintree] $1");
}

regex = Pattern.compile("<number>(.{6}).+?(.{4})</number>");
regexMatcher = regex.matcher(body);
if (regexMatcher.find()) {
body = regexMatcher.replaceAll("<number>$1******$2</number>");
}

body = body.replaceAll("<cvv>.+?</cvv>", "<cvv>***</cvv>");

return body;
}

private String getCurrentTime() {
return new SimpleDateFormat("d/MMM/yyyy HH:mm:ss Z").format(new Date());
}

private SSLSocketFactory getSSLSocketFactory() {
try {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
Expand Down Expand Up @@ -154,6 +210,8 @@ private SSLSocketFactory getSSLSocketFactory() {

return sslContext.getSocketFactory();
} catch (Exception e) {
Logger logger = configuration.getLogger();
logger.log(Level.SEVERE, "SSL Verification failed. Error message: {0}", new Object[] { e.getMessage() });
throw new UnexpectedException(e.getMessage(), e);
}
}
Expand Down

0 comments on commit aa96478

Please sign in to comment.