Skip to content

Instantly share code, notes, and snippets.

@kevinmichaelchen
kevinmichaelchen / CustomWordDelimiterFilterFactory.xml
Created April 29, 2016 19:28
CustomWordDelimiterFilterFactory
<filter class="com.mypackage.CustomWordDelimiterFilterFactory"
catenateOverApostrophe="1"
preserveOriginalApostropheWord="1"
catenateOverHyphen="0"
/>
@kevinmichaelchen
kevinmichaelchen / AggregateTextFromFiles.java
Created December 7, 2015 20:50
Aggregate text from files
public class AggregateTextFromFiles {
public static void main(String[] args) {
List<File> pieces = getFilePiecesInDirectory();
StringBuilder sb = new StringBuilder();
for ( File piece : pieces )
{
String pieceText = new String( Files.readAllBytes( Paths.get( piece.getPath() ) ) );
sb.append( pieceText );
}
String all = sb.toString();
@kevinmichaelchen
kevinmichaelchen / java-properties-to-json.py
Created November 25, 2015 14:29
Python script to convert Java properties to JSON
#!/usr/local/bin/python
import sys
import json
if __name__ == '__main__':
path = sys.argv[1]
f = open(path)
@kevinmichaelchen
kevinmichaelchen / BasicControllerTest.java
Created September 24, 2015 19:02
Spring 3 Controller Unit Test
import com.google.gson.Gson;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@kevinmichaelchen
kevinmichaelchen / headers.txt
Last active August 28, 2015 19:30
iCloud, Gmail, and Yahoo - Mail Filename Encodings
# A list of mail headers to test against
# Outlook
Content-disposition: attachment;
filename=";;;.txt"
# Outlook
Content-disposition: attachment;
filename*=UTF-8''%3B%E6%BC%A2%D8%B1%D9%82%D9%84%20%3B%3BDonaldTrump%E6%BC%A2%D8%B1%D9%82%D9%84%20%E6%BC%A2%D8%B1%D9%82%D9%84%20%E6%BC%A2%D8%B1%D9%82%D9%84%20%E6%BC%A2%D8%B1%D9%82%D9%84%20%E6%BC%A2%D8%B1%D9%82%D9%84%20%E6%BC%A2%D8%B1%D9%82%D9%84%20longTheDonald.txt
@kevinmichaelchen
kevinmichaelchen / Test.java
Created August 25, 2015 22:07
Decoding split-up email attachment filenames
class Test {
public static void main( String[] args )
{
final String contentDisposition = "name: Content-Disposition\n" +
"value: attachment;\n" +
"\tsize=1123785;\n" +
"\tmodification-date=\"Thu, 20 Aug 2015 17:27:44 GMT\";\n" +
"\tfilename*0=\"=?utf-8?B?TmltciAoNCDDlyA0KSBhbmQgKDYgw5cgNikgaGlnaCBtb2JpbGl0eS\";\n" +
"\tfilename*1=\"B2ZWhp?= cles.pdf\";\n" +
"\tcreation-date=\"Thu, 20 Aug 2015 17:27:44 GMT\"";
@kevinmichaelchen
kevinmichaelchen / Test.java
Created August 25, 2015 21:46
Pattern Matching in Java
class Test {
public static void main(String[] args) {
Pattern pattern = Pattern.compile( "filename\\*\\d+" );
System.out.println( pattern.matcher( "filename1" ).find() ); // false
System.out.println( pattern.matcher( "filename*1" ).find() ); // true
System.out.println( pattern.matcher( "filename*12" ).find() ); // true
System.out.println( pattern.matcher( "filename**12" ).find() ); // false
System.out.println( pattern.matcher( "filenamme*12" ).find() ); // false
System.out.println( pattern.matcher( " filename**12 =" ).find() ); // false
System.out.println( pattern.matcher( " filename**12=" ).find() ); // false
@kevinmichaelchen
kevinmichaelchen / Test.java
Created August 19, 2015 18:54
Broad vs Tight Try-Catch
import java.util.*;
public class Test {
public static void main(String[] args) {
try {
for (int i = 0; i < 10; i++) {
System.out.println(i);
if (i == 5) {
@kevinmichaelchen
kevinmichaelchen / Test.java
Created August 14, 2015 21:41
Quickly test Jsoup HTML Sanitization with Java 7
public class Test {
public static void main(String[] args) throws Exception
{
final String path = "/Users/kevinchen/Desktop/";
final String html = new String( Files.readAllBytes( Paths.get( path + "beforeSanitize.html" ) ) );
final String clean =
Jsoup.clean( html,
Whitelist.relaxed()
@kevinmichaelchen
kevinmichaelchen / ThreadLocal.md
Last active August 29, 2015 14:26
A Tour of ThreadLocal

ThreadLocal is a common cause of memory leaks in JVMs. In this post, we take a look under the hood to better understand what's going on.

Some background info, first. We have a superhero Wiki site with two portals in it: Batman and Superman.

public enum SuperheroPortal { BATMAN, SUPERMAN; }

Everything in the service layer depends on the currently selected portal.