Java Code Examples for java.net.http.HttpRequest#newBuilder()

The following examples show how to use java.net.http.HttpRequest#newBuilder() . 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: StoreApi.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
private HttpRequest.Builder getOrderByIdRequestBuilder(Long orderId) throws ApiException {
  // verify the required parameter 'orderId' is set
  if (orderId == null) {
    throw new ApiException(400, "Missing the required parameter 'orderId' when calling getOrderById");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/store/order/{order_id}"
      .replace("{order_id}", ApiClient.urlEncode(orderId.toString()));

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
Example 2
Source File: FakeApi.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
private HttpRequest.Builder fakeOuterNumberSerializeRequestBuilder(BigDecimal body) throws ApiException {

    HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

    String localVarPath = "/fake/outer/number";

    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

    localVarRequestBuilder.header("Content-Type", "application/json");
    localVarRequestBuilder.header("Accept", "application/json");

    try {
      byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body);
      localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
    } catch (IOException e) {
      throw new ApiException(e);
    }
    if (memberVarReadTimeout != null) {
      localVarRequestBuilder.timeout(memberVarReadTimeout);
    }
    if (memberVarInterceptor != null) {
      memberVarInterceptor.accept(localVarRequestBuilder);
    }
    return localVarRequestBuilder;
  }
 
Example 3
Source File: PetApi.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
private HttpRequest.Builder updatePetWithFormRequestBuilder(Long petId, String name, String status) throws ApiException {
  // verify the required parameter 'petId' is set
  if (petId == null) {
    throw new ApiException(400, "Missing the required parameter 'petId' when calling updatePetWithForm");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/pet/{petId}"
      .replace("{petId}", ApiClient.urlEncode(petId.toString()));

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
Example 4
Source File: StoreApi.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
private HttpRequest.Builder getInventoryRequestBuilder() throws ApiException {

    HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

    String localVarPath = "/store/inventory";

    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

    localVarRequestBuilder.header("Accept", "application/json");

    localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody());
    if (memberVarReadTimeout != null) {
      localVarRequestBuilder.timeout(memberVarReadTimeout);
    }
    if (memberVarInterceptor != null) {
      memberVarInterceptor.accept(localVarRequestBuilder);
    }
    return localVarRequestBuilder;
  }
 
Example 5
Source File: FakeApi.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
private HttpRequest.Builder fakeOuterStringSerializeRequestBuilder(String body) throws ApiException {

    HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

    String localVarPath = "/fake/outer/string";

    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

    localVarRequestBuilder.header("Content-Type", "application/json");
    localVarRequestBuilder.header("Accept", "application/json");

    try {
      byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body);
      localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
    } catch (IOException e) {
      throw new ApiException(e);
    }
    if (memberVarReadTimeout != null) {
      localVarRequestBuilder.timeout(memberVarReadTimeout);
    }
    if (memberVarInterceptor != null) {
      memberVarInterceptor.accept(localVarRequestBuilder);
    }
    return localVarRequestBuilder;
  }
 
Example 6
Source File: PetApi.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
private HttpRequest.Builder updatePetWithFormRequestBuilder(Long petId, String name, String status) throws ApiException {
  // verify the required parameter 'petId' is set
  if (petId == null) {
    throw new ApiException(400, "Missing the required parameter 'petId' when calling updatePetWithForm");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/pet/{petId}"
      .replace("{petId}", ApiClient.urlEncode(petId.toString()));

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
Example 7
Source File: FakeApi.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
private HttpRequest.Builder fakeOuterNumberSerializeRequestBuilder(BigDecimal body) throws ApiException {

    HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

    String localVarPath = "/fake/outer/number";

    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

    localVarRequestBuilder.header("Content-Type", "application/json");
    localVarRequestBuilder.header("Accept", "application/json");

    try {
      byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body);
      localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
    } catch (IOException e) {
      throw new ApiException(e);
    }
    if (memberVarReadTimeout != null) {
      localVarRequestBuilder.timeout(memberVarReadTimeout);
    }
    if (memberVarInterceptor != null) {
      memberVarInterceptor.accept(localVarRequestBuilder);
    }
    return localVarRequestBuilder;
  }
 
Example 8
Source File: PetApi.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
private HttpRequest.Builder uploadFileWithRequiredFileRequestBuilder(Long petId, File requiredFile, String additionalMetadata) throws ApiException {
  // verify the required parameter 'petId' is set
  if (petId == null) {
    throw new ApiException(400, "Missing the required parameter 'petId' when calling uploadFileWithRequiredFile");
  }
  // verify the required parameter 'requiredFile' is set
  if (requiredFile == null) {
    throw new ApiException(400, "Missing the required parameter 'requiredFile' when calling uploadFileWithRequiredFile");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/fake/{petId}/uploadImageWithRequiredFile"
      .replace("{petId}", ApiClient.urlEncode(petId.toString()));

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
Example 9
Source File: PetApi.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
private HttpRequest.Builder updatePetRequestBuilder(Pet body) throws ApiException {
  // verify the required parameter 'body' is set
  if (body == null) {
    throw new ApiException(400, "Missing the required parameter 'body' when calling updatePet");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/pet";

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Content-Type", "application/json");
  localVarRequestBuilder.header("Accept", "application/json");

  try {
    byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body);
    localVarRequestBuilder.method("PUT", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
  } catch (IOException e) {
    throw new ApiException(e);
  }
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
Example 10
Source File: FakeApi.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
private HttpRequest.Builder testEndpointParametersRequestBuilder(BigDecimal number, Double _double, String patternWithoutDelimiter, byte[] _byte, Integer integer, Integer int32, Long int64, Float _float, String string, File binary, LocalDate date, OffsetDateTime dateTime, String password, String paramCallback) throws ApiException {
  // verify the required parameter 'number' is set
  if (number == null) {
    throw new ApiException(400, "Missing the required parameter 'number' when calling testEndpointParameters");
  }
  // verify the required parameter '_double' is set
  if (_double == null) {
    throw new ApiException(400, "Missing the required parameter '_double' when calling testEndpointParameters");
  }
  // verify the required parameter 'patternWithoutDelimiter' is set
  if (patternWithoutDelimiter == null) {
    throw new ApiException(400, "Missing the required parameter 'patternWithoutDelimiter' when calling testEndpointParameters");
  }
  // verify the required parameter '_byte' is set
  if (_byte == null) {
    throw new ApiException(400, "Missing the required parameter '_byte' when calling testEndpointParameters");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/fake";

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
Example 11
Source File: PetApi.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
private HttpRequest.Builder deletePetRequestBuilder(Long petId, String apiKey) throws ApiException {
  // verify the required parameter 'petId' is set
  if (petId == null) {
    throw new ApiException(400, "Missing the required parameter 'petId' when calling deletePet");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/pet/{petId}"
      .replace("{petId}", ApiClient.urlEncode(petId.toString()));

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  if (apiKey != null) {
    localVarRequestBuilder.header("api_key", apiKey.toString());
  }
  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("DELETE", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
Example 12
Source File: UserApi.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
private HttpRequest.Builder loginUserRequestBuilder(String username, String password) throws ApiException {
  // verify the required parameter 'username' is set
  if (username == null) {
    throw new ApiException(400, "Missing the required parameter 'username' when calling loginUser");
  }
  // verify the required parameter 'password' is set
  if (password == null) {
    throw new ApiException(400, "Missing the required parameter 'password' when calling loginUser");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/user/login";

  List<Pair> localVarQueryParams = new ArrayList<>();
  localVarQueryParams.addAll(ApiClient.parameterToPairs("username", username));
  localVarQueryParams.addAll(ApiClient.parameterToPairs("password", password));

  if (!localVarQueryParams.isEmpty()) {
    StringJoiner queryJoiner = new StringJoiner("&");
    localVarQueryParams.forEach(p -> queryJoiner.add(p.getName() + '=' + p.getValue()));
    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + queryJoiner.toString()));
  } else {
    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
  }

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
Example 13
Source File: AnotherFakeApi.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
private HttpRequest.Builder call123testSpecialTagsRequestBuilder(Client body) throws ApiException {
  // verify the required parameter 'body' is set
  if (body == null) {
    throw new ApiException(400, "Missing the required parameter 'body' when calling call123testSpecialTags");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/another-fake/dummy";

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Content-Type", "application/json");
  localVarRequestBuilder.header("Accept", "application/json");

  try {
    byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body);
    localVarRequestBuilder.method("PATCH", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
  } catch (IOException e) {
    throw new ApiException(e);
  }
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
Example 14
Source File: AnotherFakeApi.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
private HttpRequest.Builder call123testSpecialTagsRequestBuilder(Client body) throws ApiException {
  // verify the required parameter 'body' is set
  if (body == null) {
    throw new ApiException(400, "Missing the required parameter 'body' when calling call123testSpecialTags");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/another-fake/dummy";

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Content-Type", "application/json");
  localVarRequestBuilder.header("Accept", "application/json");

  try {
    byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body);
    localVarRequestBuilder.method("PATCH", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
  } catch (IOException e) {
    throw new ApiException(e);
  }
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
Example 15
Source File: FakeApi.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
private HttpRequest.Builder testClientModelRequestBuilder(Client body) throws ApiException {
  // verify the required parameter 'body' is set
  if (body == null) {
    throw new ApiException(400, "Missing the required parameter 'body' when calling testClientModel");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/fake";

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Content-Type", "application/json");
  localVarRequestBuilder.header("Accept", "application/json");

  try {
    byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body);
    localVarRequestBuilder.method("PATCH", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
  } catch (IOException e) {
    throw new ApiException(e);
  }
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
Example 16
Source File: FakeApi.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
private HttpRequest.Builder testEndpointParametersRequestBuilder(BigDecimal number, Double _double, String patternWithoutDelimiter, byte[] _byte, Integer integer, Integer int32, Long int64, Float _float, String string, File binary, LocalDate date, OffsetDateTime dateTime, String password, String paramCallback) throws ApiException {
  // verify the required parameter 'number' is set
  if (number == null) {
    throw new ApiException(400, "Missing the required parameter 'number' when calling testEndpointParameters");
  }
  // verify the required parameter '_double' is set
  if (_double == null) {
    throw new ApiException(400, "Missing the required parameter '_double' when calling testEndpointParameters");
  }
  // verify the required parameter 'patternWithoutDelimiter' is set
  if (patternWithoutDelimiter == null) {
    throw new ApiException(400, "Missing the required parameter 'patternWithoutDelimiter' when calling testEndpointParameters");
  }
  // verify the required parameter '_byte' is set
  if (_byte == null) {
    throw new ApiException(400, "Missing the required parameter '_byte' when calling testEndpointParameters");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/fake";

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
Example 17
Source File: SignaturesTest.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Test
void testSigning() {
    Clock clock = Clock.fixed(Instant.EPOCH, ZoneOffset.UTC);
    RequestSigner signer = new RequestSigner(ecPemPrivateKey, "myKey", clock);

    URI requestUri = URI.create("https://host:123/path//./../more%2fpath/?yes=no");
    HttpRequest.Builder builder = HttpRequest.newBuilder(requestUri);
    HttpRequest request = signer.signed(builder, Method.GET, InputStream::nullInputStream);

    // GET request with correct signature and URI as-is.
    RequestVerifier verifier = new RequestVerifier(ecPemPublicKey, clock);
    assertTrue(verifier.verify(Method.valueOf(request.method()),
                               request.uri(),
                               request.headers().firstValue("X-Timestamp").get(),
                               request.headers().firstValue("X-Content-Hash").get(),
                               request.headers().firstValue("X-Authorization").get()));

    // POST request with correct signature and URI normalized.
    MultiPartStreamer streamer = new MultiPartStreamer().addText("message", new String(message, UTF_8))
                                                        .addBytes("copy", message);
    request = signer.signed(builder.setHeader("Content-Type", streamer.contentType()), Method.POST, streamer::data);
    assertTrue(verifier.verify(Method.valueOf(request.method()),
                               request.uri().normalize(),
                               request.headers().firstValue("X-Timestamp").get(),
                               request.headers().firstValue("X-Content-Hash").get(),
                               request.headers().firstValue("X-Authorization").get()));

    // Wrong method.
    assertFalse(verifier.verify(Method.PATCH,
                                request.uri().normalize(),
                                request.headers().firstValue("X-Timestamp").get(),
                                request.headers().firstValue("X-Content-Hash").get(),
                                request.headers().firstValue("X-Authorization").get()));

    // Wrong path.
    assertFalse(verifier.verify(Method.valueOf(request.method()),
                                request.uri().resolve("asdf"),
                                request.headers().firstValue("X-Timestamp").get(),
                                request.headers().firstValue("X-Content-Hash").get(),
                                request.headers().firstValue("X-Authorization").get()));

    // Wrong timestamp.
    assertFalse(verifier.verify(Method.valueOf(request.method()),
                                request.uri().normalize(),
                                Instant.EPOCH.plusMillis(1).toString(),
                                request.headers().firstValue("X-Content-Hash").get(),
                                request.headers().firstValue("X-Authorization").get()));

    // Wrong content hash.
    assertFalse(verifier.verify(Method.valueOf(request.method()),
                                request.uri().normalize(),
                                request.headers().firstValue("X-Timestamp").get(),
                                "Wrong/hash",
                                request.headers().firstValue("X-Authorization").get()));

    // Wrong signature.
    assertFalse(verifier.verify(Method.valueOf(request.method()),
                                request.uri().normalize(),
                                request.headers().firstValue("X-Timestamp").get(),
                                request.headers().firstValue("X-Content-Hash").get(),
                                "Wrong/signature"));

    // Key pair mismatch.
    verifier = new RequestVerifier(otherEcPemPublicKey, clock);
    assertFalse(verifier.verify(Method.valueOf(request.method()),
                                request.uri().normalize(),
                                request.headers().firstValue("X-Timestamp").get(),
                                request.headers().firstValue("X-Content-Hash").get(),
                                request.headers().firstValue("X-Authorization").get()));

    // Too old request.
    verifier = new RequestVerifier(ecPemPublicKey, Clock.fixed(Instant.EPOCH.plusSeconds(301), ZoneOffset.UTC));
    assertFalse(verifier.verify(Method.valueOf(request.method()),
                                request.uri().normalize(),
                                request.headers().firstValue("X-Timestamp").get(),
                                request.headers().firstValue("X-Content-Hash").get(),
                                request.headers().firstValue("X-Authorization").get()));

    // Too new request.
    verifier = new RequestVerifier(ecPemPublicKey, Clock.fixed(Instant.EPOCH.minusSeconds(301), ZoneOffset.UTC));
    assertFalse(verifier.verify(Method.valueOf(request.method()),
                                request.uri().normalize(),
                                request.headers().firstValue("X-Timestamp").get(),
                                request.headers().firstValue("X-Content-Hash").get(),
                                request.headers().firstValue("X-Authorization").get()));

}
 
Example 18
Source File: SignatureFilterTest.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Test
public void testFilter() {
    // Unsigned request gets no role.
    HttpRequest.Builder request = HttpRequest.newBuilder(URI.create("https://host:123/path/./..//..%2F?query=empty&%3F=%26"));
    byte[] emptyBody = new byte[0];
    verifySecurityContext(requestOf(request.copy().method("GET", HttpRequest.BodyPublishers.ofByteArray(emptyBody)).build(), emptyBody),
                          null);

    // Signed request gets no role when no key is stored for the application.
    verifySecurityContext(requestOf(signer.signed(request.copy(), Method.GET, InputStream::nullInputStream), emptyBody),
            null);

    // Signed request gets no role when only non-matching keys are stored for the application.
    applications.lockApplicationOrThrow(appId, application -> applications.store(application.withDeployKey(otherPublicKey)));
    // Signed request gets no role when no key is stored for the application.
    verifySecurityContext(requestOf(signer.signed(request.copy(), Method.GET, InputStream::nullInputStream), emptyBody),
                          null);

    // Signed request gets a headless role when a matching key is stored for the application.
    applications.lockApplicationOrThrow(appId, application -> applications.store(application.withDeployKey(publicKey)));
    verifySecurityContext(requestOf(signer.signed(request.copy(), Method.GET, InputStream::nullInputStream), emptyBody),
                          new SecurityContext(new SimplePrincipal("[email protected]"),
                                              Set.of(Role.reader(id.tenant()),
                                                     Role.headless(id.tenant(), id.application()))));

    // Signed POST request with X-Key header gets a headless role.
    byte[] hiBytes = new byte[]{0x48, 0x69};
    verifySecurityContext(requestOf(signer.signed(request.copy(), Method.POST, () -> new ByteArrayInputStream(hiBytes)), hiBytes),
                          new SecurityContext(new SimplePrincipal("[email protected]"),
                                              Set.of(Role.reader(id.tenant()),
                                                     Role.headless(id.tenant(), id.application()))));

    // Signed request gets a developer role when a matching developer key is stored for the tenant.
    tester.curator().writeTenant(new CloudTenant(appId.tenant(),
                                                 new BillingInfo("id", "code"),
                                                 ImmutableBiMap.of(publicKey, () -> "user")));
    verifySecurityContext(requestOf(signer.signed(request.copy(), Method.POST, () -> new ByteArrayInputStream(hiBytes)), hiBytes),
                          new SecurityContext(new SimplePrincipal("user"),
                                              Set.of(Role.reader(id.tenant()),
                                                     Role.developer(id.tenant()))));

    // Unsigned requests still get no roles.
    verifySecurityContext(requestOf(request.copy().method("GET", HttpRequest.BodyPublishers.ofByteArray(emptyBody)).build(), emptyBody),
                          null);
}
 
Example 19
Source File: FakeApi.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
private HttpRequest.Builder testQueryParameterCollectionFormatRequestBuilder(List<String> pipe, List<String> ioutil, List<String> http, List<String> url, List<String> context) throws ApiException {
  // verify the required parameter 'pipe' is set
  if (pipe == null) {
    throw new ApiException(400, "Missing the required parameter 'pipe' when calling testQueryParameterCollectionFormat");
  }
  // verify the required parameter 'ioutil' is set
  if (ioutil == null) {
    throw new ApiException(400, "Missing the required parameter 'ioutil' when calling testQueryParameterCollectionFormat");
  }
  // verify the required parameter 'http' is set
  if (http == null) {
    throw new ApiException(400, "Missing the required parameter 'http' when calling testQueryParameterCollectionFormat");
  }
  // verify the required parameter 'url' is set
  if (url == null) {
    throw new ApiException(400, "Missing the required parameter 'url' when calling testQueryParameterCollectionFormat");
  }
  // verify the required parameter 'context' is set
  if (context == null) {
    throw new ApiException(400, "Missing the required parameter 'context' when calling testQueryParameterCollectionFormat");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/fake/test-query-paramters";

  List<Pair> localVarQueryParams = new ArrayList<>();
  localVarQueryParams.addAll(ApiClient.parameterToPairs("csv", "pipe", pipe));
  localVarQueryParams.addAll(ApiClient.parameterToPairs("csv", "ioutil", ioutil));
  localVarQueryParams.addAll(ApiClient.parameterToPairs("ssv", "http", http));
  localVarQueryParams.addAll(ApiClient.parameterToPairs("csv", "url", url));
  localVarQueryParams.addAll(ApiClient.parameterToPairs("multi", "context", context));

  if (!localVarQueryParams.isEmpty()) {
    StringJoiner queryJoiner = new StringJoiner("&");
    localVarQueryParams.forEach(p -> queryJoiner.add(p.getName() + '=' + p.getValue()));
    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + queryJoiner.toString()));
  } else {
    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
  }

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("PUT", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
Example 20
Source File: FakeApi.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
private HttpRequest.Builder testQueryParameterCollectionFormatRequestBuilder(List<String> pipe, List<String> ioutil, List<String> http, List<String> url, List<String> context) throws ApiException {
  // verify the required parameter 'pipe' is set
  if (pipe == null) {
    throw new ApiException(400, "Missing the required parameter 'pipe' when calling testQueryParameterCollectionFormat");
  }
  // verify the required parameter 'ioutil' is set
  if (ioutil == null) {
    throw new ApiException(400, "Missing the required parameter 'ioutil' when calling testQueryParameterCollectionFormat");
  }
  // verify the required parameter 'http' is set
  if (http == null) {
    throw new ApiException(400, "Missing the required parameter 'http' when calling testQueryParameterCollectionFormat");
  }
  // verify the required parameter 'url' is set
  if (url == null) {
    throw new ApiException(400, "Missing the required parameter 'url' when calling testQueryParameterCollectionFormat");
  }
  // verify the required parameter 'context' is set
  if (context == null) {
    throw new ApiException(400, "Missing the required parameter 'context' when calling testQueryParameterCollectionFormat");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/fake/test-query-paramters";

  List<Pair> localVarQueryParams = new ArrayList<>();
  localVarQueryParams.addAll(ApiClient.parameterToPairs("csv", "pipe", pipe));
  localVarQueryParams.addAll(ApiClient.parameterToPairs("csv", "ioutil", ioutil));
  localVarQueryParams.addAll(ApiClient.parameterToPairs("ssv", "http", http));
  localVarQueryParams.addAll(ApiClient.parameterToPairs("csv", "url", url));
  localVarQueryParams.addAll(ApiClient.parameterToPairs("multi", "context", context));

  if (!localVarQueryParams.isEmpty()) {
    StringJoiner queryJoiner = new StringJoiner("&");
    localVarQueryParams.forEach(p -> queryJoiner.add(p.getName() + '=' + p.getValue()));
    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + queryJoiner.toString()));
  } else {
    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
  }

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("PUT", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}