Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
@bdkosher
bdkosher / HttpProxyServer.groovy
Created March 6, 2015 18:59
Groovy-based HTTP local proxy server heavily inspired by https://github.com/vert-x/vertx-examples/blob/master/src/raw/java/proxy/ProxyServer.java. To use, set other Java process's JAVA_OPTS: C:\dev\cp\PRPS\prps\prps-services>mvn install tomcat6:run -Ptomcat -Dhttp.proxyHost=localhost -Dhttp .proxyPort=8000
import org.vertx.groovy.core.*
String headersAsString(MultiMap headers) {
headers == null ? '' : headers.entries.collect({ e -> "$e.key : $e.value" }).join('\n')
}
String requestAsString(req) {
"$req.version $req.method $req.uri\n${headersAsString(req.headers)}\n\n"
}
String responseAsString(res) {
@bdkosher
bdkosher / curl_write-out_fmt
Last active August 29, 2015 14:17
Gathering statistics on download speeds using curl httpd wtih mod_ratelimit
%{url_effective},%{size_download},%{time_total},%{speed_download}\n
@bdkosher
bdkosher / FizzBuzz.groovy
Created March 24, 2015 18:45
FizzBuzz in Groovy
f='fizz';b='buzz';m={x,y->x%y==0};F={n->m(n,15)?"$f$b":m(n,3)?f:m(n,5)?b:n};(1..100).collect{F(it)}
@bdkosher
bdkosher / FizzBuzzCompileStatic.groovy
Created March 25, 2015 00:49
Groovy FizzBuzz with CompileStatic.
@groovy.transform.CompileStatic
List<String> fizzbuzz(int from = 1, int to = 100) {
String f = 'fizz', b = 'buzz'
def m = {int x, int y ->
x % y == 0
}
def F = { int n ->
m(n,15) ? "$f$b" : m(n,3) ? f:m(n,5) ? b : n
}
(from..to).collect {int i -> F(i) }
@bdkosher
bdkosher / GetDependencies.groovy
Last active August 29, 2015 14:20
Sonar library scraper
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2')
def url = 'https://example.com/sonar/dependencies?resource=commons-io%3Acommons-io&search=commons-io%3Acommons-io&version=1.1'
def parser = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser())
new URL(url).withReader { page ->
def html = parser.parse(page)
html.'**'.find { it.@id == 'results_col' }.'**'.findAll { it.name() == 'td' }.each { td ->
println td.span.text()
}
@bdkosher
bdkosher / InputsHaveLabels508check.groovy
Created May 15, 2015 15:23
Crude 508 verification script to ensure each input has a corresponding label.
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2')
Set<String> inputIds = [] as Set
def parser = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser())
def html = parser.parse(new File(/C:\Users\jwolf2\Desktop\swagger-ui.html/))
html.'**'.findAll { it.name() == 'select' || it.name() == 'textarea' || (it.name() == 'input' && it.@type != 'button' && it.@type != 'submit') }.each { el ->
if (!inputIds.add(el.@id)) {
println "${el.@id} is a dupe"
}
}
@bdkosher
bdkosher / blog20150708.md
Last active August 29, 2015 14:24
JAX-RS 2: Custom @context Injection of a Limited, Thread-Unsafe Resource

JAX-RS 2: Custom @Context Injection of a Limited, Thread-Unsafe Resource

I've been working on wrapping a stateful CGI/Perl web application in a stateless, RESTful interface using JAX-RS 2. Besides the mechanics of interacting with the CGI/Perl web application (think judicious use of HTTP clients and HTML scraping), one challenging aspect has been ensuring that simultaneous requests to the REST service do not end up sharing a CGI/Perl application session.

Each request must establish and end a session for a particular webapp user; any incoming requests during that time should not establish a session with that same user.

My initial idea was to customize Tomcat's thread pool executor so that each thread was bound to a particular user. I made progess in this area, but abandoned the approach after struggles with the under-documented tomcat7-maven-plugin. Besides, I was leery of being coupled to Tomcat at such a low level given how my custom org.apache.catalina.Executor "borrowed" heavily from the [standar

@bdkosher
bdkosher / shiftCipher.groovy
Created July 21, 2015 13:44
A Groovy script for applying a shift cipher to some text
String shiftCipher(String text, int amt = -3) {
new String(text.toUpperCase().chars.collect { c ->
c < 33 ? c : (c - 65 + (amt < 0 ? 26 + amt : amt)) % 26 + 65
} as char[])
}
(-25..25).each { amt -> println "$amt: ${shiftCipher('WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ', amt)}" }
@bdkosher
bdkosher / ThreadSchedulingExperiment.groovy
Created July 25, 2015 18:38
some code to test the execution of some code in a multi-threaded environment. Replace Thread.sleep with the actual web service all
import java.util.concurrent.*
def WEB_SERVICE_URL = new URL('http://localhost:8080/example/search?q=ornithology&rows=50')
def THREADS = 3
def TASKS = THREADS * 4
def exec = { url ->
def start = System.nanoTime()
Thread.sleep(3000) // new JsonSlurper().parse(url)
def end = System.nanoTime()
@bdkosher
bdkosher / batch_aliases.sh
Created August 20, 2015 14:28
I use Windows and throw a bunch of .bat files in a PATH directory that run tools (mvn, gradle, etc.). This lets me avoid having to set each tool's /bin folder on my PATH. However, Cygwin doesn't work with .bat files, so this .bashrc snippet will create an alias for all the batch files based on their names.
BATCH_FILES=/cygdrive/c/Users/jwolf2/bin/*.bat
for bfile in $BATCH_FILES; do
bname=${bfile##*/}
bcmd=${bname%.*}
alias $bcmd=$bfile
done