Search in sources :

Example 1 with HttpRequest

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

the class AbstractNamespaceQueryClient method list.

@Override
public List<NamespaceMeta> list() throws Exception {
    HttpRequest request = HttpRequest.get(resolve("namespaces")).build();
    HttpResponse response = execute(request);
    if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return ObjectResponse.fromJsonBody(response, new TypeToken<List<NamespaceMeta>>() {
        }).getResponseObject();
    }
    throw new IOException(String.format("Cannot list namespaces. Reason: %s", response.getResponseBodyAsString()));
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) TypeToken(com.google.common.reflect.TypeToken) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) HttpResponse(io.cdap.common.http.HttpResponse) IOException(java.io.IOException)

Example 2 with HttpRequest

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

the class RemotePreviewRequestFetcher method fetch.

@Override
public Optional<PreviewRequest> fetch() throws IOException, UnauthorizedException {
    HttpRequest request = remoteClientInternal.requestBuilder(HttpMethod.POST, "requests/pull").withBody(ByteBuffer.wrap(pollerInfoProvider.get())).build();
    HttpResponse httpResponse = remoteClientInternal.execute(request);
    if (httpResponse.getResponseCode() == 200) {
        PreviewRequest previewRequest = GSON.fromJson(httpResponse.getResponseBodyAsString(), PreviewRequest.class);
        if (previewRequest != null && previewRequest.getPrincipal() != null) {
            SecurityRequestContext.setUserId(previewRequest.getPrincipal().getName());
            SecurityRequestContext.setUserCredential(previewRequest.getPrincipal().getFullCredential());
        }
        return Optional.ofNullable(previewRequest);
    }
    throw new IOException(String.format("Received status code:%s and body: %s", httpResponse.getResponseCode(), httpResponse.getResponseBodyAsString(Charsets.UTF_8)));
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) IOException(java.io.IOException) PreviewRequest(io.cdap.cdap.app.preview.PreviewRequest)

Example 3 with HttpRequest

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

the class ProgramClient method start.

/**
 * Starts a batch of programs in the same call.
 *
 * @param namespace the namespace of the programs
 * @param programs the programs to start, including any runtime arguments to start them with
 * @return the result of starting each program
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public List<BatchProgramResult> start(NamespaceId namespace, List<BatchProgramStart> programs) throws IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(namespace, "start");
    HttpRequest request = HttpRequest.builder(HttpMethod.POST, url).withBody(GSON.toJson(programs), Charsets.UTF_8).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken());
    return ObjectResponse.<List<BatchProgramResult>>fromJsonBody(response, BATCH_RESULTS_TYPE, GSON).getResponseObject();
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) BatchProgramResult(io.cdap.cdap.proto.BatchProgramResult) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Example 4 with HttpRequest

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

the class ProgramClient method stop.

/**
 * Stops a batch of programs in the same call.
 *
 * @param namespace the namespace of the programs
 * @param programs the programs to stop
 * @return the result of stopping each program
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public List<BatchProgramResult> stop(NamespaceId namespace, List<BatchProgram> programs) throws IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(namespace, "stop");
    HttpRequest request = HttpRequest.builder(HttpMethod.POST, url).withBody(GSON.toJson(programs), Charsets.UTF_8).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken());
    return ObjectResponse.<List<BatchProgramResult>>fromJsonBody(response, BATCH_RESULTS_TYPE, GSON).getResponseObject();
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) BatchProgramResult(io.cdap.cdap.proto.BatchProgramResult) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Example 5 with HttpRequest

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

the class ProgramClient method setRuntimeArgs.

/**
 * Sets the runtime args of a program.
 *
 * @param program the program
 * @param runtimeArgs args of the program
 * @throws IOException if a network error occurred
 * @throws ProgramNotFoundException if the application or program could not be found
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public void setRuntimeArgs(ProgramId program, Map<String, String> runtimeArgs) throws IOException, UnauthenticatedException, ProgramNotFoundException, UnauthorizedException {
    String path = String.format("apps/%s/versions/%s/%s/%s/runtimeargs", program.getApplication(), program.getVersion(), program.getType().getCategoryName(), program.getProgram());
    URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), path);
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(runtimeArgs)).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ProgramNotFoundException(program);
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) ProgramNotFoundException(io.cdap.cdap.common.ProgramNotFoundException) 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