Search in sources :

Example 1 with HttpErrorStatusProvider

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

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);
}
Also used : HttpErrorStatusProvider(io.cdap.cdap.api.common.HttpErrorStatusProvider) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with HttpErrorStatusProvider

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));
        }
    }
}
Also used : ArtifactId(io.cdap.cdap.proto.id.ArtifactId) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) HttpErrorStatusProvider(io.cdap.cdap.api.common.HttpErrorStatusProvider) File(java.io.File) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 4 with HttpErrorStatusProvider

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;
}
Also used : DatasetMeta(io.cdap.cdap.proto.DatasetMeta) HttpErrorStatusProvider(io.cdap.cdap.api.common.HttpErrorStatusProvider) ExecutionException(java.util.concurrent.ExecutionException) HandlerException(io.cdap.cdap.common.HandlerException) NotFoundException(io.cdap.cdap.common.NotFoundException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) DatasetTypeNotFoundException(io.cdap.cdap.common.DatasetTypeNotFoundException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) IOException(java.io.IOException) DatasetAlreadyExistsException(io.cdap.cdap.common.DatasetAlreadyExistsException) ExecutionException(java.util.concurrent.ExecutionException) DatasetNotFoundException(io.cdap.cdap.common.DatasetNotFoundException)

Aggregations

HttpErrorStatusProvider (io.cdap.cdap.api.common.HttpErrorStatusProvider)4 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)2 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)2 File (java.io.File)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 DatasetAlreadyExistsException (io.cdap.cdap.common.DatasetAlreadyExistsException)1 DatasetNotFoundException (io.cdap.cdap.common.DatasetNotFoundException)1 DatasetTypeNotFoundException (io.cdap.cdap.common.DatasetTypeNotFoundException)1 HandlerException (io.cdap.cdap.common.HandlerException)1 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)1 NotFoundException (io.cdap.cdap.common.NotFoundException)1 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)1 LocationBodyProducer (io.cdap.cdap.common.http.LocationBodyProducer)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 DatasetMeta (io.cdap.cdap.proto.DatasetMeta)1 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)1 HttpRequestConfig (io.cdap.common.http.HttpRequestConfig)1