blob: 55d00425b7b24e37a7a3b09e3075989b8f50f493 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Copyright 1999-2004 The Apache Software Foundation.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21package com.sun.org.apache.xml.internal.security.utils.resolver.implementations;
22
23
24
25import java.io.FileInputStream;
26
27import com.sun.org.apache.xml.internal.utils.URI;
28import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
29import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException;
30import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi;
31import org.w3c.dom.Attr;
32
33
34/**
35 * A simple ResourceResolver for requests into the local filesystem.
36 *
37 * @author $Author: raul $
38 */
39public class ResolverLocalFilesystem extends ResourceResolverSpi {
40
41 /** {@link java.util.logging} logging facility */
42 static java.util.logging.Logger log =
43 java.util.logging.Logger.getLogger(
44 ResolverLocalFilesystem.class.getName());
45
46 /**
47 * @inheritDoc
48 */
49 public XMLSignatureInput engineResolve(Attr uri, String BaseURI)
50 throws ResourceResolverException {
51
52 try {
53 URI uriNew = new URI(new URI(BaseURI), uri.getNodeValue());
54
55 // if the URI contains a fragment, ignore it
56 URI uriNewNoFrag = new URI(uriNew);
57
58 uriNewNoFrag.setFragment(null);
59
60 String fileName =
61 ResolverLocalFilesystem
62 .translateUriToFilename(uriNewNoFrag.toString());
63 FileInputStream inputStream = new FileInputStream(fileName);
64 XMLSignatureInput result = new XMLSignatureInput(inputStream);
65
66 result.setSourceURI(uriNew.toString());
67
68 return result;
69 } catch (Exception e) {
70 throw new ResourceResolverException("generic.EmptyMessage", e, uri,
71 BaseURI);
72 }
73 }
74
75 /**
76 * Method translateUriToFilename
77 *
78 * @param uri
79 * @return the string of the filename
80 */
81 private static String translateUriToFilename(String uri) {
82
83 String subStr = uri.substring("file:/".length());
84
85 if (subStr.indexOf("%20") > -1)
86 {
87 int offset = 0;
88 int index = 0;
89 StringBuffer temp = new StringBuffer(subStr.length());
90 do
91 {
92 index = subStr.indexOf("%20",offset);
93 if (index == -1) temp.append(subStr.substring(offset));
94 else
95 {
96 temp.append(subStr.substring(offset,index));
97 temp.append(' ');
98 offset = index+3;
99 }
100 }
101 while(index != -1);
102 subStr = temp.toString();
103 }
104
105 if (subStr.charAt(1) == ':') {
106 // we're running M$ Windows, so this works fine
107 return subStr;
108 }
109 // we're running some UNIX, so we have to prepend a slash
110 return "/" + subStr;
111 }
112
113 /**
114 * @inheritDoc
115 */
116 public boolean engineCanResolve(Attr uri, String BaseURI) {
117
118 if (uri == null) {
119 return false;
120 }
121
122 String uriNodeValue = uri.getNodeValue();
123
124 if (uriNodeValue.equals("") || (uriNodeValue.charAt(0)=='#')) {
125 return false;
126 }
127
128 try {
129 //URI uriNew = new URI(new URI(BaseURI), uri.getNodeValue());
130 if (true)
131 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I was asked whether I can resolve " + uriNodeValue/*uriNew.toString()*/);
132
133 if ( uriNodeValue.startsWith("file:") ||
134 BaseURI.startsWith("file:")/*uriNew.getScheme().equals("file")*/) {
135 if (true)
136 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I state that I can resolve " + uriNodeValue/*uriNew.toString()*/);
137
138 return true;
139 }
140 } catch (Exception e) {}
141
142 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "But I can't");
143
144 return false;
145 }
146}