Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of user agent. #14

Merged
merged 5 commits into from
Feb 8, 2013
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,82 @@
/**
* 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 final String azureSDKProductToken;

/**
* Instantiates a new user agent filter.
*/
public UserAgentFilter() {
String version = getVersionFromResources();
azureSDKProductToken = "Azure SDK for Java/" + version;
}

/* (non-Javadoc)
* @see com.sun.jersey.api.client.filter.ClientFilter#handle(com.sun.jersey.api.client.ClientRequest)
*/
@Override
public ClientResponse handle(ClientRequest cr) throws ClientHandlerException {
String userAgent;
String currentUserAgent = (String) cr.getHeaders().getFirst("User-Agent");

if (currentUserAgent != null) {
userAgent = azureSDKProductToken + " " + currentUserAgent;
}
else {
userAgent = azureSDKProductToken;
}

cr.getHeaders().remove("User-Agent");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be a no-op if there is no header? If not, it could throw,

It might be simpler to have one bigger if statement, switching on if the header exists or not.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated using big if.

cr.getHeaders().add("User-Agent", userAgent);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Are the headers immutable? If not, can we just update the existing user-agent without removing it?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.


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

/**
* 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(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code seems like it could be expensive. Can you cache the resulting version string so we only do the lookup once?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, added caching for the whole product token.

"META-INF/maven/com.microsoft.windowsazure/microsoft-windowsazure-api/pom.properties");
properties.load(inputStream);
}
catch (IOException e) {
throw new RuntimeException(e);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of throwing, it would be a nicer experience to just fall back to a default string. This code gets hit for every request. If for some reason we cannot read the properties, then no requests would work. Just returning a default string, like "unknown" would make the failure to read not have big impact.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sounds like a good idea, instead of returning an unknown, it might be more informative just to return an empty String as Version Number.

}
version = properties.getProperty("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