Skip to content

Instantly share code, notes, and snippets.

View mikeapr4's full-sized avatar

Michael Gallagher mikeapr4

  • Buenos Aires, Argentina
View GitHub Profile
@mikeapr4
mikeapr4 / CacheHeaderFilter.java
Last active November 30, 2015 19:09
Java Spring Filter for Caching response headers on static and non-static resources (see comment for details)
import org.springframework.util.AntPathMatcher;
import org.springframework.web.filter.GenericFilterBean;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@mikeapr4
mikeapr4 / HttpClientConfiguration.java
Created November 30, 2015 19:38
Java Spring Configuration to provide the Apache HttpClient as per recommendations from http://www.baeldung.com/httpclient-connection-management
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
@mikeapr4
mikeapr4 / NonceFactory.java
Created February 12, 2016 15:17
Java class for managing encrypted nonces, providing them and verifying/expiring them
import java.util.BitSet;
import org.joda.time.DateTimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Creates unique (likely consecutive) integers which can be used
* within a 30min (to 1hr) period. Main methods are:
* * nextNonce - to return an integer.
@mikeapr4
mikeapr4 / CertificateUtils.java
Created February 12, 2016 15:48
Convenient In-Memory Self-signed Certificate and Keystore creation for Java 8
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.security.tools.keytool.CertAndKeyGen;
import sun.security.x509.X500Name;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
@mikeapr4
mikeapr4 / AjaxAwareAuthEntryPointWrapper.java
Created February 12, 2016 16:01
Wrapper for Spring Security AuthenticationEntryPoint interface, which will intercept 302 Redirects which are not supported generally in Browsers for Ajax Requests.
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
public class AjaxAwareAuthEntryPointWrapper implements AuthenticationEntryPoint {
@mikeapr4
mikeapr4 / inquire.js
Created February 12, 2016 16:28
A mocking / injection mechanism for RequireJS
/******************************************************************************
* Inquire: a mocking mechanism for RequireJS (v2.1.9 - tightly coupled) *
* *
* Unversioned *
* *
* NOTE: Don't use Inquire for anything which modifies global scope! *
******************************************************************************/
var inquire;
(function(require) {
@mikeapr4
mikeapr4 / backbone-model-reactive.js
Last active June 16, 2017 14:28
Add reactive properties to a backbone model to allow plain Object manipulation.
var originalSet = Backbone.Model.prototype.set
, noConflictPrefix = '$$';
/**
* During initial construction, a model
* calls set(), but there is also a check
* to ensure the reactivity only occurs on construction
*/
Backbone.Model.prototype.set = function() {
var duringConstruction = (this.changed === null);
@mikeapr4
mikeapr4 / flatten.js
Created August 9, 2017 15:40
Flatten Nested Arrays in Javascript
// See this in action: https://jsfiddle.net/mikeapr4/t4x0oe9k/
// Queue-orientated solution, will work for large-scale
// nesting (50k+), however additional overhead of space (queue)
const flatten = A => {
const output = [], queue = [...A]
while (queue.length > 0) {
const entry = queue.shift()
if (Array.isArray(entry)) {
@mikeapr4
mikeapr4 / reactive-moment.js
Created August 2, 2018 11:56
Reactive-compatible Moment.js Instances
import moment from 'moment';
import 'moment-timezone';
// Make all fields on `object` hidden (not enumerable)
// except any in the whitelist.
const hideFields = (object, whitelist) => Object.keys(object)
.forEach((field) => {
if (!whitelist.includes(field)) {
const val = object[field];
Object.defineProperty(object, field, {
@mikeapr4
mikeapr4 / getter-cache.js
Last active March 10, 2019 09:48
an example rudimentary vuex method-style getter cache - don't use this, just for illustration
mappedTask2: (state, getters) => {
const cache = {};
return (id) => {
const record = state.tasks[id];
const key = id + record.name;
if (!cache[key]) {
cache[key] = getters.mappedTask(id);
}
return cache[key];
};