HttpsURLConnection ignore SSL Certificate Error / Accept all Certificate

Categories: Java; Tagged with: ; @ April 2nd, 2014 22:47

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 ();

HttpClient: Disable automatic redirect in HttpGet

Categories: Java; Tagged with: ; @ March 31st, 2014 21:20

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);

Use Global JNDI in Tomcat with Spring

Categories: Java; Tagged with: ; @ February 13th, 2014 23:04

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

infinitest: Brilliant Continuous Test Runner for Java

Categories: Java; Tagged with: ; @ November 24th, 2013 11:49

A Continuous Test Runner for Java

http://infinitest.org

 

When you changed any test case or java code which is covered by test cases, Infinitest will invoke the test case automatically for you!

image

Jersey/Jackson Polymorphic Deserialization

Categories: Java; Tagged with: ; @ October 1st, 2013 20:56

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”);

}

Newer Posts <-> Older Posts



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.