Skip to content

Commit

Permalink
Merge pull request #14 from gcheng/useragent
Browse files Browse the repository at this point in the history
Implementation of user agent.
  • Loading branch information
Albert Cheng committed Feb 8, 2013
2 parents 64b3938 + 781a0f4 commit 8c7ffac
Show file tree
Hide file tree
Showing 17 changed files with 235 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.microsoft.windowsazure.services.blob.implementation.SharedKeyFilter;
import com.microsoft.windowsazure.services.blob.implementation.SharedKeyLiteFilter;
import com.microsoft.windowsazure.services.core.Builder;
import com.microsoft.windowsazure.services.core.UserAgentFilter;

public class Exports implements Builder.Exports {
@Override
Expand All @@ -30,5 +31,6 @@ public void register(Builder.Registry registry) {
registry.add(SharedKeyLiteFilter.class);
registry.add(SharedKeyFilter.class);
registry.add(ISO8601DateConverter.class);
registry.add(UserAgentFilter.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,37 @@
import com.microsoft.windowsazure.services.blob.BlobConfiguration;
import com.microsoft.windowsazure.services.blob.BlobContract;
import com.microsoft.windowsazure.services.core.ServiceFilter;
import com.microsoft.windowsazure.services.core.UserAgentFilter;
import com.microsoft.windowsazure.services.core.utils.pipeline.HttpURLConnectionClient;
import com.sun.jersey.api.client.Client;

public class BlobRestProxy extends BlobOperationRestProxy implements BlobContract {
private final SharedKeyFilter filter;
private final SharedKeyFilter sharedKeyFilter;

@Inject
public BlobRestProxy(HttpURLConnectionClient channel, @Named(BlobConfiguration.ACCOUNT_NAME) String accountName,
@Named(BlobConfiguration.URI) String url, SharedKeyFilter filter) {
@Named(BlobConfiguration.URI) String url, SharedKeyFilter sharedKeyFilter, UserAgentFilter userAgentFilter) {
super(channel, accountName, url);

this.filter = filter;
channel.addFilter(filter);
this.sharedKeyFilter = sharedKeyFilter;

channel.addFilter(sharedKeyFilter);
channel.addFilter(userAgentFilter);
}

public BlobRestProxy(Client client, ServiceFilter[] filters, String accountName, String url,
SharedKeyFilter filter, RFC1123DateConverter dateMapper) {
SharedKeyFilter sharedKeyFilter, RFC1123DateConverter dateMapper) {
super(client, filters, accountName, url, dateMapper);

this.filter = filter;
this.sharedKeyFilter = sharedKeyFilter;
}

@Override
public BlobContract withFilter(ServiceFilter filter) {
ServiceFilter[] currentFilters = getFilters();
ServiceFilter[] newFilters = Arrays.copyOf(currentFilters, currentFilters.length + 1);
newFilters[currentFilters.length] = filter;
return new BlobRestProxy(getChannel(), newFilters, getAccountName(), getUrl(), this.filter, getDateMapper());
return new BlobRestProxy(getChannel(), newFilters, getAccountName(), getUrl(), this.sharedKeyFilter,
getDateMapper());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* 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.core;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.filter.ClientFilter;

/**
* The Class UserAgentFilter.
*/
public class UserAgentFilter extends ClientFilter {

/** The azure sdk product token. */
private static String azureSDKProductToken;

/**
* Instantiates a new user agent filter.
*/
public UserAgentFilter() {
if ((azureSDKProductToken == null) || azureSDKProductToken.isEmpty()) {
azureSDKProductToken = createAzureSDKProductToken();
}

}

/* (non-Javadoc)
* @see com.sun.jersey.api.client.filter.ClientFilter#handle(com.sun.jersey.api.client.ClientRequest)
*/
@Override
public ClientResponse handle(ClientRequest clientRequest) throws ClientHandlerException {
String userAgent;

if (clientRequest.getHeaders().containsKey("User-Agent")) {
String currentUserAgent = (String) clientRequest.getHeaders().getFirst("User-Agent");
userAgent = azureSDKProductToken + " " + currentUserAgent;
clientRequest.getHeaders().remove("User-Agent");
}
else {
userAgent = azureSDKProductToken;
}

clientRequest.getHeaders().add("User-Agent", userAgent);

return this.getNext().handle(clientRequest);
}

/**
* Creates the azure sdk product token.
*
* @return the string
*/
private String createAzureSDKProductToken() {
String version = getVersionFromResources();
String productToken;
if ((version != null) && (!version.isEmpty())) {
productToken = "Azure-SDK-For-Java/" + version;
}
else {
productToken = "Azure-SDK-For-Java";
}

return productToken;
}

/**
* Gets the version of the SDK from resources.
*
* @return the version from resources
*/
private String getVersionFromResources() {
String version;
Properties properties = new Properties();
try {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(
"META-INF/maven/com.microsoft.windowsazure/microsoft-windowsazure-api/pom.properties");
properties.load(inputStream);
version = properties.getProperty("version");
inputStream.close();
}
catch (IOException e) {
version = "";
}

return version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.xml.parsers.ParserConfigurationException;

import com.microsoft.windowsazure.services.core.Builder;
import com.microsoft.windowsazure.services.core.UserAgentFilter;
import com.microsoft.windowsazure.services.media.implementation.MediaContentProvider;
import com.microsoft.windowsazure.services.media.implementation.MediaExceptionProcessor;
import com.microsoft.windowsazure.services.media.implementation.MediaRestProxy;
Expand Down Expand Up @@ -50,6 +51,7 @@ public void register(Builder.Registry registry) {
registry.add(ResourceLocationManager.class);
registry.add(RedirectFilter.class);
registry.add(VersionHeadersFilter.class);
registry.add(UserAgentFilter.class);

registry.alter(ClientConfig.class, new Builder.Alteration<ClientConfig>() {
@SuppressWarnings("rawtypes")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.commons.logging.LogFactory;

import com.microsoft.windowsazure.services.core.ServiceFilter;
import com.microsoft.windowsazure.services.core.UserAgentFilter;
import com.microsoft.windowsazure.services.core.utils.pipeline.ClientConfigSettings;
import com.microsoft.windowsazure.services.media.MediaContract;
import com.microsoft.windowsazure.services.media.WritableBlobContainerContract;
Expand Down Expand Up @@ -58,20 +59,24 @@ public class MediaRestProxy extends EntityRestProxy implements MediaContract {
* the redirect filter
* @param versionHeadersFilter
* the version headers filter
* @param userAgentFilter
* the user agent filter
* @param clientConfigSettings
* Currently configured HTTP client settings
*
*/
@Inject
public MediaRestProxy(Client channel, OAuthFilter authFilter, RedirectFilter redirectFilter,
VersionHeadersFilter versionHeadersFilter, ClientConfigSettings clientConfigSettings) {
VersionHeadersFilter versionHeadersFilter, UserAgentFilter userAgentFilter,
ClientConfigSettings clientConfigSettings) {
super(channel, new ServiceFilter[0]);

this.clientConfigSettings = clientConfigSettings;
this.redirectFilter = redirectFilter;
channel.addFilter(redirectFilter);
channel.addFilter(authFilter);
channel.addFilter(versionHeadersFilter);
channel.addFilter(userAgentFilter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.codehaus.jackson.type.TypeReference;

import com.microsoft.windowsazure.services.core.ServiceException;
import com.microsoft.windowsazure.services.core.UserAgentFilter;
import com.microsoft.windowsazure.services.core.utils.ServiceExceptionFactory;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
Expand All @@ -46,8 +47,9 @@ public class OAuthRestProxy implements OAuthContract {
static Log log = LogFactory.getLog(OAuthContract.class);

@Inject
public OAuthRestProxy(Client channel) {
public OAuthRestProxy(Client channel, UserAgentFilter userAgentFilter) {
this.channel = channel;
channel.addFilter(userAgentFilter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
* 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
* 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.
* 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.queue;

import com.microsoft.windowsazure.services.core.Builder;
import com.microsoft.windowsazure.services.core.UserAgentFilter;
import com.microsoft.windowsazure.services.queue.implementation.QueueExceptionProcessor;
import com.microsoft.windowsazure.services.queue.implementation.QueueRestProxy;
import com.microsoft.windowsazure.services.queue.implementation.SharedKeyFilter;
Expand All @@ -28,5 +29,6 @@ public void register(Builder.Registry registry) {
registry.add(QueueRestProxy.class);
registry.add(SharedKeyLiteFilter.class);
registry.add(SharedKeyFilter.class);
registry.add(UserAgentFilter.class);
}
}
Loading

0 comments on commit 8c7ffac

Please sign in to comment.