Search in sources :

Example 1 with ChunkResponder

use of io.cdap.http.ChunkResponder in project cdap by caskdata.

the class AppLifecycleHttpHandler method upgradeApplications.

/**
 * Upgrades a lis of existing application to use latest version of application artifact and plugin artifacts.
 *
 * <pre>
 * {@code
 * [
 *   {"name":"XYZ"},
 *   {"name":"ABC"},
 *   {"name":"FOO"},
 * ]
 * }
 * </pre>
 * The response will be an array of {@link ApplicationUpdateDetail} object, which either indicates a success (200) or
 * failure for each of the requested application in the same order as the request. The failure also indicates reason
 * for the error. The response will be sent via ChunkResponder to continuously stream upgrade result per application.
 */
@POST
@Path("/upgrade")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void upgradeApplications(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @QueryParam("artifactScope") Set<String> artifactScopes, @QueryParam("allowSnapshot") boolean allowSnapshot) throws Exception {
    // TODO: (CDAP-16910) Improve batch API performance as each application upgrade is an event independent of each
    // other.
    List<ApplicationId> appIds = decodeAndValidateBatchApplicationRecord(validateNamespace(namespaceId), request);
    Set<ArtifactScope> allowedArtifactScopes = getArtifactScopes(artifactScopes);
    try (ChunkResponder chunkResponder = responder.sendChunkStart(HttpResponseStatus.OK)) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try (JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
            jsonWriter.beginArray();
            for (ApplicationId appId : appIds) {
                ApplicationUpdateDetail updateDetail;
                try {
                    applicationLifecycleService.upgradeApplication(appId, allowedArtifactScopes, allowSnapshot);
                    updateDetail = new ApplicationUpdateDetail(appId);
                } catch (UnsupportedOperationException e) {
                    String errorMessage = String.format("Application %s does not support upgrade.", appId);
                    updateDetail = new ApplicationUpdateDetail(appId, new NotImplementedException(errorMessage));
                } catch (InvalidArtifactException | NotFoundException e) {
                    updateDetail = new ApplicationUpdateDetail(appId, e);
                } catch (Exception e) {
                    updateDetail = new ApplicationUpdateDetail(appId, new ServiceException("Upgrade failed due to internal error.", e, HttpResponseStatus.INTERNAL_SERVER_ERROR));
                    LOG.error("Application upgrade failed with exception", e);
                }
                GSON.toJson(updateDetail, ApplicationUpdateDetail.class, jsonWriter);
                jsonWriter.flush();
                chunkResponder.sendChunk(Unpooled.wrappedBuffer(outputStream.toByteArray()));
                outputStream.reset();
                chunkResponder.flush();
            }
            jsonWriter.endArray();
        }
        chunkResponder.sendChunk(Unpooled.wrappedBuffer(outputStream.toByteArray()));
    }
}
Also used : NotImplementedException(io.cdap.cdap.common.NotImplementedException) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonWriter(com.google.gson.stream.JsonWriter) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) WriteConflictException(io.cdap.cdap.internal.app.runtime.artifact.WriteConflictException) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) ConflictException(io.cdap.cdap.common.ConflictException) NotImplementedException(io.cdap.cdap.common.NotImplementedException) ExecutionException(java.util.concurrent.ExecutionException) AccessException(io.cdap.cdap.api.security.AccessException) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) ArtifactAlreadyExistsException(io.cdap.cdap.common.ArtifactAlreadyExistsException) NotFoundException(io.cdap.cdap.common.NotFoundException) ServiceException(io.cdap.cdap.common.ServiceException) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(io.cdap.cdap.common.BadRequestException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) ApplicationUpdateDetail(io.cdap.cdap.proto.ApplicationUpdateDetail) ServiceException(io.cdap.cdap.common.ServiceException) OutputStreamWriter(java.io.OutputStreamWriter) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ChunkResponder(io.cdap.http.ChunkResponder) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) Path(javax.ws.rs.Path) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Example 2 with ChunkResponder

use of io.cdap.http.ChunkResponder in project cdap by caskdata.

the class JsonListResponderTest method testSendForWholeListResponder.

@Test
public void testSendForWholeListResponder() throws IOException {
    HttpResponder responder = Mockito.mock(HttpResponder.class);
    ChunkResponder chunkResponder = Mockito.mock(ChunkResponder.class);
    Mockito.when(responder.sendChunkStart(HttpResponseStatus.OK)).thenReturn(chunkResponder);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Mockito.doAnswer(invocation -> {
        ByteBuffers.writeToStream(invocation.getArgumentAt(0, ByteBuffer.class), byteArrayOutputStream);
        return null;
    }).when(chunkResponder).sendChunk(Mockito.any(ByteBuffer.class));
    JsonWholeListResponder.respond(new Gson(), responder, (jsonListResponder) -> {
        jsonListResponder.send("application");
    });
    JsonParser parser = new JsonParser();
    JsonArray array = (JsonArray) parser.parse(byteArrayOutputStream.toString());
    Assert.assertEquals(array.get(0).getAsString(), "application");
}
Also used : JsonArray(com.google.gson.JsonArray) HttpResponder(io.cdap.http.HttpResponder) Gson(com.google.gson.Gson) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) ChunkResponder(io.cdap.http.ChunkResponder) JsonParser(com.google.gson.JsonParser) Test(org.junit.Test)

Example 3 with ChunkResponder

use of io.cdap.http.ChunkResponder in project cdap by caskdata.

the class JsonListResponderTest method testMultipleSendForPaginatedListResponder.

@Test
public void testMultipleSendForPaginatedListResponder() throws IOException {
    HttpResponder responder = Mockito.mock(HttpResponder.class);
    ChunkResponder chunkResponder = Mockito.mock(ChunkResponder.class);
    Mockito.when(responder.sendChunkStart(HttpResponseStatus.OK)).thenReturn(chunkResponder);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Mockito.doAnswer(invocation -> {
        ByteBuffers.writeToStream(invocation.getArgumentAt(0, ByteBuffer.class), byteArrayOutputStream);
        return null;
    }).when(chunkResponder).sendChunk(Mockito.any(ByteBuffer.class));
    JsonPaginatedListResponder.respond(new Gson(), responder, "applications", (jsonListResponder) -> {
        jsonListResponder.send("application0");
        jsonListResponder.send("application1");
        return "nextToken";
    });
    JsonParser parser = new JsonParser();
    JsonObject json = (JsonObject) parser.parse(byteArrayOutputStream.toString());
    Assert.assertEquals(json.get("applications").getAsJsonArray().get(0).getAsString(), "application0");
    Assert.assertEquals(json.get("applications").getAsJsonArray().get(1).getAsString(), "application1");
    Assert.assertEquals(json.get("nextPageToken").getAsString(), "nextToken");
}
Also used : HttpResponder(io.cdap.http.HttpResponder) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) ChunkResponder(io.cdap.http.ChunkResponder) JsonParser(com.google.gson.JsonParser) Test(org.junit.Test)

Example 4 with ChunkResponder

use of io.cdap.http.ChunkResponder in project cdap by caskdata.

the class JsonListResponderTest method testSendForPaginatedListResponder.

@Test
public void testSendForPaginatedListResponder() throws IOException {
    HttpResponder responder = Mockito.mock(HttpResponder.class);
    ChunkResponder chunkResponder = Mockito.mock(ChunkResponder.class);
    Mockito.when(responder.sendChunkStart(HttpResponseStatus.OK)).thenReturn(chunkResponder);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Mockito.doAnswer(invocation -> {
        ByteBuffers.writeToStream(invocation.getArgumentAt(0, ByteBuffer.class), byteArrayOutputStream);
        return null;
    }).when(chunkResponder).sendChunk(Mockito.any(ByteBuffer.class));
    JsonPaginatedListResponder.respond(new Gson(), responder, "applications", (jsonListResponder) -> {
        jsonListResponder.send("application");
        return "nextToken";
    });
    JsonParser parser = new JsonParser();
    JsonObject json = (JsonObject) parser.parse(byteArrayOutputStream.toString());
    Assert.assertEquals(json.get("applications").getAsString(), "application");
    Assert.assertEquals(json.get("nextPageToken").getAsString(), "nextToken");
}
Also used : HttpResponder(io.cdap.http.HttpResponder) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) ChunkResponder(io.cdap.http.ChunkResponder) JsonParser(com.google.gson.JsonParser) Test(org.junit.Test)

Aggregations

ChunkResponder (io.cdap.http.ChunkResponder)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Gson (com.google.gson.Gson)3 JsonParser (com.google.gson.JsonParser)3 HttpResponder (io.cdap.http.HttpResponder)3 ByteBuffer (java.nio.ByteBuffer)3 Test (org.junit.Test)3 JsonObject (com.google.gson.JsonObject)2 JsonArray (com.google.gson.JsonArray)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 JsonWriter (com.google.gson.stream.JsonWriter)1 ArtifactScope (io.cdap.cdap.api.artifact.ArtifactScope)1 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)1 AccessException (io.cdap.cdap.api.security.AccessException)1 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)1 ArtifactAlreadyExistsException (io.cdap.cdap.common.ArtifactAlreadyExistsException)1 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)1 BadRequestException (io.cdap.cdap.common.BadRequestException)1 ConflictException (io.cdap.cdap.common.ConflictException)1 InvalidArtifactException (io.cdap.cdap.common.InvalidArtifactException)1