Search in sources :

Example 6 with ConnectionEntityId

use of io.cdap.cdap.proto.id.ConnectionEntityId in project cdap by caskdata.

the class ConnectionHandler method browse.

/**
 * Browse the connection on a given path.
 */
@POST
@TransactionPolicy(value = TransactionControl.EXPLICIT)
@Path(API_VERSION + "/contexts/{context}/connections/{connection}/browse")
public void browse(HttpServiceRequest request, HttpServiceResponder responder, @PathParam("context") String namespace, @PathParam("connection") String connection) {
    respond(namespace, responder, namespaceSummary -> {
        if (namespaceSummary.getName().equalsIgnoreCase(NamespaceId.SYSTEM.getNamespace())) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Browsing connection in system namespace is currently not supported");
            return;
        }
        contextAccessEnforcer.enforce(new ConnectionEntityId(namespace, ConnectionId.getConnectionId(connection)), StandardPermission.USE);
        String browseRequestString = StandardCharsets.UTF_8.decode(request.getContent()).toString();
        BrowseRequest browseRequest = GSON.fromJson(browseRequestString, BrowseRequest.class);
        if (browseRequest == null) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "The request body is empty");
            return;
        }
        if (browseRequest.getPath() == null) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Path is not provided in the browse request");
            return;
        }
        Connection conn = store.getConnection(new ConnectionId(namespaceSummary, connection));
        if (getContext().isRemoteTaskEnabled()) {
            executeRemotely(namespace, browseRequestString, conn, RemoteConnectionBrowseTask.class, responder);
        } else {
            browseLocally(namespaceSummary.getName(), browseRequest, conn, responder);
        }
        Metrics child = metrics.child(ImmutableMap.of(Constants.Metrics.Tag.APP_ENTITY_TYPE, Constants.CONNECTION_SERVICE_NAME, Constants.Metrics.Tag.APP_ENTITY_TYPE_NAME, conn.getConnectionType()));
        child.count(Constants.Metrics.Connection.CONNECTION_BROWSE_COUNT, 1);
    });
}
Also used : ConnectionId(io.cdap.cdap.etl.proto.connection.ConnectionId) Metrics(io.cdap.cdap.api.metrics.Metrics) HttpURLConnection(java.net.HttpURLConnection) Connection(io.cdap.cdap.etl.proto.connection.Connection) BrowseRequest(io.cdap.cdap.etl.api.connector.BrowseRequest) ConnectionEntityId(io.cdap.cdap.proto.id.ConnectionEntityId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) TransactionPolicy(io.cdap.cdap.api.annotation.TransactionPolicy)

Example 7 with ConnectionEntityId

use of io.cdap.cdap.proto.id.ConnectionEntityId in project cdap by caskdata.

the class ConnectionHandler method sample.

/**
 * Retrive sample result for the connection
 */
@POST
@TransactionPolicy(value = TransactionControl.EXPLICIT)
@Path(API_VERSION + "/contexts/{context}/connections/{connection}/sample")
public void sample(HttpServiceRequest request, HttpServiceResponder responder, @PathParam("context") String namespace, @PathParam("connection") String connection) {
    respond(namespace, responder, namespaceSummary -> {
        if (namespaceSummary.getName().equalsIgnoreCase(NamespaceId.SYSTEM.getNamespace())) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Sampling connection in system namespace is currently not supported");
            return;
        }
        contextAccessEnforcer.enforce(new ConnectionEntityId(namespace, ConnectionId.getConnectionId(connection)), StandardPermission.USE);
        String sampleRequestString = StandardCharsets.UTF_8.decode(request.getContent()).toString();
        SampleRequest sampleRequest = GSON.fromJson(sampleRequestString, SampleRequest.class);
        if (sampleRequest == null) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "The request body is empty");
            return;
        }
        if (sampleRequest.getPath() == null) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Path is not provided in the sample request");
            return;
        }
        if (sampleRequest.getLimit() <= 0) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Limit should be greater than 0");
            return;
        }
        Connection conn = store.getConnection(new ConnectionId(namespaceSummary, connection));
        if (getContext().isRemoteTaskEnabled()) {
            executeRemotely(namespace, sampleRequestString, conn, RemoteConnectionSampleTask.class, responder);
        } else {
            sampleLocally(namespaceSummary.getName(), sampleRequestString, conn, responder);
        }
        Metrics child = metrics.child(ImmutableMap.of(Constants.Metrics.Tag.APP_ENTITY_TYPE, Constants.CONNECTION_SERVICE_NAME, Constants.Metrics.Tag.APP_ENTITY_TYPE_NAME, conn.getConnectionType()));
        child.count(Constants.Metrics.Connection.CONNECTION_SAMPLE_COUNT, 1);
        // sample will also generate the spec, so add the metric for it
        child.count(Constants.Metrics.Connection.CONNECTION_SPEC_COUNT, 1);
    });
}
Also used : ConnectionId(io.cdap.cdap.etl.proto.connection.ConnectionId) Metrics(io.cdap.cdap.api.metrics.Metrics) HttpURLConnection(java.net.HttpURLConnection) Connection(io.cdap.cdap.etl.proto.connection.Connection) SampleRequest(io.cdap.cdap.etl.api.connector.SampleRequest) ConnectionEntityId(io.cdap.cdap.proto.id.ConnectionEntityId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) TransactionPolicy(io.cdap.cdap.api.annotation.TransactionPolicy)

Aggregations

ConnectionEntityId (io.cdap.cdap.proto.id.ConnectionEntityId)7 TransactionPolicy (io.cdap.cdap.api.annotation.TransactionPolicy)5 Metrics (io.cdap.cdap.api.metrics.Metrics)5 Connection (io.cdap.cdap.etl.proto.connection.Connection)5 ConnectionId (io.cdap.cdap.etl.proto.connection.ConnectionId)5 HttpURLConnection (java.net.HttpURLConnection)5 Path (javax.ws.rs.Path)5 POST (javax.ws.rs.POST)3 BrowseEntity (io.cdap.cdap.etl.api.connector.BrowseEntity)1 BrowseRequest (io.cdap.cdap.etl.api.connector.BrowseRequest)1 SampleRequest (io.cdap.cdap.etl.api.connector.SampleRequest)1 ArtifactSelectorConfig (io.cdap.cdap.etl.proto.ArtifactSelectorConfig)1 ConnectionCreationRequest (io.cdap.cdap.etl.proto.connection.ConnectionCreationRequest)1 ConnectionNotFoundException (io.cdap.cdap.etl.proto.connection.ConnectionNotFoundException)1 PluginInfo (io.cdap.cdap.etl.proto.connection.PluginInfo)1 SpecGenerationRequest (io.cdap.cdap.etl.proto.connection.SpecGenerationRequest)1 File (java.io.File)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 Test (org.junit.Test)1