use of java.util.logging.SimpleFormatter in project jersey by jersey.
the class ResourceBundleTest method testBadResource.
@Test
public void testBadResource() throws Exception {
final ResourceConfig resourceConfig = new ResourceConfig(BadResource.class);
ByteArrayOutputStream logOutput = new ByteArrayOutputStream();
Handler logHandler = new StreamHandler(logOutput, new SimpleFormatter());
GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig, false);
// TODO: there should be a better way to get the log output!
final Enumeration<String> loggerNames = LogManager.getLogManager().getLoggerNames();
while (loggerNames.hasMoreElements()) {
String name = loggerNames.nextElement();
if (name.startsWith("org.glassfish")) {
LogManager.getLogManager().getLogger(Errors.class.getName()).addHandler(logHandler);
}
}
GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig, false);
logOutput.flush();
final String logOutputAsString = logOutput.toString();
Assert.assertFalse(logOutputAsString.contains("[failed to localize]"));
Assert.assertTrue(logOutputAsString.contains("BadResource"));
}
use of java.util.logging.SimpleFormatter in project android_frameworks_base by ParanoidAndroid.
the class CookiesTest method testCookiesAreNotLogged.
/**
* Test that we don't log potentially sensitive cookie values.
* http://b/3095990
*/
public void testCookiesAreNotLogged() throws IOException, URISyntaxException {
// enqueue an HTTP response with a cookie that will be rejected
server.enqueue(new MockResponse().addHeader("Set-Cookie: password=secret; Domain=fake.domain"));
server.play();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Logger logger = Logger.getLogger("org.apache.http");
StreamHandler handler = new StreamHandler(out, new SimpleFormatter());
logger.addHandler(handler);
try {
HttpClient client = new DefaultHttpClient();
client.execute(new HttpGet(server.getUrl("/").toURI()));
handler.close();
String log = out.toString("UTF-8");
assertTrue(log, log.contains("password"));
assertTrue(log, log.contains("fake.domain"));
assertFalse(log, log.contains("secret"));
} finally {
logger.removeHandler(handler);
}
}
use of java.util.logging.SimpleFormatter in project android_frameworks_base by ResurrectionRemix.
the class CookiesTest method testCookiesAreNotLogged.
/**
* Test that we don't log potentially sensitive cookie values.
* http://b/3095990
*/
public void testCookiesAreNotLogged() throws IOException, URISyntaxException {
// enqueue an HTTP response with a cookie that will be rejected
server.enqueue(new MockResponse().addHeader("Set-Cookie: password=secret; Domain=fake.domain"));
server.play();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Logger logger = Logger.getLogger("org.apache.http");
StreamHandler handler = new StreamHandler(out, new SimpleFormatter());
logger.addHandler(handler);
try {
HttpClient client = new DefaultHttpClient();
client.execute(new HttpGet(server.getUrl("/").toURI()));
handler.close();
String log = out.toString("UTF-8");
assertTrue(log, log.contains("password"));
assertTrue(log, log.contains("fake.domain"));
assertFalse(log, log.contains("secret"));
} finally {
logger.removeHandler(handler);
}
}
use of java.util.logging.SimpleFormatter in project jdk8u_jdk by JetBrains.
the class SerializeLogRecord method generate.
/**
* Serializes a log record, encode the serialized bytes in base 64, and
* prints pseudo java code that can be cut and pasted into this test.
* @param record the log record to serialize, encode in base 64, and for
* which test data will be generated.
* @return A string containing the generated pseudo java code.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static String generate(LogRecord record) throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// Now we're going to perform a number of smoke tests before
// generating the Java pseudo code.
//
// First checks that the log record can be deserialized
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord) ois.readObject();
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2))
throw new RuntimeException("Unexpected values in deserialized object:" + "\n\tExpected: " + str + "\n\tRetrieved: " + str);
// Now get a Base64 string representation of the serialized bytes.
final String base64 = Base64.getEncoder().encodeToString(baos.toByteArray());
// Check that we can deserialize a log record from the Base64 string
// representation we just computed.
final ByteArrayInputStream bais2 = new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois2 = new ObjectInputStream(bais2);
final LogRecord record3 = (LogRecord) ois2.readObject();
// Format the new deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str3 = formatter.format(record3);
if (!str.equals(str3))
throw new RuntimeException("Unexpected values in deserialized object:" + "\n\tExpected: " + str + "\n\tRetrieved: " + str);
//System.out.println(base64);
//System.out.println();
// Generates the Java Pseudo code that can be cut & pasted into
// this test (see Jdk8SerializedLog and Jdk9SerializedLog below)
final StringBuilder sb = new StringBuilder();
sb.append(" /**").append('\n');
sb.append(" * Base64 encoded string for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String base64 = ").append("\n ");
final int last = base64.length() - 1;
for (int i = 0; i < base64.length(); i++) {
if (i % 64 == 0)
sb.append("\"");
sb.append(base64.charAt(i));
if (i % 64 == 63 || i == last) {
sb.append("\"");
if (i == last)
sb.append(";\n");
else
sb.append("\n + ");
}
}
sb.append('\n');
sb.append(" /**").append('\n');
sb.append(" * SimpleFormatter output for LogRecord object.").append('\n');
sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n');
sb.append(" **/").append('\n');
sb.append(" final String str = ").append("\n ");
sb.append("\"").append(str.replace("\n", "\\n")).append("\";\n");
return sb.toString();
}
use of java.util.logging.SimpleFormatter in project platform_frameworks_base by android.
the class CookiesTest method testCookiesAreNotLogged.
/**
* Test that we don't log potentially sensitive cookie values.
* http://b/3095990
*/
public void testCookiesAreNotLogged() throws IOException, URISyntaxException {
// enqueue an HTTP response with a cookie that will be rejected
server.enqueue(new MockResponse().addHeader("Set-Cookie: password=secret; Domain=fake.domain"));
server.play();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Logger logger = Logger.getLogger("org.apache.http");
StreamHandler handler = new StreamHandler(out, new SimpleFormatter());
logger.addHandler(handler);
try {
HttpClient client = new DefaultHttpClient();
client.execute(new HttpGet(server.getUrl("/").toURI()));
handler.close();
String log = out.toString("UTF-8");
assertTrue(log, log.contains("password"));
assertTrue(log, log.contains("fake.domain"));
assertFalse(log, log.contains("secret"));
} finally {
logger.removeHandler(handler);
}
}
Aggregations