Skip to content

Instantly share code, notes, and snippets.

View kdonald's full-sized avatar

Keith Donald kdonald

View GitHub Profile
@kdonald
kdonald / MappingJackson2HttpMessageConverter.java
Created April 19, 2012 19:03
Spring Jackson 2.0 support
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@kdonald
kdonald / CorisFilter.java
Created March 29, 2012 01:12
Basic Cross Origin Resource Sharing (CORS) support
package org.springframework.web.servlet.support;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.filter.OncePerRequestFilter;
@kdonald
kdonald / facebook.js
Created March 25, 2012 03:56
Facebook AMD Module
define("facebook", ["jquery", "https://connect.facebook.net/en_US/all.js"], function($) {
$("<div id='fb-root'></div>").prependTo($("body"));
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelUrl : '//YOUR_DOMAIN/channel.html', // Channel File
status : false, // check login status
cookie : false, // enable cookies to allow the server to access the session
xfbml : false // parse XFBML
@kdonald
kdonald / JsonNodeRowMapper.java
Created March 20, 2012 16:35
Auto Mapping a JDBC ResultSet to JSON
// convenient Spring JDBC RowMapper for when you want the flexibility of Jackson's TreeModel API
// Note: Jackson can also serialize standard Java Collections (Maps and Lists) to JSON: if you don't need JsonNode,
// it's simpler and more portable to have Spring JDBC simply return a Map or List<Map>.
package org.springframework.jdbc.core;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
@kdonald
kdonald / ExternalizedSql.java
Created March 11, 2012 20:43
Spring JDBC Extension - Externalized SQL strings
// example api usage:
String sql = SqlUtils.sql(new ClassPathResource("complex.sql", getClass()));
// example complex.sql
SELECT {a bunch of fields}
FROM {table}
INNER JOIN {other} ON ...
INNER JOIN {other} ON ...
@kdonald
kdonald / ExampleController.java
Created March 10, 2012 17:54
RestTemplate + Jackson Solr Client Example
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ObjectNode;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@kdonald
kdonald / WelcomeMailerTests.java
Created January 24, 2012 17:01
Mail Sending Example
package com.example.mail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@kdonald
kdonald / jmustache-spring-mvc.java
Created January 12, 2012 21:38
JMustache Spring MVC Integration
build.gradle
------------
jmustacheVersion = '1.5'
dependencies {
compile 'com.samskivert:jmustache:$jmustacheVersion'
}
JMustacheViewResolver.java
--------------------------
package org.springframework.servlet.mvc.view.jmustache;
@kdonald
kdonald / gist:1150698
Created August 17, 2011 02:40
$.Deferred refactoring
// BEFORE $.Deferred()
mainView.render(function(root) {
var mainContent = root.find("#content");
subView.render(function(el) {
mainContent.append(el);
nestedView.render(function(el) {
mainContent.append(el);
allDoneCallback(root);
});
@kdonald
kdonald / ecmascript5-objects.js
Created July 6, 2011 16:46
ECMAScript 5 Objects and Properties
var ns = ns || {};
ns.PropertyUtils = {
// Make the named (or all) properties of o nonenumerable, if configurable.
hide : function(o) {
var props = (arguments.length == 1)
? Object.getOwnPropertyNames(o)
: Array.prototype.splice.call(arguments, 1);
props.forEach(function(p) {