Search in sources :

Example 1 with Batch

use of com.smoketurner.uploader.core.Batch in project uploader by smoketurner.

the class BatchResource method upload.

@POST
@Consumes(MediaType.WILDCARD)
public Response upload(@Context SecurityContext context, InputStream input) {
    final Optional<String> customerId = AuthHandler.getCustomerId(context.getUserPrincipal());
    if (!customerId.isPresent()) {
        throw new WebApplicationException("No customerId found in request");
    }
    final Batch batch;
    try {
        batch = Batch.create(customerId.get());
    } catch (IOException e) {
        LOGGER.error("Unable to create batch", e);
        throw new WebApplicationException("Unable to create batch", e);
    }
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
        reader.lines().map(line -> line.getBytes(StandardCharsets.UTF_8)).forEach(line -> {
            try {
                batch.add(line);
            } catch (IOException e) {
                LOGGER.error("Unable to process line", e);
            }
        });
    } catch (IOException e) {
        LOGGER.error("Unable to read input", e);
        throw new WebApplicationException("Unable to read input", e);
    }
    uploader.upload(batch);
    return Response.accepted().build();
}
Also used : Uploader(com.smoketurner.uploader.core.Uploader) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) Logger(org.slf4j.Logger) Path(javax.ws.rs.Path) SecurityContext(javax.ws.rs.core.SecurityContext) LoggerFactory(org.slf4j.LoggerFactory) AuthHandler(com.smoketurner.uploader.handler.AuthHandler) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) Batch(com.smoketurner.uploader.core.Batch) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) BufferedReader(java.io.BufferedReader) Nonnull(javax.annotation.Nonnull) InputStream(java.io.InputStream) WebApplicationException(javax.ws.rs.WebApplicationException) InputStreamReader(java.io.InputStreamReader) Batch(com.smoketurner.uploader.core.Batch) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 2 with Batch

use of com.smoketurner.uploader.core.Batch in project uploader by smoketurner.

the class BatchHandler method channelInactive.

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    final Batch batch = curBatch.get();
    if (batch != null && !batch.isEmpty()) {
        LOGGER.debug("Channel inactive, sending remaining batch of {} events", batch.getCount());
        batch.finish();
        ctx.fireChannelRead(batch);
    } else if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Channel inactive, current batch is empty");
    }
    curBatch.set(null);
}
Also used : Batch(com.smoketurner.uploader.core.Batch)

Example 3 with Batch

use of com.smoketurner.uploader.core.Batch in project uploader by smoketurner.

the class BatchHandler method getBatch.

@Nullable
private Batch getBatch(final ChannelHandlerContext ctx) throws IOException {
    final Batch batch = curBatch.get();
    if (batch != null) {
        return batch;
    }
    final Batch newBatch = newBatch(ctx);
    if (curBatch.compareAndSet(null, newBatch)) {
        return newBatch;
    }
    return curBatch.get();
}
Also used : Batch(com.smoketurner.uploader.core.Batch) Nullable(javax.annotation.Nullable)

Example 4 with Batch

use of com.smoketurner.uploader.core.Batch in project uploader by smoketurner.

the class BatchHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, byte[] msg) throws Exception {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("channelRead0: {}", new String(msg, StandardCharsets.UTF_8));
    }
    eventMeter.mark();
    final Batch batch = getBatch(ctx);
    if (batch == null) {
        LOGGER.warn("channelRead0: batch is null");
        return;
    }
    batch.add(msg);
    if (batch.size() > maxUploadBytes) {
        LOGGER.debug("Batch size {} bytes exceeds max upload size of {} bytes", batch.size(), maxUploadBytes);
        batch.finish();
        ctx.fireChannelRead(batch);
        curBatch.set(newBatch(ctx));
    }
}
Also used : Batch(com.smoketurner.uploader.core.Batch)

Aggregations

Batch (com.smoketurner.uploader.core.Batch)4 Uploader (com.smoketurner.uploader.core.Uploader)1 AuthHandler (com.smoketurner.uploader.handler.AuthHandler)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Context (javax.ws.rs.core.Context)1 MediaType (javax.ws.rs.core.MediaType)1 Response (javax.ws.rs.core.Response)1 SecurityContext (javax.ws.rs.core.SecurityContext)1