Example usage for com.google.api.client.googleapis.util Utils getDefaultTransport

List of usage examples for com.google.api.client.googleapis.util Utils getDefaultTransport

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.util Utils getDefaultTransport.

Prototype

public static HttpTransport getDefaultTransport() 

Source Link

Document

Returns a cached default implementation of the HttpTransport interface.

Usage

From source file:com.blackberry.bdp.klogger.LogReader.java

License:Apache License

@Override
public void run() {
    UTF8Validator utf8Validator = null;

    GoogleCredential credential = null;/* w  ww.  j a  v a 2 s  .c  o m*/

    // Use credentials from file if available
    try {
        credential = GoogleCredential.fromStream(new FileInputStream("credentials.json"))
                .createScoped(PubsubScopes.all());
    } catch (IOException e) {
        try {
            credential = GoogleCredential.getApplicationDefault().createScoped(PubsubScopes.all());
        } catch (Exception e1) {

        }

    }

    final Pubsub client = new Pubsub.Builder(Utils.getDefaultTransport(), Utils.getDefaultJsonFactory(),
            credential).setApplicationName("vstoyak-kloger-test").build();

    if (validateUTF8) {
        utf8Validator = new UTF8Validator();
    }

    // Calculate send buffer size. If we're validating UTF-8, then theoretically, each byte could be
    // replaced by the three byte replacement character. So the send buffer needs to be triple
    // the max line length.  If we're encoding the timestamp, then that adds 10 bytes.
    byte[] sendBytes;
    {
        int sendBufferSize = maxLine;

        if (validateUTF8) {
            sendBufferSize *= 3;
        }
        if (encodeTimestamp) {
            sendBufferSize += 10;
        }

        sendBytes = new byte[sendBufferSize];
    }

    ByteBuffer sendBuffer = ByteBuffer.wrap(sendBytes);

    try {
        prepareSource();

        while (!finished) {
            bytesRead = readSource();

            if (bytesRead > 0) {
                LOG.trace("Read {} bytes", bytesRead);
            } else {
                if (bytesRead == 0) {
                    // File's return 0 when they have reached the end of the FileChannel
                    continue;
                } else {
                    if (bytesRead == -1) {
                        LOG.warn("Received -1 while reading from source, finishing up...");
                        finished = true;
                        continue;
                    }
                }
            }

            mBytesReceived.mark(bytesRead);
            mBytesReceivedTotal.mark(bytesRead);

            limit = start + bytesRead;
            start = 0;

            while (true) {
                newline = -1;

                for (int i = start; i < limit; i++) {
                    if (bytes[i] == '\n') {
                        totalLinesRead++;
                        newline = i;
                        break;
                    }
                }

                LOG.trace("Newline at {}", newline);

                if (newline >= 0) {
                    mLinesReceived.mark();
                    mLinesReceivedTotal.mark();

                    LOG.trace("Sending (pos {}, len {}):{}", start, newline - start,
                            new String(bytes, start, newline - start, "UTF-8"));

                    sendBuffer.clear();

                    if (encodeTimestamp) {
                        sendBuffer.put(new byte[] { (byte) 0xFE, 0x00 });

                        sendBuffer.putLong(System.currentTimeMillis());
                    }

                    if (validateUTF8) {
                        utf8Validator.validate(bytes, start, newline - start);
                        sendBuffer.put(utf8Validator.getResultBytes(), 0,
                                utf8Validator.getResultBuffer().limit());
                    } else {
                        sendBuffer.put(bytes, start, newline - start);
                    }

                    String message = "test Message 1";
                    if (!"".equals(message)) {
                        String fullTopicName = String.format("projects/%s/topics/%s", "bbm-staging",
                                "vstoyak-kloger-test");
                        PubsubMessage pubsubMessage = new PubsubMessage();
                        //pubsubMessage.encodeData(message.getBytes("UTF-8"));
                        pubsubMessage.encodeData(sendBytes);
                        PublishRequest publishRequest = new PublishRequest();
                        publishRequest.setMessages(ImmutableList.of(pubsubMessage));

                        try {
                            client.projects().topics().publish(fullTopicName, publishRequest).execute();
                        } catch (Exception e) {
                            LOG.info("PubSub exception", e);
                        }
                    }
                    //producer.send(sendBytes, 0, sendBuffer.position());

                    start = newline + 1;
                    continue;

                } else {
                    LOG.trace("No newline.  start={}, limit={}", start, limit);

                    // if the buffer is full, send it all. Otherwise, do nothing.
                    if (start == 0 && limit == maxLine) {
                        mLinesReceived.mark();
                        mLinesReceivedTotal.mark();

                        LOG.trace("Sending log with no new-line:{}", new String(bytes, 0, maxLine, "UTF-8"));

                        sendBuffer.clear();

                        if (encodeTimestamp) {
                            sendBuffer.put(new byte[] { (byte) 0xFE, 0x00 });
                            sendBuffer.putLong(System.currentTimeMillis());
                        }

                        if (validateUTF8) {
                            utf8Validator.validate(bytes, 0, maxLine);
                            sendBuffer.put(utf8Validator.getResultBytes(), 0,
                                    utf8Validator.getResultBuffer().limit());
                        } else {
                            sendBuffer.put(bytes, 0, maxLine);
                        }

                        String message = "test Message 2";
                        if (!"".equals(message)) {
                            String fullTopicName = String.format("projects/%s/topics/%s", "bbm-staging",
                                    "vstoyak-kloger-test");
                            PubsubMessage pubsubMessage = new PubsubMessage();
                            //pubsubMessage.encodeData(message.getBytes("UTF-8"));
                            pubsubMessage.encodeData(sendBytes);
                            PublishRequest publishRequest = new PublishRequest();
                            publishRequest.setMessages(ImmutableList.of(pubsubMessage));

                            try {
                                client.projects().topics().publish(fullTopicName, publishRequest).execute();
                            } catch (Exception e) {
                                LOG.info("PubSub exception", e);
                            }
                        }

                        //producer.send(sendBytes, 0, sendBuffer.position());

                        start = 0;
                        break;

                    } // if there is still data, then shift it to the start
                    else {
                        if (start > 0 && start < limit) {
                            int toMove = limit - start;
                            int moveSize;
                            int done = 0;

                            while (done < toMove) {
                                moveSize = Math.min(start - done, limit - start);

                                System.arraycopy(bytes, start, bytes, done, moveSize);

                                done += moveSize;
                                start += moveSize;
                            }

                            start = toMove;
                            break;
                        } else {
                            if (start >= limit) {
                                LOG.trace("All the data has been read");
                                start = 0;
                                break;
                            } else {
                                // start == 0, so move it to the limit for the next pass.
                                start = limit;
                                break;
                            }
                        }
                    }
                }
            }

        }
    } catch (Throwable t) {
        LOG.error("An error has occured: {}", t);
    } finally {
        finished();
    }

    LOG.info("Reading source for topic {} is finished", conf.getTopicName());
}

From source file:com.dataartisans.flink.dataflow.util.PortableConfiguration.java

License:Apache License

public static Pubsub createPubsubClient() throws IOException {
    return createPubsubClient(Utils.getDefaultTransport(), Utils.getDefaultJsonFactory());
}

From source file:com.google.cloud.dataflow.examples.complete.game.injector.InjectorUtils.java

License:Apache License

/**
 * Builds a new Pubsub client with default HttpTransport and
 * JsonFactory and returns it.//from   w  w w .j  a v  a 2s . c  om
 */
public static Pubsub getClient() throws IOException {
    return getClient(Utils.getDefaultTransport(), Utils.getDefaultJsonFactory());
}

From source file:com.google.cloud.pubsub.proxy.gcloud.GcloudPubsub.java

License:Open Source License

/**
 * Constructor that will automatically instantiate a Google Cloud Pub/Sub instance.
 *
 * @throws IOException when the initialization of the Google Cloud Pub/Sub client fails.
 *//*from  www  .jav  a  2  s.  c  o m*/
public GcloudPubsub() throws IOException {
    if (CLOUD_PUBSUB_PROJECT_ID == null) {
        throw new IllegalStateException(GCLOUD_PUBSUB_PROJECT_ID_NOT_SET_ERROR);
    }
    try {
        serverName = InetAddress.getLocalHost().getCanonicalHostName();
    } catch (UnknownHostException e) {
        throw new IllegalStateException("Unable to retrieve the hostname of the system");
    }
    HttpTransport httpTransport = checkNotNull(Utils.getDefaultTransport());
    JsonFactory jsonFactory = checkNotNull(Utils.getDefaultJsonFactory());
    GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory);
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(PubsubScopes.all());
    }
    HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential);
    pubsub = new Pubsub.Builder(httpTransport, jsonFactory, initializer).build();
    logger.info("Google Cloud Pub/Sub Initialization SUCCESS");
}

From source file:com.google.cloud.sparkdemo.CloudPubsubReceiver.java

License:Apache License

private Pubsub createAuthorizedClient() {
    try {/*w w  w.j a  va 2 s.  c  o m*/
        // Create the credential
        HttpTransport httpTransport = Utils.getDefaultTransport();
        JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
        GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory);

        if (credential.createScopedRequired()) {
            credential = credential.createScoped(PubsubScopes.all());
        }
        HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential);
        return new Pubsub.Builder(httpTransport, jsonFactory, initializer)
                .setApplicationName("spark-pubsub-receiver").build();
    } catch (IOException e) {
        reportError("Unable to create Cloud Pub/sub client.", e);
        return null;
    }
}

From source file:com.google.cloud.storage.storagetransfer.samples.TransferClientCreator.java

License:Open Source License

/**
 * Create a Storage Transfer client using application default credentials and other default
 * settings./*from w w w.  j a v  a2s  . c  o  m*/
 *
 * @return a Storage Transfer client
 * @throws IOException
 *           there was an error obtaining application default credentials
 */
public static Storagetransfer createStorageTransferClient() throws IOException {
    HttpTransport httpTransport = Utils.getDefaultTransport();
    JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
    GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory);
    return createStorageTransferClient(httpTransport, jsonFactory, credential);
}

From source file:com.spotify.google.cloud.pubsub.client.Pubsub.java

License:Apache License

private static Credential defaultCredential() {
    try {/*from   www.ja  v  a  2s  . c om*/
        return GoogleCredential.getApplicationDefault(Utils.getDefaultTransport(),
                Utils.getDefaultJsonFactory());
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.spotify.styx.client.auth.GoogleIdTokenAuth.java

License:Apache License

public static GoogleIdTokenAuth of(Optional<GoogleCredential> credential) {
    return of(Utils.getDefaultTransport(), credential);
}

From source file:com.spotify.styx.client.auth.GoogleIdTokenAuth.java

License:Apache License

public static GoogleIdTokenAuth of(GoogleCredential credential) {
    return of(Utils.getDefaultTransport(), Optional.of(credential));
}