Skip to content

Instantly share code, notes, and snippets.

@KevDog
Last active August 29, 2015 14:00
Show Gist options
  • Save KevDog/11226206 to your computer and use it in GitHub Desktop.
Save KevDog/11226206 to your computer and use it in GitHub Desktop.
public static String pagePathUrl(String path) {
if(path == null || path.equals("") || path.equals("#") || checkIfExternalURL(path)) {
return path;
} else {
try {
Session session = getSession();
log.debug("HtmlUtils checking if path exists:" + path);
if (session.itemExists(path)) {
String redirectPropPath = path + "/jcr:content/redirectTarget";
if(session.itemExists(redirectPropPath)) {
Property p = (Property) session.getItem(redirectPropPath);
if(p == null) {
return path + ".html";
} else {
String v = p.getString();
if(v == null || v.equals("")) {
return path + ".html";
} else {
return pagePathUrl(v);
}
}
}
if(path.matches(".*\\.[^/]*")) {
return path;
} else {
return path + ".html";
}
} else {
return path;
}
} catch (Exception r) {
log.warn("pagePathUrl : Exception finding path in repository:" + path, r);
return path;
}
}
}
public static String pagePathUrl(String path) {
if(pathIsNullEmptyAnchorOrExternal(path)) return path;
try {
Session session = getSession();
log.debug("HtmlUtils checking if path exists:" + path);
if (!session.itemExists(path)) return path;
String redirectPropPath = path + "/jcr:content/redirectTarget";
Property p = (Property) session.getItem(redirectPropPath);
if (session.itemExists(redirectPropPath)) {
return createPathFromSession(path, p);
}
if (path.matches(".*\\.[^/]*")) {
return path;
}
return path + ".html";
} catch (PathNotFoundException r) {
log.warn("pagePathUrl : PathNotFoundException, " + path);
return path;
} catch (ValueFormatException r) {
log.warn("pagePathUrl : ValueFormatException, " + path);
return path;
} catch (RepositoryException r) {
log.warn("pagePathUrl : RepositoryException, " + path);
return path;
}
}
private static String createPathFromSession(String path, Property p) throws RepositoryException {
if (pathNotInSession(p)) return path + ".html";
String v = p.getString();
if (redirectIsNullOrEmpty(v)) return path + ".html";
return pagePathUrl(v);
}
private static boolean redirectIsNullOrEmpty(String v) {
if (v == null || v.equals("")) return true;
return false;
}
private static boolean pathNotInSession(Property p) {
if (p == null) return true;
return false;
}
private static boolean pathIsNullEmptyAnchorOrExternal(String path) {
return path == null || path.equals("") || path.equals("#") || checkIfExternalURL(path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment