| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.configuration; |
| 18 | |
|
| 19 | |
import java.io.File; |
| 20 | |
import java.io.FileNotFoundException; |
| 21 | |
import java.io.FileOutputStream; |
| 22 | |
import java.io.IOException; |
| 23 | |
import java.io.InputStream; |
| 24 | |
import java.io.OutputStream; |
| 25 | |
import java.net.HttpURLConnection; |
| 26 | |
import java.net.MalformedURLException; |
| 27 | |
import java.net.URL; |
| 28 | |
import java.net.URLConnection; |
| 29 | |
|
| 30 | |
import org.apache.commons.logging.Log; |
| 31 | |
import org.apache.commons.logging.LogFactory; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 119 | public class DefaultFileSystem extends FileSystem |
| 40 | |
{ |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | 119 | private Log log = LogFactory.getLog(DefaultFileSystem.class); |
| 45 | |
|
| 46 | |
@Override |
| 47 | |
public InputStream getInputStream(String basePath, String fileName) |
| 48 | |
throws ConfigurationException |
| 49 | |
{ |
| 50 | |
try |
| 51 | |
{ |
| 52 | 1446 | URL url = ConfigurationUtils.locate(this, basePath, fileName); |
| 53 | |
|
| 54 | 1446 | if (url == null) |
| 55 | |
{ |
| 56 | 0 | throw new ConfigurationException("Cannot locate configuration source " + fileName); |
| 57 | |
} |
| 58 | 1446 | return getInputStream(url); |
| 59 | |
} |
| 60 | 0 | catch (ConfigurationException e) |
| 61 | |
{ |
| 62 | 0 | throw e; |
| 63 | |
} |
| 64 | 0 | catch (Exception e) |
| 65 | |
{ |
| 66 | 0 | throw new ConfigurationException("Unable to load the configuration file " + fileName, e); |
| 67 | |
} |
| 68 | |
} |
| 69 | |
|
| 70 | |
@Override |
| 71 | |
public InputStream getInputStream(URL url) throws ConfigurationException |
| 72 | |
{ |
| 73 | |
|
| 74 | 21160 | File file = ConfigurationUtils.fileFromURL(url); |
| 75 | 21160 | if (file != null && file.isDirectory()) |
| 76 | |
{ |
| 77 | 4 | throw new ConfigurationException("Cannot load a configuration from a directory"); |
| 78 | |
} |
| 79 | |
|
| 80 | |
try |
| 81 | |
{ |
| 82 | 21156 | return url.openStream(); |
| 83 | |
} |
| 84 | 1 | catch (Exception e) |
| 85 | |
{ |
| 86 | 1 | throw new ConfigurationException("Unable to load the configuration from the URL " + url, e); |
| 87 | |
} |
| 88 | |
} |
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public OutputStream getOutputStream(URL url) throws ConfigurationException |
| 92 | |
{ |
| 93 | |
|
| 94 | |
|
| 95 | 77 | File file = ConfigurationUtils.fileFromURL(url); |
| 96 | 77 | if (file != null) |
| 97 | |
{ |
| 98 | 72 | return getOutputStream(file); |
| 99 | |
} |
| 100 | |
else |
| 101 | |
{ |
| 102 | |
|
| 103 | |
OutputStream out; |
| 104 | |
try |
| 105 | |
{ |
| 106 | 5 | URLConnection connection = url.openConnection(); |
| 107 | 5 | connection.setDoOutput(true); |
| 108 | |
|
| 109 | |
|
| 110 | 5 | if (connection instanceof HttpURLConnection) |
| 111 | |
{ |
| 112 | 4 | HttpURLConnection conn = (HttpURLConnection) connection; |
| 113 | 4 | conn.setRequestMethod("PUT"); |
| 114 | |
} |
| 115 | |
|
| 116 | 5 | out = connection.getOutputStream(); |
| 117 | |
|
| 118 | |
|
| 119 | 5 | if (connection instanceof HttpURLConnection) |
| 120 | |
{ |
| 121 | 4 | out = new HttpOutputStream(out, (HttpURLConnection) connection); |
| 122 | |
} |
| 123 | 5 | return out; |
| 124 | |
} |
| 125 | 0 | catch (IOException e) |
| 126 | |
{ |
| 127 | 0 | throw new ConfigurationException("Could not save to URL " + url, e); |
| 128 | |
} |
| 129 | |
} |
| 130 | |
} |
| 131 | |
|
| 132 | |
@Override |
| 133 | |
public OutputStream getOutputStream(File file) throws ConfigurationException |
| 134 | |
{ |
| 135 | |
try |
| 136 | |
{ |
| 137 | |
|
| 138 | 117 | createPath(file); |
| 139 | 117 | return new FileOutputStream(file); |
| 140 | |
} |
| 141 | 0 | catch (FileNotFoundException e) |
| 142 | |
{ |
| 143 | 0 | throw new ConfigurationException("Unable to save to file " + file, e); |
| 144 | |
} |
| 145 | |
} |
| 146 | |
|
| 147 | |
@Override |
| 148 | |
public String getPath(File file, URL url, String basePath, String fileName) |
| 149 | |
{ |
| 150 | 5 | String path = null; |
| 151 | |
|
| 152 | 5 | if (file != null) |
| 153 | |
{ |
| 154 | 2 | path = file.getAbsolutePath(); |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | 5 | if (path == null) |
| 159 | |
{ |
| 160 | 3 | if (url != null) |
| 161 | |
{ |
| 162 | 3 | path = url.getPath(); |
| 163 | |
} |
| 164 | |
else |
| 165 | |
{ |
| 166 | |
try |
| 167 | |
{ |
| 168 | 0 | path = getURL(basePath, fileName).getPath(); |
| 169 | |
} |
| 170 | 0 | catch (Exception e) |
| 171 | |
{ |
| 172 | |
|
| 173 | 0 | if (log.isDebugEnabled()) |
| 174 | |
{ |
| 175 | 0 | log.debug(String.format("Could not determine URL for " |
| 176 | |
+ "basePath = %s, fileName = %s.", basePath, |
| 177 | |
fileName), e); |
| 178 | |
} |
| 179 | 0 | } |
| 180 | |
} |
| 181 | |
} |
| 182 | |
|
| 183 | 5 | return path; |
| 184 | |
} |
| 185 | |
|
| 186 | |
@Override |
| 187 | |
public String getBasePath(String path) |
| 188 | |
{ |
| 189 | |
URL url; |
| 190 | |
try |
| 191 | |
{ |
| 192 | 0 | url = getURL(null, path); |
| 193 | 0 | return ConfigurationUtils.getBasePath(url); |
| 194 | |
} |
| 195 | 0 | catch (Exception e) |
| 196 | |
{ |
| 197 | 0 | return null; |
| 198 | |
} |
| 199 | |
} |
| 200 | |
|
| 201 | |
@Override |
| 202 | |
public String getFileName(String path) |
| 203 | |
{ |
| 204 | |
URL url; |
| 205 | |
try |
| 206 | |
{ |
| 207 | 0 | url = getURL(null, path); |
| 208 | 0 | return ConfigurationUtils.getFileName(url); |
| 209 | |
} |
| 210 | 0 | catch (Exception e) |
| 211 | |
{ |
| 212 | 0 | return null; |
| 213 | |
} |
| 214 | |
} |
| 215 | |
|
| 216 | |
|
| 217 | |
@Override |
| 218 | |
public URL getURL(String basePath, String file) throws MalformedURLException |
| 219 | |
{ |
| 220 | 51 | File f = new File(file); |
| 221 | 51 | if (f.isAbsolute()) |
| 222 | |
{ |
| 223 | 12 | return ConfigurationUtils.toURL(f); |
| 224 | |
} |
| 225 | |
|
| 226 | |
try |
| 227 | |
{ |
| 228 | 39 | if (basePath == null) |
| 229 | |
{ |
| 230 | 7 | return new URL(file); |
| 231 | |
} |
| 232 | |
else |
| 233 | |
{ |
| 234 | 32 | URL base = new URL(basePath); |
| 235 | 4 | return new URL(base, file); |
| 236 | |
} |
| 237 | |
} |
| 238 | 33 | catch (MalformedURLException uex) |
| 239 | |
{ |
| 240 | 33 | return ConfigurationUtils.toURL(ConfigurationUtils.constructFile(basePath, file)); |
| 241 | |
} |
| 242 | |
} |
| 243 | |
|
| 244 | |
|
| 245 | |
@Override |
| 246 | |
public URL locateFromURL(String basePath, String fileName) |
| 247 | |
{ |
| 248 | |
try |
| 249 | |
{ |
| 250 | |
URL url; |
| 251 | 7067 | if (basePath == null) |
| 252 | |
{ |
| 253 | 1828 | return new URL(fileName); |
| 254 | |
|
| 255 | |
} |
| 256 | |
else |
| 257 | |
{ |
| 258 | 5239 | URL baseURL = new URL(basePath); |
| 259 | 4200 | url = new URL(baseURL, fileName); |
| 260 | |
|
| 261 | |
|
| 262 | 4198 | InputStream in = null; |
| 263 | |
try |
| 264 | |
{ |
| 265 | 4198 | in = url.openStream(); |
| 266 | |
} |
| 267 | |
finally |
| 268 | |
{ |
| 269 | 4198 | if (in != null) |
| 270 | |
{ |
| 271 | 4174 | in.close(); |
| 272 | |
} |
| 273 | |
} |
| 274 | 4174 | return url; |
| 275 | |
} |
| 276 | |
} |
| 277 | 1447 | catch (IOException e) |
| 278 | |
{ |
| 279 | 1447 | if (log.isDebugEnabled()) |
| 280 | |
{ |
| 281 | 0 | log.debug("Could not locate file " + fileName + " at " + basePath + ": " + e.getMessage()); |
| 282 | |
} |
| 283 | 1447 | return null; |
| 284 | |
} |
| 285 | |
} |
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
private void createPath(File file) |
| 293 | |
{ |
| 294 | 117 | if (file != null) |
| 295 | |
{ |
| 296 | |
|
| 297 | 117 | if (!file.exists()) |
| 298 | |
{ |
| 299 | 59 | File parent = file.getParentFile(); |
| 300 | 59 | if (parent != null && !parent.exists()) |
| 301 | |
{ |
| 302 | 3 | parent.mkdirs(); |
| 303 | |
} |
| 304 | |
} |
| 305 | |
} |
| 306 | 117 | } |
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | 119 | private static class HttpOutputStream extends VerifiableOutputStream |
| 314 | |
{ |
| 315 | |
|
| 316 | |
private final OutputStream stream; |
| 317 | |
|
| 318 | |
|
| 319 | |
private final HttpURLConnection connection; |
| 320 | |
|
| 321 | |
public HttpOutputStream(OutputStream stream, HttpURLConnection connection) |
| 322 | 4 | { |
| 323 | 4 | this.stream = stream; |
| 324 | 4 | this.connection = connection; |
| 325 | 4 | } |
| 326 | |
|
| 327 | |
@Override |
| 328 | |
public void write(byte[] bytes) throws IOException |
| 329 | |
{ |
| 330 | 0 | stream.write(bytes); |
| 331 | 0 | } |
| 332 | |
|
| 333 | |
@Override |
| 334 | |
public void write(byte[] bytes, int i, int i1) throws IOException |
| 335 | |
{ |
| 336 | 2 | stream.write(bytes, i, i1); |
| 337 | 2 | } |
| 338 | |
|
| 339 | |
@Override |
| 340 | |
public void flush() throws IOException |
| 341 | |
{ |
| 342 | 4 | stream.flush(); |
| 343 | 4 | } |
| 344 | |
|
| 345 | |
@Override |
| 346 | |
public void close() throws IOException |
| 347 | |
{ |
| 348 | 4 | stream.close(); |
| 349 | 4 | } |
| 350 | |
|
| 351 | |
@Override |
| 352 | |
public void write(int i) throws IOException |
| 353 | |
{ |
| 354 | 0 | stream.write(i); |
| 355 | 0 | } |
| 356 | |
|
| 357 | |
@Override |
| 358 | |
public String toString() |
| 359 | |
{ |
| 360 | 0 | return stream.toString(); |
| 361 | |
} |
| 362 | |
|
| 363 | |
@Override |
| 364 | |
public void verify() throws IOException |
| 365 | |
{ |
| 366 | 4 | if (connection.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) |
| 367 | |
{ |
| 368 | 3 | throw new IOException("HTTP Error " + connection.getResponseCode() |
| 369 | |
+ " " + connection.getResponseMessage()); |
| 370 | |
} |
| 371 | 1 | } |
| 372 | |
} |
| 373 | |
} |