Permalink
Browse files

initial check in for refactoring of action

  • Loading branch information...
1 parent 4ed9259 commit 476b70582904f765e8c310010723170f4a069b4a Albert Cheng committed Jan 9, 2013
View
21 ...ava/com/microsoft/windowsazure/services/media/implementation/MediaExceptionProcessor.java
@@ -30,6 +30,7 @@
import com.microsoft.windowsazure.services.media.implementation.entities.EntityDeleteOperation;
import com.microsoft.windowsazure.services.media.implementation.entities.EntityGetOperation;
import com.microsoft.windowsazure.services.media.implementation.entities.EntityListOperation;
+import com.microsoft.windowsazure.services.media.implementation.entities.EntityTypeActionOperation;
import com.microsoft.windowsazure.services.media.implementation.entities.EntityUpdateOperation;
import com.microsoft.windowsazure.services.media.models.ListResult;
import com.microsoft.windowsazure.services.media.models.LocatorInfo;
@@ -176,9 +177,25 @@ public void delete(EntityDeleteOperation deleter) throws ServiceException {
* @see com.microsoft.windowsazure.services.media.implementation.entities.EntityContract#action(com.microsoft.windowsazure.services.media.implementation.entities.EntityActionOperation)
*/
@Override
- public Object action(EntityActionOperation action) throws ServiceException {
+ public void action(EntityActionOperation entityActionOperation) throws ServiceException {
try {
- return service.action(action);
+ service.action(entityActionOperation);
+ }
+ catch (UniformInterfaceException e) {
+ throw processCatch(new ServiceException(e));
+ }
+ catch (ClientHandlerException e) {
+ throw processCatch(new ServiceException(e));
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityContract#action(com.microsoft.windowsazure.services.media.implementation.entities.EntityTypeActionOperation)
+ */
+ @Override
+ public <T> T action(EntityTypeActionOperation<T> entityTypeActionOperation) throws ServiceException {
+ try {
+ return service.action(entityTypeActionOperation);
}
catch (UniformInterfaceException e) {
throw processCatch(new ServiceException(e));
View
15 ...microsoft/windowsazure/services/media/implementation/entities/DefaultActionOperation.java
@@ -39,7 +39,7 @@
private MediaType acceptType = MediaType.APPLICATION_ATOM_XML_TYPE;
/** The query parameters. */
- protected final MultivaluedMap<String, String> queryParameters;
+ protected MultivaluedMap<String, String> queryParameters;
/**
* The default action operation.
@@ -62,10 +62,6 @@ public DefaultActionOperation() {
/* (non-Javadoc)
* @see com.microsoft.windowsazure.services.media.implementation.entities.EntityOperation#setProxyData(com.microsoft.windowsazure.services.media.implementation.entities.EntityProxyData)
*/
- @Override
- public void setProxyData(EntityProxyData proxyData) {
- this.proxyData = proxyData;
- }
/**
* Get the current proxy data
@@ -116,6 +112,7 @@ public MediaType getContentType() {
* the content type
* @return the default action operation
*/
+ @Override
public DefaultActionOperation setContentType(MediaType contentType) {
this.contentType = contentType;
return this;
@@ -147,7 +144,7 @@ public DefaultActionOperation setAcceptType(MediaType acceptType) {
@Override
public Object processResponse(ClientResponse clientResponse) {
PipelineHelpers.ThrowIfNotSuccess(clientResponse);
- return null;
+ return clientResponse;
}
/* (non-Javadoc)
@@ -167,7 +164,13 @@ public Object getRequestContents() {
}
@Override
+ public void setProxyData(EntityProxyData proxyData) {
+ this.proxyData = proxyData;
+ }
+
+ @Override
public Object processResponse(Object rawResponse) throws ServiceException {
return rawResponse;
}
+
}
View
99 ...osoft/windowsazure/services/media/implementation/entities/DefaultTypeActionOperation.java
@@ -0,0 +1,99 @@
+package com.microsoft.windowsazure.services.media.implementation.entities;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+
+import com.microsoft.windowsazure.services.core.ServiceException;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.core.util.MultivaluedMapImpl;
+
+public class DefaultTypeActionOperation<T> implements EntityTypeActionOperation<T> {
+
+ private String name;
+
+ /** The content type. */
+ private MediaType contentType = MediaType.APPLICATION_ATOM_XML_TYPE;
+
+ /** The accept type. */
+ private MediaType acceptType = MediaType.APPLICATION_ATOM_XML_TYPE;
+
+ private MultivaluedMapImpl queryParameters;
+
+ private EntityProxyData proxyData;
+
+ public DefaultTypeActionOperation(String name) {
+ this();
+ this.name = name;
+ }
+
+ public DefaultTypeActionOperation() {
+ this.queryParameters = new MultivaluedMapImpl();
+ }
+
+ @Override
+ public T processTypeResponse(ClientResponse clientResponse) {
+ return null;
+ }
+
+ @Override
+ public MultivaluedMap<String, String> getQueryParameters() {
+ return this.queryParameters;
+ }
+
+ @Override
+ public String getVerb() {
+ return "GET";
+ }
+
+ @Override
+ public Object getRequestContents() {
+ return null;
+ }
+
+ @Override
+ public void setProxyData(EntityProxyData proxyData) {
+ this.proxyData = proxyData;
+ }
+
+ @Override
+ public String getUri() {
+ return this.name;
+ }
+
+ @Override
+ public MediaType getContentType() {
+ return this.contentType;
+ }
+
+ @Override
+ public MediaType getAcceptType() {
+ return this.acceptType;
+ }
+
+ @Override
+ public Object processResponse(Object rawResponse) throws ServiceException {
+ return null;
+ }
+
+ @Override
+ public DefaultTypeActionOperation<T> addQueryParameter(String key, String value) {
+ this.queryParameters.add(key, value);
+ return this;
+ }
+
+ @Override
+ public void processResponse(ClientResponse clientResponse) {
+ }
+
+ @Override
+ public EntityTypeActionOperation<T> setContentType(MediaType contentType) {
+ this.contentType = contentType;
+ return this;
+ }
+
+ public EntityTypeActionOperation<T> setAcceptType(MediaType acceptType) {
+ this.acceptType = acceptType;
+ return this;
+ }
+
+}
View
3 .../microsoft/windowsazure/services/media/implementation/entities/EntityActionOperation.java
@@ -15,6 +15,7 @@
package com.microsoft.windowsazure.services.media.implementation.entities;
+import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import com.sun.jersey.api.client.ClientResponse;
@@ -65,4 +66,6 @@
*/
Object getRequestContents();
+ DefaultActionOperation setContentType(MediaType contentType);
+
}
View
45 ...ava/com/microsoft/windowsazure/services/media/implementation/entities/EntityContract.java
@@ -26,62 +26,87 @@
public interface EntityContract {
/**
- * Create a new instance of an entity
+ * Create a new instance of an entity.
*
+ * @param <T>
+ * the generic type
* @param creator
* Object providing the details of the entity to be
* created
- * @return
- * The created entity
+ * @return the t
+ * @throws ServiceException
+ * the service exception
+ * The created entity
*/
public abstract <T> T create(EntityCreateOperation<T> creator) throws ServiceException;
/**
- * Retrieve an existing entity by id
+ * Retrieve an existing entity by id.
*
+ * @param <T>
+ * the generic type
* @param getter
* object providing the details of the entity to be retrieved
* @return The retrieved entity
* @throws ServiceException
+ * the service exception
*/
public abstract <T> T get(EntityGetOperation<T> getter) throws ServiceException;
/**
- * Retrieve a list of entities
+ * Retrieve a list of entities.
*
+ * @param <T>
+ * the generic type
* @param lister
* object providing details of entities to list
* @return The resulting list
* @throws ServiceException
+ * the service exception
*/
public abstract <T> ListResult<T> list(EntityListOperation<T> lister) throws ServiceException;
/**
- * Update an existing entity
+ * Update an existing entity.
*
* @param updater
* Object providing details of the update
* @throws ServiceException
+ * the service exception
*/
public abstract void update(EntityUpdateOperation updater) throws ServiceException;
/**
- * Delete an entity
+ * Delete an entity.
*
* @param deleter
* Object providing details of the delete
* @throws ServiceException
+ * the service exception
*/
public abstract void delete(EntityDeleteOperation deleter) throws ServiceException;
/**
- * Perform an action on an entity
+ * Perform an action on an entity.
*
* @param action
* Object providing details of the action
- * @return
* @throws ServiceException
+ * the service exception
+ */
+ public abstract void action(EntityActionOperation action) throws ServiceException;
+
+ /**
+ * Action.
+ *
+ * @param <T>
+ * the generic type
+ * @param entityActionOperation
+ * the entity action operation
+ * @return the t
+ * @throws ServiceException
+ * the service exception
*/
- public abstract Object action(EntityActionOperation action) throws ServiceException;
+ public abstract <T> T action(EntityTypeActionOperation<T> entityActionOperation) throws ServiceException;
}
View
22 ...va/com/microsoft/windowsazure/services/media/implementation/entities/EntityRestProxy.java
@@ -164,15 +164,29 @@ public void delete(EntityDeleteOperation deleter) throws ServiceException {
/* (non-Javadoc)
* @see com.microsoft.windowsazure.services.media.implementation.entities.EntityContract#action(com.microsoft.windowsazure.services.media.implementation.entities.EntityActionOperation)
*/
+ @SuppressWarnings("unchecked")
@Override
- public Object action(EntityActionOperation entityActionOperation) throws ServiceException {
- entityActionOperation.setProxyData(createProxyData());
+ public <T> T action(EntityTypeActionOperation<T> entityTypeActionOperation) throws ServiceException {
+ entityTypeActionOperation.setProxyData(createProxyData());
+ Builder webResource = getResource(entityTypeActionOperation.getUri())
+ .queryParams(entityTypeActionOperation.getQueryParameters())
+ .accept(entityTypeActionOperation.getAcceptType()).accept(MediaType.APPLICATION_XML_TYPE)
+ .entity(entityTypeActionOperation.getRequestContents(), MediaType.APPLICATION_XML_TYPE);
+ ClientResponse clientResponse = webResource.method(entityTypeActionOperation.getVerb(), ClientResponse.class);
+ return entityTypeActionOperation.processTypeResponse(clientResponse);
+ }
+ @Override
+ public void action(EntityActionOperation entityActionOperation) throws ServiceException {
+ entityActionOperation.processResponse(getActionClientResponse(entityActionOperation));
+ }
+
+ private ClientResponse getActionClientResponse(EntityActionOperation entityActionOperation) {
+ entityActionOperation.setProxyData(createProxyData());
Builder webResource = getResource(entityActionOperation.getUri())
.queryParams(entityActionOperation.getQueryParameters()).accept(entityActionOperation.getAcceptType())
.accept(MediaType.APPLICATION_XML_TYPE)
.entity(entityActionOperation.getRequestContents(), MediaType.APPLICATION_XML_TYPE);
- ClientResponse clientResponse = webResource.method(entityActionOperation.getVerb(), ClientResponse.class);
- return entityActionOperation.processResponse(clientResponse);
+ return webResource.method(entityActionOperation.getVerb(), ClientResponse.class);
}
}
View
55 ...rosoft/windowsazure/services/media/implementation/entities/EntityTypeActionOperation.java
@@ -0,0 +1,55 @@
+package com.microsoft.windowsazure.services.media.implementation.entities;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+public interface EntityTypeActionOperation<T> extends EntityOperation {
+
+ T processTypeResponse(ClientResponse clientResponse);
+
+ /**
+ * Gets the query parameters.
+ *
+ * @return the query parameters
+ */
+ MultivaluedMap<String, String> getQueryParameters();
+
+ /**
+ * Adds the query parameter.
+ *
+ * @param key
+ * the key
+ * @param value
+ * the value
+ * @return the entity action operation
+ */
+ EntityTypeActionOperation<T> addQueryParameter(String key, String value);
+
+ /**
+ * Process response.
+ *
+ * @param clientResponse
+ * the client response
+ * @return the object
+ */
+ void processResponse(ClientResponse clientResponse);
+
+ /**
+ * Gets the verb.
+ *
+ * @return the verb
+ */
+ String getVerb();
+
+ /**
+ * Gets the request contents.
+ *
+ * @return the request contents
+ */
+ Object getRequestContents();
+
+ EntityTypeActionOperation<T> setContentType(MediaType contentType);
+
+}
View
19 ...ure-api/src/main/java/com/microsoft/windowsazure/services/media/models/ProtectionKey.java
@@ -27,8 +27,8 @@
import com.microsoft.windowsazure.services.core.utils.pipeline.PipelineHelpers;
import com.microsoft.windowsazure.services.media.implementation.content.ProtectionKeyIdType;
import com.microsoft.windowsazure.services.media.implementation.content.ProtectionKeyRestType;
-import com.microsoft.windowsazure.services.media.implementation.entities.DefaultActionOperation;
-import com.microsoft.windowsazure.services.media.implementation.entities.EntityActionOperation;
+import com.microsoft.windowsazure.services.media.implementation.entities.DefaultTypeActionOperation;
+import com.microsoft.windowsazure.services.media.implementation.entities.EntityTypeActionOperation;
import com.sun.jersey.api.client.ClientResponse;
/**
@@ -44,7 +44,7 @@
* the content key type
* @return the protection key id
*/
- public static EntityActionOperation getProtectionKeyId(ContentKeyType contentKeyType) {
+ public static EntityTypeActionOperation<String> getProtectionKeyId(ContentKeyType contentKeyType) {
return new GetProtectionKeyIdActionOperation("GetProtectionKeyId").addQueryParameter("contentKeyType",
String.format("%d", contentKeyType.getCode())).setAcceptType(MediaType.APPLICATION_XML_TYPE);
}
@@ -56,15 +56,17 @@ public static EntityActionOperation getProtectionKeyId(ContentKeyType contentKey
* the protection key id
* @return the protection key
*/
- public static EntityActionOperation getProtectionKey(String protectionKeyId) {
+ public static EntityTypeActionOperation<String> getProtectionKey(String protectionKeyId) {
return new GetProtectionKeyActionOperation("GetProtectionKey").addQueryParameter("ProtectionKeyId",
String.format("'%s'", protectionKeyId)).setAcceptType(MediaType.APPLICATION_XML_TYPE);
}
/**
* The Class GetProtectionKeyIdActionOperation.
+ *
+ * @param <T>
*/
- private static class GetProtectionKeyIdActionOperation extends DefaultActionOperation {
+ private static class GetProtectionKeyIdActionOperation extends DefaultTypeActionOperation<String> {
/** The jaxb context. */
private final JAXBContext jaxbContext;
@@ -99,7 +101,7 @@ public GetProtectionKeyIdActionOperation(String name) {
* @see com.microsoft.windowsazure.services.media.implementation.entities.DefaultActionOperation#processResponse(com.sun.jersey.api.client.ClientResponse)
*/
@Override
- public Object processResponse(ClientResponse clientResponse) {
+ public String processTypeResponse(ClientResponse clientResponse) {
PipelineHelpers.ThrowIfNotSuccess(clientResponse);
ProtectionKeyIdType protectionKeyIdType;
try {
@@ -129,12 +131,13 @@ private ProtectionKeyIdType parseResponse(ClientResponse clientResponse) throws
return protectionKeyIdTypeJaxbElement.getValue();
}
+
}
/**
* The Class GetProtectionKeyActionOperation.
*/
- private static class GetProtectionKeyActionOperation extends DefaultActionOperation {
+ private static class GetProtectionKeyActionOperation extends DefaultTypeActionOperation<String> {
/** The jaxb context. */
private final JAXBContext jaxbContext;
@@ -169,7 +172,7 @@ public GetProtectionKeyActionOperation(String name) {
* @see com.microsoft.windowsazure.services.media.implementation.entities.DefaultActionOperation#processResponse(com.sun.jersey.api.client.ClientResponse)
*/
@Override
- public Object processResponse(ClientResponse clientResponse) {
+ public String processTypeResponse(ClientResponse clientResponse) {
PipelineHelpers.ThrowIfNotSuccess(clientResponse);
ProtectionKeyRestType protectionKeyRestType;
try {
View
5 ...pi/src/test/java/com/microsoft/windowsazure/services/media/EncryptionIntegrationTest.java
@@ -64,8 +64,7 @@ private String createContentKeyId(UUID uuid) {
}
private String getProtectionKeyId() throws ServiceException {
- String protectionKeyId = (String) service.action(ProtectionKey
- .getProtectionKeyId(ContentKeyType.StorageEncryption));
+ String protectionKeyId = service.action(ProtectionKey.getProtectionKeyId(ContentKeyType.StorageEncryption));
return protectionKeyId;
}
@@ -106,7 +105,7 @@ private MediaProcessorInfo GetMediaProcessor(String mediaProcessorName) throws S
}
private String getProtectionKey(String protectionKeyId) throws ServiceException {
- String protectionKey = (String) service.action(ProtectionKey.getProtectionKey(protectionKeyId));
+ String protectionKey = service.action(ProtectionKey.getProtectionKey(protectionKeyId));
return protectionKey;
}

0 comments on commit 476b705

Please sign in to comment.