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);
}
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;
}
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);
}
}
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);
}
}
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);
}
Aggregations