Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
@bdkosher
bdkosher / SearchActiveDriectory.groovy
Created February 5, 2015 16:49
Sample script for running queries against Active Directory
@Grab(group='org.springframework.security', module='spring-security-core', version='3.2.5.RELEASE')
@Grab(group='org.springframework.security', module='spring-security-ldap', version='3.2.5.RELEASE')
import groovy.transform.*
import javax.naming.NamingException
import javax.naming.directory.Attribute
import javax.naming.directory.Attributes
import org.springframework.ldap.core.*
import org.springframework.security.ldap.*
@bdkosher
bdkosher / word.html
Created January 29, 2015 16:00
Launch Word from HTML link
<html>
<body>
<a href="ms-word:ofe|u|http://localhost:8080/doc.docx">Click me</a>
</body>
</html>
@bdkosher
bdkosher / SpockMockingExample.groovy
Created January 28, 2015 14:56
Canonical Spock mocking example.
// http://www.infoq.com/presentations/groovy-test-java-spock @ 17:00
import spock.lang.Specification
class Publisher {
def subscribers = []
def send(event) {
subscribers.each {
try {
it.receive(event)
@bdkosher
bdkosher / rtf_to_html.groovy
Created January 26, 2015 16:35
RTF to HTML with Apache Tika
@Grab(group='org.apache.tika', module='tika-core', version='1.7')
@Grab(group='org.apache.tika', module='tika-parsers', version='1.7')
import org.apache.tika.metadata.*
import org.apache.tika.parser.*
import org.apache.tika.parsers.*
import org.apache.tika.sax.*
import javax.xml.transform.*
import javax.xml.transform.sax.*
import javax.xml.transform.stream.*
import org.xml.sax.*
@bdkosher
bdkosher / doethWhilst.groovy
Last active August 29, 2015 14:13
Faking a do-while construct using Groovy (which does not support the do keyword currently)
def _ = [do: { body ->
[while: { condition ->
body()
if (condition()) call(condition)
}]
}]
int i = 0; _.
@bdkosher
bdkosher / yCombinator.groovy
Last active August 29, 2015 14:13
Trying to understand Y Combinators using Groovy syntax.
/*
* Follows http://www.righto.com/2009/03/y-combinator-in-arc-and-java.html blog
*/
// standard factorial closure
def fact = { n -> n == 0 ? 1 : n * call(n - 1) }
// factorial closure generator, which requires the factorial closure as an arg (mind blown already)
def factGen = { factFn ->
{ n -> n == 0 ? 1 : n * factFn(n - 1) }
@bdkosher
bdkosher / OutageImpactOnAvailability.groovy
Created December 18, 2014 21:01
What's the impact on availability due to a single outage?
import groovy.time.*
def (from, to) = ['12/16/2014 2:52 PM','12/18/2014 3:32 PM'].collect { Date.parse('MM/dd/yyyy h:mm a', it) }
int minutesDown = TimeCategory.minus(to, from).toMilliseconds() / 1000 / 60
int minutesPerYear = 365 * 24 * 60
(minutesPerYear - minutesDown) / minutesPerYear
@bdkosher
bdkosher / PDFScanner.java
Last active August 29, 2015 14:10
Crude Java class (depends on pdfbox-1.8.5 and common-logging) to recursively scan directories of PDF files to determine if they contain text or not (or are non-PDF files)
package prpsutil;
import java.io.*;
import java.util.*;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.font.*;
public class PDFScanner {
private static final byte[] expectedMagicNumber = "%PDF".getBytes();
@bdkosher
bdkosher / StackoverflowTagCounter.groovy
Created October 9, 2014 18:54
Counts the number of tagged questions on Stackoverflow for a given tag.
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2')
import java.text.*
class SOTagCounter {
def parser = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser())
int count(String tag) {
new URL("http://stackoverflow.com/questions/tagged/${new URI(tag).path}").withReader { page ->
def html = parser.parse(page)
def c = html.body.div.find { it.@class == 'container' }.div.find { it.@id == 'content' }.div.find { it.@id == 'sidebar' }.div.find { it.@class == 'module' }.div[0].text()
@bdkosher
bdkosher / SpockUnrollDesc.groovy
Created September 26, 2014 16:24
Unroll descriptions - "Idiomatic Spock" by Rob Fletcher - http://youtu.be/RuTupC0I59M?t=12m26s
@Unroll
def "the string '#string' is #description"() {
expect:
string.isInteger() == expected
where:
string | expected
"ABC" | false
"123" | true
"1.2" | false