I know this is an old question, but officially still unresolved. I just saw it, so I am going to answer it:
You mentioned that URIs are immutable. So basically this is a hint towards the right solution: intercept object creation (constructor call) and check the result in an around()
advice. If it is OK, pass on the unchanged URI, otherwise create a new one, fixing or canonising whatever is wrong.
Assume you have this application class creating URIs:
package de.scrum_master.aspectj.sample.uri;
import java.net.URI;
import java.net.URISyntaxException;
public class URIApp {
public static void main(String[] args) throws URISyntaxException {
URI[] uris = {
new URI("https://google.com?search=olympics"),
new URI("http://yahoo.com/mail/login"),
new URI("https://facebook.com/user?name=kriegaex"),
new URI("http://stackoverflow.com/questions/123456")
};
for (URI uri : uris)
System.out.println(uri);
}
}
Further assume that we consider all URIs with a scheme/protocol of "http" insecure and thus wrong. We want to fix this by replacing the URI with another one using "https". We will do this in an aspect:
package de.scrum_master.aspectj.sample.uri;
import java.net.URI;
import java.net.URISyntaxException;
public aspect URIFixer {
pointcut uriCreation() : call(URI.new(..)) && !within(URIFixer);
URI around() throws URISyntaxException : uriCreation() {
URI result = proceed();
return isOk(result) ? result : fix(result);
}
boolean isOk(URI uri) {
return "https".equals(uri.getScheme());
}
URI fix(URI uri) throws URISyntaxException {
return new URI("https", uri.getAuthority(), uri.getPath(), uri.getQuery(), uri.getFragment());
}
}
I hope this answers your question, even though belatedly. I thought I should document the solution here for further reference by other users who might find it.