Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pub/Sub API prototype #768

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c712c8a
adding PubSubOptions, PubSub and its factories
aozarov Feb 11, 2016
99a0a5a
Merge branch 'pubsub-alpha' of http://github.com/aozarov/gcloud-java …
aozarov Feb 12, 2016
6bb23be
Merge branch 'pubsub-alpha' of https://github.com/GoogleCloudPlatform…
aozarov Mar 20, 2016
88a2ec2
Adding data types and define API
aozarov Mar 21, 2016
1ffb2a9
minor
aozarov Mar 21, 2016
eb2cf43
add ReceivedMessage and fix review comments
aozarov Mar 23, 2016
0dedcd9
minor fix
aozarov Mar 23, 2016
9ca5f9f
Merge branch 'pubsub-alpha' of https://github.com/GoogleCloudPlatform…
aozarov Mar 23, 2016
0f30906
fix code review comments + add DefaultPubSubRpc
aozarov Mar 28, 2016
360f6e5
Merge branch 'pubsub-alpha' of https://github.com/GoogleCloudPlatform…
aozarov Mar 30, 2016
b829590
Fix merge issues
aozarov Apr 5, 2016
d2e1ca4
Merge branch 'pubsub-alpha' of git://github.com/GoogleCloudPlatform/g…
aozarov Apr 12, 2016
a9a0caa
Merge branch 'master' of git://github.com/GoogleCloudPlatform/gcloud-…
aozarov Apr 12, 2016
685f185
* Merge from master to include package rename
aozarov Apr 12, 2016
05e8df0
Merge branch 'pubsub-alpha' of https://github.com/GoogleCloudPlatform…
aozarov Apr 13, 2016
2d5f9df
- Fix some codacy issues
aozarov Apr 13, 2016
3152de3
applying code review comments
aozarov Apr 20, 2016
7202b27
add ByteArray
aozarov Apr 22, 2016
1edd1f0
Merge branch 'pubsub-alpha' of git://github.com/GoogleCloudPlatform/g…
aozarov Apr 22, 2016
cefc65d
translate exceptions and add more info regarding pull
aozarov Apr 22, 2016
965f7a6
consider returnNullOn before throwing
aozarov Apr 25, 2016
6639c66
fix merge conflict
aozarov Apr 25, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions gcloud-java-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,15 @@
<version>3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.0.0-beta-1</version>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax</artifactId>
<version>0.0.11</version>
</dependency>
</dependencies>
</project>
23 changes: 23 additions & 0 deletions gcloud-java-core/src/main/java/com/google/cloud/AsyncPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud;

import java.util.concurrent.Future;

public interface AsyncPage<T> extends Page<T> {
Future<AsyncPage<T>> nextPageAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.gax.grpc.ApiException;
import com.google.common.base.MoreObjects;

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

public BaseServiceException(ApiException apiException, boolean idempotent) {
super(apiException.getMessage(), apiException);
this.code = apiException.getStatusCode().value();
this.reason = apiException.getStatusCode().name();
this.idempotent = idempotent;
this.retryable = apiException.isRetryable();
this.location = null;
this.debugInfo = null;
}

protected Set<Error> retryableErrors() {
return Collections.emptySet();
}
Expand Down
165 changes: 165 additions & 0 deletions gcloud-java-core/src/main/java/com/google/cloud/ByteArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.protobuf.ByteString;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

/**
* An immutable byte array holder.
*/
public class ByteArray implements Iterable<Byte>, Serializable {

private static final long serialVersionUID = -1908809133893782840L;
private final ByteString byteString;

protected ByteArray(ByteString byteString) {
this.byteString = byteString;
}

protected ByteArray(ByteArray byteArray) {
this.byteString = byteArray.byteString();
}

@Override
public final Iterator<Byte> iterator() {
return byteString.iterator();
}

@Override
public String toString() {
ToStringHelper toStringHelper = MoreObjects.toStringHelper(this);
StringBuilder stBuilder = new StringBuilder();
for (int i = 0; i < Math.min(256, byteString.size()); i++) {
stBuilder.append(String.format("%02x", byteString.byteAt(i)));
}
if (byteString.size() > 256) {
stBuilder.append("...");
}
return toStringHelper.add("bytes", stBuilder.toString()).toString();
}

@Override
public final int hashCode() {
return byteString.hashCode();
}

@Override
public final boolean equals(Object obj) {
return obj == this
|| obj instanceof ByteArray && byteString.equals(((ByteArray) obj).byteString);
}

/**
* Returns the size of this blob.
*/
public final int length() {
return byteString.size();
}

/**
* Returns a copy as byte array.
*/
public final byte[] toByteArray() {
return byteString.toByteArray();
}

/**
* Returns the content as {@code UTF-8} string.
*/
public final String toStringUtf8() {
return byteString.toStringUtf8();
}

/**
* Returns a read-only {@link ByteBuffer} for this blob content.
*/
public final ByteBuffer asReadOnlyByteBuffer() {
return byteString.asReadOnlyByteBuffer();
}

/**
* Returns an {@link InputStream} for this blob content.
*/
public final InputStream asInputStream() {
final ByteBuffer byteBuffer = asReadOnlyByteBuffer();
return new InputStream() {
@Override public int read() {
return !byteBuffer.hasRemaining() ? -1 : byteBuffer.get() & 0xFF;
}
};
}

protected ByteString byteString() {
return byteString;
}

/**
* Copies bytes into a ByteBuffer.
*
* @throws java.nio.ReadOnlyBufferException if the target is read-only
* @throws java.nio.BufferOverflowException if the target's remaining() space is not large
* enough to hold the data
*/
public final void copyTo(ByteBuffer target) {
byteString.copyTo(target);
}

/**
* Copies bytes into a buffer.
*
* @throws IndexOutOfBoundsException if an offset or size is negative or too large
*/
public final void copyTo(byte[] target) {
byteString.copyTo(target, 0, 0, length());
}

public static final ByteArray copyFrom(byte[] bytes) {
return new ByteArray(ByteString.copyFrom(bytes));
}

/**
* Copy the bytes using {@code UTF-8} decoding.
*/
public static final ByteArray copyFrom(String string) {
return new ByteArray(ByteString.copyFrom(string, StandardCharsets.UTF_8));
}

public static final ByteArray copyFrom(ByteBuffer bytes) {
return new ByteArray(ByteString.copyFrom(bytes));
}

public static final ByteArray copyFrom(InputStream input) throws IOException {
BufferedInputStream bufferedInput = new BufferedInputStream(input);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
int value;
while ((value = bufferedInput.read()) != -1) {
bytes.write(value);
}
return copyFrom(bytes.toByteArray());
}
}
6 changes: 6 additions & 0 deletions gcloud-java-pubsub/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<artifactId>gcloud-java-pubsub</artifactId>
<packaging>jar</packaging>
<name>GCloud Java Pub/Sub</name>
<url>https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-pubsub</url>
<description>
Java idiomatic client for Google Cloud Pub/Sub.
</description>
Expand All @@ -16,6 +17,11 @@
<site.installationModule>gcloud-java-pubsub</site.installationModule>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gcloud-java-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax</artifactId>
Expand Down