Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
@bdkosher
bdkosher / CmisQueryAll.java
Created June 27, 2016 19:43
Some code I use to iterate through CMIS query results.
import java.util.ArrayList;
import java.util.List;
import org.apache.chemistry.opencmis.client.api.ItemIterable;
import org.apache.chemistry.opencmis.client.api.OperationContext;
import org.apache.chemistry.opencmis.client.api.QueryResult;
import org.apache.chemistry.opencmis.client.api.Session;
/**
* See the CMIS query API for an understanding of items per page and all versions.
*/
@bdkosher
bdkosher / DecodeBase64.groovy
Created June 22, 2016 18:52
Simple base-64 decoder for command line use
@Grab(group='commons-codec', module='commons-codec', version='1.10')
import org.apache.commons.codec.binary.Base64
println new String(Base64.decodeBase64(args[0]))
@bdkosher
bdkosher / GenerateJWT.groovy
Created June 22, 2016 18:46
Script for generating a JWT token.
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
@Grab(group='commons-codec', module='commons-codec', version='1.10')
import org.apache.commons.codec.binary.Base64
def jwtTokenHeader = '''{
"typ": "JWT",
"alg": "HS256"
}'''
@bdkosher
bdkosher / callAngularService.js
Created June 22, 2016 15:33
Snippet for programmatically accessing services/factories defined in AngularJS in Chrome DevTools' console
// http://stackoverflow.com/questions/15527832/how-can-i-test-an-angularjs-service-from-the-console
angular.element(document.body).injector().get('DocumentSvc');
@bdkosher
bdkosher / MetallicaBassistStints.groovy
Created June 1, 2016 14:29
For those curious to know when Robert Trujillo's stint with Metallica will (or did) surpass Jason Newsted's.
def today = new Date()
def date(str) { Date.parse('yyyyMMdd', str) }
def newstedStint = date('20010117') - date('19861108')
def trujilloStint = today - date('20030224')
def diff = newstedStint - trujilloStint
// FIXME: "1 days" --> "1 day"
println "Trujillo's stint ${diff < 0 ? 'surpassed Newsted\'s ' + Math.abs(diff) + ' days ago' : 'will surpass Newsted\'s in ' + diff + ' days'} on ${(today + diff).format('MMM d, yyyy')}"
@bdkosher
bdkosher / blog20160517.md
Last active June 23, 2016 15:56
Reasons why you may need to rethink your enterprise code reuse strategy.

10 Good Excuses for Not Reusing Enterprise Code

In enterprise IT organizations with multiple development teams, code produced by one team is potentially useful to others. Such code could be, for example, a parser for common XML documents, a service for converting image files on the fly, or utility methods for validating key business data. (Note that I'm using the term "code" loosely to mean any sort of artifact produced by a development team, including libraries, executables, and live services.) In the interest of reducing development time and costs, enterprise architects may seek to establish a code reuse policy.

But simply declaring that "all teams should reuse code whenever possible" is not going to accomplish much. There are likely cultural barriers to remove and new incentive structures to establish to make code resuse both possible and advantageous to development teams.

To determine what specific changes are necessary in your enterprise, consider the following good excuses for not reusing enter

@bdkosher
bdkosher / GuavaRangeUtils.java
Created April 22, 2016 19:13
Guava Range Utilities (Java 8)
import com.google.common.collect.Range;
import java.util.function.Function;
public class GuavaRangeUtils {
public static <T extends Comparable<?>, S extends Comparable<?>> Range<S> transformRange(Range<T> range, Function<T, S> transformer) {
if (range == null) {
return null;
}
if (range.hasLowerBound()) {
@bdkosher
bdkosher / blog20160415.md
Last active February 15, 2023 21:45
My experiences with moving from Swagger Annotations to a single-file specification

Liberating an API Codebase from Swagger Annotations

When creating an API with accompanying Swagger documentation, two general paths can be taken:

  1. Build First: Implement the API --> add Swagger annotations --> generate the UI and clients from the annotations
  2. Design First: Design the API spec in Swagger YAML or JSON --> generate the UI, clients, and server stubs from the spec --> implement the server stubs

On my recent project, we had embarked down the "Build First" path. After implementing the API using the mighty Spring Boot, we integrated Swagger using the slick SpringFox library, as widely demonstrated in numerous blog posts.

But all w

@bdkosher
bdkosher / JosephusProblem.groovy
Created March 16, 2016 19:36
Groovy-based solution to the generalized Josephus problem
/*
"Flavius Josephus was a roman historian of Jewish origin. During the Jewish-Roman wars of the first century AD, he was in a cave with fellow soldiers,
40 men in all, surrounded by enemy Roman troops. They decided to commit suicide by standing in a ring and counting off each third man. Each man so
designated was to commit suicide...Josephus, not wanting to die, managed to place himself in the position of the last survivor.
In the general version of the problem, there are n soldiers numbered from 1 to n and each k-th soldier will be eliminated. The count starts from the
first soldier. What is the number of the last survivor?"
*/
int solveJosephusProblem(int n = 40, int k = 3) {
@bdkosher
bdkosher / XMLGregorianCalendarFormatter.groovy
Created March 1, 2016 02:16
Using XMLGregorianCalendar to format dates.
import javax.xml.datatype.*
def c = DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar())
c.setDay(1)
c.setMonth(1)
c.setYear(2016)
c.setTime(0,0,0,0)
c.setTimezone(0)
assert '2016-01-01T00:00:00.000Z' == c.toXMLFormat()