Search in sources :

Example 61 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class ClientMessagingService method listTopics.

@Override
public List<TopicId> listTopics(NamespaceId namespaceId) throws IOException {
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET, namespaceId.getNamespace() + "/topics").build();
    HttpResponse response = remoteClient.execute(request);
    handleError(response, "Failed to list topics in namespace " + namespaceId);
    List<String> topics = GSON.fromJson(response.getResponseBodyAsString(), TOPIC_LIST_TYPE);
    List<TopicId> result = new ArrayList<>(topics.size());
    for (String topic : topics) {
        result.add(namespaceId.topic(topic));
    }
    return Collections.unmodifiableList(result);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) ArrayList(java.util.ArrayList) HttpResponse(co.cask.common.http.HttpResponse) TopicId(co.cask.cdap.proto.id.TopicId)

Example 62 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class HDFSNodes method getNumDataNodes.

private int getNumDataNodes() throws IOException, JSONException {
    int dataNodes = 0;
    URL url = new URL(getWebURL() + "/jmx");
    HttpRequest request = HttpRequest.get(url).build();
    HttpResponse response = HttpRequests.execute(request);
    JSONObject responseJSON = new JSONObject(response.getResponseBodyAsString());
    JSONArray array = responseJSON.getJSONArray("beans");
    for (int idx = 0; idx < array.length(); ++idx) {
        if (array.getJSONObject(idx).get("name").equals("Hadoop:service=NameNode,name=FSNamesystemState")) {
            dataNodes = array.getJSONObject(idx).getInt("NumLiveDataNodes");
            break;
        }
    }
    return dataNodes;
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL)

Example 63 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class ArtifactClient method add.

/**
   * Add an artifact.
   *
   * @param namespace the namespace to add the artifact to
   * @param artifactName the name of the artifact to add
   * @param artifactContents an input supplier for the contents of the artifact
   * @param artifactVersion the version of the artifact to add. If null, the version will be derived from the
   *                        manifest of the artifact
   * @param parentArtifacts the set of artifacts this artifact extends
   * @param additionalPlugins the set of plugins contained in the artifact that cannot be determined
   *                          through jar inspection. This set should include any classes that are plugins but could
   *                          not be annotated as such. For example, 3rd party classes like jdbc drivers fall into
   *                          this category.
   * @throws ArtifactAlreadyExistsException if the artifact already exists
   * @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
   * @throws ArtifactRangeNotFoundException if the parent artifacts do not exist
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public void add(NamespaceId namespace, String artifactName, InputSupplier<? extends InputStream> artifactContents, @Nullable String artifactVersion, @Nullable Set<ArtifactRange> parentArtifacts, @Nullable Set<PluginClass> additionalPlugins) throws ArtifactAlreadyExistsException, BadRequestException, IOException, UnauthenticatedException, ArtifactRangeNotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(namespace, String.format("artifacts/%s", artifactName));
    HttpRequest.Builder requestBuilder = HttpRequest.post(url);
    if (artifactVersion != null) {
        requestBuilder.addHeader("Artifact-Version", artifactVersion);
    }
    if (parentArtifacts != null && !parentArtifacts.isEmpty()) {
        requestBuilder.addHeader("Artifact-Extends", Joiner.on('/').join(parentArtifacts));
    }
    if (additionalPlugins != null && !additionalPlugins.isEmpty()) {
        requestBuilder.addHeader("Artifact-Plugins", GSON.toJson(additionalPlugins));
    }
    HttpRequest request = requestBuilder.withBody(artifactContents).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_CONFLICT, HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_CONFLICT) {
        throw new ArtifactAlreadyExistsException(response.getResponseBodyAsString());
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    } else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ArtifactRangeNotFoundException(parentArtifacts);
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) ArtifactRangeNotFoundException(co.cask.cdap.common.ArtifactRangeNotFoundException) ArtifactAlreadyExistsException(co.cask.cdap.common.ArtifactAlreadyExistsException) HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) URL(java.net.URL)

Example 64 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class AuthorizationClient method createRole.

@Override
public void createRole(Role role) throws IOException, FeatureDisabledException, UnauthenticatedException, UnauthorizedException, RoleAlreadyExistsException, NotFoundException {
    URL url = config.resolveURLV3(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 RoleAlreadyExistsException(role);
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) RoleAlreadyExistsException(co.cask.cdap.security.spi.authorization.RoleAlreadyExistsException) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL)

Example 65 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class AuthorizationClient method dropRole.

@Override
public void dropRole(Role role) throws IOException, FeatureDisabledException, UnauthenticatedException, UnauthorizedException, RoleNotFoundException, NotFoundException {
    URL url = config.resolveURLV3(String.format(AUTHORIZATION_BASE + "roles/%s", role.getName()));
    HttpRequest request = HttpRequest.delete(url).build();
    executeExistingRolesRequest(role, request);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) URL(java.net.URL)

Aggregations

HttpRequest (co.cask.common.http.HttpRequest)97 URL (java.net.URL)75 HttpResponse (co.cask.common.http.HttpResponse)71 Test (org.junit.Test)22 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)13 BadRequestException (co.cask.cdap.common.BadRequestException)10 NotFoundException (co.cask.cdap.common.NotFoundException)9 ApplicationManager (co.cask.cdap.test.ApplicationManager)9 ServiceManager (co.cask.cdap.test.ServiceManager)9 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)6 IOException (java.io.IOException)6 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)5 StreamNotFoundException (co.cask.cdap.common.StreamNotFoundException)5 TypeToken (com.google.common.reflect.TypeToken)5 TypeToken (com.google.gson.reflect.TypeToken)5 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)4 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)4 Instances (co.cask.cdap.proto.Instances)4 SparkManager (co.cask.cdap.test.SparkManager)4 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)3