use of io.cdap.cdap.api.common.HttpErrorStatusProvider in project cdap by caskdata.
the class ArtifactCacheHttpHandlerInternal method fetchArtifact.
@GET
@Path("peers/{peer}/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}")
public void fetchArtifact(HttpRequest request, HttpResponder responder, @PathParam("peer") String peer, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion) throws Exception {
ArtifactId artifactId = new ArtifactId(namespaceId, artifactName, artifactVersion);
try {
String endpoint = tetheringStore.getPeer(peer).getEndpoint();
RemoteClientFactory factory = new RemoteClientFactory(new NoOpDiscoveryServiceClient(endpoint), new NoOpInternalAuthenticator(), remoteAuthenticator);
HttpRequestConfig config = new DefaultHttpRequestConfig(true);
RemoteClient remoteClient = factory.createRemoteClient("", config, Constants.Gateway.INTERNAL_API_VERSION_3);
File artifactPath = cache.getArtifact(artifactId, peer, remoteClient);
Location artifactLocation = Locations.toLocation(artifactPath);
responder.sendContent(HttpResponseStatus.OK, new LocationBodyProducer(artifactLocation), new DefaultHttpHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM));
} catch (Exception ex) {
if (ex instanceof HttpErrorStatusProvider) {
HttpResponseStatus status = HttpResponseStatus.valueOf(((HttpErrorStatusProvider) ex).getStatusCode());
responder.sendString(status, exceptionToJson(ex));
} else {
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, exceptionToJson(ex));
}
}
}
use of io.cdap.cdap.api.common.HttpErrorStatusProvider in project cdap by caskdata.
the class DelayedHttpServiceResponder method setFailure.
/**
* Since calling one of the send methods multiple times logs a warning, upon transaction failures this
* method is called to allow setting the failure response without an additional warning.
*/
public void setFailure(Throwable t) {
LOG.error("Exception occurred while handling request:", t);
String message;
int code;
if (t instanceof HttpErrorStatusProvider) {
HttpErrorStatusProvider statusProvider = (HttpErrorStatusProvider) t;
code = statusProvider.getStatusCode();
message = statusProvider.getMessage();
} else {
message = "Exception occurred while handling request: " + Throwables.getRootCause(t).getMessage();
code = HttpResponseStatus.INTERNAL_SERVER_ERROR.code();
}
ByteBuf content = Unpooled.copiedBuffer(message, StandardCharsets.UTF_8);
bufferedResponse = new BufferedResponse(code, "text/plain; charset=" + Charsets.UTF_8.name(), content, null, null);
}
use of io.cdap.cdap.api.common.HttpErrorStatusProvider in project cdap by caskdata.
the class ArtifactLocalizerHttpHandlerInternal method artifact.
@GET
@Path("/artifact/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}")
public void artifact(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @QueryParam("unpack") @DefaultValue("true") boolean unpack) throws Exception {
ArtifactId artifactId = new ArtifactId(namespaceId, artifactName, artifactVersion);
try {
File artifactPath = unpack ? artifactLocalizer.getAndUnpackArtifact(artifactId) : artifactLocalizer.getArtifact(artifactId);
responder.sendString(HttpResponseStatus.OK, artifactPath.toString());
} catch (Exception ex) {
if (ex instanceof HttpErrorStatusProvider) {
HttpResponseStatus status = HttpResponseStatus.valueOf(((HttpErrorStatusProvider) ex).getStatusCode());
responder.sendString(status, exceptionToJson(ex));
} else {
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, exceptionToJson(ex));
}
}
}
use of io.cdap.cdap.api.common.HttpErrorStatusProvider in project cdap by caskdata.
the class DatasetInstanceService method get.
/**
* Gets the metadata for a dataset instance.
*
* @param instance instance to get
* @return the dataset instance's {@link DatasetMeta}
* @throws NotFoundException if either the namespace or dataset instance is not found,
* @throws IOException if there is a problem in making an HTTP request to check if the namespace exists
* @throws UnauthorizedException if perimeter security and authorization are enabled, and the current user does not
* have any privileges on the #instance
*/
DatasetMeta get(final DatasetId instance) throws Exception {
// ensure user has correct privileges before getting the meta if the dataset is not a system dataset
if (!DatasetsUtil.isSystemDatasetInUserNamespace(instance)) {
LOG.trace("Authorizing GET for dataset {}", instance.getDataset());
accessEnforcer.enforce(instance, authenticationContext.getPrincipal(), StandardPermission.GET);
LOG.trace("Authorized GET for dataset {}", instance.getDataset());
}
// Application Deployment first makes a call to the dataset service to check if the instance already exists with
// a different type. To make sure that that call responds with the right exceptions if necessary, first fetch the
// meta from the cache and throw appropriate exceptions if necessary.
DatasetMeta datasetMeta;
try {
LOG.trace("Retrieving instance metadata from cache for dataset {}", instance.getDataset());
datasetMeta = metaCache.get(instance);
LOG.trace("Retrieved instance metadata from cache for dataset {}", instance.getDataset());
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if ((cause instanceof Exception) && (cause instanceof HttpErrorStatusProvider)) {
throw (Exception) cause;
}
throw e;
}
return datasetMeta;
}
Aggregations