|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
package com.sun.org.apache.xml.internal.security.utils.resolver.implementations; |
|
|
|
import java.io.InputStream; |
|
import java.net.URI; |
|
import java.net.URISyntaxException; |
|
import java.nio.file.Files; |
|
import java.nio.file.Paths; |
|
|
|
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; |
|
import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverContext; |
|
import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException; |
|
import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi; |
|
|
|
|
|
|
|
*/ |
|
public class ResolverLocalFilesystem extends ResourceResolverSpi { |
|
|
|
private static final int FILE_URI_LENGTH = "file:/".length(); |
|
|
|
private static final com.sun.org.slf4j.internal.Logger LOG = |
|
com.sun.org.slf4j.internal.LoggerFactory.getLogger(ResolverLocalFilesystem.class); |
|
|
|
@Override |
|
public boolean engineIsThreadSafe() { |
|
return true; |
|
} |
|
|
|
|
|
|
|
*/ |
|
@Override |
|
public XMLSignatureInput engineResolveURI(ResourceResolverContext context) |
|
throws ResourceResolverException { |
|
try { |
|
|
|
URI uriNew = getNewURI(context.uriToResolve, context.baseUri); |
|
|
|
String fileName = |
|
ResolverLocalFilesystem.translateUriToFilename(uriNew.toString()); |
|
InputStream inputStream = Files.newInputStream(Paths.get(fileName)); |
|
XMLSignatureInput result = new XMLSignatureInput(inputStream); |
|
result.setSecureValidation(context.secureValidation); |
|
|
|
result.setSourceURI(uriNew.toString()); |
|
|
|
return result; |
|
} catch (Exception e) { |
|
throw new ResourceResolverException(e, context.uriToResolve, context.baseUri, "generic.EmptyMessage"); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private static String translateUriToFilename(String uri) { |
|
|
|
String subStr = uri.substring(FILE_URI_LENGTH); |
|
|
|
if (subStr.indexOf("%20") > -1) { |
|
int offset = 0; |
|
int index = 0; |
|
StringBuilder temp = new StringBuilder(subStr.length()); |
|
do { |
|
index = subStr.indexOf("%20",offset); |
|
if (index == -1) { |
|
temp.append(subStr.substring(offset)); |
|
} else { |
|
temp.append(subStr.substring(offset, index)); |
|
temp.append(' '); |
|
offset = index + 3; |
|
} |
|
} while(index != -1); |
|
subStr = temp.toString(); |
|
} |
|
|
|
if (subStr.charAt(1) == ':') { |
|
|
|
return subStr; |
|
} |
|
|
|
return "/" + subStr; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public boolean engineCanResolveURI(ResourceResolverContext context) { |
|
if (context.uriToResolve == null) { |
|
return false; |
|
} |
|
|
|
if (context.uriToResolve.equals("") || context.uriToResolve.charAt(0) == '#' || |
|
context.uriToResolve.startsWith("http:")) { |
|
return false; |
|
} |
|
|
|
try { |
|
LOG.debug("I was asked whether I can resolve {}", context.uriToResolve); |
|
|
|
if (context.uriToResolve.startsWith("file:") || context.baseUri.startsWith("file:")) { |
|
LOG.debug("I state that I can resolve {}", context.uriToResolve); |
|
return true; |
|
} |
|
} catch (Exception e) { |
|
LOG.debug(e.getMessage(), e); |
|
} |
|
|
|
LOG.debug("But I can't"); |
|
|
|
return false; |
|
} |
|
|
|
private static URI getNewURI(String uri, String baseURI) throws URISyntaxException { |
|
URI newUri = null; |
|
if (baseURI == null || "".equals(baseURI)) { |
|
newUri = new URI(uri); |
|
} else { |
|
newUri = new URI(baseURI).resolve(uri); |
|
} |
|
|
|
|
|
if (newUri.getFragment() != null) { |
|
URI uriNewNoFrag = |
|
new URI(newUri.getScheme(), newUri.getSchemeSpecificPart(), null); |
|
return uriNewNoFrag; |
|
} |
|
return newUri; |
|
} |
|
} |