Search in sources :

Example 66 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class ArtifactClient method deleteProperty.

/**
   * Delete a property for an artifact. If the property does not exist, this will be a no-op.
   *
   * @param artifactId the artifact to delete a property from
   * @param key the property to delete
   * @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   * @throws ArtifactNotFoundException if the artifact does not exist
   * @throws IOException if a network error occurred
   */
public void deleteProperty(ArtifactId artifactId, String key) throws IOException, UnauthenticatedException, ArtifactNotFoundException, BadRequestException, UnauthorizedException {
    String path = String.format("artifacts/%s/versions/%s/properties/%s", artifactId.getArtifact(), artifactId.getVersion(), key);
    URL url = config.resolveNamespacedURLV3(artifactId.getParent(), path);
    HttpRequest.Builder requestBuilder = HttpRequest.delete(url);
    HttpRequest request = requestBuilder.build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ArtifactNotFoundException(artifactId);
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) URL(java.net.URL)

Example 67 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class ArtifactClient method delete.

/**
   * Delete an artifact.
   *
   * @param artifactId the artifact to delete
   *
   * @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public void delete(ArtifactId artifactId) throws IOException, UnauthenticatedException, BadRequestException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(artifactId.getParent(), String.format("artifacts/%s/versions/%s", artifactId.getArtifact(), artifactId.getVersion()));
    HttpRequest request = HttpRequest.delete(url).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) URL(java.net.URL)

Example 68 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class AuthorizationClient method grant.

@Override
public void grant(EntityId entity, Principal principal, Set<Action> actions) throws IOException, UnauthenticatedException, FeatureDisabledException, UnauthorizedException, NotFoundException {
    GrantRequest grantRequest = new GrantRequest(entity, principal, actions);
    URL url = config.resolveURLV3(AUTHORIZATION_BASE + "/privileges/grant");
    HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(grantRequest)).build();
    executePrivilegeRequest(request);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) GrantRequest(co.cask.cdap.proto.security.GrantRequest) URL(java.net.URL)

Example 69 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class RESTClientTest method testPutSuccessWithAccessToken.

@Test
public void testPutSuccessWithAccessToken() throws Exception {
    URL url = getBaseURI().resolve("/api/testPutAuth").toURL();
    HttpRequest request = HttpRequest.put(url).build();
    HttpResponse response = restClient.execute(request, new AccessToken(ACCESS_TOKEN, 82000L, "Bearer"));
    verifyResponse(response, only(200), any(), only("Access token received: " + ACCESS_TOKEN));
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) AccessToken(co.cask.cdap.security.authentication.client.AccessToken) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL) Test(org.junit.Test)

Example 70 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class RESTClientTest method testGetUnauthorizedWithAccessToken.

@Test(expected = UnauthenticatedException.class)
public void testGetUnauthorizedWithAccessToken() throws Exception {
    URL url = getBaseURI().resolve("/api/testGetAuth").toURL();
    HttpRequest request = HttpRequest.get(url).build();
    restClient.execute(request, new AccessToken("Unknown", 82000L, "Bearer"));
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) AccessToken(co.cask.cdap.security.authentication.client.AccessToken) URL(java.net.URL) Test(org.junit.Test)

Aggregations

HttpRequest (co.cask.common.http.HttpRequest)97 URL (java.net.URL)75 HttpResponse (co.cask.common.http.HttpResponse)71 Test (org.junit.Test)22 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)13 BadRequestException (co.cask.cdap.common.BadRequestException)10 NotFoundException (co.cask.cdap.common.NotFoundException)9 ApplicationManager (co.cask.cdap.test.ApplicationManager)9 ServiceManager (co.cask.cdap.test.ServiceManager)9 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)6 IOException (java.io.IOException)6 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)5 StreamNotFoundException (co.cask.cdap.common.StreamNotFoundException)5 TypeToken (com.google.common.reflect.TypeToken)5 TypeToken (com.google.gson.reflect.TypeToken)5 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)4 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)4 Instances (co.cask.cdap.proto.Instances)4 SparkManager (co.cask.cdap.test.SparkManager)4 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)3