Search in sources :

Example 1 with LocationBodyProducer

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));
        }
    }
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) NoOpInternalAuthenticator(io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator) HttpErrorStatusProvider(io.cdap.cdap.api.common.HttpErrorStatusProvider) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) LocationBodyProducer(io.cdap.cdap.common.http.LocationBodyProducer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) File(java.io.File) Location(org.apache.twill.filesystem.Location) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with LocationBodyProducer

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));
}
Also used : DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) NotFoundException(io.cdap.cdap.common.NotFoundException) Location(org.apache.twill.filesystem.Location) LocationBodyProducer(io.cdap.cdap.common.http.LocationBodyProducer) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with LocationBodyProducer

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);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ZonedDateTime(java.time.ZonedDateTime) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ArtifactDetail(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail) Location(org.apache.twill.filesystem.Location) LocationBodyProducer(io.cdap.cdap.common.http.LocationBodyProducer) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

LocationBodyProducer (io.cdap.cdap.common.http.LocationBodyProducer)3 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 Location (org.apache.twill.filesystem.Location)3 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)2 HttpErrorStatusProvider (io.cdap.cdap.api.common.HttpErrorStatusProvider)1 NotFoundException (io.cdap.cdap.common.NotFoundException)1 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)1 NoOpInternalAuthenticator (io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator)1 RemoteClient (io.cdap.cdap.common.internal.remote.RemoteClient)1 RemoteClientFactory (io.cdap.cdap.common.internal.remote.RemoteClientFactory)1 ArtifactDetail (io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail)1 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)1 HttpRequestConfig (io.cdap.common.http.HttpRequestConfig)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 File (java.io.File)1 ZonedDateTime (java.time.ZonedDateTime)1