Implemented container name and blob key validation rules

This commit is contained in:
rainbowbreeze 2010-09-02 13:14:11 +02:00
parent c51b530cce
commit 15e500ecb4
11 changed files with 200 additions and 88 deletions

View File

@ -40,6 +40,10 @@ import org.jclouds.domain.Location;
import org.jclouds.domain.LocationScope; import org.jclouds.domain.LocationScope;
import org.jclouds.domain.internal.LocationImpl; import org.jclouds.domain.internal.LocationImpl;
import org.jclouds.filesystem.FilesystemBlobStore; import org.jclouds.filesystem.FilesystemBlobStore;
import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator;
import org.jclouds.filesystem.predicates.validators.internal.FilesystemBlobKeyValidatorImpl;
import org.jclouds.filesystem.predicates.validators.internal.FilesystemContainerNameValidatorImpl;
import org.jclouds.filesystem.strategy.FilesystemStorageStrategy; import org.jclouds.filesystem.strategy.FilesystemStorageStrategy;
import org.jclouds.filesystem.strategy.internal.FilesystemStorageStrategyImpl; import org.jclouds.filesystem.strategy.internal.FilesystemStorageStrategyImpl;
import org.jclouds.filesystem.util.internal.FileSystemBlobUtilsImpl; import org.jclouds.filesystem.util.internal.FileSystemBlobUtilsImpl;
@ -60,6 +64,8 @@ public class FilesystemBlobStoreContextModule extends AbstractModule {
bind(ConsistencyModel.class).toInstance(ConsistencyModel.STRICT); bind(ConsistencyModel.class).toInstance(ConsistencyModel.STRICT);
bind(FilesystemStorageStrategy.class).to(FilesystemStorageStrategyImpl.class); bind(FilesystemStorageStrategy.class).to(FilesystemStorageStrategyImpl.class);
bind(BlobUtils.class).to(FileSystemBlobUtilsImpl.class); bind(BlobUtils.class).to(FileSystemBlobUtilsImpl.class);
bind(FilesystemBlobKeyValidator.class).to(FilesystemBlobKeyValidatorImpl.class);
bind(FilesystemContainerNameValidator.class).to(FilesystemContainerNameValidatorImpl.class);
} }
@Provides @Provides

View File

@ -19,8 +19,6 @@
package org.jclouds.filesystem.predicates.validators; package org.jclouds.filesystem.predicates.validators;
import com.google.inject.Singleton;
import java.io.File;
import org.jclouds.predicates.Validator; import org.jclouds.predicates.Validator;
/** /**
@ -31,24 +29,5 @@ import org.jclouds.predicates.Validator;
* *
* @author Alfredo "Rainbowbreeze" Morresi * @author Alfredo "Rainbowbreeze" Morresi
*/ */
@Singleton public abstract class FilesystemBlobKeyValidator extends Validator<String> {
public class FilesystemBlobKeyValidator extends Validator<String> {
@Override
public void validate(String name) throws IllegalArgumentException {
//blob key cannot be null or empty
if (name == null || name.length() < 1)
throw new IllegalArgumentException("Blob key can't be null or empty");
//blobkey cannot start with / (or \ in Windows) character
if (name.startsWith(File.separator))
throw new IllegalArgumentException(String.format(
"Blob key '%s' cannot start with character %s", name, File.separator));
//blobkey cannot end with / (or \ in Windows) character
if (name.endsWith(File.separator))
throw new IllegalArgumentException(String.format(
"Blob key '%s' cannot end with character %s", name, File.separator));
}
} }

View File

@ -19,8 +19,6 @@
package org.jclouds.filesystem.predicates.validators; package org.jclouds.filesystem.predicates.validators;
import com.google.inject.Singleton;
import java.io.File;
import org.jclouds.predicates.Validator; import org.jclouds.predicates.Validator;
/** /**
@ -31,19 +29,5 @@ import org.jclouds.predicates.Validator;
* *
* @author Alfredo "Rainbowbreeze" Morresi * @author Alfredo "Rainbowbreeze" Morresi
*/ */
@Singleton public abstract class FilesystemContainerNameValidator extends Validator<String> {
public class FilesystemContainerNameValidator extends Validator<String> {
@Override
public void validate(String name) throws IllegalArgumentException {
//container name cannot be null or empty
if (name == null || name.length() < 1)
throw new IllegalArgumentException("Container name can't be null or empty");
//container name cannot contains / (or \ in Windows) character
if (name.contains(File.separator))
throw new IllegalArgumentException(String.format(
"Container name '%s' cannot contain character %s", name, File.separator));
}
} }

View File

@ -0,0 +1,55 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.jclouds.filesystem.predicates.validators.internal;
import com.google.inject.Singleton;
import java.io.File;
import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
import org.jclouds.predicates.Validator;
/**
* Validates name for filesystem container blob keys implementation
*
* @see org.jclouds.rest.InputParamValidator
* @see org.jclouds.predicates.Validator
*
* @author Alfredo "Rainbowbreeze" Morresi
*/
@Singleton
public class FilesystemBlobKeyValidatorImpl extends FilesystemBlobKeyValidator {
@Override
public void validate(String name) throws IllegalArgumentException {
//blob key cannot be null or empty
if (name == null || name.length() < 1)
throw new IllegalArgumentException("Blob key can't be null or empty");
//blobkey cannot start with / (or \ in Windows) character
if (name.startsWith(File.separator))
throw new IllegalArgumentException(String.format(
"Blob key '%s' cannot start with character %s", name, File.separator));
//blobkey cannot end with / (or \ in Windows) character
if (name.endsWith(File.separator))
throw new IllegalArgumentException(String.format(
"Blob key '%s' cannot end with character %s", name, File.separator));
}
}

View File

@ -0,0 +1,50 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.jclouds.filesystem.predicates.validators.internal;
import com.google.inject.Singleton;
import java.io.File;
import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator;
import org.jclouds.predicates.Validator;
/**
* Validates container name for filesystem provider implementation
*
* @see org.jclouds.rest.InputParamValidator
* @see org.jclouds.predicates.Validator
*
* @author Alfredo "Rainbowbreeze" Morresi
*/
@Singleton
public class FilesystemContainerNameValidatorImpl extends FilesystemContainerNameValidator {
@Override
public void validate(String name) throws IllegalArgumentException {
//container name cannot be null or empty
if (name == null || name.length() < 1)
throw new IllegalArgumentException("Container name can't be null or empty");
//container name cannot contains / (or \ in Windows) character
if (name.contains(File.separator))
throw new IllegalArgumentException(String.format(
"Container name '%s' cannot contain character %s", name, File.separator));
}
}

View File

@ -23,7 +23,9 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.options.ListContainerOptions; import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator;
import org.jclouds.io.Payload; import org.jclouds.io.Payload;
import org.jclouds.rest.annotations.ParamValidators;
/** /**
* Strategy for filesystem operations related to container and blob * Strategy for filesystem operations related to container and blob
@ -117,11 +119,11 @@ public interface FilesystemStorageStrategy {
/** /**
* *
* @param containerName * @param container
* @param key * @param key
* @return * @return
*/ */
boolean blobExists(String containerName, String key); boolean blobExists(String container, String key);
/** /**
* Returns all the blobs key inside a container * Returns all the blobs key inside a container
@ -160,6 +162,6 @@ public interface FilesystemStorageStrategy {
* @param payload * @param payload
* @throws IOException * @throws IOException
*/ */
void writePayloadOnFile(String containerName, String key, Payload payload) throws IOException; void writePayloadOnFile(String container, String blobKey, Payload payload) throws IOException;
} }

View File

@ -19,8 +19,8 @@
package org.jclouds.filesystem.strategy.internal; package org.jclouds.filesystem.strategy.internal;
import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
import org.jclouds.rest.annotations.ParamValidators; import org.jclouds.rest.annotations.ParamValidators;
import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator; import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Set; import java.util.Set;
@ -62,35 +62,46 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
protected final Blob.Factory blobFactory; protected final Blob.Factory blobFactory;
protected final String baseDirectory; protected final String baseDirectory;
protected final FilesystemContainerNameValidator filesystemContainerNameValidator;
protected final FilesystemBlobKeyValidator filesystemBlobKeyValidator;
@Inject @Inject
protected FilesystemStorageStrategyImpl( protected FilesystemStorageStrategyImpl(
Blob.Factory blobFactory, Blob.Factory blobFactory,
@Named(FilesystemConstants.PROPERTY_BASEDIR) String baseDir) { @Named(FilesystemConstants.PROPERTY_BASEDIR) String baseDir,
FilesystemContainerNameValidator filesystemContainerNameValidator,
FilesystemBlobKeyValidator filesystemBlobKeyValidator) {
this.blobFactory = checkNotNull(blobFactory, "filesystem storage strategy blobfactory"); this.blobFactory = checkNotNull(blobFactory, "filesystem storage strategy blobfactory");
this.baseDirectory = checkNotNull(baseDir, "filesystem storage strategy base directory"); this.baseDirectory = checkNotNull(baseDir, "filesystem storage strategy base directory");
this.filesystemContainerNameValidator = checkNotNull(filesystemContainerNameValidator, "filesystem container name validator");
this.filesystemBlobKeyValidator = checkNotNull(filesystemBlobKeyValidator, "filesystem blob key validator");
} }
@Override @Override
public boolean containerExists(String container) { public boolean containerExists(String container) {
filesystemContainerNameValidator.validate(container);
return directoryExists(container, null); return directoryExists(container, null);
} }
@Override @Override
public boolean blobExists(String containerName, String key) { public boolean blobExists(String container, String key) {
return buildPathAndChecksIfFileExists(containerName, key); filesystemContainerNameValidator.validate(container);
filesystemBlobKeyValidator.validate(key);
return buildPathAndChecksIfFileExists(container, key);
} }
@Override @Override
public boolean createContainer(@ParamValidators( { FilesystemContainerNameValidator.class }) String container) { public boolean createContainer(String container) {
logger.debug("Creating container %s", container); logger.debug("Creating container %s", container);
filesystemContainerNameValidator.validate(container);
return createDirectoryWithResult(container, null); return createDirectoryWithResult(container, null);
} }
@Override @Override
public void deleteContainer(String container) { public void deleteContainer(String container) {
filesystemContainerNameValidator.validate(container);
deleteDirectory(container, null); deleteDirectory(container, null);
} }
@ -101,11 +112,13 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
*/ */
@Override @Override
public void clearContainer(final String container) { public void clearContainer(final String container) {
filesystemContainerNameValidator.validate(container);
clearContainer(container, ListContainerOptions.Builder.recursive()); clearContainer(container, ListContainerOptions.Builder.recursive());
} }
@Override @Override
public void clearContainer(String container, ListContainerOptions options) { public void clearContainer(String container, ListContainerOptions options) {
filesystemContainerNameValidator.validate(container);
//TODO //TODO
//now all is deleted, check it based on options //now all is deleted, check it based on options
try { try {
@ -125,21 +138,24 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
@Override @Override
public Blob newBlob(@ParamValidators( { FilesystemBlobKeyValidator.class }) String name) { public Blob newBlob(@ParamValidators( { FilesystemBlobKeyValidator.class }) String name) {
filesystemBlobKeyValidator.validate(name);
Blob blob = blobFactory.create(null); Blob blob = blobFactory.create(null);
blob.getMetadata().setName(name); blob.getMetadata().setName(name);
return blob; return blob;
} }
@Override @Override
public void removeBlob(final String container, final String key) { public void removeBlob(final String container, final String blobKey) {
String fileName = buildPathStartingFromBaseDir(container, key); filesystemContainerNameValidator.validate(container);
filesystemBlobKeyValidator.validate(blobKey);
String fileName = buildPathStartingFromBaseDir(container, blobKey);
logger.debug("Deleting blob %s", fileName); logger.debug("Deleting blob %s", fileName);
File fileToBeDeleted = new File(fileName); File fileToBeDeleted = new File(fileName);
fileToBeDeleted.delete(); fileToBeDeleted.delete();
//now examins if the key of the blob is a complex key (with a directory structure) //now examins if the key of the blob is a complex key (with a directory structure)
//and eventually remove empty directory //and eventually remove empty directory
removeDirectoriesTreeOfBlobKey(container, key); removeDirectoriesTreeOfBlobKey(container, blobKey);
} }
@ -168,6 +184,8 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
*/ */
@Override @Override
public File getFileForBlobKey(String container, String blobKey) { public File getFileForBlobKey(String container, String blobKey) {
filesystemContainerNameValidator.validate(container);
filesystemBlobKeyValidator.validate(blobKey);
String fileName = buildPathStartingFromBaseDir(container, blobKey); String fileName = buildPathStartingFromBaseDir(container, blobKey);
File blobFile = new File(fileName); File blobFile = new File(fileName);
return blobFile; return blobFile;
@ -176,18 +194,20 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
/** /**
* Write a {@link Blob} {@link Payload} into a file * Write a {@link Blob} {@link Payload} into a file
* @param containerName * @param container
* @param blobKey * @param blobKey
* @param payload * @param payload
* @throws IOException * @throws IOException
*/ */
@Override @Override
public void writePayloadOnFile(String containerName, String blobKey, Payload payload) throws IOException { public void writePayloadOnFile(String container, String blobKey, Payload payload) throws IOException {
filesystemContainerNameValidator.validate(container);
filesystemBlobKeyValidator.validate(blobKey);
File outputFile = null; File outputFile = null;
OutputStream output = null; OutputStream output = null;
InputStream input = null; InputStream input = null;
try { try {
outputFile = getFileForBlobKey(containerName, blobKey); outputFile = getFileForBlobKey(container, blobKey);
File parentDirectory = outputFile.getParentFile(); File parentDirectory = outputFile.getParentFile();
if (!parentDirectory.exists()) { if (!parentDirectory.exists()) {
if (!parentDirectory.mkdirs()) { if (!parentDirectory.mkdirs()) {
@ -230,6 +250,7 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
*/ */
@Override @Override
public Iterable<String> getBlobKeysInsideContainer(String container) throws IOException { public Iterable<String> getBlobKeysInsideContainer(String container) throws IOException {
filesystemContainerNameValidator.validate(container);
//check if container exists //check if container exists
//TODO maybe an error is more appropriate //TODO maybe an error is more appropriate
if (!containerExists(container)) { if (!containerExists(container)) {

View File

@ -42,6 +42,7 @@ import org.jclouds.blobstore.domain.MutableBlobMetadata;
import org.jclouds.blobstore.domain.PageSet; import org.jclouds.blobstore.domain.PageSet;
import org.jclouds.blobstore.domain.StorageMetadata; import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.domain.StorageType; import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.blobstore.domain.internal.BlobImpl;
import org.jclouds.blobstore.options.ListContainerOptions; import org.jclouds.blobstore.options.ListContainerOptions;
import org.testng.annotations.*; import org.testng.annotations.*;
@ -752,14 +753,27 @@ public class FilesystemAsyncBlobStoreTest {
} }
// public void testInvalidContainerName() {
// public void testContainerInvalidNames() throws IOException { try {
blobStore.createContainerInLocation(null, "file/system");
fail("Wrong container name not recognized");
} catch (IllegalArgumentException e) {}
try {
blobStore.containerExists("file/system");
fail("Wrong container name not recognized");
} catch (IllegalArgumentException e) {}
}
// public void testInvalidBlobKey() {
// try { // try {
// blobStore.createContainerInLocation(null, "file/system"); // blobStore.newBlob(File.separator + "testwrongblobkey");
// fail("Wrong container name not recognized"); // fail("Wrong blob key not recognized");
// } catch (IllegalArgumentException e) { // } catch (IllegalArgumentException e) {}
// //
// } // try {
// blobStore.newBlob("testwrongblobkey" + File.separator);
// fail("Wrong blob key not recognized");
// } catch (IllegalArgumentException e) {}
// } // }

View File

@ -16,9 +16,10 @@
* limitations under the License. * limitations under the License.
* ==================================================================== * ====================================================================
*/ */
package org.jclouds.filesystem.predicates.validators; package org.jclouds.filesystem.predicates.validators.internal;
import java.io.File; import java.io.File;
import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.testng.Assert.*; import static org.testng.Assert.*;
@ -34,7 +35,7 @@ public class FilesystemBlobKeyValidatorTest {
@Test @Test
public void testNamesValidity() { public void testNamesValidity() {
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidator(); FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidatorImpl();
validator.validate("all.img"); validator.validate("all.img");
validator.validate("all" + File.separator + "is" + File.separator + "" + "ok"); validator.validate("all" + File.separator + "is" + File.separator + "" + "ok");
@ -42,7 +43,7 @@ public class FilesystemBlobKeyValidatorTest {
@Test @Test
public void testInvalidNames() { public void testInvalidNames() {
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidator(); FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidatorImpl();
try { try {
validator.validate(""); validator.validate("");

View File

@ -16,9 +16,11 @@
* limitations under the License. * limitations under the License.
* ==================================================================== * ====================================================================
*/ */
package org.jclouds.filesystem.predicates.validators; package org.jclouds.filesystem.predicates.validators.internal;
import org.jclouds.filesystem.predicates.validators.internal.FilesystemBlobKeyValidatorImpl;
import java.io.File; import java.io.File;
import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.testng.Assert.*; import static org.testng.Assert.*;
@ -34,14 +36,14 @@ public class FilesystemContainerNameValidatorTest {
@Test @Test
public void testNamesValidity() { public void testNamesValidity() {
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidator(); FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidatorImpl();
validator.validate("all.img"); validator.validate("all.img");
} }
@Test @Test
public void testInvalidNames() { public void testInvalidNames() {
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidator(); FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidatorImpl();
try { try {
validator.validate(""); validator.validate("");

View File

@ -33,6 +33,8 @@ import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import org.jclouds.blobstore.options.ListContainerOptions; import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.filesystem.predicates.validators.internal.FilesystemContainerNameValidatorImpl;
import org.jclouds.filesystem.predicates.validators.internal.FilesystemBlobKeyValidatorImpl;
import org.jclouds.filesystem.strategy.FilesystemStorageStrategy; import org.jclouds.filesystem.strategy.FilesystemStorageStrategy;
import org.jclouds.filesystem.utils.TestUtils; import org.jclouds.filesystem.utils.TestUtils;
import org.jclouds.io.payloads.FilePayload; import org.jclouds.io.payloads.FilePayload;
@ -69,7 +71,9 @@ public class FilesystemStorageStrategyImplTest {
return new BlobImpl(metadata != null ? metadata : new MutableBlobMetadataImpl()); return new BlobImpl(metadata != null ? metadata : new MutableBlobMetadataImpl());
} }
}, },
TestUtils.TARGET_BASE_DIR); TestUtils.TARGET_BASE_DIR,
new FilesystemContainerNameValidatorImpl(),
new FilesystemBlobKeyValidatorImpl());
TestUtils.cleanDirectoryContent(TestUtils.TARGET_BASE_DIR); TestUtils.cleanDirectoryContent(TestUtils.TARGET_BASE_DIR);
} }
@ -507,25 +511,19 @@ public class FilesystemStorageStrategyImplTest {
} }
// public void testInvalidBlobKey() {
// try {
// public void testBlobKeyInvalidNames() throws IOException { storageStrategy.newBlob("/test.jpg");
// try { fail("Wrong blob key not recognized");
// storageStrategy.newBlob("/test.jpg"); } catch (IllegalArgumentException e) {}
// fail("Wrong blob key not recognized"); }
// } catch (IllegalArgumentException e) {
// public void testInvalidContainerName() {
// } try {
// } storageStrategy.createContainer("file/system");
// fail("Wrong container name not recognized");
// public void testContainerInvalidNames() throws IOException { } catch (IllegalArgumentException e) {}
// try { }
// storageStrategy.createContainer("file/system");
// fail("Wrong container name not recognized");
// } catch (IllegalArgumentException e) {
//
// }
// }
//---------------------------------------------------------- Private methods //---------------------------------------------------------- Private methods