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();
}
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);
}
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();
}
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));
}
}
Aggregations