Skip to content

Commit

Permalink
Merge pull request Azure#71 from gcheng/processresponse
Browse files Browse the repository at this point in the history
add response processing logic
  • Loading branch information
Albert Cheng committed Jan 7, 2013
2 parents 354ef2a + 02cf9f9 commit 9177463
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import com.microsoft.windowsazure.services.core.ServiceException;
import com.microsoft.windowsazure.services.core.utils.pipeline.PipelineHelpers;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.core.util.MultivaluedMapImpl;
Expand Down Expand Up @@ -164,4 +165,9 @@ public String getVerb() {
public Object getRequestContents() {
return null;
}

@Override
public Object processResponse(Object rawResponse) throws ServiceException {
return rawResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import javax.ws.rs.core.MultivaluedMap;

import com.microsoft.windowsazure.services.core.ServiceException;
import com.microsoft.windowsazure.services.media.models.ListResult;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.core.util.MultivaluedMapImpl;
Expand Down Expand Up @@ -94,4 +95,12 @@ public MultivaluedMap<String, String> getQueryParameters() {
public GenericType<ListResult<T>> getResponseGenericType() {
return responseType;
}

/* (non-Javadoc)
* @see com.microsoft.windowsazure.services.media.implementation.entities.EntityOperationBase#processResponse(java.lang.Object)
*/
@Override
public Object processResponse(Object rawResponse) throws ServiceException {
return rawResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,4 @@ public interface EntityCreateOperation<T> extends EntityOperationSingleResult<T>
*/
Object getRequestContents() throws ServiceException;

/**
* Post response process.
*
* @param rawResponse
* the raw response
* @return the object
* @throws ServiceException
* the service exception
*/
Object processResponse(Object rawResponse) throws ServiceException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@ public interface EntityOperation {
* Get the MIME type that we're expecting the server to send back.
*/
public abstract MediaType getAcceptType();

/**
* Process response process.
*
* @param rawResponse
* the raw response
* @return the object
* @throws ServiceException
* the service exception
*/
public abstract Object processResponse(Object rawResponse) throws ServiceException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,26 @@

import javax.ws.rs.core.MediaType;

import com.microsoft.windowsazure.services.core.ServiceException;

/**
* Default implementation of EntityOperation<T> to provide
* default values for common methods.
*
*/
public abstract class EntityOperationBase implements EntityOperation {

/** The uri builder. */
private final EntityUriBuilder uriBuilder;

/** The proxy data. */
private EntityProxyData proxyData;

/**
* Instantiates a new entity operation base.
*
* @param uri
* the uri
*/
protected EntityOperationBase(final String uri) {
this.uriBuilder = new EntityUriBuilder() {
Expand All @@ -42,6 +51,12 @@ public String getUri() {
};
}

/**
* Instantiates a new entity operation base.
*
* @param uriBuilder
* the uri builder
*/
protected EntityOperationBase(EntityUriBuilder uriBuilder) {
this.uriBuilder = uriBuilder;
}
Expand All @@ -55,7 +70,7 @@ public void setProxyData(EntityProxyData proxyData) {
}

/**
* Get the currently set proxy data
* Get the currently set proxy data.
*
* @return the proxy data
*/
Expand Down Expand Up @@ -87,14 +102,46 @@ public MediaType getAcceptType() {
return MediaType.APPLICATION_ATOM_XML_TYPE;
}

/* (non-Javadoc)
* @see com.microsoft.windowsazure.services.media.implementation.entities.EntityOperation#processResponse(java.lang.Object)
*/
@Override
public Object processResponse(Object rawResponse) throws ServiceException {
return rawResponse;
}

/**
* The Interface EntityUriBuilder.
*/
public interface EntityUriBuilder {

/**
* Gets the uri.
*
* @return the uri
*/
String getUri();
}

/**
* The Class EntityIdUriBuilder.
*/
public static class EntityIdUriBuilder implements EntityUriBuilder {

/** The entity type. */
private final String entityType;

/** The entity id. */
private final String entityId;

/**
* Instantiates a new entity id uri builder.
*
* @param entityName
* the entity name
* @param entityId
* the entity id
*/
public EntityIdUriBuilder(String entityName, String entityId) {
super();
this.entityType = entityName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ private WebResource getResource(String entityName) {
* @throws ServiceException
*/
private Builder getResource(EntityOperation operation) throws ServiceException {
return getResource(operation.getUri()).type(operation.getContentType()).accept(
operation.getAcceptType());
return getResource(operation.getUri()).type(operation.getContentType()).accept(operation.getAcceptType());
}

/* (non-Javadoc)
Expand All @@ -110,30 +109,35 @@ private Builder getResource(EntityOperation operation) throws ServiceException {
@Override
public <T> T create(EntityCreateOperation<T> creator) throws ServiceException {
creator.setProxyData(createProxyData());
Object rawResponse = getResource(creator).post(creator.getResponseClass(),
creator.getRequestContents());
Object rawResponse = getResource(creator).post(creator.getResponseClass(), creator.getRequestContents());
Object processedResponse = creator.processResponse(rawResponse);
return (T) processedResponse;
}

/* (non-Javadoc)
* @see com.microsoft.windowsazure.services.media.implementation.entities.EntityContract#get(com.microsoft.windowsazure.services.media.implementation.entities.EntityGetOperation)
*/
@SuppressWarnings("unchecked")
@Override
public <T> T get(EntityGetOperation<T> getter) throws ServiceException {
getter.setProxyData(createProxyData());
return getResource(getter).get(getter.getResponseClass());
Object rawResponse = getResource(getter).get(getter.getResponseClass());
Object processedResponse = getter.processResponse(rawResponse);
return (T) processedResponse;
}

/* (non-Javadoc)
* @see com.microsoft.windowsazure.services.media.implementation.entities.EntityContract#list(com.microsoft.windowsazure.services.media.implementation.entities.EntityListOperation)
*/
@SuppressWarnings("unchecked")
@Override
public <T> ListResult<T> list(EntityListOperation<T> lister) throws ServiceException {
lister.setProxyData(createProxyData());
return getResource(lister.getUri()).queryParams(lister.getQueryParameters())
.type(lister.getContentType()).accept(lister.getAcceptType())
.get(lister.getResponseGenericType());
Object rawResponse = getResource(lister.getUri()).queryParams(lister.getQueryParameters())
.type(lister.getContentType()).accept(lister.getAcceptType()).get(lister.getResponseGenericType());
Object processedResponse = lister.processResponse(rawResponse);
return (ListResult<T>) processedResponse;

}

/* (non-Javadoc)
Expand All @@ -142,10 +146,10 @@ public <T> ListResult<T> list(EntityListOperation<T> lister) throws ServiceExcep
@Override
public void update(EntityUpdateOperation updater) throws ServiceException {
updater.setProxyData(createProxyData());
ClientResponse response = getResource(updater).header("X-HTTP-METHOD", "MERGE").post(
ClientResponse.class, updater.getRequestContents());

PipelineHelpers.ThrowIfNotSuccess(response);
Object rawResponse = getResource(updater).header("X-HTTP-METHOD", "MERGE").post(ClientResponse.class,
updater.getRequestContents());
PipelineHelpers.ThrowIfNotSuccess((ClientResponse) rawResponse);
updater.processResponse(rawResponse);
}

/* (non-Javadoc)
Expand All @@ -165,8 +169,8 @@ public Object action(EntityActionOperation entityActionOperation) throws Service
entityActionOperation.setProxyData(createProxyData());

Builder webResource = getResource(entityActionOperation.getUri())
.queryParams(entityActionOperation.getQueryParameters())
.accept(entityActionOperation.getAcceptType()).accept(MediaType.APPLICATION_XML_TYPE)
.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);
Expand Down

0 comments on commit 9177463

Please sign in to comment.