Notes by Fang
Displaying keyword search results 1 - 10
Created by Fang on March 30, 2012 10:07:25
Last update: March 08, 2013 13:41:57
After a user resets a password, I want to force the user to change the password before she gets access to secured content. This is usually done with a servlet filter. But with Spring MVC, you can also use a HandlerInterceptor . According to Spring JavaDoc: HandlerInterceptor is basically similar to a Servlet 2.3 Filter, but in contrast to the latter it just allows custom pre-processing with the option of prohibiting the execution of the handler itself, and custom post-processing. Filters are more powerful, for example they allow for exchanging the request and response objects that are handed down the chain. Note that a filter gets configured in web.xml, a HandlerInterceptor in the application context. As a basic guideline, fine-grained handler-related preprocessing tasks are candidates...
Created by Fang on January 04, 2013 14:16:58
Last update: January 04, 2013 14:16:58
Junit does not support specifying execution order of tests until 4.11. The methods were simply invoked in the order returned by the reflection API. So, the tests are executed in a unspecified but deterministic order, i.e., you have no control over the order of execution, but if you repeat the tests, they are run in the same sequence each time. For version 4.11, you can specify the order with the FixMethodOrder annotation:
import org.junit.runners.MethodSorters; imp... From the release notes : Test execution order By design, JUnit does not specify the execution order of test method invocations. Until now, the methods were simply invoked in the order returned by the reflection API. However, using the JVM order is unwise since the Java platform does not specify...
Created by Fang on July 25, 2012 12:59:47
Last update: July 25, 2012 12:59:47
Example code:
import javax.xml.ws.BindingProvider;
import jav...
Wierdly, even though the response context ( ctx ) itself is a Map, you cannot iterate through the keys. This:
for (String key: ctx.keySet()) {
logger.inf...
fails:
WARN : InternalError - Handler execution resulted ...
Created by Fang on June 05, 2012 09:12:36
Last update: June 05, 2012 09:13:03
The apache.commons.lang package has a nice utility to escape strings for various language environments. Simply include the dependency in pom.xml :
<dependency>
<groupId>org.apache.commons</...
and use it:
// import org.apache.commons.lang3.StringEscapeUti...
Created by Fang on May 17, 2012 19:31:39
Last update: May 17, 2012 19:31:39
To inject ServletContext into a Spring bean:
implement ServletContextAware :
import javax.servlet.ServletContext;
import...
Define bean in Spring application context:
<beans:bean id="myBean"
class="com.example.M...
Created by Fang on May 03, 2012 15:07:17
Last update: May 03, 2012 15:07:52
Scaling an image with default Java API loses quality (horribly!):
public static BufferedImage resizeImage(Buffer...
The imgscalr library does the job beautifully. And its' very easy to do:
// import org.imgscalr.Scalr;
public static...
To import the library in Maven:
<dependency>
<groupId>org.imgscalr</groupId>
...
Created by Fang on April 16, 2012 12:58:35
Last update: April 16, 2012 12:58:35
To implement a JSP custom tag with dynamic attributes (for example, to pass-thru arbitrary attributes not handled by the JSP tag):
Set the dynamic-attributes element to true in the TLD:
<tag>
<name>mark</name>
<tag-class>c...
The tag handler must implement javax.servlet.jsp.tagext.DynamicAttributes :
package com.example.jsp;
import java.io.*;
...
Created by Fang on February 24, 2012 14:38:06
Last update: April 06, 2012 13:19:29
Step 1: create a Json factory:
package com.my.service.dev;
import java.io....
Step 2: use it:
ObjectMapper mapper = new ObjectMapper(new AllowCo...
Created by Fang on March 30, 2012 15:04:04
Last update: March 30, 2012 15:04:04
Spring MVC 3.1 can send either JSON or HTML response on the same URL, depending on the type of response requested. With this mechanism, a page can be sent when directly requested from a link, but a JSON response can be sent in response to an AJAX request. This is the controller code:
package com.example; import java.util.Map; ... In the above example, JSON response will be sent when the HTTP request contains header "Accept: application/json". HTML response will be sent then the header is "Accept: */*", or "Accept: text/html", or anything else. You can add a limitation that the HTML response does not produce "application/json". But then the question is which response will be sent when the HTTP header is "Accept: */*"? Both methods will...
Created by Fang on March 28, 2012 12:30:22
Last update: March 28, 2012 12:30:36
To check password against login credential:
// import org.springframework.security.authenticat...
To replace authentication credentials with a new one:
SecurityContext securityContext = SecurityContextH...