From 05d33fa185fe23e9a341ce43c123f9a2405464de Mon Sep 17 00:00:00 2001 From: Edson Yanaga Date: Wed, 25 Oct 2023 15:39:45 -0400 Subject: [PATCH] Moving the classes do package com.google.developers.wallet.rest Renaming methods to comply with the Java code conventions Some code improvements --- java/README.md | 16 +- java/settings.gradle | 2 +- .../wallet/rest}/DemoEventTicket.java | 274 +++++++++--------- .../developers/wallet/rest}/DemoFlight.java | 274 +++++++++--------- .../developers/wallet/rest}/DemoGeneric.java | 240 ++++++++------- .../developers/wallet/rest}/DemoGiftCard.java | 274 +++++++++--------- .../developers/wallet/rest}/DemoLoyalty.java | 272 +++++++++-------- .../developers/wallet/rest}/DemoOffer.java | 274 +++++++++--------- .../developers/wallet/rest}/DemoTransit.java | 274 +++++++++--------- 9 files changed, 916 insertions(+), 984 deletions(-) rename java/src/main/java/{ => com/google/developers/wallet/rest}/DemoEventTicket.java (77%) rename java/src/main/java/{ => com/google/developers/wallet/rest}/DemoFlight.java (76%) rename java/src/main/java/{ => com/google/developers/wallet/rest}/DemoGeneric.java (77%) rename java/src/main/java/{ => com/google/developers/wallet/rest}/DemoGiftCard.java (75%) rename java/src/main/java/{ => com/google/developers/wallet/rest}/DemoLoyalty.java (76%) rename java/src/main/java/{ => com/google/developers/wallet/rest}/DemoOffer.java (75%) rename java/src/main/java/{ => com/google/developers/wallet/rest}/DemoTransit.java (78%) diff --git a/java/README.md b/java/README.md index 6df9764..ffd01fb 100644 --- a/java/README.md +++ b/java/README.md @@ -8,13 +8,13 @@ creating a pass class, updating issuer permissions, and more. | Pass type | File | |----------------------------|--------------------------------------------------------------| -| Event tickets | [DemoEventTicket.java](./src/main/java/DemoEventTicket.java) | -| Flight boarding passes | [DemoFlight.java](./src/main/java/DemoFlight.java) | -| Generic passes | [DemoGeneric.java](./src/main/java/DemoGeneric.java) | -| Gift cards | [DemoGiftCard.java](./src/main/java/DemoGiftCard.java) | -| Loyalty program membership | [DemoLoyalty.java](./src/main/java/DemoLoyalty.java) | -| Offers and promotions | [DemoOffer.java](./src/main/java/DemoOffer.java) | -| Transit passes | [DemoTransit.java](./src/main/java/DemoTransit.java) | +| Event tickets | [com.google.developers.wallet.rest.DemoEventTicket.java](./src/main/java/com.google.developers.wallet.rest.DemoEventTicket.java) | +| Flight boarding passes | [com.google.developers.wallet.rest.DemoFlight.java](./src/main/java/com.google.developers.wallet.rest.DemoFlight.java) | +| Generic passes | [com.google.developers.wallet.rest.DemoGeneric.java](./src/main/java/com.google.developers.wallet.rest.DemoGeneric.java) | +| Gift cards | [com.google.developers.wallet.rest.DemoGiftCard.java](./src/main/java/com.google.developers.wallet.rest.DemoGiftCard.java) | +| Loyalty program membership | [com.google.developers.wallet.rest.DemoLoyalty.java](./src/main/java/com.google.developers.wallet.rest.DemoLoyalty.java) | +| Offers and promotions | [com.google.developers.wallet.rest.DemoOffer.java](./src/main/java/com.google.developers.wallet.rest.DemoOffer.java) | +| Transit passes | [com.google.developers.wallet.rest.DemoTransit.java](./src/main/java/com.google.developers.wallet.rest.DemoTransit.java) | ## Prerequisites @@ -54,7 +54,7 @@ for each class file. ```java // Create a demo class instance // Creates the authenticated HTTP client - DemoEventTicket demo = new DemoEventTicket(); + com.google.developers.wallet.rest.DemoEventTicket demo = new com.google.developers.wallet.rest.DemoEventTicket(); // Create a pass class demo.createClass("issuer_id", "class_suffix"); diff --git a/java/settings.gradle b/java/settings.gradle index de0756f..62c154a 100644 --- a/java/settings.gradle +++ b/java/settings.gradle @@ -1 +1 @@ -rootProject.name = 'wallet-samples' +rootProject.name = 'java-rest-samples' diff --git a/java/src/main/java/DemoEventTicket.java b/java/src/main/java/com/google/developers/wallet/rest/DemoEventTicket.java similarity index 77% rename from java/src/main/java/DemoEventTicket.java rename to java/src/main/java/com/google/developers/wallet/rest/DemoEventTicket.java index c53d7c6..c4907d4 100644 --- a/java/src/main/java/DemoEventTicket.java +++ b/java/src/main/java/com/google/developers/wallet/rest/DemoEventTicket.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.google.developers.wallet.rest; // [START setup] // [START imports] @@ -53,7 +54,7 @@ public class DemoEventTicket { keyFilePath = System.getenv().getOrDefault("GOOGLE_APPLICATION_CREDENTIALS", "/path/to/key.json"); - Auth(); + auth(); } // [END setup] @@ -61,14 +62,13 @@ public class DemoEventTicket { /** * Create authenticated HTTP client using a service account file. * - * @throws Exception */ - public void Auth() throws Exception { + public void auth() throws Exception { String scope = "https://www.googleapis.com/auth/wallet_object.issuer"; credentials = GoogleCredentials.fromStream(new FileInputStream(keyFilePath)) - .createScoped(Arrays.asList(scope)); + .createScoped(List.of(scope)); credentials.refresh(); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); @@ -91,14 +91,13 @@ public class DemoEventTicket { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String CreateClass(String issuerId, String classSuffix) throws IOException { + public String createClass(String issuerId, String classSuffix) throws IOException { // Check if the class exists try { service.eventticketclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); - System.out.println(String.format("Class %s.%s already exists!", issuerId, classSuffix)); + System.out.printf("Class %s.%s already exists!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() != 404) { @@ -139,9 +138,8 @@ public class DemoEventTicket { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String UpdateClass(String issuerId, String classSuffix) throws IOException { + public String updateClass(String issuerId, String classSuffix) throws IOException { EventTicketClass updatedClass; // Check if the class exists @@ -151,7 +149,7 @@ public class DemoEventTicket { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -192,16 +190,15 @@ public class DemoEventTicket { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String PatchClass(String issuerId, String classSuffix) throws IOException { + public String patchClass(String issuerId, String classSuffix) throws IOException { // Check if the class exists try { service.eventticketclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -244,9 +241,8 @@ public class DemoEventTicket { * @param header The message header. * @param body The message body. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String AddClassMessage(String issuerId, String classSuffix, String header, String body) + public String addClassMessage(String issuerId, String classSuffix, String header, String body) throws IOException { // Check if the class exists try { @@ -254,7 +250,7 @@ public class DemoEventTicket { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -287,15 +283,14 @@ public class DemoEventTicket { * @param classSuffix Developer-defined unique ID for this pass class. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String CreateObject(String issuerId, String classSuffix, String objectSuffix) + public String createObject(String issuerId, String classSuffix, String objectSuffix) throws IOException { // Check if the object exists try { service.eventticketobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); - System.out.println(String.format("Object %s.%s already exists!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s already exists!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() != 404) { @@ -325,11 +320,11 @@ public class DemoEventTicket { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -343,27 +338,27 @@ public class DemoEventTicket { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setSeatInfo( new EventSeat() .setSeat( @@ -403,9 +398,8 @@ public class DemoEventTicket { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String UpdateObject(String issuerId, String objectSuffix) throws IOException { + public String updateObject(String issuerId, String objectSuffix) throws IOException { EventTicketObject updatedObject; // Check if the object exists @@ -415,7 +409,7 @@ public class DemoEventTicket { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -433,7 +427,7 @@ public class DemoEventTicket { if (updatedObject.getLinksModuleData() == null) { // LinksModuleData was not set on the original object - updatedObject.setLinksModuleData(new LinksModuleData().setUris(Arrays.asList(newLink))); + updatedObject.setLinksModuleData(new LinksModuleData().setUris(List.of(newLink))); } else { updatedObject.getLinksModuleData().getUris().add(newLink); } @@ -458,9 +452,8 @@ public class DemoEventTicket { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String PatchObject(String issuerId, String objectSuffix) throws IOException { + public String patchObject(String issuerId, String objectSuffix) throws IOException { EventTicketObject existingObject; // Check if the object exists @@ -470,7 +463,7 @@ public class DemoEventTicket { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -519,16 +512,15 @@ public class DemoEventTicket { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String ExpireObject(String issuerId, String objectSuffix) throws IOException { + public String expireObject(String issuerId, String objectSuffix) throws IOException { // Check if the object exists try { service.eventticketobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -562,9 +554,8 @@ public class DemoEventTicket { * @param header The message header. * @param body The message body. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String AddObjectMessage(String issuerId, String objectSuffix, String header, String body) + public String addObjectMessage(String issuerId, String objectSuffix, String header, String body) throws IOException { // Check if the object exists try { @@ -572,7 +563,7 @@ public class DemoEventTicket { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -610,7 +601,7 @@ public class DemoEventTicket { * @param objectSuffix Developer-defined unique ID for the pass object. * @return An "Add to Google Wallet" link. */ - public String CreateJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { + public String createJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { // See link below for more information on required properties // https://developers.google.com/wallet/tickets/events/rest/v1/eventticketclass EventTicketClass newClass = @@ -643,11 +634,11 @@ public class DemoEventTicket { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -661,27 +652,27 @@ public class DemoEventTicket { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setSeatInfo( new EventSeat() .setSeat( @@ -707,13 +698,13 @@ public class DemoEventTicket { HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); // Create the Google Wallet payload and add to the JWT HashMap payload = new HashMap(); - payload.put("eventTicketClasses", Arrays.asList(newClass)); - payload.put("eventTicketObjects", Arrays.asList(newObject)); + payload.put("eventTicketClasses", List.of(newClass)); + payload.put("eventTicketObjects", List.of(newObject)); claims.put("payload", payload); // The service account credentials are used to sign the JWT @@ -723,7 +714,7 @@ public class DemoEventTicket { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -744,7 +735,7 @@ public class DemoEventTicket { * @param issuerId The issuer ID being used for this request. * @return An "Add to Google Wallet" link. */ - public String CreateJWTExistingObjects(String issuerId) { + public String createJWTExistingObjects(String issuerId) { // Multiple pass types can be added at the same time // At least one type must be specified in the JWT claims // Note: Make sure to replace the placeholder class and object suffixes @@ -753,64 +744,64 @@ public class DemoEventTicket { // Event tickets objectsToAdd.put( "eventTicketObjects", - Arrays.asList( - new EventTicketObject() - .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); + List.of( + new EventTicketObject() + .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); // Boarding passes objectsToAdd.put( "flightObjects", - Arrays.asList( - new FlightObject() - .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); + List.of( + new FlightObject() + .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); // Generic passes objectsToAdd.put( "genericObjects", - Arrays.asList( - new GenericObject() - .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); + List.of( + new GenericObject() + .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); // Gift cards objectsToAdd.put( "giftCardObjects", - Arrays.asList( - new GiftCardObject() - .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); + List.of( + new GiftCardObject() + .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); // Loyalty cards objectsToAdd.put( "loyaltyObjects", - Arrays.asList( - new LoyaltyObject() - .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); + List.of( + new LoyaltyObject() + .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); // Offers objectsToAdd.put( "offerObjects", - Arrays.asList( - new OfferObject() - .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); + List.of( + new OfferObject() + .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); // Transit passes objectsToAdd.put( "transitObjects", - Arrays.asList( - new TransitObject() - .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); + List.of( + new TransitObject() + .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); // Create the JWT as a HashMap object HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); claims.put("payload", objectsToAdd); @@ -821,7 +812,7 @@ public class DemoEventTicket { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -833,9 +824,8 @@ public class DemoEventTicket { * * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. - * @throws IOException */ - public void BatchCreateObjects(String issuerId, String classSuffix) throws IOException { + public void batchCreateObjects(String issuerId, String classSuffix) throws IOException { // Create the batch request client BatchRequest batch = service.batch(new HttpCredentialsAdapter(credentials)); @@ -879,11 +869,11 @@ public class DemoEventTicket { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -897,27 +887,27 @@ public class DemoEventTicket { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setSeatInfo( new EventSeat() .setSeat( diff --git a/java/src/main/java/DemoFlight.java b/java/src/main/java/com/google/developers/wallet/rest/DemoFlight.java similarity index 76% rename from java/src/main/java/DemoFlight.java rename to java/src/main/java/com/google/developers/wallet/rest/DemoFlight.java index 60e391a..1dc815a 100644 --- a/java/src/main/java/DemoFlight.java +++ b/java/src/main/java/com/google/developers/wallet/rest/DemoFlight.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.google.developers.wallet.rest; // [START setup] // [START imports] @@ -53,7 +54,7 @@ public class DemoFlight { keyFilePath = System.getenv().getOrDefault("GOOGLE_APPLICATION_CREDENTIALS", "/path/to/key.json"); - Auth(); + auth(); } // [END setup] @@ -61,14 +62,13 @@ public class DemoFlight { /** * Create authenticated HTTP client using a service account file. * - * @throws Exception */ - public void Auth() throws Exception { + public void auth() throws Exception { String scope = "https://www.googleapis.com/auth/wallet_object.issuer"; credentials = GoogleCredentials.fromStream(new FileInputStream(keyFilePath)) - .createScoped(Arrays.asList(scope)); + .createScoped(List.of(scope)); credentials.refresh(); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); @@ -91,14 +91,13 @@ public class DemoFlight { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String CreateClass(String issuerId, String classSuffix) throws IOException { + public String createClass(String issuerId, String classSuffix) throws IOException { // Check if the class exists try { service.flightclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); - System.out.println(String.format("Class %s.%s already exists!", issuerId, classSuffix)); + System.out.printf("Class %s.%s already exists!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() != 404) { @@ -142,9 +141,8 @@ public class DemoFlight { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String UpdateClass(String issuerId, String classSuffix) throws IOException { + public String updateClass(String issuerId, String classSuffix) throws IOException { FlightClass updatedClass; // Check if the class exists @@ -154,7 +152,7 @@ public class DemoFlight { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -195,16 +193,15 @@ public class DemoFlight { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String PatchClass(String issuerId, String classSuffix) throws IOException { + public String patchClass(String issuerId, String classSuffix) throws IOException { // Check if the class exists try { service.flightclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -247,9 +244,8 @@ public class DemoFlight { * @param header The message header. * @param body The message body. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String AddClassMessage(String issuerId, String classSuffix, String header, String body) + public String addClassMessage(String issuerId, String classSuffix, String header, String body) throws IOException { // Check if the class exists try { @@ -257,7 +253,7 @@ public class DemoFlight { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -290,15 +286,14 @@ public class DemoFlight { * @param classSuffix Developer-defined unique ID for this pass class. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String CreateObject(String issuerId, String classSuffix, String objectSuffix) + public String createObject(String issuerId, String classSuffix, String objectSuffix) throws IOException { // Check if the object exists try { service.flightobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); - System.out.println(String.format("Object %s.%s already exists!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s already exists!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() != 404) { @@ -328,11 +323,11 @@ public class DemoFlight { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -346,27 +341,27 @@ public class DemoFlight { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setPassengerName("Passenger name") .setBoardingAndSeatingInfo( new BoardingAndSeatingInfo().setBoardingGroup("B").setSeatNumber("42")) @@ -390,9 +385,8 @@ public class DemoFlight { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String UpdateObject(String issuerId, String objectSuffix) throws IOException { + public String updateObject(String issuerId, String objectSuffix) throws IOException { FlightObject updatedObject; // Check if the object exists @@ -402,7 +396,7 @@ public class DemoFlight { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -420,7 +414,7 @@ public class DemoFlight { if (updatedObject.getLinksModuleData() == null) { // LinksModuleData was not set on the original object - updatedObject.setLinksModuleData(new LinksModuleData().setUris(Arrays.asList(newLink))); + updatedObject.setLinksModuleData(new LinksModuleData().setUris(List.of(newLink))); } else { updatedObject.getLinksModuleData().getUris().add(newLink); } @@ -445,9 +439,8 @@ public class DemoFlight { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String PatchObject(String issuerId, String objectSuffix) throws IOException { + public String patchObject(String issuerId, String objectSuffix) throws IOException { FlightObject existingObject; // Check if the object exists @@ -457,7 +450,7 @@ public class DemoFlight { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -506,16 +499,15 @@ public class DemoFlight { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String ExpireObject(String issuerId, String objectSuffix) throws IOException { + public String expireObject(String issuerId, String objectSuffix) throws IOException { // Check if the object exists try { service.flightobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -549,9 +541,8 @@ public class DemoFlight { * @param header The message header. * @param body The message body. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String AddObjectMessage(String issuerId, String objectSuffix, String header, String body) + public String addObjectMessage(String issuerId, String objectSuffix, String header, String body) throws IOException { // Check if the object exists try { @@ -559,7 +550,7 @@ public class DemoFlight { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -597,7 +588,7 @@ public class DemoFlight { * @param objectSuffix Developer-defined unique ID for the pass object. * @return An "Add to Google Wallet" link. */ - public String CreateJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { + public String createJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { // See link below for more information on required properties // https://developers.google.com/wallet/tickets/boarding-passes/rest/v1/flightclass FlightClass newClass = @@ -634,11 +625,11 @@ public class DemoFlight { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -652,27 +643,27 @@ public class DemoFlight { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setPassengerName("Passenger name") .setBoardingAndSeatingInfo( new BoardingAndSeatingInfo().setBoardingGroup("B").setSeatNumber("42")) @@ -682,13 +673,13 @@ public class DemoFlight { HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); // Create the Google Wallet payload and add to the JWT HashMap payload = new HashMap(); - payload.put("flightClasses", Arrays.asList(newClass)); - payload.put("flightObjects", Arrays.asList(newObject)); + payload.put("flightClasses", List.of(newClass)); + payload.put("flightObjects", List.of(newObject)); claims.put("payload", payload); // The service account credentials are used to sign the JWT @@ -698,7 +689,7 @@ public class DemoFlight { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -719,7 +710,7 @@ public class DemoFlight { * @param issuerId The issuer ID being used for this request. * @return An "Add to Google Wallet" link. */ - public String CreateJWTExistingObjects(String issuerId) { + public String createJWTExistingObjects(String issuerId) { // Multiple pass types can be added at the same time // At least one type must be specified in the JWT claims // Note: Make sure to replace the placeholder class and object suffixes @@ -728,64 +719,64 @@ public class DemoFlight { // Event tickets objectsToAdd.put( "eventTicketObjects", - Arrays.asList( - new EventTicketObject() - .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); + List.of( + new EventTicketObject() + .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); // Boarding passes objectsToAdd.put( "flightObjects", - Arrays.asList( - new FlightObject() - .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); + List.of( + new FlightObject() + .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); // Generic passes objectsToAdd.put( "genericObjects", - Arrays.asList( - new GenericObject() - .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); + List.of( + new GenericObject() + .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); // Gift cards objectsToAdd.put( "giftCardObjects", - Arrays.asList( - new GiftCardObject() - .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); + List.of( + new GiftCardObject() + .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); // Loyalty cards objectsToAdd.put( "loyaltyObjects", - Arrays.asList( - new LoyaltyObject() - .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); + List.of( + new LoyaltyObject() + .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); // Offers objectsToAdd.put( "offerObjects", - Arrays.asList( - new OfferObject() - .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); + List.of( + new OfferObject() + .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); // Transit passes objectsToAdd.put( "transitObjects", - Arrays.asList( - new TransitObject() - .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); + List.of( + new TransitObject() + .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); // Create the JWT as a HashMap object HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); claims.put("payload", objectsToAdd); @@ -796,7 +787,7 @@ public class DemoFlight { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -808,9 +799,8 @@ public class DemoFlight { * * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. - * @throws IOException */ - public void BatchCreateObjects(String issuerId, String classSuffix) throws IOException { + public void batchCreateObjects(String issuerId, String classSuffix) throws IOException { // Create the batch request client BatchRequest batch = service.batch(new HttpCredentialsAdapter(credentials)); @@ -854,11 +844,11 @@ public class DemoFlight { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -872,27 +862,27 @@ public class DemoFlight { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setPassengerName("Passenger name") .setBoardingAndSeatingInfo( new BoardingAndSeatingInfo().setBoardingGroup("B").setSeatNumber("42")) diff --git a/java/src/main/java/DemoGeneric.java b/java/src/main/java/com/google/developers/wallet/rest/DemoGeneric.java similarity index 77% rename from java/src/main/java/DemoGeneric.java rename to java/src/main/java/com/google/developers/wallet/rest/DemoGeneric.java index 14f5b13..cc612ee 100644 --- a/java/src/main/java/DemoGeneric.java +++ b/java/src/main/java/com/google/developers/wallet/rest/DemoGeneric.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.google.developers.wallet.rest; // [START setup] // [START imports] @@ -53,7 +54,7 @@ public class DemoGeneric { keyFilePath = System.getenv().getOrDefault("GOOGLE_APPLICATION_CREDENTIALS", "/path/to/key.json"); - Auth(); + auth(); } // [END setup] @@ -61,14 +62,13 @@ public class DemoGeneric { /** * Create authenticated HTTP client using a service account file. * - * @throws Exception */ - public void Auth() throws Exception { + public void auth() throws Exception { String scope = "https://www.googleapis.com/auth/wallet_object.issuer"; credentials = GoogleCredentials.fromStream(new FileInputStream(keyFilePath)) - .createScoped(Arrays.asList(scope)); + .createScoped(List.of(scope)); credentials.refresh(); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); @@ -91,14 +91,13 @@ public class DemoGeneric { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String CreateClass(String issuerId, String classSuffix) throws IOException { + public String createClass(String issuerId, String classSuffix) throws IOException { // Check if the class exists try { service.genericclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); - System.out.println(String.format("Class %s.%s already exists!", issuerId, classSuffix)); + System.out.printf("Class %s.%s already exists!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() != 404) { @@ -130,9 +129,8 @@ public class DemoGeneric { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String UpdateClass(String issuerId, String classSuffix) throws IOException { + public String updateClass(String issuerId, String classSuffix) throws IOException { GenericClass updatedClass; // Check if the class exists @@ -142,7 +140,7 @@ public class DemoGeneric { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -186,9 +184,8 @@ public class DemoGeneric { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String PatchClass(String issuerId, String classSuffix) throws IOException { + public String patchClass(String issuerId, String classSuffix) throws IOException { GenericClass existingClass; // Check if the class exists @@ -198,7 +195,7 @@ public class DemoGeneric { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -247,15 +244,14 @@ public class DemoGeneric { * @param classSuffix Developer-defined unique ID for this pass class. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String CreateObject(String issuerId, String classSuffix, String objectSuffix) + public String createObject(String issuerId, String classSuffix, String objectSuffix) throws IOException { // Check if the object exists try { service.genericobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); - System.out.println(String.format("Object %s.%s already exists!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s already exists!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() != 404) { @@ -285,11 +281,11 @@ public class DemoGeneric { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -303,21 +299,21 @@ public class DemoGeneric { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setCardTitle( new LocalizedString() @@ -359,9 +355,8 @@ public class DemoGeneric { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String UpdateObject(String issuerId, String objectSuffix) throws IOException { + public String updateObject(String issuerId, String objectSuffix) throws IOException { GenericObject updatedObject; // Check if the object exists @@ -371,7 +366,7 @@ public class DemoGeneric { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -389,7 +384,7 @@ public class DemoGeneric { if (updatedObject.getLinksModuleData() == null) { // LinksModuleData was not set on the original object - updatedObject.setLinksModuleData(new LinksModuleData().setUris(Arrays.asList(newLink))); + updatedObject.setLinksModuleData(new LinksModuleData().setUris(List.of(newLink))); } else { updatedObject.getLinksModuleData().getUris().add(newLink); } @@ -414,9 +409,8 @@ public class DemoGeneric { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String PatchObject(String issuerId, String objectSuffix) throws IOException { + public String patchObject(String issuerId, String objectSuffix) throws IOException { GenericObject existingObject; // Check if the object exists @@ -426,7 +420,7 @@ public class DemoGeneric { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -475,16 +469,15 @@ public class DemoGeneric { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String ExpireObject(String issuerId, String objectSuffix) throws IOException { + public String expireObject(String issuerId, String objectSuffix) throws IOException { // Check if the object exists try { service.genericobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -522,7 +515,7 @@ public class DemoGeneric { * @param objectSuffix Developer-defined unique ID for the pass object. * @return An "Add to Google Wallet" link. */ - public String CreateJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { + public String createJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { // See link below for more information on required properties // https://developers.google.com/wallet/generic/rest/v1/genericclass GenericClass newClass = new GenericClass().setId(String.format("%s.%s", issuerId, classSuffix)); @@ -547,11 +540,11 @@ public class DemoGeneric { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -565,21 +558,21 @@ public class DemoGeneric { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setCardTitle( new LocalizedString() @@ -607,13 +600,13 @@ public class DemoGeneric { HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); // Create the Google Wallet payload and add to the JWT HashMap payload = new HashMap(); - payload.put("genericClasses", Arrays.asList(newClass)); - payload.put("genericObjects", Arrays.asList(newObject)); + payload.put("genericClasses", List.of(newClass)); + payload.put("genericObjects", List.of(newObject)); claims.put("payload", payload); // The service account credentials are used to sign the JWT @@ -623,7 +616,7 @@ public class DemoGeneric { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -644,7 +637,7 @@ public class DemoGeneric { * @param issuerId The issuer ID being used for this request. * @return An "Add to Google Wallet" link. */ - public String CreateJWTExistingObjects(String issuerId) { + public String createJWTExistingObjects(String issuerId) { // Multiple pass types can be added at the same time // At least one type must be specified in the JWT claims // Note: Make sure to replace the placeholder class and object suffixes @@ -653,64 +646,64 @@ public class DemoGeneric { // Event tickets objectsToAdd.put( "eventTicketObjects", - Arrays.asList( - new EventTicketObject() - .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); + List.of( + new EventTicketObject() + .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); // Boarding passes objectsToAdd.put( "flightObjects", - Arrays.asList( - new FlightObject() - .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); + List.of( + new FlightObject() + .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); // Generic passes objectsToAdd.put( "genericObjects", - Arrays.asList( - new GenericObject() - .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); + List.of( + new GenericObject() + .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); // Gift cards objectsToAdd.put( "giftCardObjects", - Arrays.asList( - new GiftCardObject() - .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); + List.of( + new GiftCardObject() + .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); // Loyalty cards objectsToAdd.put( "loyaltyObjects", - Arrays.asList( - new LoyaltyObject() - .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); + List.of( + new LoyaltyObject() + .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); // Offers objectsToAdd.put( "offerObjects", - Arrays.asList( - new OfferObject() - .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); + List.of( + new OfferObject() + .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); // Transit passes objectsToAdd.put( "transitObjects", - Arrays.asList( - new TransitObject() - .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); + List.of( + new TransitObject() + .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); // Create the JWT as a HashMap object HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); claims.put("payload", objectsToAdd); @@ -721,7 +714,7 @@ public class DemoGeneric { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -733,9 +726,8 @@ public class DemoGeneric { * * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. - * @throws IOException */ - public void BatchCreateObjects(String issuerId, String classSuffix) throws IOException { + public void batchCreateObjects(String issuerId, String classSuffix) throws IOException { // Create the batch request client BatchRequest batch = service.batch(new HttpCredentialsAdapter(credentials)); @@ -779,11 +771,11 @@ public class DemoGeneric { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -797,21 +789,21 @@ public class DemoGeneric { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setCardTitle( new LocalizedString() diff --git a/java/src/main/java/DemoGiftCard.java b/java/src/main/java/com/google/developers/wallet/rest/DemoGiftCard.java similarity index 75% rename from java/src/main/java/DemoGiftCard.java rename to java/src/main/java/com/google/developers/wallet/rest/DemoGiftCard.java index 38d61ba..eaca275 100644 --- a/java/src/main/java/DemoGiftCard.java +++ b/java/src/main/java/com/google/developers/wallet/rest/DemoGiftCard.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.google.developers.wallet.rest; // [START setup] // [START imports] @@ -53,7 +54,7 @@ public class DemoGiftCard { keyFilePath = System.getenv().getOrDefault("GOOGLE_APPLICATION_CREDENTIALS", "/path/to/key.json"); - Auth(); + auth(); } // [END setup] @@ -61,14 +62,13 @@ public class DemoGiftCard { /** * Create authenticated HTTP client using a service account file. * - * @throws Exception */ - public void Auth() throws Exception { + public void auth() throws Exception { String scope = "https://www.googleapis.com/auth/wallet_object.issuer"; credentials = GoogleCredentials.fromStream(new FileInputStream(keyFilePath)) - .createScoped(Arrays.asList(scope)); + .createScoped(List.of(scope)); credentials.refresh(); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); @@ -91,14 +91,13 @@ public class DemoGiftCard { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String CreateClass(String issuerId, String classSuffix) throws IOException { + public String createClass(String issuerId, String classSuffix) throws IOException { // Check if the class exists try { service.giftcardclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); - System.out.println(String.format("Class %s.%s already exists!", issuerId, classSuffix)); + System.out.printf("Class %s.%s already exists!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() != 404) { @@ -134,9 +133,8 @@ public class DemoGiftCard { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String UpdateClass(String issuerId, String classSuffix) throws IOException { + public String updateClass(String issuerId, String classSuffix) throws IOException { GiftCardClass updatedClass; // Check if the class exists @@ -146,7 +144,7 @@ public class DemoGiftCard { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -187,16 +185,15 @@ public class DemoGiftCard { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String PatchClass(String issuerId, String classSuffix) throws IOException { + public String patchClass(String issuerId, String classSuffix) throws IOException { // Check if the class exists try { service.giftcardclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -239,9 +236,8 @@ public class DemoGiftCard { * @param header The message header. * @param body The message body. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String AddClassMessage(String issuerId, String classSuffix, String header, String body) + public String addClassMessage(String issuerId, String classSuffix, String header, String body) throws IOException { // Check if the class exists try { @@ -249,7 +245,7 @@ public class DemoGiftCard { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -282,15 +278,14 @@ public class DemoGiftCard { * @param classSuffix Developer-defined unique ID for this pass class. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String CreateObject(String issuerId, String classSuffix, String objectSuffix) + public String createObject(String issuerId, String classSuffix, String objectSuffix) throws IOException { // Check if the object exists try { service.giftcardobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); - System.out.println(String.format("Object %s.%s already exists!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s already exists!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() != 404) { @@ -320,11 +315,11 @@ public class DemoGiftCard { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -338,27 +333,27 @@ public class DemoGiftCard { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setCardNumber("Card number") .setPin("1234") .setBalance(new Money().setMicros(20000000L).setCurrencyCode("USD")) @@ -382,9 +377,8 @@ public class DemoGiftCard { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String UpdateObject(String issuerId, String objectSuffix) throws IOException { + public String updateObject(String issuerId, String objectSuffix) throws IOException { GiftCardObject updatedObject; // Check if the object exists @@ -394,7 +388,7 @@ public class DemoGiftCard { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -412,7 +406,7 @@ public class DemoGiftCard { if (updatedObject.getLinksModuleData() == null) { // LinksModuleData was not set on the original object - updatedObject.setLinksModuleData(new LinksModuleData().setUris(Arrays.asList(newLink))); + updatedObject.setLinksModuleData(new LinksModuleData().setUris(List.of(newLink))); } else { updatedObject.getLinksModuleData().getUris().add(newLink); } @@ -437,9 +431,8 @@ public class DemoGiftCard { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String PatchObject(String issuerId, String objectSuffix) throws IOException { + public String patchObject(String issuerId, String objectSuffix) throws IOException { GiftCardObject existingObject; // Check if the object exists @@ -449,7 +442,7 @@ public class DemoGiftCard { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -498,16 +491,15 @@ public class DemoGiftCard { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String ExpireObject(String issuerId, String objectSuffix) throws IOException { + public String expireObject(String issuerId, String objectSuffix) throws IOException { // Check if the object exists try { service.giftcardobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -541,9 +533,8 @@ public class DemoGiftCard { * @param header The message header. * @param body The message body. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String AddObjectMessage(String issuerId, String objectSuffix, String header, String body) + public String addObjectMessage(String issuerId, String objectSuffix, String header, String body) throws IOException { // Check if the object exists try { @@ -551,7 +542,7 @@ public class DemoGiftCard { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -589,7 +580,7 @@ public class DemoGiftCard { * @param objectSuffix Developer-defined unique ID for the pass object. * @return An "Add to Google Wallet" link. */ - public String CreateJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { + public String createJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { // See link below for more information on required properties // https://developers.google.com/wallet/retail/gift-cards/rest/v1/giftcardclass GiftCardClass newClass = @@ -618,11 +609,11 @@ public class DemoGiftCard { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -636,27 +627,27 @@ public class DemoGiftCard { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setCardNumber("Card number") .setPin("1234") .setBalance(new Money().setMicros(20000000L).setCurrencyCode("USD")) @@ -666,13 +657,13 @@ public class DemoGiftCard { HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); // Create the Google Wallet payload and add to the JWT HashMap payload = new HashMap(); - payload.put("giftCardClasses", Arrays.asList(newClass)); - payload.put("giftCardObjects", Arrays.asList(newObject)); + payload.put("giftCardClasses", List.of(newClass)); + payload.put("giftCardObjects", List.of(newObject)); claims.put("payload", payload); // The service account credentials are used to sign the JWT @@ -682,7 +673,7 @@ public class DemoGiftCard { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -703,7 +694,7 @@ public class DemoGiftCard { * @param issuerId The issuer ID being used for this request. * @return An "Add to Google Wallet" link. */ - public String CreateJWTExistingObjects(String issuerId) { + public String createJWTExistingObjects(String issuerId) { // Multiple pass types can be added at the same time // At least one type must be specified in the JWT claims // Note: Make sure to replace the placeholder class and object suffixes @@ -712,64 +703,64 @@ public class DemoGiftCard { // Event tickets objectsToAdd.put( "eventTicketObjects", - Arrays.asList( - new EventTicketObject() - .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); + List.of( + new EventTicketObject() + .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); // Boarding passes objectsToAdd.put( "flightObjects", - Arrays.asList( - new FlightObject() - .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); + List.of( + new FlightObject() + .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); // Generic passes objectsToAdd.put( "genericObjects", - Arrays.asList( - new GenericObject() - .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); + List.of( + new GenericObject() + .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); // Gift cards objectsToAdd.put( "giftCardObjects", - Arrays.asList( - new GiftCardObject() - .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); + List.of( + new GiftCardObject() + .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); // Loyalty cards objectsToAdd.put( "loyaltyObjects", - Arrays.asList( - new LoyaltyObject() - .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); + List.of( + new LoyaltyObject() + .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); // Offers objectsToAdd.put( "offerObjects", - Arrays.asList( - new OfferObject() - .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); + List.of( + new OfferObject() + .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); // Transit passes objectsToAdd.put( "transitObjects", - Arrays.asList( - new TransitObject() - .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); + List.of( + new TransitObject() + .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); // Create the JWT as a HashMap object HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); claims.put("payload", objectsToAdd); @@ -780,7 +771,7 @@ public class DemoGiftCard { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -792,9 +783,8 @@ public class DemoGiftCard { * * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. - * @throws IOException */ - public void BatchCreateObjects(String issuerId, String classSuffix) throws IOException { + public void batchCreateObjects(String issuerId, String classSuffix) throws IOException { // Create the batch request client BatchRequest batch = service.batch(new HttpCredentialsAdapter(credentials)); @@ -838,11 +828,11 @@ public class DemoGiftCard { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -856,27 +846,27 @@ public class DemoGiftCard { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setCardNumber("Card number") .setPin("1234") .setBalance(new Money().setMicros(20000000L).setCurrencyCode("USD")) diff --git a/java/src/main/java/DemoLoyalty.java b/java/src/main/java/com/google/developers/wallet/rest/DemoLoyalty.java similarity index 76% rename from java/src/main/java/DemoLoyalty.java rename to java/src/main/java/com/google/developers/wallet/rest/DemoLoyalty.java index 2ef3b34..c5fcd31 100644 --- a/java/src/main/java/DemoLoyalty.java +++ b/java/src/main/java/com/google/developers/wallet/rest/DemoLoyalty.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.google.developers.wallet.rest; // [START setup] // [START imports] @@ -53,7 +54,7 @@ public class DemoLoyalty { keyFilePath = System.getenv().getOrDefault("GOOGLE_APPLICATION_CREDENTIALS", "/path/to/key.json"); - Auth(); + auth(); } // [END setup] @@ -61,14 +62,13 @@ public class DemoLoyalty { /** * Create authenticated HTTP client using a service account file. * - * @throws Exception */ - public void Auth() throws Exception { + public void auth() throws Exception { String scope = "https://www.googleapis.com/auth/wallet_object.issuer"; credentials = GoogleCredentials.fromStream(new FileInputStream(keyFilePath)) - .createScoped(Arrays.asList(scope)); + .createScoped(List.of(scope)); credentials.refresh(); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); @@ -91,14 +91,13 @@ public class DemoLoyalty { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String CreateClass(String issuerId, String classSuffix) throws IOException { + public String createClass(String issuerId, String classSuffix) throws IOException { // Check if the class exists try { service.loyaltyclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); - System.out.println(String.format("Class %s.%s already exists!", issuerId, classSuffix)); + System.out.printf("Class %s.%s already exists!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() != 404) { @@ -147,9 +146,8 @@ public class DemoLoyalty { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String UpdateClass(String issuerId, String classSuffix) throws IOException { + public String updateClass(String issuerId, String classSuffix) throws IOException { LoyaltyClass updatedClass; // Check if the class exists @@ -159,7 +157,7 @@ public class DemoLoyalty { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -200,16 +198,15 @@ public class DemoLoyalty { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String PatchClass(String issuerId, String classSuffix) throws IOException { + public String patchClass(String issuerId, String classSuffix) throws IOException { // Check if the class exists try { service.loyaltyclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -252,9 +249,8 @@ public class DemoLoyalty { * @param header The message header. * @param body The message body. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String AddClassMessage(String issuerId, String classSuffix, String header, String body) + public String addClassMessage(String issuerId, String classSuffix, String header, String body) throws IOException { // Check if the class exists try { @@ -262,7 +258,7 @@ public class DemoLoyalty { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -295,15 +291,14 @@ public class DemoLoyalty { * @param classSuffix Developer-defined unique ID for this pass class. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String CreateObject(String issuerId, String classSuffix, String objectSuffix) + public String createObject(String issuerId, String classSuffix, String objectSuffix) throws IOException { // Check if the object exists try { service.loyaltyobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); - System.out.println(String.format("Object %s.%s already exists!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s already exists!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() != 404) { @@ -333,11 +328,11 @@ public class DemoLoyalty { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -351,27 +346,27 @@ public class DemoLoyalty { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setAccountId("Account ID") .setAccountName("Account name") .setLoyaltyPoints( @@ -397,9 +392,8 @@ public class DemoLoyalty { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String UpdateObject(String issuerId, String objectSuffix) throws IOException { + public String updateObject(String issuerId, String objectSuffix) throws IOException { LoyaltyObject updatedObject; // Check if the object exists @@ -409,7 +403,7 @@ public class DemoLoyalty { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -427,7 +421,7 @@ public class DemoLoyalty { if (updatedObject.getLinksModuleData() == null) { // LinksModuleData was not set on the original object - updatedObject.setLinksModuleData(new LinksModuleData().setUris(Arrays.asList(newLink))); + updatedObject.setLinksModuleData(new LinksModuleData().setUris(List.of(newLink))); } else { updatedObject.getLinksModuleData().getUris().add(newLink); } @@ -452,9 +446,8 @@ public class DemoLoyalty { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String PatchObject(String issuerId, String objectSuffix) throws IOException { + public String patchObject(String issuerId, String objectSuffix) throws IOException { LoyaltyObject existingObject; // Check if the object exists @@ -464,7 +457,7 @@ public class DemoLoyalty { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -513,16 +506,15 @@ public class DemoLoyalty { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String ExpireObject(String issuerId, String objectSuffix) throws IOException { + public String expireObject(String issuerId, String objectSuffix) throws IOException { // Check if the object exists try { service.loyaltyobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -556,9 +548,8 @@ public class DemoLoyalty { * @param header The message header. * @param body The message body. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String AddObjectMessage(String issuerId, String objectSuffix, String header, String body) + public String addObjectMessage(String issuerId, String objectSuffix, String header, String body) throws IOException { // Check if the object exists try { @@ -566,7 +557,7 @@ public class DemoLoyalty { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -604,7 +595,7 @@ public class DemoLoyalty { * @param objectSuffix Developer-defined unique ID for the pass object. * @return An "Add to Google Wallet" link. */ - public String CreateJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { + public String createJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { // See link below for more information on required properties // https://developers.google.com/wallet/retail/loyalty-cards/rest/v1/loyaltyclass LoyaltyClass newClass = @@ -646,11 +637,11 @@ public class DemoLoyalty { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -664,27 +655,27 @@ public class DemoLoyalty { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setAccountId("Account ID") .setAccountName("Account name") .setLoyaltyPoints( @@ -696,13 +687,13 @@ public class DemoLoyalty { HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); // Create the Google Wallet payload and add to the JWT HashMap payload = new HashMap(); - payload.put("loyaltyClasses", Arrays.asList(newClass)); - payload.put("loyaltyObjects", Arrays.asList(newObject)); + payload.put("loyaltyClasses", List.of(newClass)); + payload.put("loyaltyObjects", List.of(newObject)); claims.put("payload", payload); // The service account credentials are used to sign the JWT @@ -712,7 +703,7 @@ public class DemoLoyalty { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -733,7 +724,7 @@ public class DemoLoyalty { * @param issuerId The issuer ID being used for this request. * @return An "Add to Google Wallet" link. */ - public String CreateJWTExistingObjects(String issuerId) { + public String createJWTExistingObjects(String issuerId) { // Multiple pass types can be added at the same time // At least one type must be specified in the JWT claims // Note: Make sure to replace the placeholder class and object suffixes @@ -742,64 +733,64 @@ public class DemoLoyalty { // Event tickets objectsToAdd.put( "eventTicketObjects", - Arrays.asList( - new EventTicketObject() - .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); + List.of( + new EventTicketObject() + .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); // Boarding passes objectsToAdd.put( "flightObjects", - Arrays.asList( - new FlightObject() - .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); + List.of( + new FlightObject() + .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); // Generic passes objectsToAdd.put( "genericObjects", - Arrays.asList( - new GenericObject() - .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); + List.of( + new GenericObject() + .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); // Gift cards objectsToAdd.put( "giftCardObjects", - Arrays.asList( - new GiftCardObject() - .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); + List.of( + new GiftCardObject() + .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); // Loyalty cards objectsToAdd.put( "loyaltyObjects", - Arrays.asList( - new LoyaltyObject() - .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); + List.of( + new LoyaltyObject() + .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); // Offers objectsToAdd.put( "offerObjects", - Arrays.asList( - new OfferObject() - .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); + List.of( + new OfferObject() + .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); // Transit passes objectsToAdd.put( "transitObjects", - Arrays.asList( - new TransitObject() - .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); + List.of( + new TransitObject() + .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); // Create the JWT as a HashMap object HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); claims.put("payload", objectsToAdd); @@ -810,7 +801,7 @@ public class DemoLoyalty { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -822,7 +813,6 @@ public class DemoLoyalty { * * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. - * @throws IOException */ public void BatchCreateObjects(String issuerId, String classSuffix) throws IOException { // Create the batch request client @@ -868,11 +858,11 @@ public class DemoLoyalty { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -886,27 +876,27 @@ public class DemoLoyalty { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setAccountId("Account ID") .setAccountName("Account name") .setLoyaltyPoints( diff --git a/java/src/main/java/DemoOffer.java b/java/src/main/java/com/google/developers/wallet/rest/DemoOffer.java similarity index 75% rename from java/src/main/java/DemoOffer.java rename to java/src/main/java/com/google/developers/wallet/rest/DemoOffer.java index 8da00f7..707e2f2 100644 --- a/java/src/main/java/DemoOffer.java +++ b/java/src/main/java/com/google/developers/wallet/rest/DemoOffer.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.google.developers.wallet.rest; // [START setup] // [START imports] @@ -53,7 +54,7 @@ public class DemoOffer { keyFilePath = System.getenv().getOrDefault("GOOGLE_APPLICATION_CREDENTIALS", "/path/to/key.json"); - Auth(); + auth(); } // [END setup] @@ -61,14 +62,13 @@ public class DemoOffer { /** * Create authenticated HTTP client using a service account file. * - * @throws Exception */ - public void Auth() throws Exception { + public void auth() throws Exception { String scope = "https://www.googleapis.com/auth/wallet_object.issuer"; credentials = GoogleCredentials.fromStream(new FileInputStream(keyFilePath)) - .createScoped(Arrays.asList(scope)); + .createScoped(List.of(scope)); credentials.refresh(); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); @@ -91,14 +91,13 @@ public class DemoOffer { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String CreateClass(String issuerId, String classSuffix) throws IOException { + public String createClass(String issuerId, String classSuffix) throws IOException { // Check if the class exists try { service.offerclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); - System.out.println(String.format("Class %s.%s already exists!", issuerId, classSuffix)); + System.out.printf("Class %s.%s already exists!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() != 404) { @@ -137,9 +136,8 @@ public class DemoOffer { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String UpdateClass(String issuerId, String classSuffix) throws IOException { + public String updateClass(String issuerId, String classSuffix) throws IOException { OfferClass updatedClass; // Check if the class exists @@ -149,7 +147,7 @@ public class DemoOffer { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -190,16 +188,15 @@ public class DemoOffer { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String PatchClass(String issuerId, String classSuffix) throws IOException { + public String patchClass(String issuerId, String classSuffix) throws IOException { // Check if the class exists try { service.offerclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -242,9 +239,8 @@ public class DemoOffer { * @param header The message header. * @param body The message body. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String AddClassMessage(String issuerId, String classSuffix, String header, String body) + public String addClassMessage(String issuerId, String classSuffix, String header, String body) throws IOException { // Check if the class exists try { @@ -252,7 +248,7 @@ public class DemoOffer { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -285,15 +281,14 @@ public class DemoOffer { * @param classSuffix Developer-defined unique ID for this pass class. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String CreateObject(String issuerId, String classSuffix, String objectSuffix) + public String createObject(String issuerId, String classSuffix, String objectSuffix) throws IOException { // Check if the object exists try { service.offerobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); - System.out.println(String.format("Object %s.%s already exists!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s already exists!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { @@ -326,11 +321,11 @@ public class DemoOffer { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -344,27 +339,27 @@ public class DemoOffer { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setValidTimeInterval( new TimeInterval() .setStart(new DateTime().setDate("2023-06-12T23:20:50.52Z")) @@ -388,9 +383,8 @@ public class DemoOffer { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String UpdateObject(String issuerId, String objectSuffix) throws IOException { + public String updateObject(String issuerId, String objectSuffix) throws IOException { OfferObject updatedObject; // Check if the object exists @@ -400,7 +394,7 @@ public class DemoOffer { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -418,7 +412,7 @@ public class DemoOffer { if (updatedObject.getLinksModuleData() == null) { // LinksModuleData was not set on the original object - updatedObject.setLinksModuleData(new LinksModuleData().setUris(Arrays.asList(newLink))); + updatedObject.setLinksModuleData(new LinksModuleData().setUris(List.of(newLink))); } else { updatedObject.getLinksModuleData().getUris().add(newLink); } @@ -443,9 +437,8 @@ public class DemoOffer { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String PatchObject(String issuerId, String objectSuffix) throws IOException { + public String patchObject(String issuerId, String objectSuffix) throws IOException { OfferObject existingObject; // Check if the object exists @@ -455,7 +448,7 @@ public class DemoOffer { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -504,16 +497,15 @@ public class DemoOffer { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String ExpireObject(String issuerId, String objectSuffix) throws IOException { + public String expireObject(String issuerId, String objectSuffix) throws IOException { // Check if the object exists try { service.offerobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -547,9 +539,8 @@ public class DemoOffer { * @param header The message header. * @param body The message body. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String AddObjectMessage(String issuerId, String objectSuffix, String header, String body) + public String addObjectMessage(String issuerId, String objectSuffix, String header, String body) throws IOException { // Check if the object exists try { @@ -557,7 +548,7 @@ public class DemoOffer { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -595,7 +586,7 @@ public class DemoOffer { * @param objectSuffix Developer-defined unique ID for the pass object. * @return An "Add to Google Wallet" link. */ - public String CreateJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { + public String createJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { // See link below for more information on required properties // https://developers.google.com/wallet/retail/offers/rest/v1/offerclass OfferClass newClass = @@ -627,11 +618,11 @@ public class DemoOffer { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -645,27 +636,27 @@ public class DemoOffer { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setValidTimeInterval( new TimeInterval() .setStart(new DateTime().setDate("2023-06-12T23:20:50.52Z")) @@ -675,13 +666,13 @@ public class DemoOffer { HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); // Create the Google Wallet payload and add to the JWT HashMap payload = new HashMap(); - payload.put("offerClasses", Arrays.asList(newClass)); - payload.put("offerObjects", Arrays.asList(newObject)); + payload.put("offerClasses", List.of(newClass)); + payload.put("offerObjects", List.of(newObject)); claims.put("payload", payload); // The service account credentials are used to sign the JWT @@ -691,7 +682,7 @@ public class DemoOffer { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -712,7 +703,7 @@ public class DemoOffer { * @param issuerId The issuer ID being used for this request. * @return An "Add to Google Wallet" link. */ - public String CreateJWTExistingObjects(String issuerId) { + public String createJWTExistingObjects(String issuerId) { // Multiple pass types can be added at the same time // At least one type must be specified in the JWT claims // Note: Make sure to replace the placeholder class and object suffixes @@ -721,64 +712,64 @@ public class DemoOffer { // Event tickets objectsToAdd.put( "eventTicketObjects", - Arrays.asList( - new EventTicketObject() - .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); + List.of( + new EventTicketObject() + .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); // Boarding passes objectsToAdd.put( "flightObjects", - Arrays.asList( - new FlightObject() - .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); + List.of( + new FlightObject() + .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); // Generic passes objectsToAdd.put( "genericObjects", - Arrays.asList( - new GenericObject() - .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); + List.of( + new GenericObject() + .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); // Gift cards objectsToAdd.put( "giftCardObjects", - Arrays.asList( - new GiftCardObject() - .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); + List.of( + new GiftCardObject() + .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); // Loyalty cards objectsToAdd.put( "loyaltyObjects", - Arrays.asList( - new LoyaltyObject() - .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); + List.of( + new LoyaltyObject() + .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); // Offers objectsToAdd.put( "offerObjects", - Arrays.asList( - new OfferObject() - .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); + List.of( + new OfferObject() + .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); // Transit passes objectsToAdd.put( "transitObjects", - Arrays.asList( - new TransitObject() - .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); + List.of( + new TransitObject() + .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); // Create the JWT as a HashMap object HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); claims.put("payload", objectsToAdd); @@ -789,7 +780,7 @@ public class DemoOffer { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -801,9 +792,8 @@ public class DemoOffer { * * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. - * @throws IOException */ - public void BatchCreateObjects(String issuerId, String classSuffix) throws IOException { + public void batchCreateObjects(String issuerId, String classSuffix) throws IOException { // Create the batch request client BatchRequest batch = service.batch(new HttpCredentialsAdapter(credentials)); @@ -847,11 +837,11 @@ public class DemoOffer { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -865,27 +855,27 @@ public class DemoOffer { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setValidTimeInterval( new TimeInterval() .setStart(new DateTime().setDate("2023-06-12T23:20:50.52Z")) diff --git a/java/src/main/java/DemoTransit.java b/java/src/main/java/com/google/developers/wallet/rest/DemoTransit.java similarity index 78% rename from java/src/main/java/DemoTransit.java rename to java/src/main/java/com/google/developers/wallet/rest/DemoTransit.java index 381b65f..a8190e9 100644 --- a/java/src/main/java/DemoTransit.java +++ b/java/src/main/java/com/google/developers/wallet/rest/DemoTransit.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.google.developers.wallet.rest; // [START setup] // [START imports] @@ -52,7 +53,7 @@ public class DemoTransit { keyFilePath = System.getenv().getOrDefault("GOOGLE_APPLICATION_CREDENTIALS", "/path/to/key.json"); - Auth(); + auth(); } // [END setup] @@ -60,14 +61,13 @@ public class DemoTransit { /** * Create authenticated HTTP client using a service account file. * - * @throws Exception */ - public void Auth() throws Exception { + public void auth() throws Exception { String scope = "https://www.googleapis.com/auth/wallet_object.issuer"; credentials = GoogleCredentials.fromStream(new FileInputStream(keyFilePath)) - .createScoped(Arrays.asList(scope)); + .createScoped(List.of(scope)); credentials.refresh(); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); @@ -90,14 +90,13 @@ public class DemoTransit { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String CreateClass(String issuerId, String classSuffix) throws IOException { + public String createClass(String issuerId, String classSuffix) throws IOException { // Check if the class exists try { service.transitclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); - System.out.println(String.format("Class %s.%s already exists!", issuerId, classSuffix)); + System.out.printf("Class %s.%s already exists!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() != 404) { @@ -146,9 +145,8 @@ public class DemoTransit { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String UpdateClass(String issuerId, String classSuffix) throws IOException { + public String updateClass(String issuerId, String classSuffix) throws IOException { TransitClass updatedClass; // Check if the class exists @@ -158,7 +156,7 @@ public class DemoTransit { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -199,16 +197,15 @@ public class DemoTransit { * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String PatchClass(String issuerId, String classSuffix) throws IOException { + public String patchClass(String issuerId, String classSuffix) throws IOException { // Check if the class exists try { service.transitclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -251,9 +248,8 @@ public class DemoTransit { * @param header The message header. * @param body The message body. * @return The pass class ID: "{issuerId}.{classSuffix}" - * @throws IOException */ - public String AddClassMessage(String issuerId, String classSuffix, String header, String body) + public String addClassMessage(String issuerId, String classSuffix, String header, String body) throws IOException { // Check if the class exists try { @@ -261,7 +257,7 @@ public class DemoTransit { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist - System.out.println(String.format("Class %s.%s not found!", issuerId, classSuffix)); + System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... @@ -294,15 +290,14 @@ public class DemoTransit { * @param classSuffix Developer-defined unique ID for this pass class. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String CreateObject(String issuerId, String classSuffix, String objectSuffix) + public String createObject(String issuerId, String classSuffix, String objectSuffix) throws IOException { // Check if the object exists try { service.transitobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); - System.out.println(String.format("Object %s.%s already exists!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s already exists!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { @@ -335,11 +330,11 @@ public class DemoTransit { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -353,27 +348,27 @@ public class DemoTransit { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setPassengerType("SINGLE_PASSENGER") .setPassengerNames("Passenger names") .setTripType("ONE_WAY") @@ -420,9 +415,8 @@ public class DemoTransit { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String UpdateObject(String issuerId, String objectSuffix) throws IOException { + public String updateObject(String issuerId, String objectSuffix) throws IOException { TransitObject updatedObject; // Check if the object exists @@ -432,7 +426,7 @@ public class DemoTransit { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -450,7 +444,7 @@ public class DemoTransit { if (updatedObject.getLinksModuleData() == null) { // LinksModuleData was not set on the original object - updatedObject.setLinksModuleData(new LinksModuleData().setUris(Arrays.asList(newLink))); + updatedObject.setLinksModuleData(new LinksModuleData().setUris(List.of(newLink))); } else { updatedObject.getLinksModuleData().getUris().add(newLink); } @@ -475,9 +469,8 @@ public class DemoTransit { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String PatchObject(String issuerId, String objectSuffix) throws IOException { + public String patchObject(String issuerId, String objectSuffix) throws IOException { TransitObject existingObject; // Check if the object exists @@ -487,7 +480,7 @@ public class DemoTransit { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -536,16 +529,15 @@ public class DemoTransit { * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String ExpireObject(String issuerId, String objectSuffix) throws IOException { + public String expireObject(String issuerId, String objectSuffix) throws IOException { // Check if the object exists try { service.transitobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -579,9 +571,8 @@ public class DemoTransit { * @param header The message header. * @param body The message body. * @return The pass object ID: "{issuerId}.{objectSuffix}" - * @throws IOException */ - public String AddObjectMessage(String issuerId, String objectSuffix, String header, String body) + public String addObjectMessage(String issuerId, String objectSuffix, String header, String body) throws IOException { // Check if the object exists try { @@ -589,7 +580,7 @@ public class DemoTransit { } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist - System.out.println(String.format("Object %s.%s not found!", issuerId, objectSuffix)); + System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... @@ -627,7 +618,7 @@ public class DemoTransit { * @param objectSuffix Developer-defined unique ID for the pass object. * @return An "Add to Google Wallet" link. */ - public String CreateJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { + public String createJWTNewObjects(String issuerId, String classSuffix, String objectSuffix) { // See link below for more information on required properties // https://developers.google.com/wallet/tickets/transit-passes/qr-code/rest/v1/transitclass TransitClass newClass = @@ -669,11 +660,11 @@ public class DemoTransit { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -687,27 +678,27 @@ public class DemoTransit { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setPassengerType("SINGLE_PASSENGER") .setPassengerNames("Passenger names") .setTripType("ONE_WAY") @@ -740,13 +731,13 @@ public class DemoTransit { HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); // Create the Google Wallet payload and add to the JWT HashMap payload = new HashMap(); - payload.put("transitClasses", Arrays.asList(newClass)); - payload.put("transitObjects", Arrays.asList(newObject)); + payload.put("transitClasses", List.of(newClass)); + payload.put("transitObjects", List.of(newObject)); claims.put("payload", payload); // The service account credentials are used to sign the JWT @@ -756,7 +747,7 @@ public class DemoTransit { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -777,7 +768,7 @@ public class DemoTransit { * @param issuerId The issuer ID being used for this request. * @return An "Add to Google Wallet" link. */ - public String CreateJWTExistingObjects(String issuerId) { + public String createJWTExistingObjects(String issuerId) { // Multiple pass types can be added at the same time // At least one type must be specified in the JWT claims // Note: Make sure to replace the placeholder class and object suffixes @@ -786,64 +777,64 @@ public class DemoTransit { // Event tickets objectsToAdd.put( "eventTicketObjects", - Arrays.asList( - new EventTicketObject() - .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); + List.of( + new EventTicketObject() + .setId(String.format("%s.%s", issuerId, "EVENT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "EVENT_CLASS_SUFFIX")))); // Boarding passes objectsToAdd.put( "flightObjects", - Arrays.asList( - new FlightObject() - .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); + List.of( + new FlightObject() + .setId(String.format("%s.%s", issuerId, "FLIGHT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "FLIGHT_CLASS_SUFFIX")))); // Generic passes objectsToAdd.put( "genericObjects", - Arrays.asList( - new GenericObject() - .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); + List.of( + new GenericObject() + .setId(String.format("%s.%s", issuerId, "GENERIC_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GENERIC_CLASS_SUFFIX")))); // Gift cards objectsToAdd.put( "giftCardObjects", - Arrays.asList( - new GiftCardObject() - .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); + List.of( + new GiftCardObject() + .setId(String.format("%s.%s", issuerId, "GIFT_CARD_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "GIFT_CARD_CLASS_SUFFIX")))); // Loyalty cards objectsToAdd.put( "loyaltyObjects", - Arrays.asList( - new LoyaltyObject() - .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); + List.of( + new LoyaltyObject() + .setId(String.format("%s.%s", issuerId, "LOYALTY_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "LOYALTY_CLASS_SUFFIX")))); // Offers objectsToAdd.put( "offerObjects", - Arrays.asList( - new OfferObject() - .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); + List.of( + new OfferObject() + .setId(String.format("%s.%s", issuerId, "OFFER_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "OFFER_CLASS_SUFFIX")))); // Transit passes objectsToAdd.put( "transitObjects", - Arrays.asList( - new TransitObject() - .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) - .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); + List.of( + new TransitObject() + .setId(String.format("%s.%s", issuerId, "TRANSIT_OBJECT_SUFFIX")) + .setClassId(String.format("%s.%s", issuerId, "TRANSIT_CLASS_SUFFIX")))); // Create the JWT as a HashMap object HashMap claims = new HashMap(); claims.put("iss", ((ServiceAccountCredentials) credentials).getClientEmail()); claims.put("aud", "google"); - claims.put("origins", Arrays.asList("www.example.com")); + claims.put("origins", List.of("www.example.com")); claims.put("typ", "savetowallet"); claims.put("payload", objectsToAdd); @@ -854,7 +845,7 @@ public class DemoTransit { String token = JWT.create().withPayload(claims).sign(algorithm); System.out.println("Add to Google Wallet link"); - System.out.println(String.format("https://pay.google.com/gp/v/save/%s", token)); + System.out.printf("https://pay.google.com/gp/v/save/%s%n", token); return String.format("https://pay.google.com/gp/v/save/%s", token); } @@ -866,9 +857,8 @@ public class DemoTransit { * * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. - * @throws IOException */ - public void BatchCreateObjects(String issuerId, String classSuffix) throws IOException { + public void batchCreateObjects(String issuerId, String classSuffix) throws IOException { // Create the batch request client BatchRequest batch = service.batch(new HttpCredentialsAdapter(credentials)); @@ -912,11 +902,11 @@ public class DemoTransit { .setLanguage("en-US") .setValue("Hero image description")))) .setTextModulesData( - Arrays.asList( - new TextModuleData() - .setHeader("Text module header") - .setBody("Text module body") - .setId("TEXT_MODULE_ID"))) + List.of( + new TextModuleData() + .setHeader("Text module header") + .setBody("Text module body") + .setId("TEXT_MODULE_ID"))) .setLinksModuleData( new LinksModuleData() .setUris( @@ -930,27 +920,27 @@ public class DemoTransit { .setDescription("Link module tel description") .setId("LINK_MODULE_TEL_ID")))) .setImageModulesData( - Arrays.asList( - new ImageModuleData() - .setMainImage( - new Image() - .setSourceUri( - new ImageUri() - .setUri( - "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) - .setContentDescription( - new LocalizedString() - .setDefaultValue( - new TranslatedString() - .setLanguage("en-US") - .setValue("Image module description")))) - .setId("IMAGE_MODULE_ID"))) + List.of( + new ImageModuleData() + .setMainImage( + new Image() + .setSourceUri( + new ImageUri() + .setUri( + "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")) + .setContentDescription( + new LocalizedString() + .setDefaultValue( + new TranslatedString() + .setLanguage("en-US") + .setValue("Image module description")))) + .setId("IMAGE_MODULE_ID"))) .setBarcode(new Barcode().setType("QR_CODE").setValue("QR code value")) .setLocations( - Arrays.asList( - new LatLongPoint() - .setLatitude(37.424015499999996) - .setLongitude(-122.09259560000001))) + List.of( + new LatLongPoint() + .setLatitude(37.424015499999996) + .setLongitude(-122.09259560000001))) .setPassengerType("SINGLE_PASSENGER") .setPassengerNames("Passenger names") .setTripType("ONE_WAY")