Skip to content

Commit 0cb8e3e

Browse files
committed
Add PubSub API prototype and related classes (#962)
* adding PubSubOptions, PubSub and its factories * Adding data types and define API * Add ReceivedMessage and fix review comments * Fix code review comments and add DefaultPubSubRpc * - Fix some codacy issues - Make messages consistent with proto defaults - Rename acknowledge to ack - Add nack - Add PullCallback * applying code review comments * add ByteArray * translate exceptions and add more info regarding pull * consider returnNullOn before throwing * Fix pubsub-related codacy issues
1 parent 8e914de commit 0cb8e3e

21 files changed

+2452
-0
lines changed

gcloud-java-core/pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,15 @@
9898
<version>3.4</version>
9999
<scope>test</scope>
100100
</dependency>
101+
<dependency>
102+
<groupId>com.google.protobuf</groupId>
103+
<artifactId>protobuf-java</artifactId>
104+
<version>3.0.0-beta-1</version>
105+
</dependency>
106+
<dependency>
107+
<groupId>com.google.api</groupId>
108+
<artifactId>gax</artifactId>
109+
<version>0.0.11</version>
110+
</dependency>
101111
</dependencies>
102112
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud;
18+
19+
import java.util.concurrent.Future;
20+
21+
public interface AsyncPage<T> extends Page<T> {
22+
Future<AsyncPage<T>> nextPageAsync();
23+
}

gcloud-java-core/src/main/java/com/google/cloud/BaseServiceException.java

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.api.client.googleapis.json.GoogleJsonError;
2020
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
21+
import com.google.api.gax.grpc.ApiException;
2122
import com.google.common.base.MoreObjects;
2223

2324
import java.io.IOException;
@@ -143,6 +144,16 @@ public BaseServiceException(int code, String message, String reason, boolean ide
143144
this.debugInfo = null;
144145
}
145146

147+
public BaseServiceException(ApiException apiException, boolean idempotent) {
148+
super(apiException.getMessage(), apiException);
149+
this.code = apiException.getStatusCode().value();
150+
this.reason = apiException.getStatusCode().name();
151+
this.idempotent = idempotent;
152+
this.retryable = apiException.isRetryable();
153+
this.location = null;
154+
this.debugInfo = null;
155+
}
156+
146157
protected Set<Error> retryableErrors() {
147158
return Collections.emptySet();
148159
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud;
18+
19+
import com.google.common.base.MoreObjects;
20+
import com.google.common.base.MoreObjects.ToStringHelper;
21+
import com.google.protobuf.ByteString;
22+
23+
import java.io.BufferedInputStream;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.Serializable;
28+
import java.nio.ByteBuffer;
29+
import java.nio.charset.StandardCharsets;
30+
import java.util.Iterator;
31+
32+
/**
33+
* An immutable byte array holder.
34+
*/
35+
public class ByteArray implements Iterable<Byte>, Serializable {
36+
37+
private static final long serialVersionUID = -1908809133893782840L;
38+
private final ByteString byteString;
39+
40+
protected ByteArray(ByteString byteString) {
41+
this.byteString = byteString;
42+
}
43+
44+
protected ByteArray(ByteArray byteArray) {
45+
this.byteString = byteArray.byteString();
46+
}
47+
48+
@Override
49+
public final Iterator<Byte> iterator() {
50+
return byteString.iterator();
51+
}
52+
53+
@Override
54+
public String toString() {
55+
ToStringHelper toStringHelper = MoreObjects.toStringHelper(this);
56+
StringBuilder stBuilder = new StringBuilder();
57+
for (int i = 0; i < Math.min(256, byteString.size()); i++) {
58+
stBuilder.append(String.format("%02x", byteString.byteAt(i)));
59+
}
60+
if (byteString.size() > 256) {
61+
stBuilder.append("...");
62+
}
63+
return toStringHelper.add("bytes", stBuilder.toString()).toString();
64+
}
65+
66+
@Override
67+
public final int hashCode() {
68+
return byteString.hashCode();
69+
}
70+
71+
@Override
72+
public final boolean equals(Object obj) {
73+
return obj == this
74+
|| obj instanceof ByteArray && byteString.equals(((ByteArray) obj).byteString);
75+
}
76+
77+
/**
78+
* Returns the size of this blob.
79+
*/
80+
public final int length() {
81+
return byteString.size();
82+
}
83+
84+
/**
85+
* Returns a copy as byte array.
86+
*/
87+
public final byte[] toByteArray() {
88+
return byteString.toByteArray();
89+
}
90+
91+
/**
92+
* Returns the content as {@code UTF-8} string.
93+
*/
94+
public final String toStringUtf8() {
95+
return byteString.toStringUtf8();
96+
}
97+
98+
/**
99+
* Returns a read-only {@link ByteBuffer} for this blob content.
100+
*/
101+
public final ByteBuffer asReadOnlyByteBuffer() {
102+
return byteString.asReadOnlyByteBuffer();
103+
}
104+
105+
/**
106+
* Returns an {@link InputStream} for this blob content.
107+
*/
108+
public final InputStream asInputStream() {
109+
final ByteBuffer byteBuffer = asReadOnlyByteBuffer();
110+
return new InputStream() {
111+
@Override public int read() {
112+
return !byteBuffer.hasRemaining() ? -1 : byteBuffer.get() & 0xFF;
113+
}
114+
};
115+
}
116+
117+
protected ByteString byteString() {
118+
return byteString;
119+
}
120+
121+
/**
122+
* Copies bytes into a ByteBuffer.
123+
*
124+
* @throws java.nio.ReadOnlyBufferException if the target is read-only
125+
* @throws java.nio.BufferOverflowException if the target's remaining() space is not large
126+
* enough to hold the data
127+
*/
128+
public final void copyTo(ByteBuffer target) {
129+
byteString.copyTo(target);
130+
}
131+
132+
/**
133+
* Copies bytes into a buffer.
134+
*
135+
* @throws IndexOutOfBoundsException if an offset or size is negative or too large
136+
*/
137+
public final void copyTo(byte[] target) {
138+
byteString.copyTo(target, 0, 0, length());
139+
}
140+
141+
public static final ByteArray copyFrom(byte[] bytes) {
142+
return new ByteArray(ByteString.copyFrom(bytes));
143+
}
144+
145+
/**
146+
* Copy the bytes using {@code UTF-8} decoding.
147+
*/
148+
public static final ByteArray copyFrom(String string) {
149+
return new ByteArray(ByteString.copyFrom(string, StandardCharsets.UTF_8));
150+
}
151+
152+
public static final ByteArray copyFrom(ByteBuffer bytes) {
153+
return new ByteArray(ByteString.copyFrom(bytes));
154+
}
155+
156+
public static final ByteArray copyFrom(InputStream input) throws IOException {
157+
BufferedInputStream bufferedInput = new BufferedInputStream(input);
158+
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
159+
int value;
160+
while ((value = bufferedInput.read()) != -1) {
161+
bytes.write(value);
162+
}
163+
return copyFrom(bytes.toByteArray());
164+
}
165+
}

gcloud-java-pubsub/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<artifactId>gcloud-java-pubsub</artifactId>
55
<packaging>jar</packaging>
66
<name>GCloud Java Pub/Sub</name>
7+
<url>https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-pubsub</url>
78
<description>
89
Java idiomatic client for Google Cloud Pub/Sub.
910
</description>
@@ -16,6 +17,11 @@
1617
<site.installationModule>gcloud-java-pubsub</site.installationModule>
1718
</properties>
1819
<dependencies>
20+
<dependency>
21+
<groupId>${project.groupId}</groupId>
22+
<artifactId>gcloud-java-core</artifactId>
23+
<version>${project.version}</version>
24+
</dependency>
1925
<dependency>
2026
<groupId>com.google.api</groupId>
2127
<artifactId>gax</artifactId>

0 commit comments

Comments
 (0)