Created
January 20, 2012 16:10
-
-
Save saltnlight5/1648056 to your computer and use it in GitHub Desktop.
Java Utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> | |
<bean id="name1" class="java.lang.String"> | |
<constructor-arg value="tcp://test?foo=bar&foo2=bar2"/> | |
</bean> | |
<bean id="name2" class="java.lang.String"> | |
<constructor-arg><value>tcp://test?foo=bar&foo2=bar2</value></constructor-arg> | |
</bean> | |
<bean id="name3" class="java.lang.String"> | |
<constructor-arg><value><![CDATA[tcp://test?foo=bar&foo2=bar2]]></value></constructor-arg> | |
</bean> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package atest; | |
import javax.annotation.Resource; | |
import org.hamcrest.Matchers; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration | |
public class AmpersandInXmlTest { | |
@Resource(name="name1") | |
String name1; | |
@Resource(name="name2") | |
String name2; | |
@Resource(name="name3") | |
String name3; | |
@Test | |
public void testAmpersand() throws Exception { | |
Assert.assertThat(name1, Matchers.is("tcp://test?foo=bar&foo2=bar2")); | |
Assert.assertThat(name2, Matchers.is("tcp://test?foo=bar&foo2=bar2")); | |
Assert.assertThat(name3, Matchers.is("tcp://test?foo=bar&foo2=bar2")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A handler to use 'classpath:' in java.net.URL class. | |
* | |
* <p>Usage: <code> | |
* URL url = new URL(null, "classpath:atest/config.properties", new ClasspathURLStreamHandler()); | |
* </code> | |
*/ | |
public class ClasspathURLStreamHandler extends URLStreamHandler { | |
@Override | |
protected URLConnection openConnection(URL url) throws IOException { | |
String protocol = url.getProtocol(); | |
if ("classpath".equals(protocol)) { | |
URL resUrl = getClassLoader().getResource(url.getPath()); | |
return resUrl.openConnection(); | |
} else { | |
// Use default JDK url impl. | |
return url.openConnection(); | |
} | |
} | |
protected ClassLoader getClassLoader() { | |
return Thread.currentThread().getContextClassLoader(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// http://blog.smartkey.co.uk/2011/01/implementing-flash-scope-in-java-web-applications/ | |
// | |
import javax.servlet.*; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpSession; | |
import java.io.IOException; | |
import java.util.Enumeration; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Ensures that any request parameters whose names start | |
* with 'flash.' are available for the next request too. | |
*/ | |
public class FlashScopeFilter implements Filter { | |
private static final String FLASH_SESSION_KEY = "FLASH_SESSION_KEY"; | |
@SuppressWarnings("unchecked") | |
public void doFilter(ServletRequest request, ServletResponse response, | |
FilterChain chain) throws IOException, ServletException { | |
//reinstate any flash scoped params from the users session | |
//and clear the session | |
if (request instanceof HttpServletRequest) { | |
HttpServletRequest httpRequest = (HttpServletRequest) request; | |
HttpSession session = httpRequest.getSession(false); | |
if (session != null) { | |
Map<String, Object> flashParams = (Map<String, Object>) | |
session.getAttribute(FLASH_SESSION_KEY); | |
if (flashParams != null) { | |
for (Map.Entry<String, Object> flashEntry : flashParams.entrySet()) { | |
request.setAttribute(flashEntry.getKey(), flashEntry.getValue()); | |
} | |
session.removeAttribute(FLASH_SESSION_KEY); | |
} | |
} | |
} | |
//process the chain | |
chain.doFilter(request, response); | |
//store any flash scoped params in the user's session for the | |
//next request | |
if (request instanceof HttpServletRequest) { | |
HttpServletRequest httpRequest = (HttpServletRequest) request; | |
Map<String, Object> flashParams = new HashMap(); | |
Enumeration e = httpRequest.getAttributeNames(); | |
while (e.hasMoreElements()) { | |
String paramName = (String) e.nextElement(); | |
if (paramName.startsWith("flash.")) { | |
Object value = request.getAttribute(paramName); | |
paramName = paramName.substring(6, paramName.length()); | |
flashParams.put(paramName, value); | |
} | |
} | |
if (flashParams.size() > 0) { | |
HttpSession session = httpRequest.getSession(false); | |
session.setAttribute(FLASH_SESSION_KEY, flashParams); | |
} | |
} | |
} | |
public void init(FilterConfig filterConfig) throws ServletException { | |
//no-op | |
} | |
public void destroy() { | |
//no-op | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This is a POJO to be use in Spring config like this: | |
* <pre>{@code | |
* <bean id="myBean" class="MyBean"> | |
* <property name="name" value="foo"/> | |
* </bean> | |
* }</pre> | |
*/ | |
public class JavaDocCodeFormatDemo{ | |
private String name; | |
public void setName(String name) { this.name = name; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> | |
<description>My Webapp</description> | |
<!-- Servlet Mappings --> | |
<servlet> | |
<servlet-name>HealthServlet</servlet-name> | |
<servlet-class>deng.mywebapp.HealthServlet</servlet-class> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>HealthServlet</servlet-name> | |
<url-pattern>/health</url-pattern> | |
</servlet-mapping> | |
</web-app> | |
# For web-app 3.0 | |
<?xml version="1.0" encoding="UTF-8"?> | |
<web-app xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" | |
version="3.0"> | |
<description>My Webapp</description> | |
<!-- Servlet Mappings --> | |
<servlet> | |
<servlet-name>HealthServlet</servlet-name> | |
<servlet-class>deng.mywebapp.HealthServlet</servlet-class> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>HealthServlet</servlet-name> | |
<url-pattern>/health</url-pattern> | |
</servlet-mapping> | |
</web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment