本文整理了Java中co.cask.common.http.HttpResponse
类的一些代码示例,展示了HttpResponse
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpResponse
类的具体详情如下:
包路径:co.cask.common.http.HttpResponse
类名称:HttpResponse
暂无
代码示例来源:origin: cdapio/cdap
private List<ArtifactInfo> getArtifactsList(HttpRequest httpRequest) throws IOException {
HttpResponse httpResponse = remoteClient.execute(httpRequest);
if (httpResponse.getResponseCode() == HttpResponseStatus.NOT_FOUND.code()) {
throw new IOException("Could not list artifacts, endpoint not found");
}
if (httpResponse.getResponseCode() == 200) {
return GSON.fromJson(httpResponse.getResponseBodyAsString(), ARTIFACT_INFO_LIST_TYPE);
}
throw new IOException("Exception while getting artifacts list " + httpResponse.getResponseBodyAsString());
}
代码示例来源:origin: caskdata/cdap
@Nullable
@Override
public RollbackDetail publish(StoreRequest request) throws TopicNotFoundException, IOException {
HttpResponse response = performWriteRequest(request, true);
byte[] body = response.getResponseBody();
if (body.length == 0) {
return null;
}
// It has rollback detail, verify the content-type and decode it
verifyContentType(response.getHeaders().asMap(), "avro/binary");
return new ClientRollbackDetail(body);
}
代码示例来源:origin: co.cask.cdap/cdap-authentication-client
/**
* Executes fetch access token request.
*
* @param request the http request to fetch access token from the authentication server
* @return {@link AccessToken} object containing the access token
* @throws IOException IOException in case of a problem or the connection was aborted or if the access token is not
* received successfully from the authentication server
*/
private AccessToken execute(HttpRequest request) throws IOException {
HttpResponse response = HttpRequests.execute(request, getHttpRequestConfig());
LOG.debug("Got response {} - {} from {}", response.getResponseCode(), response.getResponseMessage(), pingURI);
if (response.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new HttpFailureException(response.getResponseMessage(), response.getResponseCode());
}
Map<String, String> responseMap =
ObjectResponse.fromJsonBody(response, ACCESS_TOKEN_RESPONSE_TYPE_TOKEN).getResponseObject();
String tokenValue = responseMap.get(ACCESS_TOKEN_KEY);
String tokenType = responseMap.get(TOKEN_TYPE_KEY);
String expiresInStr = responseMap.get(EXPIRES_IN_KEY);
LOG.debug("Response map from auth server: {}", responseMap);
if (StringUtils.isEmpty(tokenValue) || StringUtils.isEmpty(tokenType) || StringUtils.isEmpty(expiresInStr)) {
throw new IOException("Unexpected response was received from the authentication server.");
}
return new AccessToken(tokenValue, Long.valueOf(expiresInStr), tokenType);
}
代码示例来源:origin: cdapio/cdap
public void deleteModule(String moduleName) throws DatasetManagementException {
HttpResponse response = doDelete("modules/" + moduleName);
if (HttpResponseStatus.CONFLICT.code() == response.getResponseCode()) {
throw new ModuleConflictException(String.format("Failed to delete module %s due to conflict, details: %s",
moduleName, response));
}
if (HttpResponseStatus.OK.code() != response.getResponseCode()) {
throw new DatasetManagementException(String.format("Failed to delete module %s, details: %s",
moduleName, response));
}
}
代码示例来源:origin: caskdata/cdap
/**
* Gets the live info of a system service.
*
* @param serviceName Name of the system service
* @return live info of the system service
* @throws IOException if a network error occurred
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public SystemServiceLiveInfo getSystemServiceLiveInfo(String serviceName)
throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
URL url = config.resolveURLV3(String.format("system/services/%s/live-info", serviceName));
HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(),
HttpURLConnection.HTTP_NOT_FOUND);
String responseBody = new String(response.getResponseBody());
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(new SystemServiceId(serviceName));
}
return GSON.fromJson(responseBody, SystemServiceLiveInfo.class);
}
代码示例来源:origin: caskdata/cdap
output.printf("< %s %s\n", response.getResponseCode(), response.getResponseMessage());
for (Map.Entry<String, String> header : response.getHeaders().entries()) {
output.printf("< %s: %s\n", header.getKey(), header.getValue());
output.print(response.getResponseBodyAsString());
代码示例来源:origin: cdapio/cdap
private <T> T parseJson(HttpResponse response, Type type) throws ExploreException {
String responseString = response.getResponseBodyAsString();
try {
return GSON.fromJson(responseString, type);
} catch (JsonParseException e) {
String message = String.format("Cannot parse server response: %s", responseString);
LOG.error(message, e);
throw new ExploreException(message, e);
}
}
代码示例来源:origin: cdapio/cdap
@Override
public SecureStoreData get(String namespace, String name) throws Exception {
// 1. Get metadata of the secure key
HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET,
createPath(namespace, name) + "/metadata").build();
HttpResponse response = remoteClient.execute(request);
handleResponse(response, namespace, name, String.format("Error occurred while getting metadata for key %s:%s",
namespace, name));
SecureStoreMetadata metadata = GSON.fromJson(response.getResponseBodyAsString(), SecureStoreMetadata.class);
// 2. Get sensitive data for the secure key
request = remoteClient.requestBuilder(HttpMethod.GET, createPath(namespace, name)).build();
response = remoteClient.execute(request);
handleResponse(response, namespace, name, String.format("Error occurred while getting key %s:%s",
namespace, name));
// response is not a json object
byte[] data = response.getResponseBody();
return new SecureStoreData(metadata, data);
}
代码示例来源:origin: co.cask.cdap/cdap-explore-client
private String parseResponseAsMap(HttpResponse response, String key) throws ExploreException {
Map<String, String> responseMap = parseJson(response, MAP_TYPE_TOKEN);
if (responseMap.containsKey(key)) {
return responseMap.get(key);
}
String message = String.format("Cannot find key %s in server response: %s", key,
new String(response.getResponseBody(), Charsets.UTF_8));
LOG.error(message);
throw new ExploreException(message);
}
代码示例来源:origin: cdapio/cdap
/**
* Returns the first value of the given header from the given response. If there is no such header, {@code null} is
* returned.
*/
@Nullable
private static String getFirstHeaderValue(HttpResponse response, String name) {
return response.getHeaders().get(name).stream().findFirst().orElse(null);
}
}
代码示例来源:origin: cdapio/cdap
/**
* Deletes all dataset instances inside the namespace of this client is operating in.
*/
void deleteInstances() throws DatasetManagementException {
HttpResponse response = doDelete("datasets");
if (HttpResponseStatus.OK.code() != response.getResponseCode()) {
throw new DatasetManagementException(String.format("Failed to delete instances, details: %s", response));
}
}
代码示例来源:origin: caskdata/cdap
/**
* Gets the status of a system service.
*
* @param serviceName Name of the system service
* @return status of the system service
* @throws IOException if a network error occurred
* @throws NotFoundException if the system service with the specified name could not be found
* @throws BadRequestException if the operation was not valid for the system service
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public String getSystemServiceStatus(String serviceName)
throws IOException, NotFoundException, BadRequestException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveURL(String.format("system/services/%s/status", serviceName));
HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(),
HttpURLConnection.HTTP_NOT_FOUND,
HttpURLConnection.HTTP_BAD_REQUEST);
String responseBody = new String(response.getResponseBody());
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(new SystemServiceId(serviceName));
} else if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(responseBody);
}
Map<String, String> status = GSON.fromJson(responseBody, new TypeToken<Map<String, String>>() { }.getType());
return status.get("status");
}
代码示例来源:origin: co.cask.cdap/cdap-cli
output.printf("< %s %s\n", response.getResponseCode(), response.getResponseMessage());
for (Map.Entry<String, String> header : response.getHeaders().entries()) {
output.printf("< %s: %s\n", header.getKey(), header.getValue());
output.print(response.getResponseBodyAsString());
代码示例来源:origin: caskdata/cdap
/**
* @param metadataEntity the {@link MetadataEntity} for which to retrieve metadata
* @param scope the {@link MetadataScope} to retrieve the metadata from. If null, this method retrieves
* metadata from both {@link MetadataScope#SYSTEM} and {@link MetadataScope#USER}
* @return The metadata for the entity.
*/
public Set<MetadataRecord> getMetadata(MetadataEntity metadataEntity, @Nullable MetadataScope scope,
boolean aggregateRuns)
throws BadRequestException, UnauthenticatedException, IOException, UnauthorizedException {
HttpResponse response = getMetadataHelper(metadataEntity, scope, aggregateRuns);
return GSON.fromJson(response.getResponseBodyAsString(), SET_METADATA_RECORD_TYPE);
}
代码示例来源:origin: cdapio/cdap
private String parseResponseAsMap(HttpResponse response, String key) throws ExploreException {
Map<String, String> responseMap = parseJson(response, MAP_TYPE_TOKEN);
if (responseMap.containsKey(key)) {
return responseMap.get(key);
}
String message = String.format("Cannot find key %s in server response: %s", key,
new String(response.getResponseBody(), Charsets.UTF_8));
LOG.error(message);
throw new ExploreException(message);
}
代码示例来源:origin: co.cask.cdap/cdap-app-fabric
private List<ArtifactInfo> getArtifactsList(HttpRequest httpRequest) throws IOException {
HttpResponse httpResponse = remoteClient.execute(httpRequest);
if (httpResponse.getResponseCode() == HttpResponseStatus.NOT_FOUND.code()) {
throw new IOException("Could not list artifacts, endpoint not found");
}
if (httpResponse.getResponseCode() == 200) {
return GSON.fromJson(httpResponse.getResponseBodyAsString(), ARTIFACT_INFO_LIST_TYPE);
}
throw new IOException("Exception while getting artifacts list " + httpResponse.getResponseBodyAsString());
}
代码示例来源:origin: cdapio/cdap
@Override
public QueryHandle getTables(@Nullable String catalog, @Nullable String schemaPattern, String tableNamePattern,
@Nullable List<String> tableTypes) throws ExploreException, SQLException {
String body = GSON.toJson(new TablesArgs(catalog, schemaPattern, tableNamePattern, tableTypes));
String resource = String.format("namespaces/%s/data/explore/jdbc/tables", schemaPattern);
HttpResponse response = doPost(resource, body, null);
if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
return QueryHandle.fromId(parseResponseAsMap(response, "handle"));
}
throw new ExploreException("Cannot get the tables. Reason: " + response);
}
代码示例来源:origin: caskdata/cdap
/**
* Sets the number of instances the system service is running on.
*
* @param serviceName name of the system service
* @param instances number of instances the system service is running on
* @throws IOException if a network error occurred
* @throws NotFoundException if the system service with the specified name was not found
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public void setSystemServiceInstances(String serviceName, int instances)
throws IOException, NotFoundException, BadRequestException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveURL(String.format("system/services/%s/instances", serviceName));
HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(new Instances(instances))).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(),
HttpURLConnection.HTTP_NOT_FOUND,
HttpURLConnection.HTTP_BAD_REQUEST);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(new SystemServiceId(serviceName));
} else if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(new String(response.getResponseBody()));
}
}
代码示例来源:origin: caskdata/cdap
public void ping() throws IOException, UnauthenticatedException, UnauthorizedException {
HttpResponse response = restClient.execute(
HttpMethod.GET, config.resolveURLNoVersion("ping"), config.getAccessToken());
if (!Objects.equals(response.getResponseBodyAsString(), "OK.\n")) {
throw new IOException("Unexpected response body");
}
}
代码示例来源:origin: co.cask.cdap/cdap-authentication-client
/**
* Fetches the available authentication server URL, if authentication is enabled in the gateway server,
* otherwise, empty string will be returned.
*
* @return string value of the authentication server URL
* @throws IOException IOException in case of a problem or the connection was aborted or if url list is empty
*/
private String fetchAuthURI() throws IOException {
if (pingURI == null) {
throw new IllegalStateException("Connection information not set!");
}
LOG.debug("Try to get the authentication URI from the gateway server: {}.", pingURI);
HttpResponse response = HttpRequests.execute(HttpRequest.get(pingURI.toURL()).build(), getHttpRequestConfig());
LOG.debug("Got response {} - {} from {}", response.getResponseCode(), response.getResponseMessage(), pingURI);
if (response.getResponseCode() != HttpURLConnection.HTTP_UNAUTHORIZED) {
return "";
}
Map<String, List<String>> responseMap =
ObjectResponse.fromJsonBody(response, AUTH_URL_RESPONSE_TYPE_TOKEN).getResponseObject();
LOG.debug("Response map from gateway server: {}", responseMap);
String result;
List<String> uriList = responseMap.get(AUTH_URI_KEY);
if (uriList != null && !uriList.isEmpty()) {
result = uriList.get(RANDOM.nextInt(uriList.size()));
} else {
throw new IOException("Authentication servers list is empty.");
}
return result;
}
内容来源于网络,如有侵权,请联系作者删除!