Search in sources :

Example 96 with HttpRequest

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

the class DatasetClient method update.

/**
 * Updates the properties of a dataset.
 *
 * @param instance the dataset to update
 * @param properties properties to set
 * @throws NotFoundException if the dataset is not found
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public void update(DatasetId instance, Map<String, String> properties) throws NotFoundException, IOException, UnauthenticatedException, ConflictException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(instance.getParent(), String.format("data/datasets/%s/properties", instance.getDataset()));
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(properties)).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_CONFLICT);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(instance);
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
        throw new ConflictException(response.getResponseBodyAsString());
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) ConflictException(io.cdap.cdap.common.ConflictException) HttpResponse(io.cdap.common.http.HttpResponse) DatasetTypeNotFoundException(io.cdap.cdap.common.DatasetTypeNotFoundException) DatasetNotFoundException(io.cdap.cdap.common.DatasetNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) URL(java.net.URL)

Example 97 with HttpRequest

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

the class DatasetClient method getProperties.

/**
 * Retrieve the properties with which a dataset was created or updated.
 * @param instance the dataset instance
 * @return the properties as a map
 */
public Map<String, String> getProperties(DatasetId instance) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(instance.getParent(), String.format("data/datasets/%s/properties", instance.getDataset()));
    HttpRequest request = HttpRequest.get(url).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(instance);
    }
    return ObjectResponse.fromJsonBody(response, new TypeToken<Map<String, String>>() {
    }).getResponseObject();
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) TypeToken(com.google.common.reflect.TypeToken) HttpResponse(io.cdap.common.http.HttpResponse) DatasetTypeNotFoundException(io.cdap.cdap.common.DatasetTypeNotFoundException) DatasetNotFoundException(io.cdap.cdap.common.DatasetNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) URL(java.net.URL)

Example 98 with HttpRequest

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

the class FileFetcherHttpHandlerInternalTest method download.

private HttpResponse download(File src, Location dst) throws IOException {
    // Make a request to download the source file.
    URL url = new URL(String.format("%s/v3Internal/location/%s", baseURL, src.toURI().getPath()));
    OutputStream outputStream = dst.getOutputStream();
    HttpRequest request = HttpRequest.builder(HttpMethod.GET, url).withContentConsumer(new HttpContentConsumer() {

        @Override
        public boolean onReceived(ByteBuffer chunk) {
            try {
                byte[] bytes = new byte[chunk.remaining()];
                chunk.get(bytes, 0, bytes.length);
                outputStream.write(bytes);
            } catch (IOException e) {
                LOG.error("Failed to write to {}", dst.toURI());
                return false;
            }
            return true;
        }

        @Override
        public void onFinished() {
            try {
                outputStream.close();
            } catch (Exception e) {
                LOG.error("Failed to close {}", dst.toURI());
            }
        }
    }).build();
    HttpResponse httpResponse = HttpRequests.execute(request, new DefaultHttpRequestConfig(false));
    httpResponse.consumeContent();
    return httpResponse;
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) HttpContentConsumer(io.cdap.common.http.HttpContentConsumer) HttpResponse(io.cdap.common.http.HttpResponse) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) URL(java.net.URL) IOException(java.io.IOException)

Example 99 with HttpRequest

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

the class ScheduleClient method doUpdate.

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

Example 100 with HttpRequest

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

the class TestFrameworkTestRun method testByteCodeClassLoader.

@Category(XSlowTests.class)
@Test
public void testByteCodeClassLoader() throws Exception {
    // This test verify bytecode generated classes ClassLoading
    ApplicationManager appManager = deployApplication(testSpace, ClassLoaderTestApp.class);
    ServiceManager serviceManager = appManager.getServiceManager("RecordHandler").start();
    URL serviceURL = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
    Assert.assertNotNull(serviceURL);
    // Increment record
    URL url = new URL(serviceURL, "increment/public");
    for (int i = 0; i < 10; i++) {
        HttpResponse response = executeHttp(HttpRequest.post(url).build());
        Assert.assertEquals(200, response.getResponseCode());
    }
    // Query record
    url = new URL(serviceURL, "query?type=public");
    HttpRequest request = HttpRequest.get(url).build();
    HttpResponse response = executeHttp(request);
    Assert.assertEquals(200, response.getResponseCode());
    long count = Long.parseLong(response.getResponseBodyAsString());
    serviceManager.stop();
    // Verify the record count with dataset
    DataSetManager<KeyValueTable> recordsManager = getDataset(testSpace.dataset("records"));
    KeyValueTable records = recordsManager.get();
    Assert.assertEquals(count, Bytes.toLong(records.read("PUBLIC")));
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) ApplicationManager(io.cdap.cdap.test.ApplicationManager) ServiceManager(io.cdap.cdap.test.ServiceManager) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) Category(org.junit.experimental.categories.Category) 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