-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"); | ||
cr.getHeaders().add("User-Agent", userAgent); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated using big if.