Search in sources :

Example 76 with HttpRequest

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

the class DefaultMetadataServiceClient method drop.

@Override
public void drop(MetadataMutation.Drop dropMutation) {
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.DELETE, "metadata-internals/drop").withBody(GSON.toJson(dropMutation)).build();
    HttpResponse response = execute(request);
    if (HttpResponseStatus.OK.code() != response.getResponseCode()) {
        LOG.trace("Failed to drop metadata for entity %s: %s", dropMutation.getEntity(), response);
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse)

Example 77 with HttpRequest

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

the class DefaultMetadataServiceClient method remove.

@Override
public void remove(MetadataMutation.Remove removeMutation) {
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.DELETE, "metadata-internals/remove").withBody(GSON.toJson(removeMutation)).build();
    HttpResponse response = execute(request);
    if (HttpResponseStatus.OK.code() != response.getResponseCode()) {
        LOG.trace("Failed to remove metadata for entity %s: %s", removeMutation.getEntity(), response);
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse)

Example 78 with HttpRequest

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

the class ArtifactLocalizerClient method sendRequest.

private File sendRequest(ArtifactId artifactId, boolean unpack) throws IOException, ArtifactNotFoundException {
    String urlPath = String.format("/artifact/namespaces/%s/artifacts/%s/versions/%s?unpack=%b", artifactId.getNamespace(), artifactId.getArtifact(), artifactId.getVersion(), unpack);
    URL url;
    try {
        url = new URI(sidecarBaseURL + urlPath).toURL();
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }
    LOG.debug("Sending request to {}", url);
    HttpRequest httpRequest = HttpRequest.builder(HttpMethod.GET, url).build();
    Multimap<String, String> headers = httpRequest.getHeaders();
    internalAuthenticator.applyInternalAuthenticationHeaders(headers::put);
    HttpResponse httpResponse = HttpRequests.execute(httpRequest);
    if (httpResponse.getResponseCode() != HttpURLConnection.HTTP_OK) {
        if (httpResponse.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
            throw new ArtifactNotFoundException(artifactId);
        }
        BasicThrowable basicThrowable = GSON.fromJson(httpResponse.getResponseBodyAsString(), BasicThrowable.class);
        throw new IOException(RemoteExecutionException.fromBasicThrowable(basicThrowable));
    }
    String path = httpResponse.getResponseBodyAsString();
    LOG.debug("ArtifactLocalizer request returned path {}", path);
    return new File(path);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) BasicThrowable(io.cdap.cdap.proto.BasicThrowable) URI(java.net.URI) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) File(java.io.File) URL(java.net.URL)

Example 79 with HttpRequest

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

the class TaskWorkerHttpHandlerInternal method token.

@GET
@Path("/token")
public void token(io.netty.handler.codec.http.HttpRequest request, HttpResponder responder) {
    if (metadataServiceEndpoint == null) {
        responder.sendString(HttpResponseStatus.NOT_IMPLEMENTED, String.format("%s has not been set", Constants.TaskWorker.METADATA_SERVICE_END_POINT));
        return;
    }
    try {
        URL url = new URL(metadataServiceEndpoint);
        HttpRequest tokenRequest = HttpRequest.get(url).addHeader("Metadata-Flavor", "Google").build();
        HttpResponse tokenResponse = HttpRequests.execute(tokenRequest);
        responder.sendByteArray(HttpResponseStatus.OK, tokenResponse.getResponseBody(), EmptyHttpHeaders.INSTANCE);
    } catch (Exception ex) {
        LOG.warn("Failed to fetch token from metadata service", ex);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, exceptionToJson(ex), EmptyHttpHeaders.INSTANCE);
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 80 with HttpRequest

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

the class OAuthServiceTest method makePutCall.

private <T> HttpResponse makePutCall(String endpoint, T body) throws IOException {
    URL url = serviceURI.resolve(String.format("v1/oauth/%s", endpoint)).toURL();
    HttpRequest request = HttpRequest.builder(HttpMethod.PUT, url).withBody(GSON.toJson(body)).build();
    return HttpRequests.execute(request, new DefaultHttpRequestConfig(false));
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) 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