Skip to content

Commit

Permalink
Issue #5 - CommandFactory applied to Container
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-bor committed Mar 24, 2013
1 parent c5e7204 commit 07f75ed
Show file tree
Hide file tree
Showing 26 changed files with 195 additions and 64 deletions.
19 changes: 11 additions & 8 deletions src/main/java/org/javaswift/joss/client/impl/ContainerImpl.java
@@ -1,45 +1,48 @@
package org.javaswift.joss.client.impl;

import org.javaswift.joss.command.impl.factory.ContainerCommandFactoryImpl;
import org.javaswift.joss.command.shared.factory.ContainerCommandFactory;
import org.javaswift.joss.instructions.ListInstructions;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.model.Container;
import org.javaswift.joss.model.StoredObject;
import org.javaswift.joss.client.core.AbstractContainer;
import org.javaswift.joss.command.impl.container.*;
import org.javaswift.joss.command.impl.identity.access.AccessImpl;
import org.apache.http.client.HttpClient;

import java.util.Collection;

public class ContainerImpl extends AbstractContainer {

ContainerCommandFactory commandFactory = new ContainerCommandFactoryImpl();

public ContainerImpl(Account account, String name, boolean allowCaching) {
super(account, name, allowCaching);
}

public void makePublic() {
new ContainerRightsCommand(getAccount(), getClient(), getAccess(), this, true).call();
commandFactory.createContainerRightsCommand(getAccount(), getClient(), getAccess(), this, true).call();
}

public void makePrivate() {
new ContainerRightsCommand(getAccount(), getClient(), getAccess(), this, false).call();
commandFactory.createContainerRightsCommand(getAccount(), getClient(), getAccess(), this, false).call();
}

public Collection<StoredObject> list(String prefix, String marker, int pageSize) {
ListInstructions listInstructions = new ListInstructions()
.setPrefix(prefix)
.setMarker(marker)
.setLimit(pageSize);
return new ListObjectsCommand(getAccount(), getClient(), getAccess(), this, listInstructions).call();
return commandFactory.createListObjectsCommand(getAccount(), getClient(), getAccess(), this, listInstructions).call();
}

public Container create() {
new CreateContainerCommand(getAccount(), getClient(), getAccess(), this).call();
commandFactory.createCreateContainerCommand(getAccount(), getClient(), getAccess(), this).call();
return this;
}

public void delete() {
new DeleteContainerCommand(getAccount(), getClient(), getAccess(), this).call();
commandFactory.createDeleteContainerCommand(getAccount(), getClient(), getAccess(), this).call();
}

public StoredObject getObject(String objectName) {
Expand All @@ -56,11 +59,11 @@ protected AccessImpl getAccess() {

@Override
protected void saveMetadata() {
new ContainerMetadataCommand(getAccount(), getClient(), getAccess(), this, info.getMetadata()).call();
commandFactory.createContainerMetadataCommand(getAccount(), getClient(), getAccess(), this, info.getMetadata()).call();
}

protected void getInfo() {
this.info = new ContainerInformationCommand(getAccount(), getClient(), getAccess(), this).call();
this.info = commandFactory.createContainerInformationCommand(getAccount(), getClient(), getAccess(), this).call();
this.setInfoRetrieved();
}

Expand Down
@@ -1,5 +1,6 @@
package org.javaswift.joss.command.impl.account;

import org.javaswift.joss.command.shared.account.ContainerListElement;
import org.javaswift.joss.command.shared.account.ListContainersCommand;
import org.javaswift.joss.instructions.ListInstructions;
import org.javaswift.joss.model.Account;
Expand Down
@@ -1,6 +1,7 @@
package org.javaswift.joss.command.impl.container;

import org.javaswift.joss.command.impl.identity.access.AccessImpl;
import org.javaswift.joss.command.shared.container.ContainerInformationCommand;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.command.impl.core.httpstatus.HttpStatusChecker;
import org.javaswift.joss.command.impl.core.httpstatus.HttpStatusFailCondition;
Expand All @@ -19,9 +20,9 @@

import java.io.IOException;

public class ContainerInformationCommand extends AbstractContainerCommand<HttpHead, ContainerInformation> {
public class ContainerInformationCommandImpl extends AbstractContainerCommand<HttpHead, ContainerInformation> implements ContainerInformationCommand {

public ContainerInformationCommand(Account account, HttpClient httpClient, AccessImpl access, Container container) {
public ContainerInformationCommandImpl(Account account, HttpClient httpClient, AccessImpl access, Container container) {
super(account, httpClient, access, container);
}

Expand Down
@@ -1,6 +1,7 @@
package org.javaswift.joss.command.impl.container;

import org.javaswift.joss.command.impl.identity.access.AccessImpl;
import org.javaswift.joss.command.shared.container.ContainerMetadataCommand;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.command.impl.core.httpstatus.HttpStatusChecker;
import org.javaswift.joss.command.impl.core.httpstatus.HttpStatusFailCondition;
Expand All @@ -14,9 +15,9 @@

import java.util.Collection;

public class ContainerMetadataCommand extends AbstractContainerCommand<HttpPost, Object> {
public class ContainerMetadataCommandImpl extends AbstractContainerCommand<HttpPost, Object> implements ContainerMetadataCommand {

public ContainerMetadataCommand(Account account, HttpClient httpClient, AccessImpl access, Container container, Collection<? extends Header> headers) {
public ContainerMetadataCommandImpl(Account account, HttpClient httpClient, AccessImpl access, Container container, Collection<? extends Header> headers) {
super(account, httpClient, access, container);
addHeaders(headers);
}
Expand Down
@@ -1,6 +1,7 @@
package org.javaswift.joss.command.impl.container;

import org.javaswift.joss.command.impl.identity.access.AccessImpl;
import org.javaswift.joss.command.shared.container.ContainerRightsCommand;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.command.impl.core.httpstatus.HttpStatusChecker;
import org.javaswift.joss.command.impl.core.httpstatus.HttpStatusFailCondition;
Expand All @@ -12,9 +13,9 @@
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;

public class ContainerRightsCommand extends AbstractContainerCommand<HttpPut, String[]> {
public class ContainerRightsCommandImpl extends AbstractContainerCommand<HttpPut, String[]> implements ContainerRightsCommand {

public ContainerRightsCommand(Account account, HttpClient httpClient, AccessImpl access, Container container, boolean publicContainer) {
public ContainerRightsCommandImpl(Account account, HttpClient httpClient, AccessImpl access, Container container, boolean publicContainer) {
super(account, httpClient, access, container);
setHeader(new ContainerRights(publicContainer));
}
Expand Down
@@ -1,6 +1,7 @@
package org.javaswift.joss.command.impl.container;

import org.javaswift.joss.command.impl.identity.access.AccessImpl;
import org.javaswift.joss.command.shared.container.CreateContainerCommand;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.command.impl.core.httpstatus.HttpStatusChecker;
import org.javaswift.joss.command.impl.core.httpstatus.HttpStatusFailCondition;
Expand All @@ -11,9 +12,9 @@
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;

public class CreateContainerCommand extends AbstractContainerCommand<HttpPut, Object> {
public class CreateContainerCommandImpl extends AbstractContainerCommand<HttpPut, Object> implements CreateContainerCommand {

public CreateContainerCommand(Account account, HttpClient httpClient, AccessImpl access, Container container) {
public CreateContainerCommandImpl(Account account, HttpClient httpClient, AccessImpl access, Container container) {
super(account, httpClient, access, container);
}

Expand Down
@@ -1,5 +1,6 @@
package org.javaswift.joss.command.impl.container;

import org.javaswift.joss.command.shared.container.DeleteContainerCommand;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.command.impl.core.httpstatus.HttpStatusChecker;
import org.javaswift.joss.command.impl.core.httpstatus.HttpStatusFailCondition;
Expand All @@ -11,9 +12,9 @@
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;

public class DeleteContainerCommand extends AbstractContainerCommand<HttpDelete, String[]> {
public class DeleteContainerCommandImpl extends AbstractContainerCommand<HttpDelete, String[]> implements DeleteContainerCommand {

public DeleteContainerCommand(Account account, HttpClient httpClient, AccessImpl access, Container container) {
public DeleteContainerCommandImpl(Account account, HttpClient httpClient, AccessImpl access, Container container) {
super(account, httpClient, access, container);
}

Expand Down
@@ -1,5 +1,6 @@
package org.javaswift.joss.command.impl.container;

import org.javaswift.joss.command.shared.container.ListObjectsCommand;
import org.javaswift.joss.instructions.ListInstructions;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.command.impl.core.httpstatus.HttpStatusChecker;
Expand All @@ -19,11 +20,11 @@
import java.util.Collection;
import java.util.List;

public class ListObjectsCommand extends AbstractContainerCommand<HttpGet, Collection<StoredObject>> {
public class ListObjectsCommandImpl extends AbstractContainerCommand<HttpGet, Collection<StoredObject>> implements ListObjectsCommand {

protected Container container;

public ListObjectsCommand(Account account, HttpClient httpClient, AccessImpl access, Container container, ListInstructions listInstructions) {
public ListObjectsCommandImpl(Account account, HttpClient httpClient, AccessImpl access, Container container, ListInstructions listInstructions) {
super(account, httpClient, access, container);
this.container = container;
modifyURI(listInstructions.getQueryParameters());
Expand Down
@@ -0,0 +1,46 @@
package org.javaswift.joss.command.impl.factory;

import org.apache.http.client.HttpClient;
import org.javaswift.joss.command.impl.container.*;
import org.javaswift.joss.command.impl.identity.access.AccessImpl;
import org.javaswift.joss.command.shared.container.*;
import org.javaswift.joss.command.shared.factory.ContainerCommandFactory;
import org.javaswift.joss.headers.Header;
import org.javaswift.joss.instructions.ListInstructions;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.model.Container;

import java.util.Collection;

public class ContainerCommandFactoryImpl implements ContainerCommandFactory {

@Override
public ContainerInformationCommand createContainerInformationCommand(Account account, HttpClient httpClient, AccessImpl access, Container container) {
return new ContainerInformationCommandImpl(account, httpClient, access, container);
}

@Override
public ContainerMetadataCommand createContainerMetadataCommand(Account account, HttpClient httpClient, AccessImpl access, Container container, Collection<? extends Header> headers) {
return new ContainerMetadataCommandImpl(account, httpClient, access, container, headers);
}

@Override
public ContainerRightsCommand createContainerRightsCommand(Account account, HttpClient httpClient, AccessImpl access, Container container, boolean publicContainer) {
return new ContainerRightsCommandImpl(account, httpClient, access, container, publicContainer);
}

@Override
public CreateContainerCommand createCreateContainerCommand(Account account, HttpClient httpClient, AccessImpl access, Container container) {
return new CreateContainerCommandImpl(account, httpClient, access, container);
}

@Override
public DeleteContainerCommand createDeleteContainerCommand(Account account, HttpClient httpClient, AccessImpl access, Container container) {
return new DeleteContainerCommandImpl(account, httpClient, access, container);
}

@Override
public ListObjectsCommand createListObjectsCommand(Account account, HttpClient httpClient, AccessImpl access, Container container, ListInstructions listInstructions) {
return new ListObjectsCommandImpl(account, httpClient, access, container, listInstructions);
}
}
@@ -1,4 +1,4 @@
package org.javaswift.joss.command.impl.account;
package org.javaswift.joss.command.shared.account;

public class ContainerListElement {

Expand Down
@@ -0,0 +1,7 @@
package org.javaswift.joss.command.shared.container;

import org.javaswift.joss.command.shared.core.Command;
import org.javaswift.joss.information.ContainerInformation;

public interface ContainerInformationCommand extends Command<ContainerInformation> {
}
@@ -0,0 +1,6 @@
package org.javaswift.joss.command.shared.container;

import org.javaswift.joss.command.shared.core.Command;

public interface ContainerMetadataCommand extends Command<Object> {
}
@@ -0,0 +1,6 @@
package org.javaswift.joss.command.shared.container;

import org.javaswift.joss.command.shared.core.Command;

public interface ContainerRightsCommand extends Command<String[]> {
}
@@ -0,0 +1,6 @@
package org.javaswift.joss.command.shared.container;

import org.javaswift.joss.command.shared.core.Command;

public interface CreateContainerCommand extends Command<Object> {
}
@@ -0,0 +1,6 @@
package org.javaswift.joss.command.shared.container;

import org.javaswift.joss.command.shared.core.Command;

public interface DeleteContainerCommand extends Command<String[]> {
}
@@ -0,0 +1,9 @@
package org.javaswift.joss.command.shared.container;

import org.javaswift.joss.command.shared.core.Command;
import org.javaswift.joss.model.StoredObject;

import java.util.Collection;

public interface ListObjectsCommand extends Command<Collection<StoredObject>> {
}
@@ -0,0 +1,34 @@
package org.javaswift.joss.command.shared.factory;

import org.apache.http.client.HttpClient;
import org.javaswift.joss.command.impl.identity.access.AccessImpl;
import org.javaswift.joss.command.shared.container.*;
import org.javaswift.joss.headers.Header;
import org.javaswift.joss.instructions.ListInstructions;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.model.Container;

import java.util.Collection;

public interface ContainerCommandFactory {

ContainerInformationCommand createContainerInformationCommand(Account account, HttpClient httpClient,
AccessImpl access, Container container);

ContainerMetadataCommand createContainerMetadataCommand(Account account, HttpClient httpClient,
AccessImpl access, Container container,
Collection<? extends Header> headers);

ContainerRightsCommand createContainerRightsCommand(Account account, HttpClient httpClient, AccessImpl access,
Container container, boolean publicContainer);

CreateContainerCommand createCreateContainerCommand(Account account, HttpClient httpClient,
AccessImpl access, Container container);

DeleteContainerCommand createDeleteContainerCommand(Account account, HttpClient httpClient,
AccessImpl access, Container container);

ListObjectsCommand createListObjectsCommand(Account account, HttpClient httpClient, AccessImpl access,
Container container, ListInstructions listInstructions);

}
@@ -1,6 +1,6 @@
package org.javaswift.joss.command.impl.account;

import org.javaswift.joss.command.impl.container.ContainerInformationCommand;
import org.javaswift.joss.command.impl.container.ContainerInformationCommandImpl;
import org.javaswift.joss.command.impl.core.BaseCommandTest;
import org.javaswift.joss.exception.CommandException;
import org.javaswift.joss.information.AccountInformation;
Expand Down Expand Up @@ -49,7 +49,7 @@ public void getInfoSuccess() throws IOException {

@Test (expected = CommandException.class)
public void unknownError() throws IOException {
checkForError(500, new ContainerInformationCommand(this.account, httpClient, defaultAccess, account.getContainer("containerName")));
checkForError(500, new ContainerInformationCommandImpl(this.account, httpClient, defaultAccess, account.getContainer("containerName")));
}

@Test
Expand Down
@@ -1,5 +1,6 @@
package org.javaswift.joss.command.impl.account;

import org.javaswift.joss.command.shared.account.ContainerListElement;
import org.javaswift.joss.util.ClasspathTemplateResource;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;
Expand Down
Expand Up @@ -21,7 +21,7 @@
import static org.javaswift.joss.headers.container.ContainerObjectCount.X_CONTAINER_OBJECT_COUNT;
import static org.javaswift.joss.headers.container.ContainerRights.X_CONTAINER_READ;

public class ContainerInformationCommandTest extends BaseCommandTest {
public class ContainerInformationCommandImplTest extends BaseCommandTest {

@Before
public void setup() throws IOException {
Expand All @@ -42,7 +42,7 @@ private void prepareMetadata() {
@Test
public void getInfoSuccess() throws IOException {
when(statusLine.getStatusCode()).thenReturn(204);
ContainerInformation info = new ContainerInformationCommand(this.account, httpClient, defaultAccess, account.getContainer("containerName")).call();
ContainerInformation info = new ContainerInformationCommandImpl(this.account, httpClient, defaultAccess, account.getContainer("containerName")).call();
assertEquals("Photo album", info.getMetadata("Description"));
assertEquals("1984", info.getMetadata("Year"));
assertEquals(123, info.getObjectCount());
Expand All @@ -52,16 +52,16 @@ public void getInfoSuccess() throws IOException {

@Test (expected = NotFoundException.class)
public void createContainerFail() throws IOException {
checkForError(404, new ContainerInformationCommand(this.account, httpClient, defaultAccess, account.getContainer("containerName")));
checkForError(404, new ContainerInformationCommandImpl(this.account, httpClient, defaultAccess, account.getContainer("containerName")));
}

@Test (expected = CommandException.class)
public void unknownError() throws IOException {
checkForError(500, new ContainerInformationCommand(this.account, httpClient, defaultAccess, account.getContainer("containerName")));
checkForError(500, new ContainerInformationCommandImpl(this.account, httpClient, defaultAccess, account.getContainer("containerName")));
}

@Test
public void isSecure() throws IOException {
isSecure(new ContainerInformationCommand(this.account, httpClient, defaultAccess, account.getContainer("containerName")), 204);
isSecure(new ContainerInformationCommandImpl(this.account, httpClient, defaultAccess, account.getContainer("containerName")), 204);
}
}

0 comments on commit 07f75ed

Please sign in to comment.