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

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

Introduction

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

Prototype

public static JsonFactory getDefaultJsonFactory() 

Source Link

Document

Returns a cached default implementation of the JsonFactory interface.

Usage

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

License:Apache License

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

    GoogleCredential credential = null;/*from   w  ww. ja  v  a 2s.  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  www .ja v a2 s  .  c  o  m*/
 */
public static Pubsub getClient() throws IOException {
    return getClient(Utils.getDefaultTransport(), Utils.getDefaultJsonFactory());
}

From source file:com.google.cloud.genomics.dockerflow.util.StringUtils.java

License:Apache License

/** Deserialize from json. */
public static <T> T fromJson(String s, Class<T> c) throws IOException {
    FileUtils.LOG.debug("Deserializing from json to " + c);
    T retval;/* ww w .j  a  v  a 2 s.c o  m*/

    // For some reason, this only works for auto-generated Google API
    // classes
    if (c.toString().startsWith("com.google.api.services.")) {
        FileUtils.LOG.debug("Using Google APIs JsonParser");
        retval = Utils.getDefaultJsonFactory().createJsonParser(s).parse(c);
    } else {
        FileUtils.LOG.debug("Using Gson");
        retval = new GsonBuilder().setLenient().create().fromJson(s, c);
    }
    return retval;
}

From source file:com.google.cloud.genomics.gatk.common.grpc.GenomicsDataSource.java

License:Open Source License

private Channel initGenomicsChannel() throws FileNotFoundException, IOException, GeneralSecurityException {
    checkParamsForAuth(AUTH_REQUIREMENTS.CLIENT_SECRETS_ONLY);
    final GoogleClientSecrets secrets = GoogleClientSecrets.load(Utils.getDefaultJsonFactory(),
            new FileReader(clientSecretsFilename));
    final OfflineAuth auth = getFactory().getOfflineAuthFromClientSecretsFile(clientSecretsFilename);
    final UserCredentials userCredentials = new UserCredentials(secrets.getInstalled().getClientId(),
            secrets.getInstalled().getClientSecret(), auth.refreshToken);

    // Java 8's implementation of GCM ciphers is extremely slow. Therefore we disable
    // them here.
    List<String> defaultCiphers = GrpcSslContexts.forClient().ciphers(null).build().cipherSuites();
    List<String> performantCiphers = new ArrayList<>();
    for (String cipher : defaultCiphers) {
        if (!cipher.contains("GCM")) {
            performantCiphers.add(cipher);
        }/*from  w  w  w  .j a v  a2s .  c  o m*/
    }

    channelImpl = NettyChannelBuilder.forAddress("genomics.googleapis.com", 443)
            .negotiationType(NegotiationType.TLS).streamWindowSize(1000000)
            .sslContext(GrpcSslContexts.forClient().ciphers(performantCiphers).build()).build();
    /*userCredentials = userCredentials.createScoped(
        Arrays.asList("https://www.googleapis.com/auth/genomics"));*/
    ClientAuthInterceptor interceptor = new ClientAuthInterceptor(userCredentials,
            Executors.newSingleThreadExecutor());
    return ClientInterceptors.intercept(channelImpl, interceptor);
}

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.
 *//*  w ww. j a  v a 2s .  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  ww  . jav a 2s . 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./* w  ww . j  av  a2  s. 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.google.firebase.FirebaseApp.java

License:Apache License

private static FirebaseOptions getOptionsFromEnvironment() throws IOException {
    String defaultConfig = System.getenv(FIREBASE_CONFIG_ENV_VAR);
    if (Strings.isNullOrEmpty(defaultConfig)) {
        return new FirebaseOptions.Builder().setCredentials(GoogleCredentials.getApplicationDefault()).build();
    }//from w  w  w  .j  a va  2s  . c o m

    JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
    FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
    JsonParser parser;
    if (defaultConfig.startsWith("{")) {
        parser = jsonFactory.createJsonParser(defaultConfig);
    } else {
        FileReader reader;
        reader = new FileReader(defaultConfig);
        parser = jsonFactory.createJsonParser(reader);
    }
    parser.parseAndClose(builder);
    builder.setCredentials(GoogleCredentials.getApplicationDefault());
    return builder.build();
}

From source file:com.google.firebase.testing.IntegrationTestUtils.java

License:Apache License

private static synchronized GenericJson ensureServiceAccount() {
    if (serviceAccount == null) {
        try (InputStream stream = new FileInputStream(IT_SERVICE_ACCOUNT_PATH)) {
            serviceAccount = Utils.getDefaultJsonFactory().fromInputStream(stream, GenericJson.class);
        } catch (IOException e) {
            String msg = String.format("Failed to read service account certificate from %s. "
                    + "Integration tests require a service account credential obtained from a Firebase "
                    + "project. See CONTRIBUTING.md for more details.", IT_SERVICE_ACCOUNT_PATH);
            throw new RuntimeException(msg, e);
        }//  www  .  j  a v  a 2 s .c o  m
    }
    return serviceAccount;
}