Requirement: HttpsURLConnection ignore SSL Certificate Error / Accept all Certificate
package com.test; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; public final class SSLUtils { public static void trustCertificate () throws KeyManagementException, NoSuchAlgorithmException { System.setProperty ( "https.protocols", "SSLv3" ); TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { System.out.println ( "checkClientTrusted()" ); } @Override public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { System.out.println ( "checkServerTrusted()" ); } @Override public X509Certificate[] getAcceptedIssuers() { System.out.println ( "X509Certificate()" ); return new X509Certificate[0]; } } }; final SSLContext sc = SSLContext.getInstance("SSLv3"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier ( new HostnameVerifier () { @Override public boolean verify(String arg0, SSLSession arg1) { return true; } } ); } }
Usage:
before url.openConnection() call trustCertificate ();
Requirement: disable auto redirection in HttpClient. e.g. for test purpose.
// HttpClient Version: 4.3.3 RequestConfig requestConfig = RequestConfig.custom().setRedirectsEnabled(false).build(); httpGet.setConfig(requestConfig); CloseableHttpResponse response = httpClient.execute(httpGet); System.out.println("Status Code: " + response.getStatusLine().getStatusCode()); // Older version: http://stackoverflow.com/questions/1519392/how-to-prevent-apache-http-client-from-following-a-redirect // HTTP parameters stores header etc. HttpParams params = new BasicHttpParams(); params.setParameter("http.protocol.handle-redirects",false); httpget.setParams(params);
1. Set global resource in server.xml
<GlobalNamingResources>
<Resource name="jdbc/DatabaseName" auth="Container" type="javax.sql.DataSource"
username="dbUsername" password="dbPasswd"
url="jdbc:postgresql://localhost/dbname"
driverClassName="org.postgresql.Driver"
initialSize="5" maxWait="5000"
maxActive="120" maxIdle="5"
validationQuery="select 1"
poolPreparedStatements="true"/>
</GlobalNamingResources/>
2. use it in the spring config file:
<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/DatabaseName"/>
</bean>
Ref: http://stackoverflow.com/questions/9183321/how-to-use-jndi-datasource-provided-by-tomcat-in-spring/9183486#9183486
A Continuous Test Runner for Java
When you changed any test case or java code which is covered by test cases, Infinitest will invoke the test case automatically for you!
Jersey can handle concrete data binding smoothly, however, for Polymorphic, it’s not very configurable.
e.g.:
IUser
DefaultUser, SGUser, CNUser
We need a web service, which accept POST request from Client side:
saveUser(IUser newUser)…
Nobody knows it’s a DefaultUser or SGUser, or CNUser, except Spring config XML. and there’re some ways to use Jersey do the deserialization.:
http://programmerbruce.blogspot.sg/2011/05/deserialize-json-with-jackson-into.html
However, I prefer to use “Simple” Data Binding from Jackson to do the mapping manually:
“Simple here just means that range of value types is limited to core JDK types”
Object root = mapper.readValue(src, Object.class); Map<?,?> rootAsMap = mapper.readValue(src, Map.class);
So in the saveUser() method, we can get the raw values and then initialize the instances with the help of Spring to make sure what we know and what we only need to know is ‘Interface’/Contract:
saveUser(Map<String, Object> json) {
(Cast) json.get(“key”);
}
// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.