Java + LetsEncrypt certificate giving SunCertPathBuilderException

During testing some java code i have for doing a check against check.torproject.org i got an exception is have not seen before:

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

After digging around i realised that the site in question has started using certificates from Lets Encrypt.

Java keeps their known certificate providers bundled with the installation, so if your JRE/JDK installation is not quite up to date, it will be as simple as to simply upgrade to the latest release. (Anything >= Oracle Java 8u101 has Lets Encrypt root cert bundled)

java.io.file renameTo return false

Had a issue today where a java function which moves files stopped working. I realised that Java`s io.file.renameTo only throws Exceptions for some scenarios. For some errors, it will only return false when something goes wrong.

Only getting true/false on a rather vital operation is rather silly, so my “fix” was to move over to a library from Apache, aka FileUtils.

So now i simply do this: FileUtils.moveFile(oldFile, newFile);

That function actually throws exceptions when something fails 🙂

GWT: “For security purposes, this type will not be serialized.”

The full error message you can get is: “Foo was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.”

This is normally caused by using a non-serializable class, which can happen if your class does not implement serializable or if you have forgotten to add a empty constructor.

EXT GWT: Adding onClick to Labels

I had a issue where i needed to add a click handler for labels in EXT GWT, the solution i came up with was as follows

theLabel.addListener(Events.OnClick, new Listener() {
@Override
public void handleEvent(BaseEvent be) {
    // TODO Auto-generated method stub
}});

 

Guice: java.lang.IllegalStateException: No SessionHandler or SessionManager

One reason for this error would be that you should inject the session object directly, not via constructor:

So instead of:

@Singleton
public class Foo {
 
@Inject public Foo(HttpServletSession s) {
this.session = s  }
}

do this:

@Singleton
public class Foo{
 
@Inject Provider<HttpServletSession> session;
 
}

And it should work much better 🙂

class Foo is public, should be declared in a file named Foo.java

When working with Java you might encounter this error. The reason for this is that you have tried to declare a public class in a file with a different name. Each java file can only contain one public java class, which is named the same as the java file itself. If you need another class inside your other class you should looking into private inner classes.

jpackages error: Missing Dependency: /usr/bin/rebuild-security-providers

Jpackages on Red Hat has a nifty bug that causes dependency errors.

Luckily, somebody has created a fix as a rpm package 🙂

wget http://plone.lucidsolutions.co.nz/linux/centos/images/jpackage-utils-compat-el5-0.0.1-1.noarch.rpm
rpm -ivh jpackage-utils-compat-el5-0.0.1-1.noarch.rpm

And then jpackages works.

Tips for implementing a custom JAAS login module for Jetty

1) jetty-web.xml

You can gather all specific settings for Jetty in the file WEB-INF/jetty-web.xml, remember to also specify paths for the role principals if you use your own classes for that.

Example jetty-web.xml file:

<!-- Jetty specific config file -->
<Configure>
 
<!-- Tell jetty where to find login config -->
<Call name="setProperty">
	<Arg>java.security.<strong>auth</strong>.login.config</Arg>
	<Arg>WEB-INF/login.config</Arg>
</Call>
 
<!-- Bug fix for Jetty to properly handle login config in a per-project setup -->
<Set name="serverClasses">
	<Array type="java.lang.String">
		<Item>-org.mortbay.jetty.plus.jaas.</Item>
		<Item>org.mortbay.jetty</Item>
		<Item>org.slf4j.</Item>
	</Array>
</Set>
 
<!-- Create login realm -->
<Get name="securityHandler">
	<Set name="userRealm">
		<New>
			<Set name="name">*NAME OF YOUR REALM*</Set>
			<Set name="LoginModuleName">*NAME OF YOUR LOGIN CLASS*</Set>
			<Set name="roleClassNames">
				<Array type="java.lang.String">
					<Item>*PATH TO YOUR ROLE PRINCIPAL CLASS*</Item>
				</Array>
			</Set>
		</New>
	</Set>
</Get>
</Configure>

2) Adding roles to a user in commit

When you want to give a user roles(like admin, moderator etc), you can add those to the user when he or she is authenticated and commit is called upon:

	public boolean commit() throws LoginException {
		MyUserPrincipal user = new MyUserPrincipal("username");
 
		//Add logged in user as first principal
		subject.getPrincipals().add(user);
 
		//Add roles for the logged in user
		subject.getPrincipals().add( new MyGroupPrincipal("admin") );
		subject.getPrincipals().add( new MyGroupPrincipal("moderator") );
	}