forked from aws-amplify/amplify-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add User-Agent header to all outbound network requests
- Loading branch information
1 parent
5981bdd
commit 6f13bc0
Showing
6 changed files
with
224 additions
and
2 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
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
69 changes: 69 additions & 0 deletions
69
aws-api/src/main/java/com/amplifyframework/api/aws/UserAgentInterceptor.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,69 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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.amplifyframework.api.aws; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
/** | ||
* An OkHttp3 interceptor which applies a User-Agent header to an outgoing request. | ||
*/ | ||
final class UserAgentInterceptor implements Interceptor { | ||
private final UserAgentProvider userAgentProvider; | ||
|
||
/** | ||
* Constructs a UserAgentInterceptor. | ||
* @param userAgentProvider A Provider of a user-agent string | ||
*/ | ||
private UserAgentInterceptor(final UserAgentProvider userAgentProvider) { | ||
this.userAgentProvider = userAgentProvider; | ||
} | ||
|
||
/** | ||
* Creates a user agent interceptor using a user-agent string provider. | ||
* @param userAgentProvider Provider of user-agent string | ||
* @return A UserAgentInterceptor | ||
*/ | ||
static UserAgentInterceptor using(UserAgentProvider userAgentProvider) { | ||
return new UserAgentInterceptor(userAgentProvider); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Response intercept(@NonNull Chain chain) throws IOException { | ||
Request originalRequest = chain.request(); | ||
Request requestWithUserAgent = originalRequest.newBuilder() | ||
.header("User-Agent", userAgentProvider.getUserAgent()) | ||
.build(); | ||
return chain.proceed(requestWithUserAgent); | ||
} | ||
|
||
/** | ||
* A provider of a user-agent string. | ||
*/ | ||
interface UserAgentProvider { | ||
/** | ||
* Gets the User-Agent string. | ||
* @return User-Agent string | ||
*/ | ||
String getUserAgent(); | ||
} | ||
} |
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
134 changes: 134 additions & 0 deletions
134
core/src/main/java/com/amplifyframework/util/UserAgent.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,134 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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.amplifyframework.util; | ||
|
||
import android.annotation.SuppressLint; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import com.amazonaws.amplify.core.BuildConfig; | ||
|
||
/** | ||
* A utility to construct a User-Agent header, to be sent with all network operations. | ||
*/ | ||
public final class UserAgent { | ||
private static String instance = null; | ||
|
||
@SuppressWarnings("checkstyle:all") private UserAgent() {} | ||
|
||
/** | ||
* Gets a String to use as the value of a User-Agent header. | ||
* @return A value for a User-Agent header. | ||
*/ | ||
@SuppressLint("SyntheticAccessor") | ||
@NonNull | ||
public static String string() { | ||
if (instance == null) { | ||
instance = new UserAgent.Builder() | ||
.libraryName("amplify-android") | ||
.libraryVersion(BuildConfig.VERSION_NAME) | ||
.systemName(System.getProperty("os.name")) | ||
.systemVersion(System.getProperty("os.version")) | ||
.javaVmName(System.getProperty("java.vm.name")) | ||
.javaVmVersion(System.getProperty("java.vm.version")) | ||
.javaVersion(System.getProperty("java.version")) | ||
.userLanguage(System.getProperty("user.language")) | ||
.userRegion(System.getProperty("user.region")) | ||
.toString(); | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
@SuppressWarnings("SameParameterValue") | ||
private static final class Builder { | ||
private String libraryName; | ||
private String libraryVersion; | ||
private String systemName; | ||
private String systemVersion; | ||
private String javaVmName; | ||
private String javaVmVersion; | ||
private String javaVersion; | ||
private String userLanguage; | ||
private String userRegion; | ||
|
||
Builder libraryName(String libraryName) { | ||
this.libraryName = sanitize(libraryName); | ||
return this; | ||
} | ||
|
||
Builder libraryVersion(String libraryVersion) { | ||
this.libraryVersion = sanitize(libraryVersion); | ||
return this; | ||
} | ||
|
||
Builder systemName(String systemName) { | ||
this.systemName = sanitize(systemName); | ||
return this; | ||
} | ||
|
||
Builder systemVersion(String systemVersion) { | ||
this.systemVersion = sanitize(systemVersion); | ||
return this; | ||
} | ||
|
||
Builder javaVmName(String javaVmName) { | ||
this.javaVmName = sanitize(javaVmName); | ||
return this; | ||
} | ||
|
||
Builder javaVmVersion(String javaVmVersion) { | ||
this.javaVmVersion = sanitize(javaVmVersion); | ||
return this; | ||
} | ||
|
||
Builder javaVersion(String javaVersion) { | ||
this.javaVersion = sanitize(javaVersion); | ||
return this; | ||
} | ||
|
||
Builder userLanguage(String userLanguage) { | ||
this.userLanguage = sanitize(userLanguage); | ||
return this; | ||
} | ||
|
||
Builder userRegion(String userRegion) { | ||
this.userRegion = sanitize(userRegion); | ||
return this; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public String toString() { | ||
return String.format( | ||
"%s/%s %s/%s %s/%s/%s %s_%s", | ||
libraryName, libraryVersion, | ||
systemName, systemVersion, | ||
javaVmName, javaVmVersion, javaVersion, | ||
userLanguage, userRegion | ||
); | ||
} | ||
|
||
@NonNull | ||
private static String sanitize(@Nullable String string) { | ||
if (string == null) { | ||
return "UNKNOWN"; | ||
} | ||
|
||
return string.replace(' ', '_'); | ||
} | ||
} | ||
} |