Author: matthieu
Date: Tue Oct 20 15:48:17 2015
New Revision: 1709627
URL: http://svn.apache.org/viewvc?rev=1709627&view=rev
Log:
JAMES-1606: FileSystem implementation without Spring dependency
Added:
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ClassPathResource.java
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/FileSystemImpl.java
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/FileSystemResource.java
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/Resource.java
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ResourceFactory.java
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ResourceUtils.java
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/SimpleUrl.java
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/UrlResource.java
james/project/trunk/server/container/core/src/test/java/org/apache/james/core/filesystem/
james/project/trunk/server/container/core/src/test/java/org/apache/james/core/filesystem/FileSystemImplTest.java
james/project/trunk/server/container/core/src/test/java/org/apache/james/core/filesystem/SimpleUrlTest.java
Modified:
james/project/trunk/server/container/core/pom.xml
Modified: james/project/trunk/server/container/core/pom.xml
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/core/pom.xml?rev=1709627&r1=1709626&r2=1709627&view=diff
==============================================================================
--- james/project/trunk/server/container/core/pom.xml (original)
+++ james/project/trunk/server/container/core/pom.xml Tue Oct 20 15:48:17 2015
@@ -37,6 +37,12 @@
<dependencies>
<dependency>
<groupId>org.apache.james</groupId>
+ <artifactId>james-server-filesystem-api</artifactId>
+ <scope>test</scope>
+ <type>test-jar</type>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.james</groupId>
<artifactId>james-server-lifecycle-api</artifactId>
</dependency>
@@ -50,7 +56,6 @@
<groupId>org.apache.james</groupId>
<artifactId>apache-mailet-base</artifactId>
</dependency>
-
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
@@ -77,6 +82,16 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>pl.pragmatists</groupId>
+ <artifactId>JUnitParams</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
Added:
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ClassPathResource.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ClassPathResource.java?rev=1709627&view=auto
==============================================================================
---
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ClassPathResource.java
(added)
+++
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ClassPathResource.java
Tue Oct 20 15:48:17 2015
@@ -0,0 +1,110 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.core.filesystem;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+public class ClassPathResource implements Resource {
+
+ private final String path;
+ private final ClassLoader classLoader;
+
+ public ClassPathResource(String path) {
+ this.path = sanitizePath(path);
+ this.classLoader = getDefaultClassLoader();
+ }
+
+ private String sanitizePath(String path) {
+ String pathToUse = new SimpleUrl(path).getSimplified();
+ if (pathToUse.startsWith("/")) {
+ return pathToUse.substring(1);
+ }
+ return pathToUse;
+ }
+
+ @Override
+ public File getFile() throws IOException {
+ URL url = getURL();
+ return ResourceUtils.getFile(url, getDescription());
+ }
+
+ public URL getURL() throws IOException {
+ URL url = resolveURL();
+ if (url == null) {
+ throw new FileNotFoundException(getDescription() + " cannot be
resolved to URL because it does not exist");
+ }
+ return url;
+ }
+
+ protected URL resolveURL() {
+ return this.classLoader.getResource(this.path);
+ }
+
+ @Override
+ public InputStream getInputStream() throws IOException {
+ InputStream is = this.classLoader.getResourceAsStream(this.path);
+ if (is == null) {
+ throw new FileNotFoundException(getDescription() + " cannot be
opened because it does not exist");
+ }
+ return is;
+ }
+
+ public String getDescription() {
+ return "class path resource [" + path + "]";
+ }
+
+ private ClassLoader getDefaultClassLoader() {
+ ClassLoader currentThreadClassLoader = getcurrentThreadClassLoader();
+ if (currentThreadClassLoader != null) {
+ return currentThreadClassLoader;
+ }
+
+ // No thread context class loader -> use class loader of this class.
+ ClassLoader currentClassClassLoader =
ClassPathResource.class.getClassLoader();
+ if (currentClassClassLoader != null) {
+ return currentClassClassLoader;
+ }
+
+ // getClassLoader() returning null indicates the bootstrap ClassLoader
+ return getSystemClassLoader();
+ }
+
+ private ClassLoader getcurrentThreadClassLoader() {
+ try {
+ return Thread.currentThread().getContextClassLoader();
+ } catch (Throwable ex) {
+ // Cannot access thread context ClassLoader - falling back...
+ return null;
+ }
+ }
+
+ private ClassLoader getSystemClassLoader() {
+ try {
+ return ClassLoader.getSystemClassLoader();
+ } catch (Throwable ex) {
+ // Cannot access system ClassLoader - oh well, maybe the
+ // caller can live with null...
+ return null;
+ }
+ }
+}
\ No newline at end of file
Added:
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/FileSystemImpl.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/FileSystemImpl.java?rev=1709627&view=auto
==============================================================================
---
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/FileSystemImpl.java
(added)
+++
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/FileSystemImpl.java
Tue Oct 20 15:48:17 2015
@@ -0,0 +1,57 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.core.filesystem;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.james.filesystem.api.FileSystem;
+import org.apache.james.filesystem.api.JamesDirectoriesProvider;
+
+public class FileSystemImpl implements FileSystem {
+
+ private final JamesDirectoriesProvider directoryProvider;
+ private final ResourceFactory resourceLoader;
+
+ public FileSystemImpl(JamesDirectoriesProvider directoryProvider) {
+ this.directoryProvider = directoryProvider;
+ this.resourceLoader = new ResourceFactory(directoryProvider);
+ }
+
+ @Override
+ public File getBasedir() throws FileNotFoundException {
+ return new File(directoryProvider.getRootDirectory());
+ }
+
+ @Override
+ public InputStream getResource(String url) throws IOException {
+ return resourceLoader.getResource(url).getInputStream();
+ }
+
+ @Override
+ public File getFile(String fileURL) throws FileNotFoundException {
+ try {
+ return resourceLoader.getResource(fileURL).getFile();
+ } catch (IOException e) {
+ throw new FileNotFoundException(e.getMessage());
+ }
+ }
+}
Added:
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/FileSystemResource.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/FileSystemResource.java?rev=1709627&view=auto
==============================================================================
---
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/FileSystemResource.java
(added)
+++
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/FileSystemResource.java
Tue Oct 20 15:48:17 2015
@@ -0,0 +1,44 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.core.filesystem;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+class FileSystemResource implements Resource {
+
+ private final File file;
+
+ public FileSystemResource(File file) {
+ this.file = file;
+ }
+
+ @Override
+ public File getFile() {
+ return this.file;
+ }
+
+ @Override
+ public InputStream getInputStream() throws IOException {
+ return new FileInputStream(this.file);
+ }
+
+}
\ No newline at end of file
Added:
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/Resource.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/Resource.java?rev=1709627&view=auto
==============================================================================
---
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/Resource.java
(added)
+++
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/Resource.java
Tue Oct 20 15:48:17 2015
@@ -0,0 +1,29 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.core.filesystem;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+public interface Resource {
+ File getFile() throws IOException;
+
+ InputStream getInputStream() throws IOException;
+}
\ No newline at end of file
Added:
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ResourceFactory.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ResourceFactory.java?rev=1709627&view=auto
==============================================================================
---
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ResourceFactory.java
(added)
+++
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ResourceFactory.java
Tue Oct 20 15:48:17 2015
@@ -0,0 +1,100 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.core.filesystem;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.apache.james.filesystem.api.FileSystem;
+import org.apache.james.filesystem.api.JamesDirectoriesProvider;
+
+public class ResourceFactory {
+
+ private final JamesDirectoriesProvider directoryProvider;
+
+ public ResourceFactory(JamesDirectoriesProvider directoryProvider) {
+ this.directoryProvider = directoryProvider;
+ }
+
+ public Resource getResource(String fileURL) {
+ if (fileURL.startsWith(FileSystem.CLASSPATH_PROTOCOL)) {
+ return handleClasspathProtocol(fileURL);
+ } else if (fileURL.startsWith(FileSystem.FILE_PROTOCOL)) {
+ return handleFileProtocol(fileURL);
+ } else {
+ try {
+ // Try to parse the location as a URL...
+ return handleUrlResource(fileURL);
+ } catch (MalformedURLException ex) {
+ // No URL -> resolve as resource path.
+ return new ClassPathResource(fileURL);
+ }
+ }
+ }
+
+ private Resource handleUrlResource(String fileURL) throws
MalformedURLException {
+ URL url = new URL(fileURL);
+ return new UrlResource(url);
+ }
+
+ private Resource handleClasspathProtocol(String fileURL) {
+ String resourceName =
fileURL.substring(FileSystem.CLASSPATH_PROTOCOL.length());
+ return new ClassPathResource(resourceName);
+ }
+
+ private Resource handleFileProtocol(String fileURL) {
+ File file = interpretPath(fileURL);
+ return new FileSystemResource(file);
+ }
+
+ private File interpretPath(String fileURL) {
+ if (FileProtocol.CONF.match(fileURL)) {
+ return new File(directoryProvider.getConfDirectory() + "/" +
FileProtocol.CONF.removeProtocolFromPath(fileURL));
+ } else if (FileProtocol.VAR.match(fileURL)) {
+ return new File(directoryProvider.getVarDirectory() + "/" +
FileProtocol.VAR.removeProtocolFromPath(fileURL));
+ } else if (FileProtocol.ABSOLUTE.match(fileURL)) {
+ return new File(directoryProvider.getAbsoluteDirectory() +
FileProtocol.ABSOLUTE.removeProtocolFromPath(fileURL));
+ } else {
+ // move to the root folder of the spring deployment
+ return new File(directoryProvider.getRootDirectory() + "/" +
FileProtocol.OTHER.removeProtocolFromPath(fileURL));
+ }
+ }
+
+ private enum FileProtocol {
+ CONF(FileSystem.FILE_PROTOCOL_AND_CONF),
+ VAR(FileSystem.FILE_PROTOCOL_AND_VAR),
+ ABSOLUTE(FileSystem.FILE_PROTOCOL_ABSOLUTE),
+ OTHER(FileSystem.FILE_PROTOCOL);
+
+ private final String protocolPrefix;
+
+ private FileProtocol(String protocolPrefix) {
+ this.protocolPrefix = protocolPrefix;
+ }
+
+ private boolean match(String path) {
+ return path.startsWith(protocolPrefix);
+ }
+
+ private String removeProtocolFromPath(String path) {
+ return path.substring(protocolPrefix.length());
+ }
+ }
+}
\ No newline at end of file
Added:
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ResourceUtils.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ResourceUtils.java?rev=1709627&view=auto
==============================================================================
---
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ResourceUtils.java
(added)
+++
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/ResourceUtils.java
Tue Oct 20 15:48:17 2015
@@ -0,0 +1,41 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.core.filesystem;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+public class ResourceUtils {
+ public static final String URL_PROTOCOL_FILE = "file";
+
+ public static File getFile(URL url, String description) throws
FileNotFoundException {
+ if (!URL_PROTOCOL_FILE.equals(url.getProtocol())) {
+ throw new FileNotFoundException(description + " cannot be resolved
to absolute file path " + "because it does not reside in the file system: " +
url);
+ }
+ try {
+ return new File(url.toURI().getSchemeSpecificPart());
+ } catch (URISyntaxException ex) {
+ // Fallback for URLs that are not valid URIs (should hardly ever
+ // happen).
+ return new File(url.getFile());
+ }
+ }
+}
\ No newline at end of file
Added:
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/SimpleUrl.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/SimpleUrl.java?rev=1709627&view=auto
==============================================================================
---
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/SimpleUrl.java
(added)
+++
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/SimpleUrl.java
Tue Oct 20 15:48:17 2015
@@ -0,0 +1,74 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.core.filesystem;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.CharMatcher;
+import com.google.common.base.Optional;
+import com.google.common.io.Files;
+
+public class SimpleUrl {
+ private static final String FOLDER_SEPARATOR = "/";
+
+ private static final char WINDOWS_FOLDER_SEPARATOR = '\\';
+
+ private static final String CURRENT_PATH = ".";
+
+ private static final Pattern URL_REGEXP =
Pattern.compile("^([^/][^/]*:(?://)?)?(.*)");
+
+ private static String url;
+ private static String protocol;
+ private static String path;
+ private static String simplifiedUrl;
+
+ public SimpleUrl(String url) {
+ SimpleUrl.url = url;
+ String urlWithUnixSeparators =
CharMatcher.is(WINDOWS_FOLDER_SEPARATOR).replaceFrom(url, FOLDER_SEPARATOR);
+ extractComponents(urlWithUnixSeparators);
+ simplifiedUrl = protocol + simplifyPath(path);
+ }
+
+ private static void extractComponents(String urlWithUnixSeparators) {
+ Matcher m = URL_REGEXP.matcher(urlWithUnixSeparators);
+ m.matches();
+ protocol = Optional.fromNullable(m.group(1)).or("");
+ path = Optional.fromNullable(m.group(2)).or("");
+ }
+
+ @VisibleForTesting
+ static String simplifyPath(String path) {
+ String simplified = Files.simplifyPath(path);
+ if (CURRENT_PATH.equals(simplified)) {
+ return "";
+ }
+ return simplified;
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ public String getSimplified() {
+ return simplifiedUrl;
+ }
+
+}
\ No newline at end of file
Added:
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/UrlResource.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/UrlResource.java?rev=1709627&view=auto
==============================================================================
---
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/UrlResource.java
(added)
+++
james/project/trunk/server/container/core/src/main/java/org/apache/james/core/filesystem/UrlResource.java
Tue Oct 20 15:48:17 2015
@@ -0,0 +1,59 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.core.filesystem;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+
+public class UrlResource implements Resource {
+ public static final String URL_PROTOCOL_FILE = "file";
+ private URL url;
+
+ public UrlResource(URL url) {
+ this.url = url;
+ }
+
+ @Override
+ public InputStream getInputStream() throws IOException {
+ URLConnection con = this.url.openConnection();
+ useCachesIfNecessary(con);
+ try {
+ return con.getInputStream();
+ } catch (IOException ex) {
+ // Close the HTTP connection (if applicable).
+ if (con instanceof HttpURLConnection) {
+ ((HttpURLConnection) con).disconnect();
+ }
+ throw ex;
+ }
+ }
+
+ public static void useCachesIfNecessary(URLConnection con) {
+ con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP"));
+ }
+
+ @Override
+ public File getFile() throws IOException {
+ return ResourceUtils.getFile(url, "URL [" + this.url + "]");
+ }
+}
\ No newline at end of file
Added:
james/project/trunk/server/container/core/src/test/java/org/apache/james/core/filesystem/FileSystemImplTest.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/core/src/test/java/org/apache/james/core/filesystem/FileSystemImplTest.java?rev=1709627&view=auto
==============================================================================
---
james/project/trunk/server/container/core/src/test/java/org/apache/james/core/filesystem/FileSystemImplTest.java
(added)
+++
james/project/trunk/server/container/core/src/test/java/org/apache/james/core/filesystem/FileSystemImplTest.java
Tue Oct 20 15:48:17 2015
@@ -0,0 +1,45 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.core.filesystem;
+
+import org.apache.james.core.JamesServerResourceLoader;
+import org.apache.james.filesystem.api.AbstractFileSystemTest;
+import org.apache.james.filesystem.api.FileSystem;
+
+public class FileSystemImplTest extends AbstractFileSystemTest {
+
+ @Override
+ protected FileSystem buildFileSystem(String configurationRootDirectory) {
+ return new FileSystemImpl(new
TestDirectoryProvider(configurationRootDirectory));
+ }
+
+ private class TestDirectoryProvider extends JamesServerResourceLoader {
+ private String configurationRootDirectory;
+
+ public TestDirectoryProvider(String configurationRootDirectory) {
+ this.configurationRootDirectory = configurationRootDirectory;
+ }
+
+ @Override
+ public String getRootDirectory() {
+ return configurationRootDirectory;
+ }
+ }
+
+}
Added:
james/project/trunk/server/container/core/src/test/java/org/apache/james/core/filesystem/SimpleUrlTest.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/core/src/test/java/org/apache/james/core/filesystem/SimpleUrlTest.java?rev=1709627&view=auto
==============================================================================
---
james/project/trunk/server/container/core/src/test/java/org/apache/james/core/filesystem/SimpleUrlTest.java
(added)
+++
james/project/trunk/server/container/core/src/test/java/org/apache/james/core/filesystem/SimpleUrlTest.java
Tue Oct 20 15:48:17 2015
@@ -0,0 +1,114 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.core.filesystem;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.Test;
+
+public class SimpleUrlTest {
+
+ @Test(expected=NullPointerException.class)
+ public void simplifyPathShouldThrowOnNull() {
+ SimpleUrl.simplifyPath(null);
+ }
+
+ @Test
+ public void simplifyPathShouldReturnEmptyOnEmptyArray() {
+ String actual = SimpleUrl.simplifyPath("");
+ assertThat(actual).isEmpty();
+ }
+
+ @Test
+ public void simplifyPathShoudReturnEmptyWhenSimplifyingCurrentDir() {
+ String actual = SimpleUrl.simplifyPath("./bar/.././foo/..");
+ assertThat(actual).isEmpty();
+ }
+
+ @Test
+ public void simplifyPathShoudReturnSimplifiedDirectory() {
+ String actual = SimpleUrl.simplifyPath("../foo/../bar/./baz");
+ assertThat(actual).isEqualTo("../bar/baz");
+ }
+
+ @Test
+ public void simplifiedShouldReturnEmptyWhenEmptyInput() {
+ assertThat(new SimpleUrl("").getSimplified()).isEmpty();
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void simplifiedShouldThrowWhenNullInput() {
+ new SimpleUrl(null);
+ }
+
+ @Test
+ public void simplifiedShouldReturnInputValueWhenProtocolOnlyInput() {
+ assertThat(new SimpleUrl("file:").getSimplified()).isEqualTo("file:");
+ }
+
+ @Test
+ public void simplifiedShouldReturnInputValueWhenRelativePath() {
+ assertThat(new
SimpleUrl("abcd/ef/gh").getSimplified()).isEqualTo("abcd/ef/gh");
+ }
+
+ @Test
+ public void simplifiedShouldReturnInputValueWhenAbsolutePath() {
+ assertThat(new
SimpleUrl("/abcd/ef/gh").getSimplified()).isEqualTo("/abcd/ef/gh");
+ }
+
+ @Test
+ public void simplifiedShouldReturnInputValueWhenHttpUrl() {
+ assertThat(new
SimpleUrl("http://example.com/ef/gh").getSimplified()).isEqualTo("http://example.com/ef/gh");
+ }
+
+ @Test
+ public void simplifiedShouldReturnInputValueWhenPathContainsColumn() {
+ assertThat(new
SimpleUrl("ab/cd:ef/gh").getSimplified()).isEqualTo("ab/cd:ef/gh");
+ }
+
+ @Test
+ public void
simplifiedShouldCollapseComplexePathWhenContainingParentDirElement() {
+ assertThat(new
SimpleUrl("file:///home/user/./foo/../.bar/baz").getSimplified()).isEqualTo("file:///home/user/.bar/baz");
+ }
+
+ @Test
+ public void
simplifiedShouldCollapseComplexePathWhenContainingParentDirElementInRelativePath()
{
+ assertThat(new
SimpleUrl("file://../.././foo/../.bar/baz").getSimplified()).isEqualTo("file://../../.bar/baz");
+ }
+
+ @Test
+ public void
simplifiedShouldCollapseComplexePathWhenContainingParentDirElementWithoutDoubleSlashes()
{
+ assertThat(new
SimpleUrl("file:/home/user/./foo/../.bar/baz").getSimplified()).isEqualTo("file:/home/user/.bar/baz");
+ }
+
+ @Test
+ public void
simplifiedShouldCollapseComplexePathWhenContainingParentDirElementInRelativePathWithoutDoubleSlashes()
{
+ assertThat(new
SimpleUrl("file:../.././foo/../.bar/baz").getSimplified()).isEqualTo("file:../../.bar/baz");
+ }
+
+ @Test
+ public void simplifiedShouldReplaceASingleWindowSeperatorByASlash() {
+ assertThat(new SimpleUrl("\\").getSimplified()).isEqualTo("/");
+ }
+
+ @Test
+ public void simplifiedShouldReplaceAllWindowsSeperatorBySlashes() {
+ assertThat(new
SimpleUrl("file:c:\\\\programs\\run.exe").getSimplified()).isEqualTo("file:c://programs/run.exe");
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]