use of io.cdap.cdap.common.http.LocationBodyProducer 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.common.http.LocationBodyProducer in project cdap by caskdata.
the class FileFetcherHttpHandlerInternal method download.
/**
* Download the file specified by the given path in the URL.
*
* @param request {@link HttpRequest}
* @param responder {@link HttpResponse}
*/
@GET
@Path("/location/**")
public void download(HttpRequest request, HttpResponder responder) throws Exception {
String prefix = String.format("%s/location/", Constants.Gateway.INTERNAL_API_VERSION_3);
String path = request.uri().substring(prefix.length());
Location location = Locations.getLocationFromAbsolutePath(locationFactory, path);
if (!location.exists()) {
throw new NotFoundException(String.format("Path %s is not found", path));
}
responder.sendContent(HttpResponseStatus.OK, new LocationBodyProducer(location), new DefaultHttpHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM));
}
use of io.cdap.cdap.common.http.LocationBodyProducer in project cdap by caskdata.
the class ArtifactHttpHandlerInternal method getArtifactBytes.
@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}/download")
public void getArtifactBytes(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @QueryParam("scope") @DefaultValue("user") String scope) throws Exception {
NamespaceId namespace = validateAndGetScopedNamespace(Ids.namespace(namespaceId), scope);
ArtifactId artifactId = new ArtifactId(namespace.getNamespace(), artifactName, artifactVersion);
ArtifactDetail artifactDetail = artifactRepository.getArtifact(Id.Artifact.fromEntityId(artifactId));
Location location = artifactDetail.getDescriptor().getLocation();
ZonedDateTime newModifiedDate = ZonedDateTime.ofInstant(Instant.ofEpochMilli(location.lastModified()), ZoneId.of("GMT"));
HttpHeaders headers = new DefaultHttpHeaders().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_OCTET_STREAM).add(HttpHeaderNames.LAST_MODIFIED, newModifiedDate.format(DateTimeFormatter.RFC_1123_DATE_TIME));
String lastModified = request.headers().get(HttpHeaderNames.IF_MODIFIED_SINCE);
if (areDatesEqual(lastModified, newModifiedDate)) {
responder.sendStatus(HttpResponseStatus.NOT_MODIFIED, headers);
return;
}
responder.sendContent(HttpResponseStatus.OK, new LocationBodyProducer(location), headers);
}
Aggregations