utility Posts

SSLHandshakeException When Connecting to Let’s Encrypt CA SSL

Found this weird error when connecting to a HTTPS website, using apache commons httpclient.

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Very weird because i can connect to almost every other HTTPS website, but only one website causing this exception. After googling for a while, i found a very interesting solution from MKYong, that is to register the website’s certificate on my JVM.

http://www.mkyong.com/webservices/jax-ws/suncertpathbuilderexception-unable-to-find-valid-certification-path-to-requested-target/

Run this command to generate a new jssecacerts file,

c:\>java InstallCert localhost:443

The only difference is the location where i downloaded InstallCert.java, i downloaded it from this link.

https://github.com/OopsMouse/java-use-examples/blob/master/src/com/aw/ad/util/InstallCert.java

And i also removed the file’s package name, to make it easier to run.

Google+

ServletContext.getRealPath Always Return Null on Apache Tomcat 8

I met a very strange error, that somehow works on Tomcat 7, but not working on Tomcat 8. I’m using ServletContext’s class getRealPath method, for getting my application’s absolute file path. Here is my code,

@PostConstruct
    private void init() {
        try {
            // get report location
            String reportLocation = servletContext.getRealPath("WEB-INF");           
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

On this code, i’m trying to get my report’s location and then processing it, by using servletContext.getRealPath. Which works well on my Apache Tomcat 7, but not well enough on Apache Tomcat 8. Somehow it always show null instead of the absolute path of my “WEB-INF” folder.

The workaround is actually quite simple, adding slash in front of “WEB-INF” make the problem disappear.

@PostConstruct
    private void init() {
        try {
            // get report location
            String reportLocation = servletContext.getRealPath("/WEB-INF");           
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
Google+

How to Create A HTTP Request with Proxy Using Apache HTTP Client

In this tutorial, im trying to simuate a http GET request using apache http client, the version im using is 4.5.1

<!-- http client -->
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.5.1</version>
</dependency>
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpmime</artifactId>
	<version>4.5.1</version>
</dependency>

Usually, my application connects well without proxy. But on production environment, I need to put a proxy configuration. So this is my method for making a proxy request connection.

public String process(String lat, String lon) {
	try {
		HttpClient httpclient = HttpClientBuilder.create().build();
		HttpGet httpGet = new HttpGet("<INPUT YOUR URL HERE>");
		
		HttpHost proxy = new HttpHost("127.0.0.1", 3128, "http");
		RequestConfig config = RequestConfig.custom()
				.setProxy(proxy)
				.build();
		httpGet.setConfig(config);
		
		HttpResponse response = httpclient.execute(httpGet);
		HttpEntity resEntity = response.getEntity();

		String responseText = EntityUtils.toString(resEntity);		
		return responseText;
	} catch (IOException e) {
		logger.error(e, e);
	} catch (Exception e) {
		logger.error(e, e);
	}
	return null;
}
Google+

Weird Error on Java Mail, “javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client”

I run into a weird error that never happened before, somehow it happened since my user upgrade their Microsoft Mail Exchange’s version. Here is the complete stacktrace,

Caused by: javax.mail.AuthenticationFailedException: 
No authentication mechansims supported by both server and client
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:760)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:673)
    at javax.mail.Service.connect(Service.java:317)

I guess there are several reasons why it happens. First of all, it happens because im using starttls = “true” despite im connecting to port 25, first i change it into “false”. And the other thing is that im using username and password for connecting to my email exchange server, so the workaround is quite easy, just setting my mail.smtp.auth parameter into “false” instead of “true”. Or try sending email without setting username and password.

But actually, it’s much easier to fix the error on the mail server’s side, because it’s only a simple checklist configuration :-P

Google+

How to Perform “Autopsy” on OutOfMemoryError Application

Just several days ago, my application was down because of OutOfMemoryError, and I haven’t got a clue what the root cause is. That’s why I’m looking for a way on how to find the culprit when my application is down again. So im using this jvm parameter to dump all the object on memory when OutOfMemoryError happen.

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=E:\ -Xmx10m -Xms10m

For example, im using this java class to simulate OutOfMemoryError,

package mybatistesting;

import java.util.ArrayList;
import java.util.List;

public class MyBatisTesting {
    
    public static void main(String[] args) throws Exception {
        List list = new ArrayList();
        for (int i = 0; i < 10000000000l; i++) {
            list.add(i+""+i+""+i);
            if(i%1000==0)
                System.out.println("adding -- "+i);
        }
    }
}

As you can see on your console log,

java.lang.OutOfMemoryError: GC overhead limit exceeded
Dumping heap to E:\java_pid5812.hprof ...
Heap dump file created [15155736 bytes in 0.237 secs]

You can open java_pid5812.hprof using your preferable application, for example using Eclipse MAT or JProfiler.

eclipse mat

jprofiler 1

jprofiler 2

Have fun ;)

Google+