Added logic for filesystem blobstore container names and blobb keys validation

This commit is contained in:
rainbowbreeze 2010-09-01 21:49:24 +02:00
parent ddd1e76832
commit cbe9019f0b
8 changed files with 282 additions and 5 deletions

View File

@ -94,14 +94,18 @@ import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.internal.Nullable;
import javax.annotation.Resource;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.ContainerNotFoundException;
import org.jclouds.blobstore.KeyNotFoundException;
import org.jclouds.blobstore.domain.internal.MutableBlobMetadataImpl;
import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator;
import org.jclouds.filesystem.utils.FilesystemStorageStrategy;
import org.jclouds.io.Payload;
import org.jclouds.io.payloads.FilePayload;
import org.jclouds.logging.Logger;
import org.jclouds.rest.annotations.ParamValidators;
/**
*
@ -111,7 +115,6 @@ import org.jclouds.logging.Logger;
* @author Alfredo "Rainbowbreeze" Morresi
*/
public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
private static final String BACK_SLASH = "\\";
@Resource
protected Logger logger = Logger.NULL;
@ -330,8 +333,11 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
/**
* {@inheritDoc}
*/
@Path("{container}")
@Override
public ListenableFuture<Boolean> createContainerInLocation(final Location location, final String name) {
public ListenableFuture<Boolean> createContainerInLocation(
final Location location,
@PathParam("container") @ParamValidators( { FilesystemContainerNameValidator.class }) String name) {
boolean result = storageStrategy.createContainer(name);
return immediateFuture(result);
}

View File

@ -0,0 +1,54 @@
/**
*
* 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;
import com.google.inject.Singleton;
import java.io.File;
import org.jclouds.predicates.Validator;
/**
* Validates name for filesystem container blob keys
*
* @see org.jclouds.rest.InputParamValidator
* @see 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));
}
}

View File

@ -0,0 +1,49 @@
/**
*
* 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;
import com.google.inject.Singleton;
import java.io.File;
import org.jclouds.predicates.Validator;
/**
* Validates container name for filesystem provider
*
* @see org.jclouds.rest.InputParamValidator
* @see 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));
}
}

View File

@ -18,6 +18,9 @@
*/
package org.jclouds.filesystem.utils;
import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
import org.jclouds.rest.annotations.ParamValidators;
import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator;
import java.io.OutputStream;
import java.util.Set;
import java.util.HashSet;
@ -77,7 +80,7 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
}
@Override
public boolean createContainer(String container) {
public boolean createContainer(@ParamValidators( { FilesystemContainerNameValidator.class }) String container) {
logger.debug("Creating container %s", container);
return createDirectoryWithResult(container, null);
}
@ -118,7 +121,7 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
@Override
public Blob newBlob(String name) {
public Blob newBlob(@ParamValidators( { FilesystemBlobKeyValidator.class }) String name) {
Blob blob = blobFactory.create(null);
blob.getMetadata().setName(name);
return blob;

View File

@ -762,6 +762,16 @@ public class FilesystemAsyncBlobStoreTest {
public void testContainerInvalidNames() throws IOException {
try {
blobStore.createContainerInLocation(null, "file/system");
fail("Wrong container name not recognized");
} catch (IllegalArgumentException e) {
}
}
//---------------------------------------------------------- Private Methods

View File

@ -0,0 +1,66 @@
/**
*
* Copyright (C) 2009 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;
import java.io.File;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/**
* Test class for {@link FilesystemBlobKeyValidator } class
*
* @author Alfredo "Rainbowbreeze" Morresi
*/
@Test(groups = "unit", testName = "filesystem.FilesystemBlobKeyValidatorTest")
public class FilesystemBlobKeyValidatorTest {
@Test
public void testNamesValidity() {
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidator();
validator.validate("all.img");
validator.validate("all" + File.separator + "is" + File.separator + "" + "ok");
}
@Test
public void testInvalidNames() {
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidator();
try {
validator.validate("");
fail("Blob key value incorrect, but was not recognized");
} catch(IllegalArgumentException e) {}
try {
validator.validate(File.separator + "is" + File.separator + "" + "ok");
fail("Blob key value incorrect, but was not recognized");
} catch(IllegalArgumentException e) {}
try {
validator.validate("all" + File.separator + "is" + File.separator);
fail("Blob key value incorrect, but was not recognized");
} catch(IllegalArgumentException e) {}
}
//---------------------------------------------------------- Private methods
}

View File

@ -0,0 +1,70 @@
/**
*
* Copyright (C) 2009 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;
import java.io.File;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/**
* Test class for {@link FilesystemContainerNameValidator } class
*
* @author Alfredo "Rainbowbreeze" Morresi
*/
@Test(groups = "unit", testName = "filesystem.FilesystemContainerNameValidatorTest")
public class FilesystemContainerNameValidatorTest {
@Test
public void testNamesValidity() {
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidator();
validator.validate("all.img");
}
@Test
public void testInvalidNames() {
FilesystemBlobKeyValidator validator = new FilesystemBlobKeyValidator();
try {
validator.validate("");
fail("Container name value incorrect, but was not recognized");
} catch(IllegalArgumentException e) {}
try {
validator.validate(null);
fail("Container name value incorrect, but was not recognized");
} catch(IllegalArgumentException e) {}
try {
validator.validate(File.separator + "is" + File.separator + "" + "ok");
fail("Container name value incorrect, but was not recognized");
} catch(IllegalArgumentException e) {}
try {
validator.validate("all" + File.separator + "is" + File.separator);
fail("Container name value incorrect, but was not recognized");
} catch(IllegalArgumentException e) {}
}
//---------------------------------------------------------- Private methods
}

View File

@ -31,7 +31,6 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.io.payloads.FilePayload;
import org.testng.annotations.*;
@ -505,6 +504,26 @@ 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) {
}
}
//---------------------------------------------------------- Private methods