Skip to content

Commit

Permalink
Merge pull request #3 from christav/odata-collection-serialization
Browse files Browse the repository at this point in the history
Odata collection serialization
  • Loading branch information
Chris Tavares committed Sep 21, 2012
2 parents 73ee449 + f80e5ef commit e8cf07b
Show file tree
Hide file tree
Showing 49 changed files with 4,983 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2011 Microsoft Corporation
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,15 +14,26 @@
*/
package com.microsoft.windowsazure.services.media;

import java.util.Map;

import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;

import com.microsoft.windowsazure.services.core.Builder;
import com.microsoft.windowsazure.services.media.implementation.MediaContentProvider;
import com.microsoft.windowsazure.services.media.implementation.MediaExceptionProcessor;
import com.microsoft.windowsazure.services.media.implementation.MediaRestProxy;
import com.microsoft.windowsazure.services.media.implementation.OAuthContract;
import com.microsoft.windowsazure.services.media.implementation.OAuthFilter;
import com.microsoft.windowsazure.services.media.implementation.OAuthRestProxy;
import com.microsoft.windowsazure.services.media.implementation.OAuthTokenManager;
import com.microsoft.windowsazure.services.media.implementation.ODataEntityCollectionProvider;
import com.microsoft.windowsazure.services.media.implementation.ODataEntityProvider;
import com.microsoft.windowsazure.services.media.implementation.RedirectFilter;
import com.microsoft.windowsazure.services.media.implementation.ResourceLocationManager;
import com.microsoft.windowsazure.services.media.implementation.VersionHeadersFilter;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;

public class Exports implements Builder.Exports {

Expand All @@ -39,6 +50,31 @@ public void register(Builder.Registry registry) {
registry.add(OAuthFilter.class);
registry.add(ResourceLocationManager.class);
registry.add(RedirectFilter.class);
}
registry.add(VersionHeadersFilter.class);

registry.alter(ClientConfig.class, new Builder.Alteration<ClientConfig>() {
@Override
public ClientConfig alter(ClientConfig instance, Builder builder, Map<String, Object> properties) {

instance.getProperties().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

// Turn off auto-follow redirects, because Media Services rest calls break if it's on
instance.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, false);

try {
instance.getSingletons().add(new ODataEntityProvider());
instance.getSingletons().add(new ODataEntityCollectionProvider());
instance.getSingletons().add(new MediaContentProvider());
}
catch (JAXBException e) {
throw new RuntimeException(e);
}
catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}

return instance;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2011 Microsoft Corporation
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2011 Microsoft Corporation
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,11 @@
*/
package com.microsoft.windowsazure.services.media;

import java.util.List;

import com.microsoft.windowsazure.services.core.FilterableService;
import com.microsoft.windowsazure.services.core.ServiceException;
import com.microsoft.windowsazure.services.media.models.AssetInfo;

/**
*
Expand All @@ -23,6 +27,7 @@
*/
public interface MediaContract extends FilterableService<MediaContract> {

// Will fill in as we implement the various Media Services entities
AssetInfo createAsset(String name) throws ServiceException;

List<AssetInfo> getAssets() throws ServiceException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2011 Microsoft Corporation
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2011 Microsoft Corporation
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright 2012 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;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;

import com.microsoft.windowsazure.services.media.implementation.content.MediaServiceDTO;
import com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider;

/**
* Class to plug into Jersey to properly serialize
* raw Media Services DTO types.
*
*/
public class MediaContentProvider<T extends MediaServiceDTO> extends AbstractMessageReaderWriterProvider<T> {
private final ODataAtomMarshaller marshaller;

/**
* Creates the instance
*
* @throws JAXBException
* @throws ParserConfigurationException
*/
public MediaContentProvider() throws JAXBException, ParserConfigurationException {
marshaller = new ODataAtomMarshaller();
}

@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
// This class only does marshalling, not unmarshalling.
return false;
}

@Override
public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException,
WebApplicationException {
throw new UnsupportedOperationException();
}

@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return MediaServiceDTO.class.isAssignableFrom(type);
}

@Override
public void writeTo(T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException,
WebApplicationException {
try {
marshaller.marshalEntry(t, entityStream);
}
catch (JAXBException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2011 Microsoft Corporation
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,8 @@

package com.microsoft.windowsazure.services.media.implementation;

import java.util.List;

import javax.inject.Inject;

import org.apache.commons.logging.Log;
Expand All @@ -24,6 +26,9 @@
import com.microsoft.windowsazure.services.core.ServiceFilter;
import com.microsoft.windowsazure.services.core.utils.ServiceExceptionFactory;
import com.microsoft.windowsazure.services.media.MediaContract;
import com.microsoft.windowsazure.services.media.models.AssetInfo;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.UniformInterfaceException;

/**
* Wrapper implementation of <code>MediaServicesContract</code> that
Expand Down Expand Up @@ -54,4 +59,29 @@ private ServiceException processCatch(ServiceException e) {
return ServiceExceptionFactory.process("MediaServices", e);
}

@Override
public AssetInfo createAsset(String name) throws ServiceException {
try {
return next.createAsset(name);
}
catch (UniformInterfaceException e) {
throw processCatch(new ServiceException(e));
}
catch (ClientHandlerException e) {
throw processCatch(new ServiceException(e));
}
}

@Override
public List<AssetInfo> getAssets() throws ServiceException {
try {
return next.getAssets();
}
catch (UniformInterfaceException e) {
throw processCatch(new ServiceException(e));
}
catch (ClientHandlerException e) {
throw processCatch(new ServiceException(e));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2011 Microsoft Corporation
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,31 +16,40 @@
package com.microsoft.windowsazure.services.media.implementation;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.core.MediaType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.microsoft.windowsazure.services.core.ServiceException;
import com.microsoft.windowsazure.services.core.ServiceFilter;
import com.microsoft.windowsazure.services.core.utils.pipeline.ClientFilterAdapter;
import com.microsoft.windowsazure.services.media.MediaContract;
import com.microsoft.windowsazure.services.media.implementation.content.AssetType;
import com.microsoft.windowsazure.services.media.models.AssetInfo;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;

public class MediaRestProxy implements MediaContract {

private Client channel;
static Log log = LogFactory.getLog(MediaContract.class);

static Log log = LogFactory.getLog(MediaContract.class);
ServiceFilter[] filters;

@Inject
public MediaRestProxy(Client channel, OAuthFilter authFilter, RedirectFilter redirectFilter) {
public MediaRestProxy(Client channel, OAuthFilter authFilter, RedirectFilter redirectFilter,
VersionHeadersFilter versionHeadersFilter) {
this.channel = channel;
this.filters = new ServiceFilter[0];

channel.addFilter(redirectFilter);
channel.addFilter(authFilter);
channel.addFilter(versionHeadersFilter);
}

public MediaRestProxy(Client channel, ServiceFilter[] filters) {
Expand Down Expand Up @@ -71,4 +80,28 @@ private WebResource getResource(String entityName) {
return resource;
}

@Override
public AssetInfo createAsset(String name) throws ServiceException {
WebResource resource = getResource("Assets");

AssetType request = new AssetType();
request.setName(name);

return resource.type(MediaType.APPLICATION_ATOM_XML).accept(MediaType.APPLICATION_ATOM_XML)
.post(AssetInfo.class, request);

}

/* (non-Javadoc)
* @see com.microsoft.windowsazure.services.media.MediaContract#getAssets()
*/
@Override
public List<AssetInfo> getAssets() throws ServiceException {
WebResource resource = getResource("Assets");

return resource.type(MediaType.APPLICATION_ATOM_XML).accept(MediaType.APPLICATION_ATOM_XML)
.get(new GenericType<List<AssetInfo>>() {
});

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2011 Microsoft Corporation
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Microsoft Corporation
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2011 Microsoft Corporation
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2011 Microsoft Corporation
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,16 +59,17 @@ public class OAuthTokenManager {
*
* @param clientSecret
* A <code>String</code> object instance that represents the client secret.
* @throws URISyntaxException
*
*/
public OAuthTokenManager(OAuthContract contract, DateFactory dateFactory,
@Named(MediaConfiguration.OAUTH_URI) URI acsBaseUri,
@Named(MediaConfiguration.OAUTH_URI) String oauthUri,
@Named(MediaConfiguration.OAUTH_CLIENT_ID) String clientId,
@Named(MediaConfiguration.OAUTH_CLIENT_SECRET) String clientSecret,
@Named(MediaConfiguration.OAUTH_SCOPE) String scope) {
@Named(MediaConfiguration.OAUTH_SCOPE) String scope) throws URISyntaxException {
this.contract = contract;
this.dateFactory = dateFactory;
this.acsBaseUri = acsBaseUri;
this.acsBaseUri = new URI(oauthUri);
this.clientId = clientId;
this.clientSecret = clientSecret;
this.scope = scope;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2011 Microsoft Corporation
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit e8cf07b

Please sign in to comment.