Skip to content

Commit ffaf376

Browse files
authored
Merge pull request #1473 from scmacdon/master
Update the Java V2 Cognito Java Examples
2 parents a0a6635 + a3b1dfa commit ffaf376

17 files changed

+263
-450
lines changed

javav2/example_code/cognito/Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ For systems with Bash support, once you set the **CLASSPATH**, you can run a par
2323

2424
## Testing the Amazon Cognito files
2525

26-
You can test the Java code examples for Amazon Cognito by running a test file named **CognitoServiceIntegrationTest**. This file uses JUnit 5 to run the JUnit tests and is located in the **src/test/java** folder. For more information, see [https://junit.org/junit5/](https://junit.org/junit5/).
26+
You can test the Java code examples for Amazon Cognito by running a test file named **AmazonCognitoTest**. This file uses JUnit 5 to run the JUnit tests and is located in the **src/test/java** folder. For more information, see [https://junit.org/junit5/](https://junit.org/junit5/).
2727

2828
You can execute the JUnit tests from a Java IDE, such as IntelliJ, or from the command line by using Maven. As each test is run, you can view messages that inform you if the various tests succeed or fail. For example, the following message informs you that Test 3 passed.
2929

@@ -52,7 +52,7 @@ You will see output from the JUnit tests, as shown here.
5252
[INFO] -------------------------------------------------------
5353
[INFO] T E S T S
5454
[INFO] -------------------------------------------------------
55-
[INFO] Running CognitoServiceIntegrationTest
55+
[INFO] Running AmazonCognitoTest
5656
Test 1 passed
5757
Test 2 passed
5858
...

javav2/example_code/cognito/metadata.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ files:
4242
- path: src/main/java/com/example/cognito/ListUsers.java
4343
services:
4444
- cognito
45-
- path: src/test/java/CognitoServiceIntegrationTest.java
45+
- path: src/test/java/AmazonCognitoTest.java
4646
services:
4747
- cognito

javav2/example_code/cognito/src/main/java/com/example/cognito/AddLoginProvider.java

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
//snippet-sourcedescription:[AddLoginProvider.java demonstrates how to associate an Amazon Cognito identity pool with an identity provider.]
2-
//snippet-keyword:[Java]
3-
//snippet-sourcesyntax:[java]
2+
//snippet-keyword:[AWS SDK for Java v2]
43
//snippet-keyword:[Code Sample]
54
//snippet-keyword:[Amazon Cognito]
6-
//snippet-service:[cognito]
75
//snippet-sourcetype:[full-example]
8-
//snippet-sourcedate:[8/14/2020]
6+
//snippet-sourcedate:[11/04/2020]
97
//snippet-sourceauthor:[scmacdon AWS]
108
/*
11-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
12-
*
13-
* Licensed under the Apache License, Version 2.0 (the "License").
14-
* You may not use this file except in compliance with the License.
15-
* A copy of the License is located at
16-
*
17-
* http://aws.amazon.com/apache2.0
18-
*
19-
* or in the "license" file accompanying this file. This file is distributed
20-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
21-
* express or implied. See the License for the specific language governing
22-
* permissions and limitations under the License.
23-
*/
9+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
10+
SPDX-License-Identifier: Apache-2.0
11+
*/
2412

2513
package com.example.cognito;
2614

@@ -38,21 +26,21 @@
3826
public class AddLoginProvider {
3927

4028
public static void main(String[] args) {
29+
4130
final String USAGE = "\n" +
4231
"Usage:\n" +
43-
" AddLoginProvider <app_id> <identity_pool_name> <identity_pool_id><providerName>\n\n" +
32+
" AddLoginProvider <appId> <identityPoolName> <identityPoolId> <providerName>\n\n" +
4433
"Where:\n" +
45-
" appId - The application ID from the login provider.\n\n" +
46-
" identityPoolName - The name of your identity pool.\n\n" +
47-
" identityPoolId - The AWS Region and GUID of your identity pool.\n\n" +
48-
" providerName - The provider name (i.e., cognito-idp.us-east-1.amazonaws.com/us-east-1_Taz4Yxxxx).\n\n";
34+
" appId - the application ID value from the login provider.\n\n" +
35+
" identityPoolName - the name of your identity pool.\n\n" +
36+
" identityPoolId - the Id value of your identity pool.\n\n" +
37+
" providerName - the provider name (i.e., cognito-idp.us-east-1.amazonaws.com/us-east-1_Taz4Yxxxx).\n\n";
4938

50-
if (args.length < 4) {
39+
if (args.length != 4) {
5140
System.out.println(USAGE);
5241
System.exit(1);
5342
}
5443

55-
/* Read the name from command args */
5644
String appId = args[0];
5745
String identityPoolName = args[1];
5846
String identityPoolId = args[2];
@@ -63,14 +51,15 @@ public static void main(String[] args) {
6351
.build();
6452

6553
setLoginProvider(cognitoclient,appId, identityPoolName, identityPoolId, providerName) ;
54+
cognitoclient.close();
6655
}
6756

6857
//snippet-start:[cognito.java2.add_login_provider.main]
6958
public static void setLoginProvider(CognitoIdentityClient cognitoclient,
70-
String appId,
71-
String identityPoolName,
72-
String identityPoolId,
73-
String providerName) {
59+
String appId,
60+
String identityPoolName,
61+
String identityPoolId,
62+
String providerName) {
7463

7564
CognitoIdentityProvider identityProvider = CognitoIdentityProvider.builder()
7665
.providerName(providerName)
@@ -103,4 +92,4 @@ public static void setLoginProvider(CognitoIdentityClient cognitoclient,
10392
}
10493
//snippet-end:[cognito.java2.add_login_provider.main]
10594
}
106-
}
95+
}
Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
//snippet-sourcedescription:[CreateAdminUser.java demonstrates how to add a new admin to your user pool.]
2-
//snippet-keyword:[Java]
3-
//snippet-sourcesyntax:[java]
2+
//snippet-keyword:[AWS SDK for Java v2]
43
//snippet-keyword:[Code Sample]
54
//snippet-keyword:[Amazon Cognito]
6-
//snippet-service:[cognito]
75
//snippet-sourcetype:[full-example]
8-
//snippet-sourcedate:[8/14/2020]
6+
//snippet-sourcedate:[11/04/2020]
97
//snippet-sourceauthor:[scmacdon AWS]
108
/*
11-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
12-
*
13-
* Licensed under the Apache License, Version 2.0 (the "License").
14-
* You may not use this file except in compliance with the License.
15-
* A copy of the License is located at
16-
*
17-
* http://aws.amazon.com/apache2.0
18-
*
19-
* or in the "license" file accompanying this file. This file is distributed
20-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
21-
* express or implied. See the License for the specific language governing
22-
* permissions and limitations under the License.
23-
*/
9+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
10+
SPDX-License-Identifier: Apache-2.0
11+
*/
2412

2513
package com.example.cognito;
2614

@@ -38,29 +26,27 @@ public class CreateAdminUser {
3826
public static void main(String[] args) {
3927
final String USAGE = "\n" +
4028
"Usage:\n" +
41-
" CreateAdminUser <user_pool_id> <username> <email>\n\n" +
29+
" CreateAdminUser <userPoolId> <userName> <email>\n\n" +
4230
"Where:\n" +
43-
" user_pool_id - The ID for the user pool where the user will be created.\n\n" +
44-
" username - The user name for the user.\n\n" +
45-
" email - The email to use for verifying the admin account.\n\n" +
46-
"Example:\n" +
47-
" CreateTable HelloTable\n";
31+
" userPoolId - the Id value for the user pool where the user will be created.\n\n" +
32+
" userName - the user name for the admin user.\n\n" +
33+
" email - the email to use for verifying the admin account.\n\n" ;
4834

49-
if (args.length < 3) {
50-
System.out.println(USAGE);
51-
System.exit(1);
35+
if (args.length != 3) {
36+
System.out.println(USAGE);
37+
System.exit(1);
5238
}
5339

54-
/* Read the name from command args */
5540
String userPoolId = args[0];
56-
String name = args[1];
41+
String userName = args[1];
5742
String email = args[2];
5843

5944
CognitoIdentityProviderClient cognitoclient = CognitoIdentityProviderClient.builder()
6045
.region(Region.US_EAST_1)
6146
.build();
6247

63-
createAdmin(cognitoclient, userPoolId, name, email);
48+
createAdmin(cognitoclient, userPoolId, userName, email);
49+
cognitoclient.close();
6450
}
6551

6652
//snippet-start:[cognito.java2.add_login_provider.main]
@@ -71,16 +57,16 @@ public static void createAdmin(CognitoIdentityProviderClient cognitoclient,
7157

7258
try{
7359
AdminCreateUserResponse response = cognitoclient.adminCreateUser(
74-
AdminCreateUserRequest.builder()
75-
.userPoolId(userPoolId)
76-
.username(name)
77-
.userAttributes(AttributeType.builder()
78-
.name("email")
79-
.value(email)
80-
.build())
81-
.messageAction("SUPPRESS")
82-
.build()
83-
);
60+
AdminCreateUserRequest.builder()
61+
.userPoolId(userPoolId)
62+
.username(name)
63+
.userAttributes(AttributeType.builder()
64+
.name("email")
65+
.value(email)
66+
.build())
67+
.messageAction("SUPPRESS")
68+
.build()
69+
);
8470

8571
System.out.println("User " + response.user().username() + "is created. Status: " + response.user().userStatus());
8672

@@ -90,4 +76,4 @@ public static void createAdmin(CognitoIdentityProviderClient cognitoclient,
9076
}
9177
//snippet-end:[cognito.java2.add_login_provider.main]
9278
}
93-
}
79+
}
Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
//snippet-sourcedescription:[CreateIdentityPool.java demonstrates how to create a new Amazon Cognito identity pool. The identity pool is a store of user identity information that is specific to your AWS account.]
2-
//snippet-keyword:[Java]
3-
//snippet-sourcesyntax:[java]
2+
//snippet-keyword:[AWS SDK for Java v2]
43
//snippet-keyword:[Code Sample]
54
//snippet-keyword:[Amazon Cognito]
6-
//snippet-service:[cognito]
75
//snippet-sourcetype:[full-example]
8-
//snippet-sourcedate:[8/14/2020]
6+
//snippet-sourcedate:[11/04/2020]
97
//snippet-sourceauthor:[scmacdon AWS]
108
/*
11-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
12-
*
13-
* Licensed under the Apache License, Version 2.0 (the "License").
14-
* You may not use this file except in compliance with the License.
15-
* A copy of the License is located at
16-
*
17-
* http://aws.amazon.com/apache2.0
18-
*
19-
* or in the "license" file accompanying this file. This file is distributed
20-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
21-
* express or implied. See the License for the specific language governing
22-
* permissions and limitations under the License.
23-
*/
9+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
10+
SPDX-License-Identifier: Apache-2.0
11+
*/
2412

2513
package com.example.cognito;
2614

@@ -37,45 +25,43 @@ public class CreateIdentityPool {
3725
public static void main(String[] args) {
3826
final String USAGE = "\n" +
3927
"Usage:\n" +
40-
" CreateIdentityPool <identity_pool_name> \n\n" +
28+
" CreateIdentityPool <identityPoolName> \n\n" +
4129
"Where:\n" +
42-
" identity_pool_name - The name to give your identity pool.\n\n" +
43-
"Example:\n" +
44-
" CreateTable HelloTable\n";
30+
" identityPoolName - the name to give your identity pool.\n\n" ;
4531

46-
if (args.length < 1) {
32+
if (args.length != 1) {
4733
System.out.println(USAGE);
48-
System.exit(1);
34+
System.exit(1);
4935
}
5036

5137
String identityPoolName = args[0];
52-
5338
CognitoIdentityClient cognitoclient = CognitoIdentityClient.builder()
5439
.region(Region.US_EAST_1)
5540
.build();
5641

5742
String identityPoolId = createIdPool(cognitoclient, identityPoolName) ;
5843
System.out.println("Unity pool ID " + identityPoolId);
44+
cognitoclient.close();
5945
}
6046

6147
//snippet-start:[cognito.java2.create_identity_pool.main]
6248
public static String createIdPool(CognitoIdentityClient cognitoclient, String identityPoolName ) {
6349

6450
try {
6551
CreateIdentityPoolResponse response = cognitoclient.createIdentityPool(
66-
CreateIdentityPoolRequest.builder()
67-
.allowUnauthenticatedIdentities(false)
68-
.identityPoolName(identityPoolName)
69-
.build()
70-
);
52+
CreateIdentityPoolRequest.builder()
53+
.allowUnauthenticatedIdentities(false)
54+
.identityPoolName(identityPoolName)
55+
.build()
56+
);
7157

72-
return response.identityPoolId();
58+
return response.identityPoolId();
7359

7460
} catch (CognitoIdentityProviderException e){
7561
System.err.println(e.awsErrorDetails().errorMessage());
7662
System.exit(1);
77-
}
78-
return "";
63+
}
64+
return "";
7965
//snippet-end:[cognito.java2.create_identity_pool.main]
8066
}
81-
}
67+
}
Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
//snippet-sourcedescription:[CreateUserPool.java demonstrates how to create a user pool for Amazon Cognito.]
2-
//snippet-keyword:[Java]
3-
//snippet-sourcesyntax:[java]
2+
//snippet-keyword:[AWS SDK for Java v2]
43
//snippet-keyword:[Code Sample]
54
//snippet-keyword:[Amazon Cognito]
6-
//snippet-service:[cognito]
75
//snippet-sourcetype:[full-example]
8-
//snippet-sourcedate:[8/14/2020]
6+
//snippet-sourcedate:[11/04/2020]
97
//snippet-sourceauthor:[scmacdon AWS]
108
/*
11-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
12-
*
13-
* Licensed under the Apache License, Version 2.0 (the "License").
14-
* You may not use this file except in compliance with the License.
15-
* A copy of the License is located at
16-
*
17-
* http://aws.amazon.com/apache2.0
18-
*
19-
* or in the "license" file accompanying this file. This file is distributed
20-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
21-
* express or implied. See the License for the specific language governing
22-
* permissions and limitations under the License.
23-
*/
9+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
10+
SPDX-License-Identifier: Apache-2.0
11+
*/
2412

2513
package com.example.cognito;
2614

@@ -39,14 +27,14 @@ public static void main(String[] args) {
3927
"Usage:\n" +
4028
" CreateUserPool <userPoolName> \n\n" +
4129
"Where:\n" +
42-
" userPoolName - The name to give your user pool when it's created.\n\n" +
30+
" userPoolName - the name to give your user pool when it's created.\n\n" +
4331
"Example:\n" +
4432
" CreateTable HelloTable\n";
4533

46-
if (args.length < 1) {
47-
System.out.println(USAGE);
48-
System.exit(1);
49-
}
34+
if (args.length != 1) {
35+
System.out.println(USAGE);
36+
System.exit(1);
37+
}
5038
/* Read the name from command args */
5139
String userPoolName = args[0];
5240

@@ -56,6 +44,7 @@ public static void main(String[] args) {
5644

5745
String id = createPool(cognitoclient,userPoolName);
5846
System.out.println("User pool ID: " + id);
47+
cognitoclient.close();
5948
}
6049

6150
//snippet-start:[cognito.java2.create_user_pool.main]
@@ -67,7 +56,7 @@ public static String createPool(CognitoIdentityProviderClient cognitoclient,Stri
6756
.poolName(userPoolName)
6857
.build()
6958
);
70-
return repsonse.userPool().id();
59+
return repsonse.userPool().id();
7160

7261
} catch (CognitoIdentityProviderException e){
7362
System.err.println(e.awsErrorDetails().errorMessage());
@@ -76,4 +65,4 @@ public static String createPool(CognitoIdentityProviderClient cognitoclient,Stri
7665
return "";
7766
//snippet-end:[cognito.java2.create_user_pool.main]
7867
}
79-
}
68+
}

0 commit comments

Comments
 (0)