Search in sources :

Example 86 with HttpRequest

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

the class AuthorizationClient method createRole.

@Override
public void createRole(Role role) throws AccessException {
    URL url = resolveURL(String.format(AUTHORIZATION_BASE + "roles/%s", role.getName()));
    HttpRequest request = HttpRequest.put(url).build();
    HttpResponse httpResponse = doExecuteRequest(request, HttpURLConnection.HTTP_CONFLICT);
    if (httpResponse.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
        throw new AlreadyExistsException(role);
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) AlreadyExistsException(io.cdap.cdap.security.spi.authorization.AlreadyExistsException) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Example 87 with HttpRequest

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

the class AuthorizationClient method listGrants.

@Override
public Set<GrantedPermission> listGrants(Principal principal) throws AccessException {
    String urlStr = String.format(AUTHORIZATION_BASE + "%s/%s/privileges", principal.getType(), principal.getName());
    URL url = resolveURL(urlStr);
    HttpRequest request = HttpRequest.get(url).build();
    HttpResponse response = doExecuteRequest(request);
    if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return ObjectResponse.fromJsonBody(response, TYPE_OF_PRIVILEGE_SET, GSON).getResponseObject();
    }
    throw new AccessIOException(String.format("Cannot list privileges. Reason: %s", response.getResponseBodyAsString()));
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) AccessIOException(io.cdap.cdap.security.spi.AccessIOException) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Example 88 with HttpRequest

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

the class ProgramClient method getStatus.

/**
 * Gets the status of multiple programs.
 *
 * @param namespace the namespace of the programs
 * @param programs the list of programs to get status for
 * @return the status of each program
 */
public List<BatchProgramStatus> getStatus(NamespaceId namespace, List<BatchProgram> programs) throws IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(namespace, "status");
    HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(programs)).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken());
    return ObjectResponse.<List<BatchProgramStatus>>fromJsonBody(response, BATCH_STATUS_RESPONSE_TYPE, GSON).getResponseObject();
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) BatchProgramStatus(io.cdap.cdap.proto.BatchProgramStatus) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Example 89 with HttpRequest

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

the class ScheduleClient method doAdd.

/*------------ private helpers ---------------------*/
private void doAdd(ScheduleId scheduleId, String json) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException, AlreadyExistsException {
    String path = String.format("apps/%s/versions/%s/schedules/%s", scheduleId.getApplication(), scheduleId.getVersion(), scheduleId.getSchedule());
    URL url = config.resolveNamespacedURLV3(scheduleId.getNamespaceId(), path);
    HttpRequest request = HttpRequest.put(url).withBody(json).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_CONFLICT);
    if (HttpURLConnection.HTTP_NOT_FOUND == response.getResponseCode()) {
        throw new NotFoundException(scheduleId);
    } else if (HttpURLConnection.HTTP_CONFLICT == response.getResponseCode()) {
        throw new AlreadyExistsException(scheduleId);
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) AlreadyExistsException(io.cdap.cdap.common.AlreadyExistsException) HttpResponse(io.cdap.common.http.HttpResponse) NotFoundException(io.cdap.cdap.common.NotFoundException) URL(java.net.URL)

Example 90 with HttpRequest

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

the class RESTClientTest method testPostUnauthorizedWithAccessToken.

@Test(expected = UnauthenticatedException.class)
public void testPostUnauthorizedWithAccessToken() throws Exception {
    URL url = getBaseURI().resolve("/api/testPostAuth").toURL();
    HttpRequest request = HttpRequest.post(url).build();
    restClient.execute(request, new AccessToken("Unknown", 82000L, "Bearer"));
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequest(io.cdap.common.http.HttpRequest) AccessToken(io.cdap.cdap.security.authentication.client.AccessToken) URL(java.net.URL) Test(org.junit.Test)

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