Search in sources :

Example 81 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class DynamicPluginServiceTestRun method testDynamicPluginSimple.

@Test
public void testDynamicPluginSimple() throws Exception {
    // test a single plugin
    Map<String, String> properties = new HashMap<>();
    properties.put("value", "x");
    URL url = baseURI.resolve(String.format("plugins/%s/apply", ConstantFunction.NAME)).toURL();
    HttpRequest request = HttpRequest.builder(HttpMethod.POST, url).withBody(GSON.toJson(properties)).build();
    HttpResponse response = executeHttp(request);
    Assert.assertEquals(200, response.getResponseCode());
    Assert.assertEquals("x", response.getResponseBodyAsString());
    // test plugin that uses a plugin
    Map<String, String> delegateProperties = new HashMap<>();
    delegateProperties.put("value", "y");
    properties = new HashMap<>();
    properties.put("delegateName", ConstantFunction.NAME);
    properties.put("properties", GSON.toJson(delegateProperties));
    url = baseURI.resolve(String.format("plugins/%s/apply", DelegatingFunction.NAME)).toURL();
    request = HttpRequest.builder(HttpMethod.POST, url).withBody(GSON.toJson(properties)).build();
    response = executeHttp(request);
    Assert.assertEquals(200, response.getResponseCode());
    Assert.assertEquals("y", response.getResponseBodyAsString());
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HashMap(java.util.HashMap) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) Test(org.junit.Test)

Example 82 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class ApplicationClient method update.

/**
 * Update an existing app to use a different artifact version or config.
 *
 * @param appId the id of the application to update
 * @param updateRequest the request to update the application with
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws NotFoundException if the app or requested artifact could not be found
 * @throws BadRequestException if the request is invalid
 */
public void update(ApplicationId appId, AppRequest<?> updateRequest) throws IOException, UnauthenticatedException, NotFoundException, BadRequestException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(appId.getParent(), String.format("apps/%s/update", appId.getApplication()));
    HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(updateRequest)).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException("app or app artifact");
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(String.format("Bad Request. Reason: %s", response.getResponseBodyAsString()));
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) BadRequestException(io.cdap.cdap.common.BadRequestException) URL(java.net.URL)

Example 83 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class ApplicationClient method deploy.

/**
 * Creates an application with a version using an existing artifact.
 *
 * @param appId the id of the application to add
 * @param createRequest the request body, which contains the artifact to use and any application config
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public void deploy(ApplicationId appId, AppRequest<?> createRequest) throws IOException, UnauthenticatedException {
    URL url = config.resolveNamespacedURLV3(new NamespaceId(appId.getNamespace()), String.format("apps/%s/versions/%s/create", appId.getApplication(), appId.getVersion()));
    HttpRequest request = HttpRequest.post(url).addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withBody(GSON.toJson(createRequest)).build();
    restClient.upload(request, config.getAccessToken());
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) URL(java.net.URL)

Example 84 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class AuthorizationClient method addRoleToPrincipal.

@Override
public void addRoleToPrincipal(Role role, Principal principal) throws AccessException {
    URL url = resolveURL(String.format(AUTHORIZATION_BASE + "%s/%s/roles/%s", principal.getType(), principal.getName(), role.getName()));
    HttpRequest request = HttpRequest.put(url).build();
    executeExistingRolesRequest(role, request);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) URL(java.net.URL)

Example 85 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class AuthorizationClient method grant.

@Override
public void grant(Authorizable authorizable, Principal principal, Set<? extends Permission> permissions) throws AccessException {
    GrantRequest grantRequest = new GrantRequest(authorizable, principal, permissions);
    URL url = resolveURL(AUTHORIZATION_BASE + "/privileges/grant");
    HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(grantRequest)).build();
    executePrivilegeRequest(request);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) GrantRequest(io.cdap.cdap.proto.security.GrantRequest) URL(java.net.URL)

Aggregations

HttpRequest (io.cdap.common.http.HttpRequest)124 HttpResponse (io.cdap.common.http.HttpResponse)92 URL (java.net.URL)81 Test (org.junit.Test)33 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 IOException (java.io.IOException)14 AccessToken (io.cdap.cdap.security.authentication.client.AccessToken)13 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)10 BadRequestException (io.cdap.cdap.common.BadRequestException)9 NotFoundException (io.cdap.cdap.common.NotFoundException)8 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)5 ServiceManager (io.cdap.cdap.test.ServiceManager)5 TypeToken (com.google.gson.reflect.TypeToken)4 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)4 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)4 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)4 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)4 TopicId (io.cdap.cdap.proto.id.TopicId)4 ApplicationManager (io.cdap.cdap.test.ApplicationManager)4 ByteBuffer (java.nio.ByteBuffer)4