Skip to content

Instantly share code, notes, and snippets.

View agentgt's full-sized avatar

Adam Gent agentgt

View GitHub Profile
@agentgt
agentgt / gist:1100030
Created July 22, 2011 18:15
Parse URI Query Parameters
public static Map<String, String> parseQueryParameters(String uri, String encoding) {
URI url = toURI(uri);
if (url == null) return null;
List<NameValuePair> nvps = URLEncodedUtils.parse(url, encoding);
LinkedHashMap<String, String> m = new LinkedHashMap<String, String>();
for (NameValuePair nvp : nvps) {
m.put(nvp.getName(), nvp.getValue());
}
return m;
}
@agentgt
agentgt / gist:1100035
Created July 22, 2011 18:19
Gracefully downgrading a UTF-8 String with weird characters to ASCII
public static String toAscii(String str) {
if (str == null) return null;
return Normalizer.normalize(str, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]","");
}
@agentgt
agentgt / attribute.tagx
Created July 26, 2011 12:33
Conditionally set attribute in JSPX element.
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<jsp:output omit-xml-declaration="yes"/>
<jsp:directive.attribute name="name" type="java.lang.String" required="true" rtexprvalue="true" description="Name"/>
<jsp:directive.attribute name="value" type="java.lang.String" required="true" rtexprvalue="true" description="Value"/>
<jsp:scriptlet>
//<![CDATA[
String name = (String) jspContext.getAttribute("name");
String value = (String) jspContext.getAttribute("value");
out.write(" " + name + "=" + "\"" + value + "\"");
<!--[if lt IE 8 ]>
<script src="//ajax.googleapis.com/ajax/libs/chrome-frame/1.0.3/CFInstall.min.js">/* */</script>
<script>window.attachEvent('onload',function(){CFInstall.check({mode:'overlay'})})</script>
<![endif]-->
@agentgt
agentgt / SpringAtmosphereServlet.java
Created September 12, 2011 18:09
Tomcat Spring Atomsphere Servlet
package com.snaphop.spring;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.ServletConfig;
import org.atmosphere.cpr.AtmosphereServlet;
import org.atmosphere.cpr.Broadcaster;
import org.atmosphere.cpr.BroadcasterFactory;
import org.atmosphere.handler.ReflectorServletProcessor;
@agentgt
agentgt / gist:1306570
Created October 22, 2011 22:28
Spring Social LinkedIn Fake Realease
<repositories>
<repository>
<id>mvn-adamgent</id>
<url>http://mvn-adamgent.googlecode.com/svn/maven/release</url>
<name>Adam Gent Maven Repository</name>
</repository>
</repositories>
<dependencies>
<dependency>
@agentgt
agentgt / CheckForClientAbortTomcat.java
Created November 17, 2011 22:55
Checks for ClientAbort on Tomcat. Used for logging purposes (to ignore the exception).
public class ServletUtils {
/**
* Checks if a Throwable is Tomcat ClientAbortException.
* A ClientAbortException is caused usually by a client (browser) that closes the socket to early.
* This method is used to determine whether to ignore these exceptions for logging purposes.
* http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/connector/ClientAbortException.html
* @param t maybe <code>null</code>
* @return <code>true</code> if the exception chain contains a ClientAbort Exception, false otherwise and on null.
*/
public static boolean isClientAbortException(Throwable t) {
@agentgt
agentgt / cvscut.py
Created November 21, 2011 16:44
A drop in replacement for the unix command 'cut' that works with CSV files.
#!/usr/bin/env python
import sys
import csv
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-d", "--delimiter",
action="store", dest="delim", default='\t',
help="delimter")
You redirect the deep elf demonologist's attack!
The deep elf demonologist gestures wildly while chanting.
The deep elf demonologist is devoured by a tear in reality.
@agentgt
agentgt / gist:1455479
Created December 10, 2011 16:13
Twilio maven support.
<repositories>
<repository>
<id>mvn-adamgent</id>
<url>http://mvn-adamgent.googlecode.com/svn/maven/release</url>
<name>Adam Gent Maven Repository</name>
</repository>
</repositories>
<dependencies>
<dependency>