com.google.protobuf.BytesValue Java Examples
The following examples show how to use
com.google.protobuf.BytesValue.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WrappedPrimitiveTest.java From jackson-datatype-protobuf with Apache License 2.0 | 6 votes |
@Test public void itSetsFieldsWhenZeroInJson() throws IOException { String json = camelCase().writeValueAsString(defaultPopulatedJsonNode(camelCase())); HasWrappedPrimitives message = camelCase().readValue(json, HasWrappedPrimitives.class); assertThat(message.hasDoubleWrapper()).isTrue(); assertThat(message.getDoubleWrapper()).isEqualTo(DoubleValue.getDefaultInstance()); assertThat(message.hasFloatWrapper()).isTrue(); assertThat(message.getFloatWrapper()).isEqualTo(FloatValue.getDefaultInstance()); assertThat(message.hasInt64Wrapper()).isTrue(); assertThat(message.getInt64Wrapper()).isEqualTo(Int64Value.getDefaultInstance()); assertThat(message.hasUint64Wrapper()).isTrue(); assertThat(message.getUint64Wrapper()).isEqualTo(UInt64Value.getDefaultInstance()); assertThat(message.hasInt32Wrapper()).isTrue(); assertThat(message.getInt32Wrapper()).isEqualTo(Int32Value.getDefaultInstance()); assertThat(message.hasUint32Wrapper()).isTrue(); assertThat(message.getUint32Wrapper()).isEqualTo(UInt32Value.getDefaultInstance()); assertThat(message.hasBoolWrapper()).isTrue(); assertThat(message.getBoolWrapper()).isEqualTo(BoolValue.getDefaultInstance()); assertThat(message.hasStringWrapper()).isTrue(); assertThat(message.getStringWrapper()).isEqualTo(StringValue.getDefaultInstance()); assertThat(message.hasBytesWrapper()).isTrue(); assertThat(message.getBytesWrapper()).isEqualTo(BytesValue.getDefaultInstance()); }
Example #2
Source File: AddSmartDisplayAd.java From google-ads-java with Apache License 2.0 | 5 votes |
/** * Creates an image asset to be used for creating ads. * * @param googleAdsClient the Google Ads API client. * @param customerId the client customer ID. * @param imageUrl the image URL to be downloaded. * @param imageName the image name. * @return the created image asset's resource name. */ private static String createImageAsset( GoogleAdsClient googleAdsClient, long customerId, String imageUrl, String imageName) throws IOException { // Creates a media file. byte[] assetBytes = ByteStreams.toByteArray(new URL(imageUrl).openStream()); Asset asset = Asset.newBuilder() .setName(StringValue.of(imageName)) .setType(AssetType.IMAGE) .setImageAsset( ImageAsset.newBuilder() .setData(BytesValue.of(ByteString.copyFrom(assetBytes))) .build()) .build(); // Creates an asset operation. AssetOperation operation = AssetOperation.newBuilder().setCreate(asset).build(); // Creates the asset service client. try (AssetServiceClient assetServiceClient = googleAdsClient.getLatestVersion().createAssetServiceClient()) { // Adds the image asset. MutateAssetsResponse response = assetServiceClient.mutateAssets(Long.toString(customerId), ImmutableList.of(operation)); String imageResourceName = response.getResults(0).getResourceName(); System.out.printf("Created image asset with resource name '%s'.%n", imageResourceName); return imageResourceName; } }
Example #3
Source File: AddDisplayUploadAd.java From google-ads-java with Apache License 2.0 | 5 votes |
/** * Creates a media bundle from the assets in a zip file. The zip file contains the HTML5 * components. * * @param googleAdsClient the Google Ads API client. * @param customerId the client customer ID. * @return the resource name of the newly created media bundle. * @throws IOException if there is an error reading the media bundle. */ private String createMediaBundleAsset(GoogleAdsClient googleAdsClient, long customerId) throws IOException { // The HTML5 zip file contains all the HTML, CSS, and images needed for the // HTML5 ad. For help on creating an HTML5 zip file, check out Google Web // Designer (https://www.google.com/webdesigner/). byte[] html5Zip = ByteStreams.toByteArray(new URL(BUNDLE_URL).openStream()); // Creates the media bundle asset. Asset asset = Asset.newBuilder() .setType(AssetType.MEDIA_BUNDLE) .setMediaBundleAsset( MediaBundleAsset.newBuilder() .setData(BytesValue.of(ByteString.copyFrom(html5Zip))) .build()) .build(); // Creates the asset operation. AssetOperation operation = AssetOperation.newBuilder().setCreate(asset).build(); // Gets the AssetService. try (AssetServiceClient assetServiceClient = googleAdsClient.getLatestVersion().createAssetServiceClient()) { // Adds the asset to the client account. MutateAssetsResponse response = assetServiceClient.mutateAssets(Long.toString(customerId), ImmutableList.of(operation)); // Displays and returns the resulting resource name. String uploadedAssetResourceName = response.getResults(0).getResourceName(); System.out.printf( "Uploaded media bundle with resource name: '%s'.%n", uploadedAssetResourceName); return uploadedAssetResourceName; } }
Example #4
Source File: AddMerchantCenterDynamicRemarketingCampaign.java From google-ads-java with Apache License 2.0 | 5 votes |
/** * Adds an image to the Google Ads account. * * @param googleAdsClient the Google Ads API client. * @param customerId the client customer ID. * @param imageUrl the url of the image. * @param assetName the name of the asset. * @throws GoogleAdsException if an API request failed with one or more service errors. */ private String uploadAsset( GoogleAdsClient googleAdsClient, long customerId, String imageUrl, String assetName) throws IOException { byte[] imageData = ByteStreams.toByteArray(new URL(imageUrl).openStream()); // Creates the image asset. Asset asset = Asset.newBuilder() .setName(StringValue.of(assetName)) .setType(AssetType.IMAGE) .setImageAsset( ImageAsset.newBuilder() .setData(BytesValue.of(ByteString.copyFrom(imageData))) .build()) .build(); // Creates the asset operation. AssetOperation operation = AssetOperation.newBuilder().setCreate(asset).build(); // Creates the asset service client. try (AssetServiceClient assetServiceClient = googleAdsClient.getLatestVersion().createAssetServiceClient()) { // Adds the image asset. MutateAssetsResponse response = assetServiceClient.mutateAssets(Long.toString(customerId), ImmutableList.of(operation)); String imageResourceName = response.getResults(0).getResourceName(); System.out.printf("Created image asset with resource name '%s'.%n", imageResourceName); return imageResourceName; } }
Example #5
Source File: UploadMediaBundle.java From google-ads-java with Apache License 2.0 | 5 votes |
/** * Runs the example. * * @param googleAdsClient the Google Ads API client. * @param customerId the client customer ID. * @throws GoogleAdsException if an API request failed with one or more service errors. */ private void runExample(GoogleAdsClient googleAdsClient, long customerId) throws IOException { // Reads the sample media bundle from the URL into a byte array. byte[] bundleData = ByteStreams.toByteArray(new URL(BUNDLE_URL).openStream()); // Creates a media bundle file. MediaBundle bundle = MediaBundle.newBuilder().setData(BytesValue.of(ByteString.copyFrom(bundleData))).build(); // Creates a media file. MediaFile file = MediaFile.newBuilder() .setName(StringValue.of("Ad Media Bundle")) .setType(MediaType.MEDIA_BUNDLE) .setSourceUrl(StringValue.of(BUNDLE_URL)) .setMediaBundle(bundle) .build(); // Creates a media file operation. MediaFileOperation op = MediaFileOperation.newBuilder().setCreate(file).build(); // Issues a mutate request to add the media file. try (MediaFileServiceClient mediaFileServiceClient = googleAdsClient.getLatestVersion().createMediaFileServiceClient()) { MutateMediaFilesResponse response = mediaFileServiceClient.mutateMediaFiles(Long.toString(customerId), Arrays.asList(op)); System.out.printf( "The media bundle with resource name '%s' was added.%n", response.getResults(0).getResourceName()); } }
Example #6
Source File: UploadImage.java From google-ads-java with Apache License 2.0 | 5 votes |
/** * Runs the example. * * @param googleAdsClient the Google Ads API client. * @param customerId the client customer ID. * @throws GoogleAdsException if an API request failed with one or more service errors. */ private void runExample(GoogleAdsClient googleAdsClient, long customerId) throws IOException { byte[] imageData = ByteStreams.toByteArray(new URL("https://goo.gl/3b9Wfh").openStream()); MediaImage image = MediaImage.newBuilder().setData(BytesValue.of(ByteString.copyFrom(imageData))).build(); MediaFile file = MediaFile.newBuilder() .setName(StringValue.of("Ad Image")) .setType(MediaType.IMAGE) .setSourceUrl(StringValue.of("https://goo.gl/3b9Wfh")) .setImage(image) .build(); MediaFileOperation op = MediaFileOperation.newBuilder().setCreate(file).build(); try (MediaFileServiceClient mediaFileServiceClient = googleAdsClient.getLatestVersion().createMediaFileServiceClient()) { MutateMediaFilesResponse response = mediaFileServiceClient.mutateMediaFiles(Long.toString(customerId), Arrays.asList(op)); System.out.printf("Added %d images:%n", response.getResultsCount()); for (MutateMediaFileResult result : response.getResultsList()) { System.out.println(result.getResourceName()); } } }
Example #7
Source File: UploadImageAsset.java From google-ads-java with Apache License 2.0 | 5 votes |
/** * Runs the example. * * @param googleAdsClient the Google Ads API client. * @param customerId the client customer ID. * @throws IOException if there are errors related to image processing. */ private void runExample(GoogleAdsClient googleAdsClient, long customerId) throws IOException { byte[] imageData = ByteStreams.toByteArray(new URL(IMAGE_URL).openStream()); // Create the image asset. ImageAsset imageAsset = ImageAsset.newBuilder().setData(BytesValue.of(ByteString.copyFrom(imageData))).build(); // Creates an asset. Asset asset = Asset.newBuilder() // Optional: Provide a unique friendly name to identify your asset. // If you specify the name field, then both the asset name and the image being // uploaded should be unique, and should not match another ACTIVE asset in this // customer account. // .setName(StringValue.of("Jupiter Trip # " + System.currentTimeMillis())) .setType(AssetType.IMAGE) .setImageAsset(imageAsset) .build(); // Creates the operation. AssetOperation operation = AssetOperation.newBuilder().setCreate(asset).build(); // Creates the service client. try (AssetServiceClient assetServiceClient = googleAdsClient.getLatestVersion().createAssetServiceClient()) { // Issues a mutate request to add the asset. MutateAssetsResponse response = assetServiceClient.mutateAssets(Long.toString(customerId), ImmutableList.of(operation)); // Prints the result. System.out.printf( "The image asset with resource name '%s' was created.%n", response.getResults(0).getResourceName()); } }
Example #8
Source File: WrappedPrimitiveTest.java From jackson-datatype-protobuf with Apache License 2.0 | 5 votes |
private static HasWrappedPrimitives defaultPopulatedMessage() { return HasWrappedPrimitives .newBuilder() .setDoubleWrapper(DoubleValue.getDefaultInstance()) .setFloatWrapper(FloatValue.getDefaultInstance()) .setInt64Wrapper(Int64Value.getDefaultInstance()) .setUint64Wrapper(UInt64Value.getDefaultInstance()) .setInt32Wrapper(Int32Value.getDefaultInstance()) .setUint32Wrapper(UInt32Value.getDefaultInstance()) .setBoolWrapper(BoolValue.getDefaultInstance()) .setStringWrapper(StringValue.getDefaultInstance()) .setBytesWrapper(BytesValue.getDefaultInstance()) .build(); }
Example #9
Source File: AddGmailAd.java From google-ads-java with Apache License 2.0 | 4 votes |
/** * Adds the image files. * * @param googleAdsClient the Google Ads API client. * @param customerId the client customer ID. * @throws GoogleAdsException if an API request failed with one or more service errors. * @throws IOException if there is an error opening the image files. * @return a hash map of the image file resource names. */ private Map<String, String> addMediaFiles(GoogleAdsClient googleAdsClient, long customerId) throws IOException { // Creates a bytes array from the logo image data. byte[] logoImageData = ByteStreams.toByteArray(new URL("https://goo.gl/mtt54n").openStream()); // Creates the logo image. MediaFile mediaFileLogo = MediaFile.newBuilder() .setType(MediaType.IMAGE) .setImage( MediaImage.newBuilder() .setData(BytesValue.of(ByteString.copyFrom(logoImageData))) .build()) .setMimeType(MimeType.IMAGE_PNG) .build(); // Creates the operation for the logo image. MediaFileOperation mediaFileLogoOperation = MediaFileOperation.newBuilder().setCreate(mediaFileLogo).build(); // Creates a bytes array from the marketing image data. byte[] marketingImageData = ByteStreams.toByteArray(new URL("https://goo.gl/3b9Wfh").openStream()); // Creates the marketing image. MediaFile mediaFileMarketing = MediaFile.newBuilder() .setType(MediaType.IMAGE) .setImage( MediaImage.newBuilder() .setData(BytesValue.of(ByteString.copyFrom(marketingImageData))) .build()) .setMimeType(MimeType.IMAGE_JPEG) .build(); // Creates the operation for the marketing image. MediaFileOperation mediaFileMarketingOperation = MediaFileOperation.newBuilder().setCreate(mediaFileMarketing).build(); // Creates the media file service client. try (MediaFileServiceClient mediaFileServiceClient = googleAdsClient.getLatestVersion().createMediaFileServiceClient()) { // Adds the media files. MutateMediaFilesResponse response = mediaFileServiceClient.mutateMediaFiles( Long.toString(customerId), ImmutableList.of(mediaFileLogoOperation, mediaFileMarketingOperation)); // Displays the results. for (MutateMediaFileResult result : response.getResultsList()) { System.out.printf( "Created media file with resource name '%s'.%n", result.getResourceName()); } // Creates a map of the media files to return. Map<String, String> mediaFiles = new HashMap<>(); mediaFiles.put("logoResourceName", response.getResults(0).getResourceName()); mediaFiles.put("marketingImageResourceName", response.getResults(1).getResourceName()); return mediaFiles; } }
Example #10
Source File: WellKnownTypeMarshaller.java From curiostack with MIT License | 4 votes |
BytesValueMarshaller() { super(BytesValue.getDefaultInstance()); }
Example #11
Source File: WellKnownTypeMarshaller.java From curiostack with MIT License | 4 votes |
@Override protected final void doMerge(JsonParser parser, int unused, Message.Builder messageBuilder) throws IOException { BytesValue.Builder b = (BytesValue.Builder) messageBuilder; b.setValue(ParseSupport.parseBytes(parser)); }
Example #12
Source File: WellKnownTypeMarshaller.java From curiostack with MIT License | 4 votes |
@Override protected final void doWrite(BytesValue message, JsonGenerator gen) throws IOException { SerializeSupport.printBytes(message.getValue(), gen); }
Example #13
Source File: MessageMarshallerTest.java From curiostack with MIT License | 4 votes |
@Test public void anyFields() throws Exception { TestAllTypes content = TestAllTypes.newBuilder().setOptionalInt32(1234).build(); TestAny message = TestAny.newBuilder().setAnyValue(Any.pack(content)).build(); assertMatchesUpstream(message, TestAllTypes.getDefaultInstance()); TestAny messageWithDefaultAnyValue = TestAny.newBuilder().setAnyValue(Any.getDefaultInstance()).build(); assertMatchesUpstream(messageWithDefaultAnyValue); // Well-known types have a special formatting when embedded in Any. // // 1. Any in Any. Any anyMessage = Any.pack(Any.pack(content)); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); // 2. Wrappers in Any. anyMessage = Any.pack(Int32Value.newBuilder().setValue(12345).build()); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); anyMessage = Any.pack(UInt32Value.newBuilder().setValue(12345).build()); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); anyMessage = Any.pack(Int64Value.newBuilder().setValue(12345).build()); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); anyMessage = Any.pack(UInt64Value.newBuilder().setValue(12345).build()); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); anyMessage = Any.pack(FloatValue.newBuilder().setValue(12345).build()); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); anyMessage = Any.pack(DoubleValue.newBuilder().setValue(12345).build()); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); anyMessage = Any.pack(BoolValue.newBuilder().setValue(true).build()); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); anyMessage = Any.pack(StringValue.newBuilder().setValue("Hello").build()); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); anyMessage = Any.pack(BytesValue.newBuilder().setValue(ByteString.copyFrom(new byte[] {1, 2})).build()); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); // 3. Timestamp in Any. anyMessage = Any.pack(Timestamps.parse("1969-12-31T23:59:59Z")); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); // 4. Duration in Any anyMessage = Any.pack(Durations.parse("12345.10s")); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); // 5. FieldMask in Any anyMessage = Any.pack(FieldMaskUtil.fromString("foo.bar,baz")); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); // 6. Struct in Any Struct.Builder structBuilder = Struct.newBuilder(); structBuilder.putFields("number", Value.newBuilder().setNumberValue(1.125).build()); anyMessage = Any.pack(structBuilder.build()); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); // 7. Value (number type) in Any Value.Builder valueBuilder = Value.newBuilder(); valueBuilder.setNumberValue(1); anyMessage = Any.pack(valueBuilder.build()); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); // 8. Value (null type) in Any anyMessage = Any.pack(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build()); assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance()); }
Example #14
Source File: ProtobufModule.java From jackson-datatype-protobuf with Apache License 2.0 | 4 votes |
@Override public void setupModule(SetupContext context) { SimpleSerializers serializers = new SimpleSerializers(); serializers.addSerializer(new MessageSerializer(config)); serializers.addSerializer(new DurationSerializer()); serializers.addSerializer(new FieldMaskSerializer()); serializers.addSerializer(new ListValueSerializer()); serializers.addSerializer(new NullValueSerializer()); serializers.addSerializer(new StructSerializer()); serializers.addSerializer(new TimestampSerializer()); serializers.addSerializer(new ValueSerializer()); serializers.addSerializer(new WrappedPrimitiveSerializer<>(DoubleValue.class)); serializers.addSerializer(new WrappedPrimitiveSerializer<>(FloatValue.class)); serializers.addSerializer(new WrappedPrimitiveSerializer<>(Int64Value.class)); serializers.addSerializer(new WrappedPrimitiveSerializer<>(UInt64Value.class)); serializers.addSerializer(new WrappedPrimitiveSerializer<>(Int32Value.class)); serializers.addSerializer(new WrappedPrimitiveSerializer<>(UInt32Value.class)); serializers.addSerializer(new WrappedPrimitiveSerializer<>(BoolValue.class)); serializers.addSerializer(new WrappedPrimitiveSerializer<>(StringValue.class)); serializers.addSerializer(new WrappedPrimitiveSerializer<>(BytesValue.class)); context.addSerializers(serializers); context.addDeserializers(new MessageDeserializerFactory(config)); SimpleDeserializers deserializers = new SimpleDeserializers(); deserializers.addDeserializer(Duration.class, new DurationDeserializer()); deserializers.addDeserializer(FieldMask.class, new FieldMaskDeserializer()); deserializers.addDeserializer(ListValue.class, new ListValueDeserializer().buildAtEnd()); deserializers.addDeserializer(NullValue.class, new NullValueDeserializer()); deserializers.addDeserializer(Struct.class, new StructDeserializer().buildAtEnd()); deserializers.addDeserializer(Timestamp.class, new TimestampDeserializer()); deserializers.addDeserializer(Value.class, new ValueDeserializer().buildAtEnd()); deserializers.addDeserializer(DoubleValue.class, wrappedPrimitiveDeserializer(DoubleValue.class)); deserializers.addDeserializer(FloatValue.class, wrappedPrimitiveDeserializer(FloatValue.class)); deserializers.addDeserializer(Int64Value.class, wrappedPrimitiveDeserializer(Int64Value.class)); deserializers.addDeserializer(UInt64Value.class, wrappedPrimitiveDeserializer(UInt64Value.class)); deserializers.addDeserializer(Int32Value.class, wrappedPrimitiveDeserializer(Int32Value.class)); deserializers.addDeserializer(UInt32Value.class, wrappedPrimitiveDeserializer(UInt32Value.class)); deserializers.addDeserializer(BoolValue.class, wrappedPrimitiveDeserializer(BoolValue.class)); deserializers.addDeserializer(StringValue.class, wrappedPrimitiveDeserializer(StringValue.class)); deserializers.addDeserializer(BytesValue.class, wrappedPrimitiveDeserializer(BytesValue.class)); context.addDeserializers(deserializers); context.setMixInAnnotations(MessageOrBuilder.class, MessageOrBuilderMixin.class); }
Example #15
Source File: AllMapValuesTest.java From jackson-datatype-protobuf with Apache License 2.0 | 4 votes |
private static HasAllMapValues hasAllMapValues() { Value value = Value.newBuilder().setStringValue("test").build(); ByteString byteString = ByteString.copyFromUtf8("test"); Any any = Any .newBuilder() .setTypeUrl("type.googleapis.com/google.protobuf.Value") .setValue(value.toByteString()) .build(); return HasAllMapValues .newBuilder() .putDoubleMap("double", 1.5d) .putFloatMap("float", 2.5f) .putInt32Map("int32", 1) .putInt64Map("int64", 2) .putUint32Map("uint32", 3) .putUint64Map("uint64", 4) .putSint32Map("sint32", 5) .putSint64Map("sint64", 6) .putFixed32Map("fixed32", 7) .putFixed64Map("fixed64", 8) .putSfixed32Map("sfixed32", 9) .putSfixed64Map("sfixed64", 10) .putBoolMap("bool", true) .putStringMap("string", "test") .putBytesMap("bytes", byteString) .putAnyMap("any", any) .putDurationMap("duration", Duration.newBuilder().setSeconds(30).build()) .putFieldMaskMap("field_mask", FieldMask.newBuilder().addPaths("path_one").addPaths("path_two").build()) .putListValueMap("list_value", ListValue.newBuilder().addValues(value).build()) .putNullValueMap("null_value", NullValue.NULL_VALUE) .putStructMap("struct", Struct.newBuilder().putFields("field", value).build()) .putTimestampMap("timestamp", Timestamp.newBuilder().setSeconds(946684800).build()) .putValueMap("value", value) .putDoubleWrapperMap("double_wrapper", DoubleValue.newBuilder().setValue(3.5d).build()) .putFloatWrapperMap("float_wrapper", FloatValue.newBuilder().setValue(4.5f).build()) .putInt32WrapperMap("int32_wrapper", Int32Value.newBuilder().setValue(11).build()) .putInt64WrapperMap("int64_wrapper", Int64Value.newBuilder().setValue(12).build()) .putUint32WrapperMap("uint32_wrapper", UInt32Value.newBuilder().setValue(13).build()) .putUint64WrapperMap("uint64_wrapper", UInt64Value.newBuilder().setValue(14).build()) .putBoolWrapperMap("bool_wrapper", BoolValue.newBuilder().setValue(true).build()) .putStringWrapperMap("string_wrapper", StringValue.newBuilder().setValue("test").build()) .putBytesWrapperMap("bytes_wrapper", BytesValue.newBuilder().setValue(byteString).build()) .putEnumMap("enum", EnumProto3.FIRST) .putProto2MessageMap("proto2", AllFields.newBuilder().setString("proto2").build()) .putProto3MessageMap("proto3", AllFieldsProto3.newBuilder().setString("proto3").build()) .build(); }