com.google.api.client.util.Charsets Java Examples

The following examples show how to use com.google.api.client.util.Charsets. 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: SeriesKeyEncoding.java    From heroic with Apache License 2.0 6 votes vote down vote up
public SeriesKey decode(ByteString key, Transform<String, Series> transform) throws Exception {
    final String string = key.toString(Charsets.UTF_8);
    final List<String> parts = SPLITTER.splitToList(string);

    if (parts.size() != 3) {
        throw new IllegalArgumentException("Not a valid key: " + string);
    }

    final String category = parts.get(0);

    if (!this.category.equals(category)) {
        throw new IllegalArgumentException(
            "Key is in the wrong category (expected " + this.category + "): " + string);
    }

    final LocalDate date = LocalDate.parse(parts.get(1));
    final Series series = transform.transform(parts.get(2));
    return new SeriesKey(date, series);
}
 
Example #2
Source File: AddCanonical.java    From akarnokd-misc with Apache License 2.0 6 votes vote down vote up
static void appendLink(Path file, String url) throws IOException {
    byte[] data = Files.readAllBytes(file);
    
    byte[] alreadyThere = "<link rel=\"canonical\"".getBytes(Charsets.ISO_8859_1);
    
    if (arrayIndexOf(data, alreadyThere, 0) != -1) {
        System.out.printf("File %s has a link-rel-canonical already.%n", file);
    } else {
        byte[] endHead = "</head>".getBytes(Charsets.ISO_8859_1);
        int endHeadIndex = arrayIndexOf(data, endHead, 0);
        if (endHeadIndex < 0) {
            System.out.printf("File %s has no </head>?!%n", file);
        } else {
            System.out.printf("Adding link %s to file %s.%n", url, file);
            byte[] toInsert = ("<link rel=\"canonical\" href=\"" + url + "\"/>").getBytes(Charsets.ISO_8859_1);
            
            byte[] newData = new byte[data.length + toInsert.length];
            System.arraycopy(data, 0, newData, 0, endHeadIndex);
            System.arraycopy(toInsert, 0, newData, endHeadIndex, toInsert.length);
            System.arraycopy(data, endHeadIndex, newData, endHeadIndex + toInsert.length, data.length - endHeadIndex);
            Files.write(file, newData);
        }
    }
}
 
Example #3
Source File: RemoteRpcTest.java    From google-cloud-datastore with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptyProtoExceptionUnauthenticated() {
  Status statusProto = Status.newBuilder().build();
  DatastoreException exception =
      RemoteRpc.makeException(
          "url",
          METHOD_NAME,
          new ByteArrayInputStream(statusProto.toByteArray()),
          "application/x-protobuf",
          Charsets.UTF_8,
          new RuntimeException(),
          401);
  assertEquals(Code.UNAUTHENTICATED, exception.getCode());
  assertEquals("Unauthenticated.", exception.getMessage());
  assertEquals(METHOD_NAME, exception.getMethodName());
}
 
Example #4
Source File: SeriesKeyFilterEncodingTest.java    From heroic with Apache License 2.0 6 votes vote down vote up
@Test
public void testKeyEncoding() throws Exception {
    doReturn("series").when(toString).transform(series);

    final ByteString bytes =
        foo.encode(new SeriesKey(date, series), toString);

    assertEquals(ByteString.copyFrom("foo/2016-01-31/series", Charsets.UTF_8), bytes);

    doReturn(series).when(fromString).transform("series");
    final SeriesKey k = foo.decode(bytes, fromString);

    assertEquals(date, k.getDate());
    assertEquals(series, k.getSeries());

    assertEquals(ByteString.copyFrom("foo/2016-02-01", Charsets.UTF_8),
        foo.rangeKey(date.plusDays(1)));
}
 
Example #5
Source File: RemoteRpcTest.java    From google-cloud-datastore with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptyProtoException() {
  Status statusProto = Status.newBuilder().build();
  DatastoreException exception =
      RemoteRpc.makeException(
          "url",
          METHOD_NAME,
          new ByteArrayInputStream(statusProto.toByteArray()),
          "application/x-protobuf",
          Charsets.UTF_8,
          new RuntimeException(),
          404);
  assertEquals(Code.INTERNAL, exception.getCode());
  assertEquals(
      "Unexpected OK error code with HTTP status code of 404. Message: .",
      exception.getMessage());
  assertEquals(METHOD_NAME, exception.getMethodName());
}
 
Example #6
Source File: MultipartContentTest.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
private void subtestContent(String expectedContent, String boundaryString, String... contents)
    throws Exception {
  // multipart content
  MultipartContent content =
      new MultipartContent(boundaryString == null ? BOUNDARY : boundaryString);
  for (String contentValue : contents) {
    content.addPart(
        new MultipartContent.Part(ByteArrayContent.fromString(CONTENT_TYPE, contentValue)));
  }
  if (boundaryString != null) {
    content.setBoundary(boundaryString);
  }
  // write to string
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  content.writeTo(out);
  assertEquals(expectedContent, out.toString(Charsets.UTF_8.name()));
  assertEquals(StringUtils.getBytesUtf8(expectedContent).length, content.getLength());
  assertEquals(
      boundaryString == null
          ? "multipart/related; boundary=" + BOUNDARY
          : "multipart/related; boundary=" + boundaryString,
      content.getType());
}
 
Example #7
Source File: MockLowLevelHttpRequest.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * Returns HTTP content as a string, taking care of any encodings of the content if necessary.
 *
 * <p>Returns an empty string if there is no HTTP content.
 *
 * @since 1.12
 */
public String getContentAsString() throws IOException {
  if (getStreamingContent() == null) {
    return "";
  }
  // write content to a byte[]
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  getStreamingContent().writeTo(out);
  // determine gzip encoding
  String contentEncoding = getContentEncoding();
  if (contentEncoding != null && contentEncoding.contains("gzip")) {
    InputStream contentInputStream =
        new GZIPInputStream(new ByteArrayInputStream(out.toByteArray()));
    out = new ByteArrayOutputStream();
    IOUtils.copy(contentInputStream, out);
  }
  // determine charset parameter from content type
  String contentType = getContentType();
  HttpMediaType mediaType = contentType != null ? new HttpMediaType(contentType) : null;
  Charset charset =
      mediaType == null || mediaType.getCharsetParameter() == null
          ? Charsets.ISO_8859_1
          : mediaType.getCharsetParameter();
  return out.toString(charset.name());
}
 
Example #8
Source File: RemoteRpcTest.java    From google-cloud-datastore with Apache License 2.0 5 votes vote down vote up
@Test
public void testPlainTextException() {
  DatastoreException exception = RemoteRpc.makeException("url", METHOD_NAME,
      new ByteArrayInputStream("Text Error".getBytes()), "text/plain", Charsets.UTF_8,
      new RuntimeException(), 401);
  assertEquals(Code.INTERNAL, exception.getCode());
  assertEquals(
      "Non-protobuf error: Text Error. HTTP status code was 401.", exception.getMessage());
  assertEquals(METHOD_NAME, exception.getMethodName());
}
 
Example #9
Source File: RemoteRpcTest.java    From google-cloud-datastore with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidProtoException() {
  DatastoreException exception = RemoteRpc.makeException("url", METHOD_NAME,
      new ByteArrayInputStream("<invalid proto>".getBytes()), "application/x-protobuf",
      Charsets.UTF_8, new RuntimeException(), 401);
  assertEquals(Code.INTERNAL, exception.getCode());
  assertEquals("Unable to parse Status protocol buffer: HTTP status code was 401.",
      exception.getMessage());
  assertEquals(METHOD_NAME, exception.getMethodName());
}
 
Example #10
Source File: RemoteRpcTest.java    From google-cloud-datastore with Apache License 2.0 5 votes vote down vote up
@Test
public void testException() {
  Status statusProto =
      Status.newBuilder()
          .setCode(Code.UNAUTHENTICATED_VALUE)
          .setMessage("The request does not have valid authentication credentials.")
          .build();
  DatastoreException exception = RemoteRpc.makeException("url", METHOD_NAME,
      new ByteArrayInputStream(statusProto.toByteArray()), "application/x-protobuf",
      Charsets.UTF_8, new RuntimeException(), 401);
  assertEquals(Code.UNAUTHENTICATED, exception.getCode());
  assertEquals("The request does not have valid authentication credentials.",
      exception.getMessage());
  assertEquals(METHOD_NAME, exception.getMethodName());
}
 
Example #11
Source File: FileCredentialStore.java    From android-oauth-client with Apache License 2.0 5 votes vote down vote up
private void save() throws IOException {
  FileOutputStream fos = new FileOutputStream(file);
  try {
    JsonGenerator generator = jsonFactory.createJsonGenerator(fos, Charsets.UTF_8);
    generator.serialize(credentials);
    generator.close();
  } finally {
    fos.close();
  }
}
 
Example #12
Source File: ITestHandleHttpRequest.java    From nifi with Apache License 2.0 5 votes vote down vote up
private File createTextFile(String fileName, String... lines) throws IOException {
    File file = new File(fileName);
    file.deleteOnExit();
    for (String string : lines) {
        Files.append(string, file, Charsets.UTF_8);
    }
    return file;
}
 
Example #13
Source File: FileCredentialStore.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
private void save() throws IOException {
  FileOutputStream fos = new FileOutputStream(file);
  try {
    JsonGenerator generator = jsonFactory.createJsonGenerator(fos, Charsets.UTF_8);
    generator.serialize(credentials);
    generator.close();
  } finally {
    fos.close();
  }
}
 
Example #14
Source File: EmbeddedGobblinDistcpTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithVersionPreserve() throws Exception {
  String fileName = "file";

  File tmpSource = Files.createTempDir();
  tmpSource.deleteOnExit();
  File tmpTarget = Files.createTempDir();
  tmpTarget.deleteOnExit();

  File tmpFile = new File(tmpSource, fileName);
  tmpFile.createNewFile();

  FileOutputStream os = new FileOutputStream(tmpFile);
  for (int i = 0; i < 100; i++) {
    os.write("myString".getBytes(Charsets.UTF_8));
  }
  os.close();

  MyDataFileVersion versionStrategy = new MyDataFileVersion();
  versionStrategy.setVersion(new Path(tmpFile.getAbsolutePath()), 123L);

  Assert.assertTrue(new File(tmpSource, fileName).exists());
  Assert.assertFalse(new File(tmpTarget, fileName).exists());

  EmbeddedGobblinDistcp embedded = new EmbeddedGobblinDistcp(new Path(tmpSource.getAbsolutePath()),
      new Path(tmpTarget.getAbsolutePath()));
  embedded.setLaunchTimeout(30, TimeUnit.SECONDS);
  embedded.setConfiguration(DataFileVersionStrategy.DATA_FILE_VERSION_STRATEGY_KEY, MyDataFileVersion.class.getName());
  embedded.setConfiguration(CopyConfiguration.PRESERVE_ATTRIBUTES_KEY, "v");
  embedded.run();

  Assert.assertTrue(new File(tmpSource, fileName).exists());
  Assert.assertTrue(new File(tmpTarget, fileName).exists());
  Assert.assertEquals((long) versionStrategy.getVersion(new Path(tmpTarget.getAbsolutePath(), fileName)), 123l);
}
 
Example #15
Source File: EmbeddedGobblinDistcpTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
  String fileName = "file";

  File tmpSource = Files.createTempDir();
  tmpSource.deleteOnExit();
  File tmpTarget = Files.createTempDir();
  tmpTarget.deleteOnExit();

  File tmpFile = new File(tmpSource, fileName);
  tmpFile.createNewFile();

  FileOutputStream os = new FileOutputStream(tmpFile);
  for (int i = 0; i < 100; i++) {
    os.write("myString".getBytes(Charsets.UTF_8));
  }
  os.close();

  Assert.assertTrue(new File(tmpSource, fileName).exists());
  Assert.assertFalse(new File(tmpTarget, fileName).exists());

  EmbeddedGobblinDistcp embedded = new EmbeddedGobblinDistcp(new Path(tmpSource.getAbsolutePath()),
      new Path(tmpTarget.getAbsolutePath()));
  embedded.setLaunchTimeout(30, TimeUnit.SECONDS);
  embedded.run();

  Assert.assertTrue(new File(tmpSource, fileName).exists());
  Assert.assertTrue(new File(tmpTarget, fileName).exists());
}
 
Example #16
Source File: MultipartContentTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testRandomContent() throws Exception {
  MultipartContent content = new MultipartContent();
  String boundaryString = content.getBoundary();
  assertNotNull(boundaryString);
  assertTrue(boundaryString.startsWith(BOUNDARY));
  assertTrue(boundaryString.endsWith("__"));
  assertEquals("multipart/related; boundary=" + boundaryString, content.getType());

  final String[][] VALUES =
      new String[][] {
        {"Hello world", "text/plain"},
        {"<xml>Hi</xml>", "application/xml"},
        {"{x:1,y:2}", "application/json"}
      };
  StringBuilder expectedStringBuilder = new StringBuilder();
  for (String[] valueTypePair : VALUES) {
    String contentValue = valueTypePair[0];
    String contentType = valueTypePair[1];
    content.addPart(
        new MultipartContent.Part(ByteArrayContent.fromString(contentType, contentValue)));
    expectedStringBuilder
        .append("--")
        .append(boundaryString)
        .append(CRLF)
        .append(headers(contentType, contentValue))
        .append(CRLF)
        .append(contentValue)
        .append(CRLF);
  }
  expectedStringBuilder.append("--").append(boundaryString).append("--").append(CRLF);
  // write to string
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  content.writeTo(out);
  String expectedContent = expectedStringBuilder.toString();
  assertEquals(expectedContent, out.toString(Charsets.UTF_8.name()));
  assertEquals(StringUtils.getBytesUtf8(expectedContent).length, content.getLength());
}
 
Example #17
Source File: JsonFactory.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a UTF-8 byte array output stream of the serialized JSON representation for the given
 * item using {@link JsonGenerator#serialize(Object)}.
 *
 * @param item data key/value pairs
 * @param pretty whether to return a pretty representation
 * @return serialized JSON string representation
 */
private ByteArrayOutputStream toByteStream(Object item, boolean pretty) throws IOException {
  ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
  JsonGenerator generator = createJsonGenerator(byteStream, Charsets.UTF_8);
  if (pretty) {
    generator.enablePrettyPrint();
  }
  generator.serialize(item);
  generator.flush();
  return byteStream;
}
 
Example #18
Source File: AbstractHttpContent.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the charset specified in the media type or {@code Charsets#UTF_8} if not specified.
 *
 * @since 1.10
 */
protected final Charset getCharset() {
  return mediaType == null || mediaType.getCharsetParameter() == null
      ? Charsets.ISO_8859_1
      : mediaType.getCharsetParameter();
}
 
Example #19
Source File: GsonFactory.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonParser createJsonParser(InputStream in) {
  // TODO(mlinder): Parser should try to detect the charset automatically when using GSON
  // https://github.com/googleapis/google-http-java-client/issues/6
  return createJsonParser(new InputStreamReader(in, Charsets.UTF_8));
}
 
Example #20
Source File: PinotAuditCountHttpClient.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
/**
 * A thread-safe method which fetches a tier-to-count mapping.
 * The returned json object from Pinot contains below information
 * {
 *    "aggregationResults":[
 *      {
 *          "groupByResult":[
 *            {
 *              "value":"172765137.00000",
 *              "group":[
 *                "kafka-08-tracking-local"
 *              ]
 *            }
 *          ]
 *      }
 *    ],
 *    "exceptions":[
 *    ]
 *    .....
 * }
 * @param datasetName name of dataset
 * @param start time start point in milliseconds
 * @param end time end point in milliseconds
 * @return A tier to record count mapping when succeeded. Otherwise a null value is returned
 */
public Map<String, Long> fetch (String datasetName, long start, long end)  throws IOException {
  Map<String, Long> map = new HashMap<>();
  String query = "select tier, sum(count) from kafkaAudit where " +
          "eventType=\""            + datasetName   + "\" and " +
          "beginTimestamp >= \""    + start         + "\" and " +
          "beginTimestamp < \""     + end           + "\" group by tier";

  String fullURL = targetUrl + URLEncoder.encode (query, Charsets.UTF_8.toString());
  HttpGet req = new HttpGet(fullURL);
  String rst = null;
  HttpEntity entity = null;
  log.info ("Full url for {} is {}", datasetName, fullURL);

  try {
    CloseableHttpResponse response = httpClient.execute(req, HttpClientContext.create());
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode < 200 || statusCode >= 300) {
      throw new IOException(
              String.format("status code: %d, reason: %s", statusCode, response.getStatusLine().getReasonPhrase()));
    }

    entity = response.getEntity();
    rst = EntityUtils.toString(entity);
  } finally {
    if (entity != null) {
      EntityUtils.consume(entity);
    }
  }

  JsonObject all = PARSER.parse(rst).getAsJsonObject();
  JsonArray aggregationResults = all.getAsJsonArray("aggregationResults");
  if (aggregationResults == null || aggregationResults.size() == 0) {
    log.error (all.toString());
    throw new IOException("No aggregation results " + all.toString());
  }

  JsonObject aggregation = (JsonObject) aggregationResults.get(0);
  JsonArray groupByResult = aggregation.getAsJsonArray("groupByResult");
  if (groupByResult == null || groupByResult.size() == 0) {
    log.error (aggregation.toString());
    throw new IOException("No aggregation results " + aggregation.toString());
  }

  log.info ("Audit count for {} is {}", datasetName, groupByResult);
  for (JsonElement ele : groupByResult){
    JsonObject record = (JsonObject) ele;
    map.put(record.getAsJsonArray("group").get(0).getAsString(), (long) Double.parseDouble(record.get("value").getAsString()));
  }

  return map;
}
 
Example #21
Source File: AtomTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * Read an XML ATOM Feed from a file to a string and assert if all the {@link FeedEntry}s are
 * present. No detailed assertion of each element
 *
 * <p>The purpose of this test is to read a bunch of elements which contain additional elements
 * (HTML in this case), that are not part of the {@link FeedEntry} and to see if there is an issue
 * if we parse some more entries.
 */
@Test
public void testSampleFeedParser() throws Exception {
  XmlPullParser parser = Xml.createParser();
  URL url = Resources.getResource("sample-atom.xml");
  String read = Resources.toString(url, Charsets.UTF_8);
  parser.setInput(new StringReader(read));
  XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
  AbstractAtomFeedParser atomParser =
      new AtomFeedParser<Feed, FeedEntry>(
          namespaceDictionary,
          parser,
          new ByteArrayInputStream(read.getBytes()),
          Feed.class,
          FeedEntry.class);
  Feed feed = (Feed) atomParser.parseFeed();
  assertNotNull(feed);

  // validate feed 1 -- Long Content
  FeedEntry entry = (FeedEntry) atomParser.parseNextEntry();
  assertNotNull(entry);
  assertNotNull(entry.id);
  assertNotNull(entry.title);
  assertNotNull(entry.summary);
  assertNotNull(entry.link);
  assertNotNull(entry.updated);
  assertNotNull(entry.content);
  assertEquals(5000, entry.content.length());

  // validate feed 2 -- Special Charts
  entry = (FeedEntry) atomParser.parseNextEntry();
  assertNotNull(entry);
  assertNotNull(entry.id);
  assertNotNull(entry.title);
  assertNotNull(entry.summary);
  assertNotNull(entry.link);
  assertNotNull(entry.updated);
  assertNotNull(entry.content);
  assertEquals(
      "aäb cde fgh ijk lmn oöpoöp tuü vwx yz AÄBC DEF GHI JKL MNO ÖPQ RST UÜV WXYZ "
          + "!\"§ $%& /() =?* '<> #|; ²³~ @`´ ©«» ¼× {} aäb cde fgh ijk lmn oöp qrsß tuü vwx yz "
          + "AÄBC DEF GHI JKL MNO",
      entry.content);

  // validate feed 3 -- Missing Content
  entry = (FeedEntry) atomParser.parseNextEntry();
  assertNotNull(entry);
  assertNotNull(entry.id);
  assertNotNull(entry.title);
  assertNotNull(entry.summary);
  assertNotNull(entry.link);
  assertNotNull(entry.updated);
  assertNull(entry.content);

  // validate feed 4 -- Missing Updated
  entry = (FeedEntry) atomParser.parseNextEntry();
  assertNotNull(entry);
  assertNotNull(entry.id);
  assertNotNull(entry.title);
  assertNotNull(entry.summary);
  assertNotNull(entry.link);
  assertNull(entry.updated);
  assertNotNull(entry.content);

  // validate feed 5
  entry = (FeedEntry) atomParser.parseNextEntry();
  assertNotNull(entry);
  assertNotNull(entry.id);
  assertNotNull(entry.title);
  assertNull(entry.summary);
  assertNotNull(entry.link);
  assertNotNull(entry.updated);
  assertNotNull(entry.content);

  // validate feed 6
  entry = (FeedEntry) atomParser.parseNextEntry();
  assertNull(entry);

  atomParser.close();
}
 
Example #22
Source File: AndroidJsonFactory.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonParser createJsonParser(InputStream in) {
  // TODO(mlinder): Charset should be detected automatically by the parser. Related to:
  // http://github.com/googleapis/google-http-java-client/issues/6
  return createJsonParser(new InputStreamReader(in, Charsets.UTF_8));
}
 
Example #23
Source File: TestMessageProcessorImpl.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Test
public void testProcessMessages() throws Exception {
  PushSource.Context context = mock(PushSource.Context.class);
  BatchContext batchContext = mock(BatchContext.class);
  BatchMaker batchMaker = mock(BatchMaker.class);

  when(context.isPreview()).thenReturn(isPreview);
  when(context.createRecord(anyString())).thenReturn(RecordCreator.create());
  when(context.startBatch()).thenReturn(batchContext);
  when(batchContext.getBatchMaker()).thenReturn(batchMaker);

  int batchSize = 1;
  long maxWaitTime = 100;
  DataParserFactory parserFactory = new DataParserFactoryBuilder(
      context,
      DataParserFormat.TEXT
  ).setCharset(Charsets.UTF_8).setMaxDataLen(1000).build();
  SynchronousQueue<MessageReplyConsumerBundle> queue = new SynchronousQueue<>();

  MessageProcessor processor = new MessageProcessorImpl(
      context,
      batchSize,
      maxWaitTime,
      parserFactory,
      queue,
      Clock.fixed(Instant.now(), ZoneId.of("UTC"))
  );

  ExecutorService executor = Executors.newSingleThreadExecutor();
  Future future = executor.submit(processor);
  PubsubMessage message = PubsubMessage.newBuilder()
      .setMessageId("1")
      .setData(ByteString.copyFrom("Hello, World!", Charsets.UTF_8))
      .putAttributes("attribute1", "attributevalue")
      .setPublishTime(Timestamp.getDefaultInstance())
      .build();

  AckReplyConsumer consumer = mock(AckReplyConsumer.class);

  queue.offer(new MessageReplyConsumerBundle(message, consumer), 1000, TimeUnit.MILLISECONDS);

  ThreadUtil.sleep(50);
  processor.stop();
  future.get();
  executor.shutdownNow();

  if (isPreview) {
    verify(consumer, times(1)).nack();
  } else {
    verify(consumer, times(1)).ack();
  }
}
 
Example #24
Source File: DeviceRegistryExample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
/** Patch the device to add an RSA256 key for authentication. */
protected static void patchRsa256ForAuth(
    String deviceId,
    String publicKeyFilePath,
    String projectId,
    String cloudRegion,
    String registryName)
    throws GeneralSecurityException, IOException {
  GoogleCredentials credential =
      GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
  JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
  HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
  final CloudIot service =
      new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init)
          .setApplicationName(APP_NAME)
          .build();

  final String devicePath =
      String.format(
          "projects/%s/locations/%s/registries/%s/devices/%s",
          projectId, cloudRegion, registryName, deviceId);

  PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
  String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);
  publicKeyCredential.setKey(key);
  publicKeyCredential.setFormat("RSA_X509_PEM");

  DeviceCredential devCredential = new DeviceCredential();
  devCredential.setPublicKey(publicKeyCredential);

  Device device = new Device();
  device.setCredentials(Collections.singletonList(devCredential));

  Device patchedDevice =
      service
          .projects()
          .locations()
          .registries()
          .devices()
          .patch(devicePath, device)
          .setUpdateMask("credentials")
          .execute();

  System.out.println("Patched device is " + patchedDevice.toPrettyString());
}
 
Example #25
Source File: DeviceRegistryExample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
/** Patch the device to add an ES256 key for authentication. */
protected static void patchEs256ForAuth(
    String deviceId,
    String publicKeyFilePath,
    String projectId,
    String cloudRegion,
    String registryName)
    throws GeneralSecurityException, IOException {
  GoogleCredentials credential =
      GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
  JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
  HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
  final CloudIot service =
      new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init)
          .setApplicationName(APP_NAME)
          .build();

  final String devicePath =
      String.format(
          "projects/%s/locations/%s/registries/%s/devices/%s",
          projectId, cloudRegion, registryName, deviceId);

  PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
  String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);
  publicKeyCredential.setKey(key);
  publicKeyCredential.setFormat("ES256_PEM");

  DeviceCredential devCredential = new DeviceCredential();
  devCredential.setPublicKey(publicKeyCredential);

  Device device = new Device();
  device.setCredentials(Collections.singletonList(devCredential));

  Device patchedDevice =
      service
          .projects()
          .locations()
          .registries()
          .devices()
          .patch(devicePath, device)
          .setUpdateMask("credentials")
          .execute();

  System.out.println("Patched device is " + patchedDevice.toPrettyString());
}
 
Example #26
Source File: DeviceRegistryExample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
/** Create a device that is authenticated using RS256. */
protected static void createDeviceWithRs256(
    String deviceId,
    String certificateFilePath,
    String projectId,
    String cloudRegion,
    String registryName)
    throws GeneralSecurityException, IOException {
  GoogleCredentials credential =
      GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
  JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
  HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
  final CloudIot service =
      new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init)
          .setApplicationName(APP_NAME)
          .build();

  final String registryPath =
      String.format(
          "projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);

  PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
  String key = Files.toString(new File(certificateFilePath), Charsets.UTF_8);
  publicKeyCredential.setKey(key);
  publicKeyCredential.setFormat("RSA_X509_PEM");

  DeviceCredential devCredential = new DeviceCredential();
  devCredential.setPublicKey(publicKeyCredential);

  System.out.println("Creating device with id: " + deviceId);
  Device device = new Device();
  device.setId(deviceId);
  device.setCredentials(Collections.singletonList(devCredential));
  Device createdDevice =
      service
          .projects()
          .locations()
          .registries()
          .devices()
          .create(registryPath, device)
          .execute();

  System.out.println("Created device: " + createdDevice.toPrettyString());
}
 
Example #27
Source File: DeviceRegistryExample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
/** Create a device that is authenticated using ES256. */
protected static void createDeviceWithEs256(
    String deviceId,
    String publicKeyFilePath,
    String projectId,
    String cloudRegion,
    String registryName)
    throws GeneralSecurityException, IOException {
  GoogleCredentials credential =
      GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
  JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
  HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
  final CloudIot service =
      new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init)
          .setApplicationName(APP_NAME)
          .build();

  final String registryPath =
      String.format(
          "projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);

  PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
  final String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);
  publicKeyCredential.setKey(key);
  publicKeyCredential.setFormat("ES256_PEM");

  DeviceCredential devCredential = new DeviceCredential();
  devCredential.setPublicKey(publicKeyCredential);

  System.out.println("Creating device with id: " + deviceId);
  Device device = new Device();
  device.setId(deviceId);
  device.setCredentials(Collections.singletonList(devCredential));

  Device createdDevice =
      service
          .projects()
          .locations()
          .registries()
          .devices()
          .create(registryPath, device)
          .execute();

  System.out.println("Created device: " + createdDevice.toPrettyString());
}
 
Example #28
Source File: HttpExample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
/** Publish an event or state message using Cloud IoT Core via the HTTP API. */
protected static void getConfig(
    String urlPath,
    String token,
    String projectId,
    String cloudRegion,
    String registryId,
    String deviceId,
    String version)
    throws IOException {
  // Build the resource path of the device that is going to be authenticated.
  String devicePath =
      String.format(
          "projects/%s/locations/%s/registries/%s/devices/%s",
          projectId, cloudRegion, registryId, deviceId);
  urlPath = urlPath + devicePath + "/config?local_version=" + version;

  HttpRequestFactory requestFactory =
      HTTP_TRANSPORT.createRequestFactory(
          new HttpRequestInitializer() {
            @Override
            public void initialize(HttpRequest request) {
              request.setParser(new JsonObjectParser(JSON_FACTORY));
            }
          });

  final HttpRequest req = requestFactory.buildGetRequest(new GenericUrl(urlPath));
  HttpHeaders heads = new HttpHeaders();

  heads.setAuthorization(String.format("Bearer %s", token));
  heads.setContentType("application/json; charset=UTF-8");
  heads.setCacheControl("no-cache");

  req.setHeaders(heads);
  ExponentialBackOff backoff =
      new ExponentialBackOff.Builder()
          .setInitialIntervalMillis(500)
          .setMaxElapsedTimeMillis(900000)
          .setMaxIntervalMillis(6000)
          .setMultiplier(1.5)
          .setRandomizationFactor(0.5)
          .build();
  req.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff));
  HttpResponse res = req.execute();
  System.out.println(res.getStatusCode());
  System.out.println(res.getStatusMessage());
  InputStream in = res.getContent();

  System.out.println(CharStreams.toString(new InputStreamReader(in, Charsets.UTF_8.name())));
}
 
Example #29
Source File: SeriesKeyEncoding.java    From heroic with Apache License 2.0 4 votes vote down vote up
public ByteString rangeKey(final LocalDate date) {
    return ByteString.copyFrom(category + "/" + date.toString(), Charsets.UTF_8);
}
 
Example #30
Source File: SeriesKeyEncoding.java    From heroic with Apache License 2.0 4 votes vote down vote up
public ByteString encode(SeriesKey key, Transform<Series, String> transform) throws Exception {
    return ByteString.copyFrom(
        category + "/" + key.getDate().toString() + "/" + transform.transform(key.getSeries()),
        Charsets.UTF_8);
}