org.bson.internal.Base64 Java Examples

The following examples show how to use org.bson.internal.Base64. 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: GitHubRawContentTest.java    From gaia with Mozilla Public License 2.0 6 votes vote down vote up
@Test
void getReadmeContent_shouldCallTheApiWithoutAuth_ifNoOwner_andServeDecodedContent(){
    // given
    var module = new TerraformModule();
    module.setRegistryDetails(new RegistryDetails(RegistryType.GITHUB,  "Apophis/Chulak"));

    var requestCaptor = ArgumentCaptor.forClass(HttpEntity.class);

    var githubFile = new RegistryFile(Base64.encode("# Module Readme".getBytes()));
    var response = new ResponseEntity<>(githubFile, HttpStatus.OK);

    when(restTemplate.exchange(
            eq("https://api.github.com/repos/Apophis/Chulak/contents/README.md?ref=master"),
            eq(HttpMethod.GET),
            requestCaptor.capture(),
            eq(RegistryFile.class))).thenReturn(response);

    // when
    var result = gitHubRawContent.getReadme(module);

    // then
    // content is served decoded
    assertThat(result).isPresent().contains("# Module Readme");
    // request is authenticated
    assertThat(requestCaptor.getValue().getHeaders()).isEmpty();
}
 
Example #2
Source File: CoreStitchServiceClientImpl.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
private StitchAuthRequest getStreamServiceFunctionRequest(
    final String name,
    final List<?> args) {
  final Document body = new Document();
  body.put(FunctionFields.NAME, name);
  if (serviceName != null) {
    body.put(FunctionFields.SERVICE, serviceName);
  }
  body.put(FunctionFields.ARGUMENTS, args);

  final StitchAuthRequest.Builder reqBuilder = new StitchAuthRequest.Builder();
  reqBuilder.withMethod(Method.GET).withPath(serviceRoutes.getFunctionCallRoute()
      + (FunctionFields.STITCH_REQUEST
      + Base64.encode(body.toJson().getBytes(StandardCharsets.UTF_8))));
  return reqBuilder.build();
}
 
Example #3
Source File: NonReactiveAggregateQueryProvider.java    From mongodb-aggregate-query-support with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the serialized value to be used for the given {@link ParameterBinding}.
 *
 * @param accessor - the accessor
 * @param binding  - the binding
 * @return - the value of the parameter
 */
@SuppressWarnings("Duplicates")
private String getParameterValueForBinding(ConvertingParameterAccessor accessor, ParameterBinding binding) {

  Object value = accessor.getBindableValue(binding.getParameterIndex());

  if (value instanceof String && binding.isQuoted()) {
    return (String) value;
  }
  else if (value instanceof byte[]) {

    String base64representation = Base64.encode((byte[]) value);
    if (!binding.isQuoted()) {
      return "{ '$binary' : '" + base64representation + "', '$type' : " + BSON.B_GENERAL + "}";
    }

    return base64representation;
  }

  return serialize(value);
}
 
Example #4
Source File: AggregateQueryProvider.java    From mongodb-aggregate-query-support with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the serialized value to be used for the given {@link ParameterBinding}.
 *
 * @param accessor - the accessor
 * @param binding  - the binding
 * @return - the value of the parameter
 */
@SuppressWarnings("Duplicates")
private String getParameterValueForBinding(ConvertingParameterAccessor accessor, ParameterBinding binding) {

  Object value = accessor.getBindableValue(binding.getParameterIndex());

  if (value instanceof String && binding.isQuoted()) {
    return (String) value;
  }
  else if (value instanceof byte[]) {

    String base64representation = Base64.encode((byte[]) value);
    if (!binding.isQuoted()) {
      return "{ '$binary' : '" + base64representation + "', '$type' : " + BSON.B_GENERAL + "}";
    }

    return base64representation;
  }

  return serialize(value);
}
 
Example #5
Source File: GitHubRawContentTest.java    From gaia with Mozilla Public License 2.0 5 votes vote down vote up
@Test
void getReadmeContent_shouldCallTheApi_andServeDecodedContent(){
    // given
    var module = new TerraformModule();
    module.setRegistryDetails(new RegistryDetails(RegistryType.GITHUB, "Apophis/Chulak"));

    var jack = new User("Jack", null);
    jack.setOAuth2User(new OAuth2User("GITHUB","TOKENSTRING", null));
    module.getModuleMetadata().setCreatedBy(jack);

    var requestCaptor = ArgumentCaptor.forClass(HttpEntity.class);

    var githubFile = new RegistryFile(Base64.encode("# Module Readme".getBytes()) + "\n");
    var response = new ResponseEntity<>(githubFile, HttpStatus.OK);

    when(restTemplate.exchange(
            eq("https://api.github.com/repos/Apophis/Chulak/contents/README.md?ref=master"),
            eq(HttpMethod.GET),
            requestCaptor.capture(),
            eq(RegistryFile.class))).thenReturn(response);

    // when
    var result = gitHubRawContent.getReadme(module);

    // then
    // content is served decoded
    assertThat(result).isPresent().contains("# Module Readme");
    // request is authenticated
    assertThat(requestCaptor.getValue().getHeaders()).containsEntry("Authorization", List.of("Bearer TOKENSTRING"));
}
 
Example #6
Source File: GitHubRawContentTest.java    From gaia with Mozilla Public License 2.0 5 votes vote down vote up
@Test
void getReadmeContent_shouldCallTheApiWithoutAuth_ifNoToken_andServeDecodedContent(){
    // given
    var module = new TerraformModule();
    module.setRegistryDetails(new RegistryDetails(RegistryType.GITHUB,  "Apophis/Chulak"));

    var jack = new User("Jack", null);
    module.getModuleMetadata().setCreatedBy(jack);

    var requestCaptor = ArgumentCaptor.forClass(HttpEntity.class);

    var githubFile = new RegistryFile(Base64.encode("# Module Readme".getBytes()));
    var response = new ResponseEntity<>(githubFile, HttpStatus.OK);

    when(restTemplate.exchange(
            eq("https://api.github.com/repos/Apophis/Chulak/contents/README.md?ref=master"),
            eq(HttpMethod.GET),
            requestCaptor.capture(),
            eq(RegistryFile.class))).thenReturn(response);

    // when
    var result = gitHubRawContent.getReadme(module);

    // then
    // content is served decoded
    assertThat(result).isPresent().contains("# Module Readme");
    // request is authenticated
    assertThat(requestCaptor.getValue().getHeaders()).isEmpty();
}
 
Example #7
Source File: GitLabRawContentTest.java    From gaia with Mozilla Public License 2.0 5 votes vote down vote up
@Test
void getReadmeContent_shouldCallTheApi_andServeDecodedContent(){
    // given
    var module = new TerraformModule();
    module.setRegistryDetails(new RegistryDetails(RegistryType.GITLAB, "123"));

    var jack = new User("Jack", null);
    jack.setOAuth2User(new OAuth2User("GITLAB","TOKENSTRING", null));
    module.getModuleMetadata().setCreatedBy(jack);

    var requestCaptor = ArgumentCaptor.forClass(HttpEntity.class);

    var gitlabFile = new RegistryFile(Base64.encode("# Module Readme".getBytes()));
    var response = new ResponseEntity<>(gitlabFile, HttpStatus.OK);

    when(restTemplate.exchange(
            eq("https://gitlab.com/api/v4/projects/123/repository/files/README.md?ref=master"),
            eq(HttpMethod.GET),
            requestCaptor.capture(),
            eq(RegistryFile.class))).thenReturn(response);

    // when
    var result = gitLabRawContent.getReadme(module);

    // then
    // content is served decoded
    assertThat(result).isPresent().contains("# Module Readme");
    // request is authenticated
    assertThat(requestCaptor.getValue().getHeaders()).containsEntry("Authorization", List.of("Bearer TOKENSTRING"));
}
 
Example #8
Source File: CoreStitchServiceUnitTests.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testStreamFunction() throws InterruptedException, IOException {
  final Stream<Integer> stream = StreamTestUtils.createStream(new IntegerCodec(), "42");

  doReturn(stream)
      .when(requestClient)
      .openAuthenticatedStream(
          any(StitchAuthRequest.class), ArgumentMatchers.<Decoder<Integer>>any());

  final String funcName = "myFunc1";
  final List<Integer> args = Arrays.asList(1, 2, 3);
  final Document expectedRequestDoc = new Document();
  expectedRequestDoc.put("name", funcName);
  expectedRequestDoc.put("service", TEST_SERVICE_NAME);
  expectedRequestDoc.put("arguments", args);

  assertEquals(
      stream, underTest.streamFunction(
          funcName, args, new IntegerCodec()));

  final ArgumentCaptor<StitchAuthRequest> reqArgument =
      ArgumentCaptor.forClass(StitchAuthRequest.class);
  @SuppressWarnings("unchecked")
  final ArgumentCaptor<Decoder<Integer>> decArgument = ArgumentCaptor.forClass(Decoder.class);

  verify(requestClient).openAuthenticatedStream(reqArgument.capture(), decArgument.capture());
  assertEquals(reqArgument.getValue().getMethod(), Method.GET);
  assertEquals(reqArgument.getValue().getPath(),
      routes.getFunctionCallRoute() + "?stitch_request="
          + Base64.encode(expectedRequestDoc.toJson().getBytes(StandardCharsets.UTF_8)));
  assertTrue(decArgument.getValue() instanceof IntegerCodec);
  assertFalse(reqArgument.getValue().getUseRefreshToken());
}
 
Example #9
Source File: Jwt.java    From stitch-android-sdk with Apache License 2.0 4 votes vote down vote up
static Jwt fromEncoded(final String encodedJwt) throws IOException {
  final String[] parts = splitToken(encodedJwt);
  final byte[] json = Base64.decode(parts[1]);
  return StitchObjectMapper.getInstance().readValue(json, Jwt.class);
}
 
Example #10
Source File: BsonBinaryToByteArrayDeserializer.java    From mongodb-aggregate-query-support with Apache License 2.0 4 votes vote down vote up
@Override
protected byte[] doDeserialize(JsonNode nodeValue) {
  return Base64.decode(nodeValue.textValue());
}