//String path = packageName+request.getResourceRef().getRemainingPart();
String path = request.getResourceRef().getRemainingPart();
URL script = null;
URI resource = (URI)request.getAttributes().get(RESOURCE_ATTR);
int extPos = path.lastIndexOf('.');
Application app = this.getApplication();
Properties resourceProperties = resourceConfig.match(path);
String stype = resourceProperties.getProperty("content-type");
MediaType type = stype==null ? null : MediaType.valueOf(stype);
String defaultExtension = resourceProperties.getProperty("extension");
if (defaultExtension==null) {
defaultExtension = ".ats";
}
String agentsValue = resourceProperties.getProperty("agents");
String resourceTemplate = resourceProperties.getProperty("resource");
getLogger().info("resource template: "+resourceTemplate);
if (resource==null && resourceTemplate!=null) {
String href = null;
String uri = null;
try {
Template t = new Template(resourceTemplate);
href = t.format(request.getAttributes());
String baseURISpec = resourceProperties.getProperty("base-uri");
URI baseURI = null;
if (baseURISpec!=null) {
Template tbase = new Template(baseURISpec);
uri = tbase.format(request.getAttributes());
baseURI = new URI(uri);
}
resource = baseURI==null ? new URI(href) : baseURI.resolve(href);
} catch (URISyntaxException ex) {
getLogger().log(Level.SEVERE,"Cannot construct resource URI, href="+href+", base="+uri,ex);
return null;
}
}
String [] agents = null;
if (agentsValue!=null) {
agents = agentsValue.split(",");
}
MediaType forceType = null;
if (type==null && extPos>=0) {
String ext = path.substring(extPos+1);
Metadata mdata = this.getApplication().getMetadataService().getMetadata(ext);
if (mdata!=null) {
type = MediaType.valueOf(mdata.getName());
}
} else if (path.length()==0 || path.charAt(path.length()-1)=='/') {
// no extension and is a directory path, default to index.xml template
script = baseClass.getResource(packageName+path+"index.ats");
if (script==null) {
script = baseClass.getResource(packageName+path+"index.xsl");
}
if (resource==null) {
try {
URL url = baseClass.getResource(packageName+path + "index.xml");
/*
if (url==null) {
getLogger().warning("Canot find resource via class path: "+path+"index.xml");
return null;
}
resource = url.toURI();
*/
if (url!=null) {
resource = url.toURI();
}
} catch (URISyntaxException ex) {
getLogger().log(Level.SEVERE,"Cannot convert URL to URI: "+path,ex);
return null;
}
}
if (type==null && agents!=null) {
String agent = request.getClientInfo().getAgent();
for (int i=0; i<agents.length; i++) {
String exp = resourceProperties.getProperty(agents[i]+".match");
if (agent.matches(exp)) {
if (getLogger().isLoggable(Level.FINE)) {
getLogger().fine("Matched agent "+agent+" with "+agents[i]);
}
stype = resourceProperties.getProperty(agents[i]+".content-type");
forceType = stype==null ? null : MediaType.valueOf(stype);
break;
}
}
}
if (type==null) {
type = MediaType.APPLICATION_XHTML_XML;
}
} else {
// no extension, so lookup based on xml file
script = baseClass.getResource(packageName+path+defaultExtension);
if (script==null) {
script = baseClass.getResource(packageName+path+".xsl");
}
if (resource==null) {
try {
URL url = baseClass.getResource(packageName+path + ".xml");
/*
if (url==null) {
getLogger().warning("Canot find resource via class path: "+path+".xml");
return null;
}
resource = url.toURI();
*/
if (url!=null) {
resource = url.toURI();
}
} catch (URISyntaxException ex) {
getLogger().log(Level.SEVERE,"Cannot convert URL to URI: "+path);
return null;
}
}
if (type==null && agents!=null) {
String agent = request.getClientInfo().getAgent();
for (int i=0; i<agents.length; i++) {
String exp = resourceProperties.getProperty(agents[i]+".match");
if (agent.matches(exp)) {
stype = resourceProperties.getProperty(agents[i]+".content-type");
forceType = stype==null ? null : MediaType.valueOf(stype);
break;
}
}
}
if (type==null) {
type = MediaType.APPLICATION_XHTML_XML;
}
}
if (type==null) {
type = app.getMetadataService().getDefaultMediaType();
}
boolean isResource = (type.getMainType().equals("image") || (script==null && type.getMainType().equals("text")) || (type.getMainType().equals("application") && !type.getName().equals("application/xml") && !type.getName().endsWith("+xml")));
String sflag = resourceProperties.getProperty("force-resource");
if (sflag!=null && sflag.equals("true")) {
isResource = true;