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.internal.LocationImpl;
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.internal.FilesystemStorageStrategyImpl;
import org.jclouds.filesystem.util.internal.FileSystemBlobUtilsImpl;
@ -60,6 +64,8 @@ public class FilesystemBlobStoreContextModule extends AbstractModule {
bind(ConsistencyModel.class).toInstance(ConsistencyModel.STRICT);
bind(FilesystemStorageStrategy.class).to(FilesystemStorageStrategyImpl.class);
bind(BlobUtils.class).to(FileSystemBlobUtilsImpl.class);
bind(FilesystemBlobKeyValidator.class).to(FilesystemBlobKeyValidatorImpl.class);
bind(FilesystemContainerNameValidator.class).to(FilesystemContainerNameValidatorImpl.class);
}
@Provides

View File

@ -19,8 +19,6 @@
package org.jclouds.filesystem.predicates.validators;
import com.google.inject.Singleton;
import java.io.File;
import org.jclouds.predicates.Validator;
/**
@ -31,24 +29,5 @@ import org.jclouds.predicates.Validator;
*
* @author Alfredo "Rainbowbreeze" Morresi
*/
@Singleton
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));
}
public abstract class FilesystemBlobKeyValidator extends Validator<String> {
}

View File

@ -19,8 +19,6 @@
package org.jclouds.filesystem.predicates.validators;
import com.google.inject.Singleton;
import java.io.File;
import org.jclouds.predicates.Validator;
/**
@ -31,19 +29,5 @@ import org.jclouds.predicates.Validator;
*
* @author Alfredo "Rainbowbreeze" Morresi
*/
@Singleton
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));
}
public abstract class FilesystemContainerNameValidator extends Validator<String> {
}

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 org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator;
import org.jclouds.io.Payload;
import org.jclouds.rest.annotations.ParamValidators;
/**
* Strategy for filesystem operations related to container and blob
@ -117,11 +119,11 @@ public interface FilesystemStorageStrategy {
/**
*
* @param containerName
* @param container
* @param key
* @return
*/
boolean blobExists(String containerName, String key);
boolean blobExists(String container, String key);
/**
* Returns all the blobs key inside a container
@ -160,6 +162,6 @@ public interface FilesystemStorageStrategy {
* @param payload
* @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;
import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
import org.jclouds.rest.annotations.ParamValidators;
import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator;
import java.io.OutputStream;
import java.util.Set;
@ -62,35 +62,46 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
protected final Blob.Factory blobFactory;
protected final String baseDirectory;
protected final FilesystemContainerNameValidator filesystemContainerNameValidator;
protected final FilesystemBlobKeyValidator filesystemBlobKeyValidator;
@Inject
protected FilesystemStorageStrategyImpl(
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.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
public boolean containerExists(String container) {
filesystemContainerNameValidator.validate(container);
return directoryExists(container, null);
}
@Override
public boolean blobExists(String containerName, String key) {
return buildPathAndChecksIfFileExists(containerName, key);
public boolean blobExists(String container, String key) {
filesystemContainerNameValidator.validate(container);
filesystemBlobKeyValidator.validate(key);
return buildPathAndChecksIfFileExists(container, key);
}
@Override
public boolean createContainer(@ParamValidators( { FilesystemContainerNameValidator.class }) String container) {
public boolean createContainer(String container) {
logger.debug("Creating container %s", container);
filesystemContainerNameValidator.validate(container);
return createDirectoryWithResult(container, null);
}
@Override
public void deleteContainer(String container) {
filesystemContainerNameValidator.validate(container);
deleteDirectory(container, null);
}
@ -101,11 +112,13 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
*/
@Override
public void clearContainer(final String container) {
filesystemContainerNameValidator.validate(container);
clearContainer(container, ListContainerOptions.Builder.recursive());
}
@Override
public void clearContainer(String container, ListContainerOptions options) {
filesystemContainerNameValidator.validate(container);
//TODO
//now all is deleted, check it based on options
try {
@ -125,21 +138,24 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
@Override
public Blob newBlob(@ParamValidators( { FilesystemBlobKeyValidator.class }) String name) {
filesystemBlobKeyValidator.validate(name);
Blob blob = blobFactory.create(null);
blob.getMetadata().setName(name);
return blob;
}
@Override
public void removeBlob(final String container, final String key) {
String fileName = buildPathStartingFromBaseDir(container, key);
logger.debug("Deleting blob %s", fileName);
File fileToBeDeleted = new File(fileName);
fileToBeDeleted.delete();
public void removeBlob(final String container, final String blobKey) {
filesystemContainerNameValidator.validate(container);
filesystemBlobKeyValidator.validate(blobKey);
String fileName = buildPathStartingFromBaseDir(container, blobKey);
logger.debug("Deleting blob %s", fileName);
File fileToBeDeleted = new File(fileName);
fileToBeDeleted.delete();
//now examins if the key of the blob is a complex key (with a directory structure)
//and eventually remove empty directory
removeDirectoriesTreeOfBlobKey(container, key);
removeDirectoriesTreeOfBlobKey(container, blobKey);
}
@ -168,6 +184,8 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
*/
@Override
public File getFileForBlobKey(String container, String blobKey) {
filesystemContainerNameValidator.validate(container);
filesystemBlobKeyValidator.validate(blobKey);
String fileName = buildPathStartingFromBaseDir(container, blobKey);
File blobFile = new File(fileName);
return blobFile;
@ -176,18 +194,20 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
/**
* Write a {@link Blob} {@link Payload} into a file
* @param containerName
* @param container
* @param blobKey
* @param payload
* @throws IOException
*/
@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;
OutputStream output = null;
InputStream input = null;
try {
outputFile = getFileForBlobKey(containerName, blobKey);
outputFile = getFileForBlobKey(container, blobKey);
File parentDirectory = outputFile.getParentFile();
if (!parentDirectory.exists()) {
if (!parentDirectory.mkdirs()) {
@ -230,6 +250,7 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
*/
@Override
public Iterable<String> getBlobKeysInsideContainer(String container) throws IOException {
filesystemContainerNameValidator.validate(container);
//check if container exists
//TODO maybe an error is more appropriate
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.StorageMetadata;
import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.blobstore.domain.internal.BlobImpl;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.testng.annotations.*;
@ -752,14 +753,27 @@ public class FilesystemAsyncBlobStoreTest {
}
//
// public void testContainerInvalidNames() throws IOException {
public void testInvalidContainerName() {
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 {
// blobStore.createContainerInLocation(null, "file/system");
// fail("Wrong container name not recognized");
// } catch (IllegalArgumentException e) {
// blobStore.newBlob(File.separator + "testwrongblobkey");
// fail("Wrong blob key not recognized");
// } 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.
* ====================================================================
*/
package org.jclouds.filesystem.predicates.validators;
package org.jclouds.filesystem.predicates.validators.internal;
import java.io.File;
import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
@ -34,7 +35,7 @@ public class FilesystemBlobKeyValidatorTest {
@Test
public void testNamesValidity() {
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidator();
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidatorImpl();
validator.validate("all.img");
validator.validate("all" + File.separator + "is" + File.separator + "" + "ok");
@ -42,7 +43,7 @@ public class FilesystemBlobKeyValidatorTest {
@Test
public void testInvalidNames() {
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidator();
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidatorImpl();
try {
validator.validate("");

View File

@ -16,9 +16,11 @@
* 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 org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
@ -34,14 +36,14 @@ public class FilesystemContainerNameValidatorTest {
@Test
public void testNamesValidity() {
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidator();
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidatorImpl();
validator.validate("all.img");
}
@Test
public void testInvalidNames() {
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidator();
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidatorImpl();
try {
validator.validate("");

View File

@ -33,6 +33,8 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
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.utils.TestUtils;
import org.jclouds.io.payloads.FilePayload;
@ -69,7 +71,9 @@ public class FilesystemStorageStrategyImplTest {
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);
}
@ -507,25 +511,19 @@ public class FilesystemStorageStrategyImplTest {
}
//
//
// public void testBlobKeyInvalidNames() throws IOException {
// try {
// storageStrategy.newBlob("/test.jpg");
// fail("Wrong blob key not recognized");
// } catch (IllegalArgumentException e) {
//
// }
// }
//
// public void testContainerInvalidNames() throws IOException {
// try {
// storageStrategy.createContainer("file/system");
// fail("Wrong container name not recognized");
// } catch (IllegalArgumentException e) {
//
// }
// }
public void testInvalidBlobKey() {
try {
storageStrategy.newBlob("/test.jpg");
fail("Wrong blob key not recognized");
} catch (IllegalArgumentException e) {}
}
public void testInvalidContainerName() {
try {
storageStrategy.createContainer("file/system");
fail("Wrong container name not recognized");
} catch (IllegalArgumentException e) {}
}
//---------------------------------------------------------- Private methods