Skip to content

Commit

Permalink
Merge pull request Azure#57 from WindowsAzure/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Albert Cheng committed Dec 13, 2012
2 parents 81070bf + 26b12f8 commit 57dba1e
Show file tree
Hide file tree
Showing 48 changed files with 1,078 additions and 1,509 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class Configuration {
*/
public static final String PROPERTY_READ_TIMEOUT = "com.microsoft.windowsazure.services.core.Configuration.readTimeout";

/**
* Property name to control if HTTP logging to console is on or off. If property is set, logging is on, regardless
* of value.
*/
public static final String PROPERTY_LOG_HTTP_REQUESTS = "com.microsoft.windowsazure.services.core.Configuration.logHttpRequests";

private static Configuration instance;
Map<String, Object> properties;
Builder builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* 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.core.utils.pipeline;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.filter.LoggingFilter;

/**
* Class used for injecting timeout settings into the various places that need it.
*
*/
public class ClientConfigSettings {
private static final int DEFAULT_TIMEOUT_MS = 90 * 1000;

private final Integer connectTimeout;
private final Integer readTimeout;
private final boolean shouldLog;

/**
* Construct a {@link ClientConfigSettings} object with the default
* settings.
*/
public ClientConfigSettings() {
connectTimeout = Integer.valueOf(null);
readTimeout = Integer.valueOf(null);
shouldLog = false;
}

/**
* Construct a {@link ClientConfigSettings} object wit the given
* settings.
*
* @param connectTimeout
* Connection timeout in milliseconds
* @param readTimeout
* read timeout in milliseconds
* @param shouldLog
* if true, add logging filter to clients.
*/
public ClientConfigSettings(Object connectTimeout, Object readTimeout, boolean shouldLog) {
this.connectTimeout = getTimeout(connectTimeout);
this.readTimeout = getTimeout(readTimeout);
this.shouldLog = shouldLog;
}

/**
* Update the given {@link ClientConfig} object with the appropriate
* settings from configuration.
*
* @param clientConfig
* object to update.
*/
public void applyConfig(ClientConfig clientConfig) {
clientConfig.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, connectTimeout);
clientConfig.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, readTimeout);
}

/**
* Update the given {@link client} object with the appropriate settings
* from configuration.
*
* @param client
*/
public void applyConfig(Client client) {
if (shouldLog) {
client.addFilter(new LoggingFilter());
}
}

private Integer getTimeout(Object timeoutValue) {
if (timeoutValue == null) {
return new Integer(DEFAULT_TIMEOUT_MS);
}

if (timeoutValue instanceof Integer) {
return (Integer) timeoutValue;
}

if (timeoutValue instanceof String) {
return Integer.valueOf((String) timeoutValue);
}

throw new IllegalArgumentException("timeoutValue");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package com.microsoft.windowsazure.services.core.utils.pipeline;

import java.util.Map;
import java.util.Map.Entry;

import com.microsoft.windowsazure.services.core.Builder;
import com.microsoft.windowsazure.services.core.Builder.Registry;
Expand All @@ -32,48 +31,31 @@ public void register(Registry registry) {
@Override
public ClientConfig create(String profile, Builder builder, Map<String, Object> properties) {
ClientConfig clientConfig = new DefaultClientConfig();
profile = normalizeProfile(profile);

// Lower levels of the stack assume timeouts are set.
// Set default timeout on clientConfig in case user
// hasn't set it yet in their configuration

clientConfig.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, new Integer(90 * 1000));
clientConfig.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, new Integer(90 * 1000));
ClientConfigSettings settings = builder.build(profile, ClientConfigSettings.class, properties);
settings.applyConfig(clientConfig);
return clientConfig;
}
});

for (Entry<String, Object> entry : properties.entrySet()) {
Object propertyValue = entry.getValue();
String propertyKey = entry.getKey();
registry.add(new Builder.Factory<ClientConfigSettings>() {

if (propertyKey.equals(profile + Configuration.PROPERTY_CONNECT_TIMEOUT)) {
propertyKey = ClientConfig.PROPERTY_CONNECT_TIMEOUT;
}
if (propertyKey.equals(profile + Configuration.PROPERTY_READ_TIMEOUT)) {
propertyKey = ClientConfig.PROPERTY_READ_TIMEOUT;
}
@Override
public ClientConfigSettings create(String profile, Builder builder, Map<String, Object> properties) {
Object connectTimeout = getPropertyIfExists(profile, properties, Configuration.PROPERTY_CONNECT_TIMEOUT);
Object readTimeout = getPropertyIfExists(profile, properties, Configuration.PROPERTY_READ_TIMEOUT);

// ClientConfig requires instance of Integer to properly set
// timeouts, but config file will deliver strings. Special
// case these timeout properties and convert them to Integer
// if necessary.
if (propertyKey.equals(ClientConfig.PROPERTY_CONNECT_TIMEOUT)
|| propertyKey.equals(ClientConfig.PROPERTY_READ_TIMEOUT)) {
if (propertyValue instanceof String) {
propertyValue = Integer.valueOf((String) propertyValue);
}
}
clientConfig.getProperties().put(propertyKey, propertyValue);
}
return clientConfig;
return new ClientConfigSettings(connectTimeout, readTimeout, getPropertyIfExists(profile, properties,
Configuration.PROPERTY_LOG_HTTP_REQUESTS) != null);
}
});

registry.add(new Builder.Factory<Client>() {
@Override
public Client create(String profile, Builder builder, Map<String, Object> properties) {
ClientConfig clientConfig = builder.build(profile, ClientConfig.class, properties);
ClientConfigSettings settings = builder.build(profile, ClientConfigSettings.class, properties);
Client client = Client.create(clientConfig);
// client.addFilter(new LoggingFilter());
settings.applyConfig(client);
return client;
}
});
Expand All @@ -82,15 +64,16 @@ public Client create(String profile, Builder builder, Map<String, Object> proper
@Override
public HttpURLConnectionClient create(String profile, Builder builder, Map<String, Object> properties) {
ClientConfig clientConfig = builder.build(profile, ClientConfig.class, properties);
ClientConfigSettings settings = builder.build(profile, ClientConfigSettings.class, properties);
HttpURLConnectionClient client = HttpURLConnectionClient.create(clientConfig);
// client.addFilter(new LoggingFilter());
settings.applyConfig(client);
return client;
}
});
}

private static String normalizeProfile(String profile) {
if (profile == null) {
if (profile == null || profile.equals("")) {
return "";
}

Expand All @@ -100,4 +83,13 @@ private static String normalizeProfile(String profile) {

return profile + ".";
}

private static Object getPropertyIfExists(String profile, Map<String, Object> properties, String propertyName) {
String fullPropertyName = normalizeProfile(profile) + propertyName;

if (properties.containsKey(fullPropertyName)) {
return properties.get(fullPropertyName);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ public class MediaConfiguration {
* Defines the OAUTH configuration URI constant.
*
*/
public static final String OAUTH_URI = "oauth.uri";
public static final String OAUTH_URI = "media.oauth.uri";

/**
* Defines the OAUTH configuration client ID constant.
*
*/
public static final String OAUTH_CLIENT_ID = "oauth.client.id";
public static final String OAUTH_CLIENT_ID = "media.oauth.client.id";

/**
* Defines the OAUTH configuration client secret constant.
*
*/
public static final String OAUTH_CLIENT_SECRET = "oauth.client.secret";
public static final String OAUTH_CLIENT_SECRET = "media.oauth.client.secret";

/**
* Defines the SCOPE of the media service sent to OAUTH.
*/
public static final String OAUTH_SCOPE = "oauth.scope";
public static final String OAUTH_SCOPE = "media.oauth.scope";

/**
* Creates a media service configuration using the specified media service base URI, OAUTH URI,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.microsoft.windowsazure.services.core.FilterableService;
import com.microsoft.windowsazure.services.media.implementation.entities.EntityContract;
import com.microsoft.windowsazure.services.media.models.LocatorInfo;

/**
* Contract for interacting with the back end of Media Services
Expand All @@ -28,4 +29,13 @@ public interface MediaContract extends FilterableService<MediaContract>, EntityC

URI getRestServiceUri();

/**
* Creates an instance of the <code>WritableBlobContainerContract</code> API that will
* write to the blob container given by the provided locator.
*
* @param locator
* locator specifying where to upload to
* @return the implementation of <code>WritableBlobContainerContract</code>
*/
WritableBlobContainerContract createBlobWriter(LocatorInfo locator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@
*/
package com.microsoft.windowsazure.services.media;

import java.net.URI;

import com.microsoft.windowsazure.services.core.Configuration;
import com.microsoft.windowsazure.services.media.implementation.MediaBlobContainerWriter;
import com.microsoft.windowsazure.services.media.models.LocatorInfo;
import com.microsoft.windowsazure.services.media.models.LocatorType;
import com.sun.jersey.api.client.Client;

/**
*
Expand Down Expand Up @@ -70,55 +64,4 @@ public static MediaContract create(String profile) {
public static MediaContract create(String profile, Configuration config) {
return config.create(profile, MediaContract.class);
}

/**
* Creates an instance of the <code>WritableBlobContainerContract</code> API that will
* write to the blob container given by the provided locator.
*
* @param locator
* locator specifying where to upload to
* @return the implementation of <code>WritableBlobContainerContract</code>
*/
public static WritableBlobContainerContract createBlobWriter(LocatorInfo locator) {
if (locator.getLocatorType() != LocatorType.SAS) {
throw new IllegalArgumentException("Can only write to SAS locators");
}

LocatorParser p = new LocatorParser(locator);

return new MediaBlobContainerWriter(createUploaderClient(), p.getAccountName(), p.getStorageUri(),
p.getContainer(), p.getSASToken());
}

/**
* Helper class to encapsulate pulling information out of the locator.
*/
private static class LocatorParser {
URI locatorPath;

LocatorParser(LocatorInfo locator) {
locatorPath = URI.create(locator.getPath());
}

String getAccountName() {
return locatorPath.getHost().split("\\.")[0];
}

String getStorageUri() {
return locatorPath.getScheme() + "://" + locatorPath.getAuthority();
}

String getContainer() {
return locatorPath.getPath().substring(1);
}

String getSASToken() {
return locatorPath.getRawQuery();
}
}

private static Client createUploaderClient() {
Client client = Client.create();
return client;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
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.WritableBlobContainerContract;
import com.microsoft.windowsazure.services.media.implementation.entities.EntityActionOperation;
import com.microsoft.windowsazure.services.media.implementation.entities.EntityCreationOperation;
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.EntityUpdateOperation;
import com.microsoft.windowsazure.services.media.models.ListResult;
import com.microsoft.windowsazure.services.media.models.LocatorInfo;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.UniformInterfaceException;

Expand Down Expand Up @@ -196,4 +198,12 @@ public URI getRestServiceUri() {
return service.getRestServiceUri();
}

/* (non-Javadoc)
* @see com.microsoft.windowsazure.services.media.MediaContract#createBlobWriter(com.microsoft.windowsazure.services.media.models.LocatorInfo)
*/
@Override
public WritableBlobContainerContract createBlobWriter(LocatorInfo locator) {
return service.createBlobWriter(locator);
}

}
Loading

0 comments on commit 57dba1e

Please sign in to comment.