Skip to content

Instantly share code, notes, and snippets.

module.exports = function url_rewrite_based_on_hostname(hostname,prefix_path){
if (!hostname) throw new Error('url rewrite hostname required');
if (!prefix_path) throw new Error('url rewrite prefix path required');
var regexp = new RegExp('^' + hostname.replace(/[*]/g, '(.*?)') + '$');
// this is the actual middleware implementation, that is called when you get an HTTP request.
return function fn(req, res, next){
if (!req.headers.host) return next(); // skip if there is no HOST header.
var host = req.headers.host.split(':')[0]; // get the HOST header
@criminy
criminy / AbstractMockitoMultipartHttpServletRequestUnitTest.java
Created July 21, 2011 14:02
Abstract Unit test for a @controller with MultipartHttpServletRequest
import static org.mockito.Mockito.*;
/**
* Boostraps a MultipartHttpServletRequest using mockito.
* To add a request parameter, define a method called 'parameterName', where request attribute is 'name'.
* To add a multipart file, define a method called 'fileName', where the multipart file is called 'name'.
* Class is very limited but works as it should providing you use a specific subset of the methods on {@link MultipartHttpServletRequest}.
* Subset:
* MultipartHttpServletRequest.getParameter
* MultipartHttpServletRequest.getFileMap
@criminy
criminy / MultipleFileUploadExample.java
Created July 20, 2011 13:30
Spring 3 multiple file upload
@Controller
@RequestMapping(value="/")
public class MultipleFileUploadExample
{
Logger log = Logger.getLogger(NewController.class);
@RequestMapping(method=RequestMethod.POST)
public String onMultipleFiles(MultipartHttpServletRequest request)
{
@criminy
criminy / StandardWebAppInitializer.java
Created July 19, 2011 17:36
Bootstraps a Servlet 3.0 system using Spring MVC, Spring Security, etc.
public class MyWebAppinitializer extends StandardWebAppInitializer {
//customize the creatino of the web application context
@Override
protected WebApplicationContext createWebApplicationContext() {
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.scan("com.whatever");
root.getEnvironment().setDefaultProfiles("embedded");
return root;
@criminy
criminy / example.js
Created June 3, 2011 01:56
Form validation example for expressjs
server.all('/admin/content/add_article',function(req,res)
{
server.form('post',req,res,function(form) {
form.required('title')
form.optional('tags')
form.required('slug')
form.required('body')
form.valid(function(form) {
res.send('Creating blog post titled : ' + form.values.title)
/**
* Abstract class which bootstraps the Spring MVC system on top of the OSGI interface 'JettyService' on startup.
*/
public abstract class AbstractOsgiSpringMvcApplication implements BundleContextAware,InitializingBean
{
private BundleContext bundleContext;
private JettyService jettyService;
private Handler registered;
public abstract String getContextConfigLocation();