Skip to content

Instantly share code, notes, and snippets.

@jsfeng
jsfeng / streamingImageInRestService.java
Created April 5, 2012 18:10
Streaming images in JAX-RS REST service.
@Produces("image/jpeg")
@Path("/images")
public class ImageRestServiceImpl {
@Path("{id}")
public StreamingOutput getImage(@PathParam("id") int id) {
BufferedImage image = generateImage(id); //generate image using
return new StreamingImageOutput(image);
}
}
@jsfeng
jsfeng / gistStyle.css
Created April 5, 2012 18:24
Customized style for embedded gist
.gist{
margin: 15px 0 !important;
}
.gist-file{
border: none !important;
}
.gist-meta{
border: 1px solid #D2d2d2 !important;
@jsfeng
jsfeng / groovyCallService.groovy
Created April 6, 2012 17:28
Groovy Service Test Script
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.POST
import static groovyx.net.http.ContentType.JSON
import java.util.Random
String[] inputs=[
'200',
'400',
'600',
'800',
@jsfeng
jsfeng / StrutsUpload.java
Created April 12, 2012 18:20
Struts upload files
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
/**
* Form bean for Struts File Upload.
*
*/
public class StrutsUploadForm extends ActionForm
{
private FormFile theFile;
@jsfeng
jsfeng / upload.jsp
Created April 12, 2012 18:25
Struts upload JSP
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html:html locale="true">
<head>
<title>Struts File Upload Example</title>
<html:base/>
</head>
<body bgcolor="white">
<html:form action="/FileUpload" method="post" enctype="multipart/form-data">
@jsfeng
jsfeng / StrutsPreventDoubleSubmit.md
Created May 3, 2012 14:15
Struts Prevent Double Submission

On initial request:

  • Call saveToken(request) in your Action class
  • Forward to the JSP displaying the form.

On form submit:

  • Have the Action check if isTokenValid(request)
  • If true, process request then call resetToken(); otherwise, we're dealing with a double-submit and will skip processing it.

What's happening under the hood:

@jsfeng
jsfeng / Cryptographer.java
Created May 7, 2012 18:31
Cryptographer encryption /decryption
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
@jsfeng
jsfeng / SimpleHashUtil.java
Created May 7, 2012 18:33
Simple Hash Utility Tool
import java.util.Random;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import org.apache.commons.codec.binary.Base64;
public class SimpleHashUtil {
/**
* Generates a hash for the given plain text value and returns a
@jsfeng
jsfeng / FileServlet.java
Created May 7, 2012 19:31
FileServlet supporting resume, caching and GZIP
/*
* net/balusc/webapp/FileServlet.java
*
* Copyright (C) 2009 BalusC
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
@jsfeng
jsfeng / AmfProvider.java
Created August 17, 2012 14:29
AMF Provider for REST service
package net.java.ws.providers;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;