Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #85 from gcheng/betteraction

DCR of action to expose two types of action API
  • Loading branch information...
commit 9f0b32fc951596ff542756723c9f0ae216b4a2b9 2 parents 1a1717e + fd3b016
Albert Cheng authored
21 ...-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/MediaExceptionProcessor.java
View
@@ -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));
21 .../main/java/com/microsoft/windowsazure/services/media/implementation/entities/DefaultActionOperation.java
View
@@ -27,6 +27,8 @@
* Generic implementation of Delete operation usable by most entities.
*/
public class DefaultActionOperation implements EntityActionOperation {
+
+ /** The proxy data. */
private EntityProxyData proxyData;
/** The name. */
@@ -39,7 +41,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.
@@ -68,7 +70,7 @@ public void setProxyData(EntityProxyData proxyData) {
}
/**
- * Get the current proxy data
+ * Get the current proxy data.
*
* @return the proxy data
*/
@@ -116,6 +118,7 @@ public MediaType getContentType() {
* the content type
* @return the default action operation
*/
+ @Override
public DefaultActionOperation setContentType(MediaType contentType) {
this.contentType = contentType;
return this;
@@ -142,15 +145,6 @@ public DefaultActionOperation setAcceptType(MediaType acceptType) {
}
/* (non-Javadoc)
- * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityActionOperation#processResponse(com.sun.jersey.api.client.ClientResponse)
- */
- @Override
- public Object processResponse(ClientResponse clientResponse) {
- PipelineHelpers.ThrowIfNotSuccess(clientResponse);
- return null;
- }
-
- /* (non-Javadoc)
* @see com.microsoft.windowsazure.services.media.implementation.entities.EntityActionOperation#getVerb()
*/
@Override
@@ -166,8 +160,13 @@ public Object getRequestContents() {
return null;
}
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityOperation#processResponse(java.lang.Object)
+ */
@Override
public Object processResponse(Object rawResponse) throws ServiceException {
+ PipelineHelpers.ThrowIfNotSuccess((ClientResponse) rawResponse);
return rawResponse;
}
+
}
165 .../com/microsoft/windowsazure/services/media/implementation/entities/DefaultEntityTypeActionOperation.java
View
@@ -0,0 +1,165 @@
+/**
+ * Copyright Microsoft Corporation
+ *
+ * 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 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;
+
+/**
+ * The Class DefaultTypeActionOperation.
+ *
+ * @param <T>
+ * the generic type
+ */
+public class DefaultEntityTypeActionOperation<T> implements EntityTypeActionOperation<T> {
+
+ /** The name. */
+ 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;
+
+ /** The query parameters. */
+ private final MultivaluedMap<String, String> queryParameters;
+
+ /** The proxy data. */
+ protected EntityProxyData proxyData;
+
+ /**
+ * Instantiates a new default type action operation.
+ *
+ * @param name
+ * the name
+ */
+ public DefaultEntityTypeActionOperation(String name) {
+ this();
+ this.name = name;
+ }
+
+ /**
+ * Instantiates a new default type action operation.
+ */
+ public DefaultEntityTypeActionOperation() {
+ this.queryParameters = new MultivaluedMapImpl();
+ }
+
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityTypeActionOperation#processTypeResponse(com.sun.jersey.api.client.ClientResponse)
+ */
+ @Override
+ public T processTypeResponse(ClientResponse clientResponse) {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityTypeActionOperation#getQueryParameters()
+ */
+ @Override
+ public MultivaluedMap<String, String> getQueryParameters() {
+ return this.queryParameters;
+ }
+
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityTypeActionOperation#getVerb()
+ */
+ @Override
+ public String getVerb() {
+ return "GET";
+ }
+
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityTypeActionOperation#getRequestContents()
+ */
+ @Override
+ public Object getRequestContents() {
+ return null;
+ }
+
+ /* (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;
+ }
+
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityOperation#getUri()
+ */
+ @Override
+ public String getUri() {
+ return this.name;
+ }
+
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityOperation#getContentType()
+ */
+ @Override
+ public MediaType getContentType() {
+ return this.contentType;
+ }
+
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityOperation#getAcceptType()
+ */
+ @Override
+ public MediaType getAcceptType() {
+ return this.acceptType;
+ }
+
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityOperation#processResponse(java.lang.Object)
+ */
+ @Override
+ public Object processResponse(Object rawResponse) throws ServiceException {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityTypeActionOperation#addQueryParameter(java.lang.String, java.lang.String)
+ */
+ @Override
+ public DefaultEntityTypeActionOperation<T> addQueryParameter(String key, String value) {
+ this.queryParameters.add(key, value);
+ return this;
+ }
+
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityTypeActionOperation#setContentType(javax.ws.rs.core.MediaType)
+ */
+ @Override
+ public EntityTypeActionOperation<T> setContentType(MediaType contentType) {
+ this.contentType = contentType;
+ return this;
+ }
+
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityTypeActionOperation#setAcceptType(javax.ws.rs.core.MediaType)
+ */
+ @Override
+ public EntityTypeActionOperation<T> setAcceptType(MediaType acceptType) {
+ this.acceptType = acceptType;
+ return this;
+ }
+
+}
21 ...c/main/java/com/microsoft/windowsazure/services/media/implementation/entities/EntityActionOperation.java
View
@@ -15,10 +15,9 @@
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;
-
/**
* Action operation for Entities.
*/
@@ -43,15 +42,6 @@
EntityActionOperation addQueryParameter(String key, String value);
/**
- * Process response.
- *
- * @param clientResponse
- * the client response
- * @return the object
- */
- Object processResponse(ClientResponse clientResponse);
-
- /**
* Gets the verb.
*
* @return the verb
@@ -65,4 +55,13 @@
*/
Object getRequestContents();
+ /**
+ * Sets the content type.
+ *
+ * @param contentType
+ * the content type
+ * @return the default action operation
+ */
+ EntityActionOperation setContentType(MediaType contentType);
+
}
45 ...-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/entities/EntityContract.java
View
@@ -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;
}
11 ...src/main/java/com/microsoft/windowsazure/services/media/implementation/entities/EntityLinkOperation.java
View
@@ -31,10 +31,8 @@
import org.w3c.dom.Document;
-import com.microsoft.windowsazure.services.core.utils.pipeline.PipelineHelpers;
import com.microsoft.windowsazure.services.media.implementation.content.Constants;
import com.microsoft.windowsazure.services.media.implementation.content.MediaUriType;
-import com.sun.jersey.api.client.ClientResponse;
/**
* Generic implementation of $link operation of two entities.
@@ -130,15 +128,6 @@ public String getVerb() {
}
/* (non-Javadoc)
- * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityActionOperation#processResponse(com.sun.jersey.api.client.ClientResponse)
- */
- @Override
- public Object processResponse(ClientResponse clientResponse) {
- PipelineHelpers.ThrowIfNotSuccess(clientResponse);
- return null;
- }
-
- /* (non-Javadoc)
* @see com.microsoft.windowsazure.services.media.implementation.entities.DefaultActionOperation#getRequestContents()
*/
@Override
2  ...api/src/main/java/com/microsoft/windowsazure/services/media/implementation/entities/EntityOperation.java
View
@@ -27,7 +27,7 @@
* @param proxyData
* @return
*/
- void setProxyData(EntityProxyData proxyData);
+ public void setProxyData(EntityProxyData proxyData);
/**
* Get the URI the creation request should be sent to.
32 ...api/src/main/java/com/microsoft/windowsazure/services/media/implementation/entities/EntityRestProxy.java
View
@@ -97,6 +97,7 @@ private WebResource getResource(String entityName) {
* the operation
* @return the resource
* @throws ServiceException
+ * the service exception
*/
private Builder getResource(EntityOperation operation) throws ServiceException {
return getResource(operation.getUri()).type(operation.getContentType()).accept(operation.getAcceptType());
@@ -165,14 +166,37 @@ 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 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);
+ }
+
+ /* (non-Javadoc)
+ * @see com.microsoft.windowsazure.services.media.implementation.entities.EntityContract#action(com.microsoft.windowsazure.services.media.implementation.entities.EntityActionOperation)
+ */
+ @Override
+ public void action(EntityActionOperation entityActionOperation) throws ServiceException {
+ entityActionOperation.processResponse(getActionClientResponse(entityActionOperation));
+ }
+ /**
+ * Gets the action client response.
+ *
+ * @param entityActionOperation
+ * the entity action operation
+ * @return the action client response
+ */
+ 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);
}
}
90 ...in/java/com/microsoft/windowsazure/services/media/implementation/entities/EntityTypeActionOperation.java
View
@@ -0,0 +1,90 @@
+/**
+ * Copyright Microsoft Corporation
+ *
+ * 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 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;
+
+/**
+ * The Interface EntityTypeActionOperation.
+ *
+ * @param <T>
+ * the generic type
+ */
+public interface EntityTypeActionOperation<T> extends EntityOperation {
+
+ /**
+ * Process type response.
+ *
+ * @param clientResponse
+ * the client response
+ * @return the t
+ */
+ 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);
+
+ /**
+ * Gets the verb.
+ *
+ * @return the verb
+ */
+ String getVerb();
+
+ /**
+ * Gets the request contents.
+ *
+ * @return the request contents
+ */
+ Object getRequestContents();
+
+ /**
+ * Sets the content type.
+ *
+ * @param contentType
+ * the content type
+ * @return the entity type action operation
+ */
+ EntityTypeActionOperation<T> setContentType(MediaType contentType);
+
+ /**
+ * Sets the accept type.
+ *
+ * @param acceptType
+ * the accept type
+ * @return the entity type action operation
+ */
+ EntityTypeActionOperation<T> setAcceptType(MediaType acceptType);
+
+}
19 microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/ProtectionKey.java
View
@@ -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.DefaultEntityTypeActionOperation;
+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 DefaultEntityTypeActionOperation<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 DefaultEntityTypeActionOperation<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 {
Please sign in to comment.
Something went wrong with that request. Please try again.