-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from gcheng/useragent
Implementation of user agent.
- Loading branch information
Showing
17 changed files
with
235 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...oft-azure-api/src/main/java/com/microsoft/windowsazure/services/core/UserAgentFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.