lab5
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||
import org.springframework.web.servlet.resource.NoResourceFoundException;
|
||||
import static ru.mtusi.messenger.Models.*;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ApiErrors {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ApiErrors.class);
|
||||
|
||||
@ExceptionHandler(ApiException.class)
|
||||
ResponseEntity<ErrorBody> application(ApiException error, HttpServletRequest request) {
|
||||
return response(error.status(), error.code(), error.getMessage(), request);
|
||||
}
|
||||
|
||||
@ExceptionHandler({HttpMessageNotReadableException.class, MethodArgumentTypeMismatchException.class,
|
||||
MissingServletRequestParameterException.class})
|
||||
ResponseEntity<ErrorBody> invalid(Exception error, HttpServletRequest request) {
|
||||
return response(400, "invalid_request", "Некорректный JSON или параметры запроса", request);
|
||||
}
|
||||
|
||||
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
|
||||
ResponseEntity<ErrorBody> media(Exception error, HttpServletRequest request) {
|
||||
return response(415, "unsupported_media_type", "Ожидается Content-Type: application/json", request);
|
||||
}
|
||||
|
||||
@ExceptionHandler(DataAccessException.class)
|
||||
ResponseEntity<ErrorBody> database(DataAccessException error, HttpServletRequest request) {
|
||||
LOG.error("request_id={} error_type={}", request.getAttribute(RequestFilter.REQUEST_ID), error.getClass().getSimpleName());
|
||||
return response(503, "unavailable", "Хранилище данных недоступно", request);
|
||||
}
|
||||
|
||||
@ExceptionHandler({NoResourceFoundException.class, HttpRequestMethodNotSupportedException.class})
|
||||
ResponseEntity<ErrorBody> missing(Exception error, HttpServletRequest request) {
|
||||
return response(404, "not_found", "Объект не найден", request);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
ResponseEntity<ErrorBody> unexpected(Exception error, HttpServletRequest request) {
|
||||
for (Throwable cause = error; cause != null; cause = cause.getCause()) {
|
||||
if (cause instanceof DataAccessException) {
|
||||
LOG.error("request_id={} error_type={}", request.getAttribute(RequestFilter.REQUEST_ID), cause.getClass().getSimpleName());
|
||||
return response(503, "unavailable", "Хранилище данных недоступно", request);
|
||||
}
|
||||
}
|
||||
LOG.error("request_id={} error_type={}", request.getAttribute(RequestFilter.REQUEST_ID),
|
||||
error.getClass().getSimpleName());
|
||||
return response(500, "internal_error", "Внутренняя ошибка сервера", request);
|
||||
}
|
||||
|
||||
private ResponseEntity<ErrorBody> response(int status, String code, String message, HttpServletRequest request) {
|
||||
String id = (String) request.getAttribute(RequestFilter.REQUEST_ID);
|
||||
ResponseEntity.BodyBuilder builder = ResponseEntity.status(status);
|
||||
if (status == 401) {
|
||||
String challenge = request.getRequestURI().equals("/api/v1/auth/sessions")
|
||||
? "Basic realm=\"messenger\", charset=\"UTF-8\""
|
||||
: "Bearer realm=\"messenger\"";
|
||||
builder.header(HttpHeaders.WWW_AUTHENTICATE, challenge);
|
||||
}
|
||||
return builder.body(new ErrorBody(new ErrorDetail(code, message, id)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
public class ApiException extends RuntimeException {
|
||||
private final int status;
|
||||
private final String code;
|
||||
|
||||
public ApiException(int status, String code, String message) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public int status() { return status; }
|
||||
public String code() { return code; }
|
||||
public static ApiException invalid(String message) {
|
||||
return new ApiException(400, "invalid_request", message);
|
||||
}
|
||||
public static ApiException unauthorized() {
|
||||
return new ApiException(401, "unauthorized", "Требуется действующая Bearer-сессия");
|
||||
}
|
||||
public static ApiException notFound() {
|
||||
return new ApiException(404, "not_found", "Объект не найден");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import xyz.lychee.webp4j.WebPCodec;
|
||||
|
||||
import static ru.mtusi.messenger.Models.*;
|
||||
|
||||
public final class AttachmentImageProcessor {
|
||||
private AttachmentImageProcessor() {}
|
||||
|
||||
public static List<Rendered> render(Path source) throws IOException {
|
||||
BufferedImage image = ImageIO.read(source.toFile());
|
||||
if (image == null) throw new ApiException(415, "unsupported_media_type", "Повреждённое изображение");
|
||||
List<Rendered> result = new ArrayList<>();
|
||||
result.add(renderFit("thumbnail", image, 256, 256, "jpg"));
|
||||
result.add(renderCrop(image, 128));
|
||||
result.add(renderFit("optimized", image, 1280, 1280, "webp"));
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Rendered renderFit(String name, BufferedImage source, int maxWidth, int maxHeight, String format) throws IOException {
|
||||
double scale = Math.min(1.0, Math.min((double) maxWidth / source.getWidth(), (double) maxHeight / source.getHeight()));
|
||||
int width = Math.max(1, (int) Math.floor(source.getWidth() * scale));
|
||||
int height = Math.max(1, (int) Math.floor(source.getHeight() * scale));
|
||||
BufferedImage target = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D graphics = target.createGraphics();
|
||||
graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, width, height);
|
||||
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
graphics.drawImage(source, 0, 0, width, height, null); graphics.dispose();
|
||||
return encode(name, target, format);
|
||||
}
|
||||
|
||||
private static Rendered renderCrop(BufferedImage source, int size) throws IOException {
|
||||
int side = Math.min(source.getWidth(), source.getHeight());
|
||||
int x = (source.getWidth() - side) / 2, y = (source.getHeight() - side) / 2;
|
||||
BufferedImage target = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D graphics = target.createGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, size, size);
|
||||
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
graphics.drawImage(source, 0, 0, size, size, x, y, x + side, y + side, null); graphics.dispose();
|
||||
return encode("crop", target, "jpg");
|
||||
}
|
||||
|
||||
private static Rendered encode(String name, BufferedImage image, String format) throws IOException {
|
||||
if (format.equals("webp")) {
|
||||
byte[] bytes = WebPCodec.encodeImage(image, 0.82f);
|
||||
return new Rendered(name, "image/webp", bytes, image.getWidth(), image.getHeight());
|
||||
}
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
if (!ImageIO.write(image, format, output)) throw new IOException("No ImageIO writer for " + format);
|
||||
return new Rendered(name, "image/jpeg", output.toByteArray(), image.getWidth(), image.getHeight());
|
||||
}
|
||||
|
||||
public record Rendered(String name, String contentType, byte[] bytes, int width, int height) {}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReader;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import static ru.mtusi.messenger.Models.*;
|
||||
|
||||
@Service
|
||||
public class AttachmentService {
|
||||
public static final long MAX_BYTES = 10_485_760L;
|
||||
public static final long MAX_PIXELS = 40_000_000L;
|
||||
private final JdbcTemplate jdbc;
|
||||
private final MessengerStore store;
|
||||
private final S3Storage s3;
|
||||
private final TransactionTemplate transaction;
|
||||
|
||||
public AttachmentService(JdbcTemplate jdbc, MessengerStore store, S3Storage s3,
|
||||
org.springframework.transaction.PlatformTransactionManager manager) {
|
||||
this.jdbc = jdbc; this.store = store; this.s3 = s3; this.transaction = new TransactionTemplate(manager);
|
||||
}
|
||||
|
||||
public Attachment upload(UUID actor, UUID chatId, String filename, String contentType, InputStream input) throws IOException {
|
||||
store.chat(actor, chatId); validateFilename(filename);
|
||||
String normalizedType = contentType == null ? "" : contentType.split(";", 2)[0].trim().toLowerCase();
|
||||
if (!normalizedType.equals("image/png") && !normalizedType.equals("image/jpeg")) throw new ApiException(415, "unsupported_media_type", "Поддерживаются только PNG и JPEG");
|
||||
Path temp = Files.createTempFile("messenger-upload-", ".bin"); UUID id = UUID.randomUUID(); String stagingKey = "staging/" + id; boolean uploaded = false;
|
||||
try {
|
||||
long size = copyWithLimit(input, temp, MAX_BYTES); String actualType = detectType(temp);
|
||||
if (!normalizedType.equals(actualType)) throw new ApiException(415, "unsupported_media_type", "MIME не совпадает с содержимым");
|
||||
validateImage(temp); String sha256 = sha256(temp);
|
||||
try (InputStream file = Files.newInputStream(temp)) { s3.put(stagingKey, file, size, actualType); uploaded = true; }
|
||||
Instant now = Instant.now(); UUID jobId = UUID.randomUUID();
|
||||
try {
|
||||
transaction.executeWithoutResult(status -> {
|
||||
jdbc.update("""
|
||||
INSERT INTO attachments(id, chat_id, owner_id, filename, content_type, size_bytes, sha256, object_key, staging_key, status, error_code, pipeline_version, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'queued', NULL, 1, ?, ?)
|
||||
""", id, chatId, actor, filename, actualType, size, sha256, "original/" + id, stagingKey, Timestamp.from(now), Timestamp.from(now));
|
||||
jdbc.update("""
|
||||
INSERT INTO attachment_jobs(job_id, attachment_id, pipeline_version, attempt, status, next_attempt_at, lease_until, error_code, published_at, created_at, updated_at)
|
||||
VALUES (?, ?, 1, 0, 'queued', ?, NULL, NULL, NULL, ?, ?)
|
||||
""", jobId, id, Timestamp.from(now), Timestamp.from(now), Timestamp.from(now));
|
||||
});
|
||||
} catch (RuntimeException exception) { if (uploaded) s3.delete(stagingKey); throw exception; }
|
||||
return get(actor, id);
|
||||
} finally { Files.deleteIfExists(temp); }
|
||||
}
|
||||
|
||||
public Attachment get(UUID actor, UUID id) {
|
||||
try {
|
||||
return jdbc.queryForObject("""
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type, a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
||||
FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id WHERE a.id=? AND m.user_id=?
|
||||
""", (rs, row) -> new Attachment(rs.getObject("id", UUID.class), rs.getObject("chat_id", UUID.class), rs.getObject("owner_id", UUID.class), rs.getString("filename"), rs.getString("content_type"), rs.getLong("size_bytes"), rs.getString("sha256"), rs.getString("status"), loadVariants(rs.getObject("id", UUID.class)), rs.getString("error_code"), rs.getTimestamp("created_at").toInstant(), rs.getTimestamp("updated_at").toInstant()), id, actor);
|
||||
} catch (EmptyResultDataAccessException exception) { throw ApiException.notFound(); }
|
||||
}
|
||||
|
||||
public Download download(UUID actor, UUID id, String variant) {
|
||||
if (!List.of("original", "thumbnail", "crop", "optimized").contains(variant)) throw ApiException.notFound();
|
||||
Attachment metadata = get(actor, id);
|
||||
if (metadata.status().equals("queued") || metadata.status().equals("processing")) throw new ApiException(409, "conflict", "Вложение ещё обрабатывается");
|
||||
if (metadata.status().equals("failed")) throw new ApiException(409, "conflict", "Обработка вложения завершилась ошибкой");
|
||||
try {
|
||||
String key; String type;
|
||||
if (variant.equals("original")) { key = jdbc.queryForObject("SELECT object_key FROM attachments WHERE id=?", String.class, id); type = metadata.contentType(); }
|
||||
else { Variant v = metadata.variants().stream().filter(x -> x.name().equals(variant)).findFirst().orElseThrow(ApiException::notFound); key = jdbc.queryForObject("SELECT object_key FROM attachment_variants WHERE attachment_id=? AND name=?", String.class, id, variant); type = v.contentType(); }
|
||||
return new Download(s3.get(key), type);
|
||||
} catch (EmptyResultDataAccessException exception) { throw ApiException.notFound(); }
|
||||
}
|
||||
|
||||
private List<Variant> loadVariants(UUID id) { return jdbc.query("SELECT name, content_type, size_bytes, width, height, sha256 FROM attachment_variants WHERE attachment_id=? ORDER BY name", (rs, row) -> new Variant(rs.getString("name"), rs.getString("content_type"), rs.getLong("size_bytes"), rs.getInt("width"), rs.getInt("height"), rs.getString("sha256")), id); }
|
||||
private static long copyWithLimit(InputStream input, Path target, long max) throws IOException { long total = 0; byte[] buffer = new byte[8192]; try (var output = Files.newOutputStream(target)) { int read; while ((read = input.read(buffer)) != -1) { total += read; if (total > max) throw new ApiException(413, "payload_too_large", "Файл слишком большой"); output.write(buffer, 0, read); } } if (total == 0) throw new ApiException(400, "invalid_request", "Пустой файл"); return total; }
|
||||
private static String detectType(Path path) throws IOException { byte[] h = new byte[12]; int n; try (InputStream in = Files.newInputStream(path)) { n = in.read(h); } if (n >= 8 && (h[0] & 255) == 0x89 && h[1] == 0x50 && h[2] == 0x4e && h[3] == 0x47 && h[4] == 0x0d && h[5] == 0x0a && h[6] == 0x1a && h[7] == 0x0a) return "image/png"; if (n >= 3 && (h[0] & 255) == 0xff && (h[1] & 255) == 0xd8 && (h[2] & 255) == 0xff) return "image/jpeg"; throw new ApiException(415, "unsupported_media_type", "Файл не является PNG или JPEG"); }
|
||||
static Dimensions validateImage(Path path) throws IOException { try (ImageInputStream stream = ImageIO.createImageInputStream(path.toFile())) { Iterator<ImageReader> readers = ImageIO.getImageReaders(stream); if (!readers.hasNext()) throw new ApiException(415, "unsupported_media_type", "Повреждённое изображение"); ImageReader reader = readers.next(); try { reader.setInput(stream, true, true); int width = reader.getWidth(0), height = reader.getHeight(0); if ((long) width * height > MAX_PIXELS) throw new ApiException(413, "payload_too_large", "Слишком много пикселей"); if (ImageIO.read(path.toFile()) == null) throw new ApiException(415, "unsupported_media_type", "Повреждённое изображение"); return new Dimensions(width, height); } finally { reader.dispose(); } } }
|
||||
static String sha256(Path path) throws IOException { try (InputStream input = Files.newInputStream(path)) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] buffer = new byte[8192]; int n; while ((n = input.read(buffer)) != -1) digest.update(buffer, 0, n); StringBuilder result = new StringBuilder(); for (byte value : digest.digest()) result.append(String.format("%02x", value)); return result.toString(); } catch (java.security.NoSuchAlgorithmException exception) { throw new IllegalStateException(exception); } } }
|
||||
private static void validateFilename(String value) { if (value == null || value.isEmpty() || value.codePointCount(0, value.length()) > 255 || value.indexOf('/') >= 0 || value.indexOf('\\') >= 0 || value.indexOf('\0') >= 0 || value.codePoints().anyMatch(cp -> cp < 32 || cp == 127)) throw ApiException.invalid("Некорректное имя файла"); }
|
||||
record Dimensions(int width, int height) {}
|
||||
public record Download(InputStream stream, String contentType) {}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.data.redis.connection.stream.MapRecord;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@Component
|
||||
@ConditionalOnProperty(name = "WORKER_ONLY", havingValue = "true")
|
||||
public class AttachmentWorker {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(AttachmentWorker.class);
|
||||
private final JdbcTemplate jdbc;
|
||||
private final JobBroker broker;
|
||||
private final S3Storage s3;
|
||||
private final String consumer = "worker-" + UUID.randomUUID();
|
||||
|
||||
public AttachmentWorker(JdbcTemplate jdbc, JobBroker broker, S3Storage s3) { this.jdbc = jdbc; this.broker = broker; this.s3 = s3; }
|
||||
|
||||
@PostConstruct
|
||||
void start() { LOG.info("attachment worker started consumer={}", consumer); }
|
||||
|
||||
@Scheduled(fixedDelayString = "${WORKER_POLL_MS:500}", initialDelayString = "${WORKER_POLL_INITIAL_DELAY_MS:500}")
|
||||
public void poll() {
|
||||
try {
|
||||
MapRecord<String, Object, Object> record = broker.read(consumer);
|
||||
if (record != null) {
|
||||
UUID jobId = UUID.fromString(String.valueOf(record.getValue().get("job_id")));
|
||||
LOG.info("attachment job received job_id={}", jobId);
|
||||
try { process(jobId); } finally { broker.ack(record.getId()); }
|
||||
} else {
|
||||
processQueuedFallback();
|
||||
}
|
||||
recoverLeases();
|
||||
} catch (Exception exception) {
|
||||
LOG.warn("worker poll error", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private void processQueuedFallback() {
|
||||
List<UUID> jobs = jdbc.query("SELECT job_id FROM attachment_jobs WHERE status='queued' AND next_attempt_at <= now() ORDER BY created_at LIMIT 1", (rs, row) -> rs.getObject("job_id", UUID.class));
|
||||
if (!jobs.isEmpty()) { LOG.info("processing queued fallback job_id={}", jobs.getFirst()); process(jobs.getFirst()); }
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString = "${WORKER_LEASE_SCAN_MS:1000}")
|
||||
public void recoverLeases() {
|
||||
jdbc.update("UPDATE attachment_jobs SET status='queued', published_at=NULL, lease_until=NULL, updated_at=now() WHERE status='processing' AND lease_until < now() AND attempt < 5");
|
||||
}
|
||||
|
||||
private void process(UUID jobId) {
|
||||
Job job = claim(jobId);
|
||||
if (job == null) return;
|
||||
LOG.info("processing attachment job_id={} attempt={}", job.jobId(), job.attempt());
|
||||
Path temp = null;
|
||||
try {
|
||||
temp = Files.createTempFile("messenger-worker-", ".bin");
|
||||
try (InputStream input = s3.get(job.stagingKey()); var output = Files.newOutputStream(temp)) { input.transferTo(output); }
|
||||
AttachmentService.Dimensions dimensions = AttachmentService.validateImage(temp);
|
||||
List<AttachmentImageProcessor.Rendered> variants = AttachmentImageProcessor.render(temp);
|
||||
try (InputStream original = Files.newInputStream(temp)) {
|
||||
s3.put(job.originalKey(), original, Files.size(temp), job.contentType());
|
||||
}
|
||||
for (AttachmentImageProcessor.Rendered variant : variants) {
|
||||
String key = "variants/" + job.attachmentId() + "/" + variant.name();
|
||||
s3.put(key, new ByteArrayInputStream(variant.bytes()), variant.bytes().length, variant.contentType());
|
||||
}
|
||||
Instant now = Instant.now();
|
||||
for (AttachmentImageProcessor.Rendered variant : variants) {
|
||||
String key = "variants/" + job.attachmentId() + "/" + variant.name();
|
||||
jdbc.update("""
|
||||
INSERT INTO attachment_variants(attachment_id, name, content_type, size_bytes, width, height, sha256, object_key)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT (attachment_id, name) DO NOTHING
|
||||
""", job.attachmentId(), variant.name(), variant.contentType(), variant.bytes().length,
|
||||
variant.width(), variant.height(), sha256(variant.bytes()), key);
|
||||
}
|
||||
jdbc.update("UPDATE attachments SET status='ready', error_code=NULL, updated_at=? WHERE id=?", Timestamp.from(now), job.attachmentId());
|
||||
jdbc.update("UPDATE attachment_jobs SET status='ready', lease_until=NULL, updated_at=? WHERE job_id=?", Timestamp.from(now), job.jobId());
|
||||
s3.delete(job.stagingKey());
|
||||
LOG.info("attachment ready job_id={} attachment_id={}", job.jobId(), job.attachmentId());
|
||||
} catch (Exception exception) {
|
||||
LOG.warn("attachment processing failed job_id={} attempt={}", job.jobId(), job.attempt(), exception);
|
||||
failOrRetry(job, exception);
|
||||
} finally { if (temp != null) try { Files.deleteIfExists(temp); } catch (Exception ignored) {} LOG.info("attachment job finished job_id={}", job.jobId()); }
|
||||
}
|
||||
|
||||
private Job claim(UUID jobId) {
|
||||
int changed = jdbc.update("UPDATE attachment_jobs SET status='processing', attempt=attempt+1, lease_until=now()+interval '30 seconds', updated_at=now() WHERE job_id=? AND (status='queued' OR (status='processing' AND lease_until < now())) AND attempt < 5", jobId);
|
||||
if (changed == 0) return null;
|
||||
return jdbc.queryForObject("SELECT j.job_id, j.attachment_id, a.staging_key, a.object_key, a.content_type, j.attempt FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE j.job_id=?", (rs, row) -> new Job(rs.getObject("job_id", UUID.class), rs.getObject("attachment_id", UUID.class), rs.getString("staging_key"), rs.getString("object_key"), rs.getString("content_type"), rs.getInt("attempt")), jobId);
|
||||
}
|
||||
|
||||
private void failOrRetry(Job job, Exception exception) {
|
||||
boolean permanent = exception instanceof ApiException api && (api.status() == 413 || api.status() == 415);
|
||||
if (permanent || job.attempt() >= 5) {
|
||||
String code = permanent ? "invalid_image" : "retry_exhausted";
|
||||
jdbc.update("UPDATE attachments SET status='failed', error_code=?, updated_at=now() WHERE id=?", code, job.attachmentId());
|
||||
jdbc.update("UPDATE attachment_jobs SET status='failed', error_code=?, lease_until=NULL, updated_at=now() WHERE job_id=?", code, job.jobId());
|
||||
} else {
|
||||
long delay = Math.min(5000L, 200L * (1L << Math.min(job.attempt(), 4))) + (long) (Math.random() * 300);
|
||||
jdbc.update("UPDATE attachment_jobs SET status='queued', published_at=NULL, lease_until=NULL, next_attempt_at=now() + (? * interval '1 millisecond'), error_code=?, updated_at=now() WHERE job_id=?", delay, "temporary_error", job.jobId());
|
||||
jdbc.update("UPDATE attachments SET status='queued', error_code=NULL, updated_at=now() WHERE id=?", job.attachmentId());
|
||||
}
|
||||
}
|
||||
|
||||
private static String sha256(byte[] bytes) throws Exception { var digest = java.security.MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(bytes); StringBuilder s = new StringBuilder(); for (byte b : hash) s.append(String.format("%02x", b)); return s.toString(); }
|
||||
private record Job(UUID jobId, UUID attachmentId, String stagingKey, String originalKey, String contentType, int attempt) {}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class CursorCodec {
|
||||
private final byte[] secret;
|
||||
private static final Base64.Encoder ENCODER = Base64.getUrlEncoder().withoutPadding();
|
||||
private static final Base64.Decoder DECODER = Base64.getUrlDecoder();
|
||||
|
||||
public CursorCodec() {
|
||||
String configured = System.getenv("CURSOR_SECRET");
|
||||
if (configured == null || configured.isBlank()) {
|
||||
throw new IllegalStateException("CURSOR_SECRET must be configured for all API replicas");
|
||||
}
|
||||
try {
|
||||
byte[] decoded = DECODER.decode(configured);
|
||||
if (decoded.length < 32) throw new IllegalArgumentException("secret is too short");
|
||||
this.secret = Arrays.copyOf(decoded, 32);
|
||||
} catch (IllegalArgumentException exception) {
|
||||
throw new IllegalStateException("CURSOR_SECRET must be URL-safe base64 with at least 32 bytes", exception);
|
||||
}
|
||||
}
|
||||
|
||||
public record Position(Instant createdAt, UUID id) {}
|
||||
|
||||
public String encode(String scope, Position position) {
|
||||
String payload = scope + "|" + position.createdAt() + "|" + position.id();
|
||||
String encoded = ENCODER.encodeToString(payload.getBytes(StandardCharsets.UTF_8));
|
||||
return encoded + "." + ENCODER.encodeToString(sign(encoded));
|
||||
}
|
||||
|
||||
public Position decode(String cursor, String scope) {
|
||||
if (cursor == null) return null;
|
||||
try {
|
||||
if (cursor.length() > 1024) throw new IllegalArgumentException();
|
||||
String[] parts = cursor.split("\\.", -1);
|
||||
if (parts.length != 2 || !MessageDigest.isEqual(sign(parts[0]), DECODER.decode(parts[1]))) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
String[] fields = new String(DECODER.decode(parts[0]), StandardCharsets.UTF_8).split("\\|", -1);
|
||||
if (fields.length != 3 || !scope.equals(fields[0])) throw new IllegalArgumentException();
|
||||
return new Position(Instant.parse(fields[1]), Input.uuid(fields[2]));
|
||||
} catch (RuntimeException exception) {
|
||||
throw ApiException.invalid("Некорректный cursor или cursor от другого ресурса");
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] sign(String payload) {
|
||||
try {
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(secret, "HmacSHA256"));
|
||||
return mac.doFinal(payload.getBytes(StandardCharsets.UTF_8));
|
||||
} catch (java.security.GeneralSecurityException exception) {
|
||||
throw new IllegalStateException("HMAC is unavailable", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import org.springframework.boot.availability.ApplicationAvailability;
|
||||
import org.springframework.boot.availability.ReadinessState;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class HealthController {
|
||||
private final ApplicationAvailability availability;
|
||||
private final JdbcTemplate jdbc;
|
||||
private final SessionService sessions;
|
||||
private final S3Storage s3;
|
||||
|
||||
public HealthController(ApplicationAvailability availability, JdbcTemplate jdbc, SessionService sessions, S3Storage s3) {
|
||||
this.availability = availability; this.jdbc = jdbc; this.sessions = sessions; this.s3 = s3;
|
||||
}
|
||||
|
||||
@GetMapping("/health/live")
|
||||
Map<String, String> live() { return Map.of("status", "ok"); }
|
||||
|
||||
@GetMapping("/health/ready")
|
||||
Map<String, String> ready() {
|
||||
if (availability.getReadinessState() != ReadinessState.ACCEPTING_TRAFFIC) throw unavailable();
|
||||
try {
|
||||
jdbc.queryForObject("SELECT 1", Integer.class);
|
||||
sessions.check();
|
||||
s3.check();
|
||||
return Map.of("status", "ok");
|
||||
} catch (RuntimeException exception) {
|
||||
throw unavailable();
|
||||
}
|
||||
}
|
||||
|
||||
private ApiException unavailable() { return new ApiException(503, "unavailable", "Зависимость недоступна"); }
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
final class Input {
|
||||
private Input() {}
|
||||
|
||||
static void fields(JsonNode body, String... allowed) {
|
||||
Set<String> names = Set.of(allowed);
|
||||
if (body == null || !body.isObject() || body.size() != names.size()) {
|
||||
throw ApiException.invalid("Неверный набор полей JSON");
|
||||
}
|
||||
body.fieldNames().forEachRemaining(name -> {
|
||||
if (!names.contains(name)) throw ApiException.invalid("Неизвестное поле JSON");
|
||||
});
|
||||
}
|
||||
|
||||
static String text(JsonNode body, String field, int max) {
|
||||
JsonNode node = body.get(field);
|
||||
if (node == null || !node.isTextual()) throw ApiException.invalid("Поле должно быть строкой: " + field);
|
||||
String value = node.textValue();
|
||||
if (value.codePointCount(0, value.length()) > max || value.codePoints().allMatch(Input::isUnicodeWhitespace)) {
|
||||
throw ApiException.invalid("Недопустимая длина или пустая строка: " + field);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static String password(JsonNode body) {
|
||||
JsonNode node = body.get("password");
|
||||
if (node == null || !node.isTextual()) throw ApiException.invalid("Пароль должен быть строкой");
|
||||
String value = node.textValue();
|
||||
int length = value.codePointCount(0, value.length());
|
||||
if (length < 12 || length > 128 || value.codePoints().allMatch(Input::isUnicodeWhitespace)) {
|
||||
throw ApiException.invalid("Пароль должен содержать от 12 до 128 непробельных символов");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private static boolean isUnicodeWhitespace(int codePoint) {
|
||||
return codePoint == 0x0085
|
||||
|| Character.isWhitespace(codePoint)
|
||||
|| Character.isSpaceChar(codePoint);
|
||||
}
|
||||
|
||||
static UUID uuid(String value) {
|
||||
if (value == null || !value.matches("(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) {
|
||||
throw ApiException.invalid("Некорректный UUID");
|
||||
}
|
||||
return UUID.fromString(value);
|
||||
}
|
||||
|
||||
static List<UUID> members(JsonNode body) {
|
||||
JsonNode array = body.get("member_ids");
|
||||
if (array == null || !array.isArray() || array.isEmpty() || array.size() > 100) {
|
||||
throw ApiException.invalid("member_ids должен содержать от 1 до 100 участников");
|
||||
}
|
||||
List<UUID> ids = new ArrayList<>();
|
||||
for (JsonNode item : array) {
|
||||
if (!item.isTextual()) throw ApiException.invalid("Участник должен быть UUID-строкой");
|
||||
ids.add(uuid(item.textValue()));
|
||||
}
|
||||
if (new HashSet<>(ids).size() != ids.size()) throw ApiException.invalid("Участники повторяются");
|
||||
return ids;
|
||||
}
|
||||
|
||||
static int limit(String value) {
|
||||
if (value == null) return 50;
|
||||
try {
|
||||
if (!value.matches("[0-9]{1,3}")) throw new NumberFormatException();
|
||||
int limit = Integer.parseInt(value);
|
||||
if (limit < 1 || limit > 100) throw new NumberFormatException();
|
||||
return limit;
|
||||
} catch (NumberFormatException exception) {
|
||||
throw ApiException.invalid("limit должен быть целым числом от 1 до 100");
|
||||
}
|
||||
}
|
||||
|
||||
static String idempotencyKey(String value) {
|
||||
if (value == null || !value.matches("[A-Za-z0-9._:-]{1,128}")) {
|
||||
throw ApiException.invalid("Некорректный Idempotency-Key");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static void messageFields(JsonNode body) {
|
||||
if (body == null || !body.isObject() || !body.has("text") || body.size() > 2
|
||||
|| (body.has("attachment_ids") && !body.get("attachment_ids").isArray())) {
|
||||
throw ApiException.invalid("Неверный набор полей JSON сообщения");
|
||||
}
|
||||
body.fieldNames().forEachRemaining(name -> {
|
||||
if (!name.equals("text") && !name.equals("attachment_ids")) {
|
||||
throw ApiException.invalid("Неизвестное поле JSON");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static List<UUID> attachmentIds(JsonNode body) {
|
||||
JsonNode array = body.get("attachment_ids");
|
||||
if (array == null) return List.of();
|
||||
if (!array.isArray() || array.size() > 10) throw ApiException.invalid("Слишком много вложений");
|
||||
List<UUID> ids = new ArrayList<>();
|
||||
for (JsonNode item : array) {
|
||||
if (!item.isTextual()) throw ApiException.invalid("attachment_ids должны быть UUID-строками");
|
||||
ids.add(uuid(item.textValue()));
|
||||
}
|
||||
if (new HashSet<>(ids).size() != ids.size()) throw ApiException.invalid("Вложения повторяются");
|
||||
return ids;
|
||||
}
|
||||
|
||||
static String messageText(JsonNode body, boolean hasAttachments) {
|
||||
JsonNode node = body.get("text");
|
||||
if (node == null || !node.isTextual()) throw ApiException.invalid("Поле должно быть строкой: text");
|
||||
String value = node.textValue();
|
||||
if (value.codePointCount(0, value.length()) > 4000
|
||||
|| (!hasAttachments && (value.isEmpty() || value.codePoints().allMatch(Input::isUnicodeWhitespace)))) {
|
||||
throw ApiException.invalid("Сообщение должно содержать текст или вложение");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.springframework.data.redis.connection.stream.Consumer;
|
||||
import org.springframework.data.redis.connection.stream.MapRecord;
|
||||
import org.springframework.data.redis.connection.stream.ReadOffset;
|
||||
import org.springframework.data.redis.connection.stream.RecordId;
|
||||
import org.springframework.data.redis.connection.stream.StreamRecords;
|
||||
import org.springframework.data.redis.connection.stream.StreamOffset;
|
||||
import org.springframework.data.redis.connection.stream.StreamReadOptions;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class JobBroker {
|
||||
public static final String STREAM = "messenger:attachment-jobs";
|
||||
public static final String GROUP = "attachment-workers";
|
||||
private final StringRedisTemplate redis;
|
||||
|
||||
public JobBroker(StringRedisTemplate redis) { this.redis = redis; }
|
||||
|
||||
@PostConstruct
|
||||
void ensureGroup() {
|
||||
try { redis.opsForStream().createGroup(STREAM, ReadOffset.from("0-0"), GROUP); }
|
||||
catch (RuntimeException ignored) { /* BUSYGROUP means it already exists. */ }
|
||||
}
|
||||
|
||||
public void publish(String jobId) {
|
||||
MapRecord<String, String, String> record = StreamRecords.newRecord()
|
||||
.in(STREAM).ofMap(Map.of("job_id", jobId, "schema_version", "1", "type", "attachment.process.v1"));
|
||||
redis.opsForStream().add(record);
|
||||
}
|
||||
|
||||
public MapRecord<String, Object, Object> read(String consumer) {
|
||||
List<MapRecord<String, Object, Object>> records = redis.opsForStream().read(
|
||||
Consumer.from(GROUP, consumer),
|
||||
StreamReadOptions.empty().count(1).block(Duration.ofSeconds(1)),
|
||||
StreamOffset.create(STREAM, ReadOffset.lastConsumed()));
|
||||
return records == null || records.isEmpty() ? null : records.getFirst();
|
||||
}
|
||||
|
||||
public void ack(RecordId id) { redis.opsForStream().acknowledge(STREAM, GROUP, id); }
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
public class MessengerApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MessengerApplication.class, args);
|
||||
}
|
||||
|
||||
@org.springframework.context.annotation.Bean
|
||||
CommandLineRunner migrationOnly(ApplicationContext context, Environment environment) {
|
||||
return args -> {
|
||||
if (environment.getProperty("MIGRATION_ONLY", Boolean.class, false)) {
|
||||
int exitCode = SpringApplication.exit(context, () -> 0);
|
||||
System.exit(exitCode);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
import java.util.List;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||
|
||||
import static ru.mtusi.messenger.Models.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1")
|
||||
public class MessengerController {
|
||||
private final MessengerStore store;
|
||||
private final SessionService sessions;
|
||||
private final AttachmentService attachments;
|
||||
|
||||
public MessengerController(MessengerStore store, SessionService sessions, AttachmentService attachments) {
|
||||
this.store = store; this.sessions = sessions; this.attachments = attachments;
|
||||
}
|
||||
|
||||
@PostMapping(value = "/users", consumes = "application/json")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
User register(@RequestBody JsonNode body) {
|
||||
Input.fields(body, "username", "display_name", "password");
|
||||
String username = Input.text(body, "username", 32);
|
||||
if (!username.matches("[a-z0-9_]{3,32}")) throw ApiException.invalid("Некорректный username");
|
||||
return store.createUser(username, Input.text(body, "display_name", 100), sessions.hash(Input.password(body)));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/auth/sessions", consumes = "*/*")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
Session login(HttpServletRequest request) throws java.io.IOException {
|
||||
if (request.getInputStream().read() != -1) {
|
||||
throw ApiException.invalid("Вход не принимает тело запроса");
|
||||
}
|
||||
String header = request.getHeader("Authorization");
|
||||
if (header == null || !header.startsWith("Basic ")) throw ApiException.unauthorized();
|
||||
try {
|
||||
String decoded = new String(Base64.getDecoder().decode(header.substring(6)), StandardCharsets.UTF_8);
|
||||
int colon = decoded.indexOf(':');
|
||||
if (colon <= 0) throw new IllegalArgumentException();
|
||||
String username = decoded.substring(0, colon);
|
||||
String password = decoded.substring(colon + 1);
|
||||
if (!username.matches("[a-z0-9_]{3,32}") || password.codePointCount(0, password.length()) < 12
|
||||
|| password.codePointCount(0, password.length()) > 128) throw new IllegalArgumentException();
|
||||
return sessions.login(username, password);
|
||||
} catch (IllegalArgumentException exception) {
|
||||
throw ApiException.unauthorized();
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/auth/sessions/current")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
void logout(@RequestAttribute(RequestFilter.TOKEN) String token) { sessions.revoke(token); }
|
||||
|
||||
@GetMapping("/users/me")
|
||||
User me(@RequestAttribute(RequestFilter.ACTOR) User actor) { return actor; }
|
||||
|
||||
@PostMapping(value = "/chats", consumes = "application/json")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
Chat createChat(@RequestAttribute(RequestFilter.ACTOR) User actor, @RequestBody JsonNode body) {
|
||||
Input.fields(body, "title", "member_ids");
|
||||
return store.createChat(actor.id(), Input.text(body, "title", 100), Input.members(body));
|
||||
}
|
||||
|
||||
@GetMapping("/chats")
|
||||
Page<Chat> chats(@RequestAttribute(RequestFilter.ACTOR) User actor,
|
||||
@RequestParam(required = false) String limit, @RequestParam(required = false) String cursor) {
|
||||
return store.chats(actor.id(), Input.limit(limit), cursor);
|
||||
}
|
||||
|
||||
@GetMapping("/chats/{chatId}")
|
||||
Chat chat(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String chatId) {
|
||||
return store.chat(actor.id(), Input.uuid(chatId));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/chats/{chatId}/messages", consumes = "application/json")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
Message send(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String chatId,
|
||||
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
|
||||
HttpServletRequest request, @RequestBody JsonNode body) {
|
||||
UUID id = Input.uuid(chatId);
|
||||
store.chat(actor.id(), id);
|
||||
String key = Input.idempotencyKey(idempotencyKey);
|
||||
Input.messageFields(body);
|
||||
List<UUID> attachmentIds = Input.attachmentIds(body);
|
||||
String text = Input.messageText(body, !attachmentIds.isEmpty());
|
||||
return store.sendIdempotent(actor.id(), id, text, attachmentIds, request.getMethod(), request.getRequestURI(), key);
|
||||
}
|
||||
|
||||
@GetMapping("/chats/{chatId}/messages")
|
||||
Page<Message> history(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String chatId,
|
||||
@RequestParam(required = false) String limit, @RequestParam(required = false) String cursor) {
|
||||
UUID id = Input.uuid(chatId);
|
||||
return store.history(actor.id(), id, Input.limit(limit), cursor);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/chats/{chatId}/attachments", consumes = {"image/png", "image/jpeg"})
|
||||
@ResponseStatus(HttpStatus.ACCEPTED)
|
||||
Attachment upload(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String chatId,
|
||||
@RequestParam String filename, HttpServletRequest request) throws java.io.IOException {
|
||||
return attachments.upload(actor.id(), Input.uuid(chatId), filename, request.getContentType(), request.getInputStream());
|
||||
}
|
||||
|
||||
@GetMapping("/attachments/{attachmentId}")
|
||||
Attachment attachment(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String attachmentId) {
|
||||
return attachments.get(actor.id(), Input.uuid(attachmentId));
|
||||
}
|
||||
|
||||
@GetMapping("/attachments/{attachmentId}/content")
|
||||
ResponseEntity<StreamingResponseBody> content(@RequestAttribute(RequestFilter.ACTOR) User actor,
|
||||
@PathVariable String attachmentId,
|
||||
@RequestParam(defaultValue = "original") String variant) {
|
||||
AttachmentService.Download download = attachments.download(actor.id(), Input.uuid(attachmentId), variant);
|
||||
StreamingResponseBody body = output -> {
|
||||
try (java.io.InputStream input = download.stream()) { input.transferTo(output); }
|
||||
};
|
||||
return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, download.contentType()).body(body);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static ru.mtusi.messenger.Models.*;
|
||||
|
||||
@Service
|
||||
public class MessengerStore {
|
||||
private final JdbcTemplate jdbc;
|
||||
private final CursorCodec cursors;
|
||||
|
||||
public MessengerStore(JdbcTemplate jdbc, CursorCodec cursors) { this.jdbc = jdbc; this.cursors = cursors; }
|
||||
|
||||
@Transactional
|
||||
public User createUser(String username, String displayName, String passwordHash) {
|
||||
UUID id = UUID.randomUUID();
|
||||
Instant created = Instant.now();
|
||||
try {
|
||||
jdbc.update("INSERT INTO users(id, username, display_name, password_hash, created_at) VALUES (?, ?, ?, ?, ?)",
|
||||
id, username, displayName, passwordHash, Timestamp.from(created));
|
||||
} catch (DuplicateKeyException exception) {
|
||||
throw new ApiException(409, "conflict", "username уже занят");
|
||||
}
|
||||
return findUser(id);
|
||||
}
|
||||
|
||||
public Credentials credentials(String username) {
|
||||
try {
|
||||
return jdbc.queryForObject("SELECT id, username, display_name, created_at, password_hash FROM users WHERE username = ?",
|
||||
(rs, row) -> new Credentials(new User(rs.getObject("id", UUID.class), rs.getString("username"),
|
||||
rs.getString("display_name"), rs.getTimestamp("created_at").toInstant()),
|
||||
rs.getString("password_hash")), username);
|
||||
} catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
public User findUser(UUID id) {
|
||||
try {
|
||||
return jdbc.queryForObject("SELECT id, username, display_name, created_at FROM users WHERE id = ?",
|
||||
(rs, row) -> user(rs), id);
|
||||
} catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Chat createChat(UUID actor, String title, List<UUID> memberIds) {
|
||||
if (!memberIds.contains(actor)) throw ApiException.invalid("Участники должны существовать и включать создателя");
|
||||
for (UUID member : memberIds) {
|
||||
if (findUser(member) == null) throw ApiException.invalid("Участники должны существовать и включать создателя");
|
||||
}
|
||||
UUID id = UUID.randomUUID();
|
||||
Instant created = Instant.now();
|
||||
jdbc.update("INSERT INTO chats(id, title, created_at) VALUES (?, ?, ?)", id, title, Timestamp.from(created));
|
||||
for (UUID member : memberIds) jdbc.update("INSERT INTO chat_members(chat_id, user_id) VALUES (?, ?)", id, member);
|
||||
return chat(actor, id);
|
||||
}
|
||||
|
||||
public Chat chat(UUID actor, UUID id) {
|
||||
try {
|
||||
Chat chat = jdbc.queryForObject("SELECT c.id, c.title, c.created_at FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE c.id=? AND m.user_id=?",
|
||||
(rs, row) -> new Chat(rs.getObject("id", UUID.class), rs.getString("title"),
|
||||
loadMembers(rs.getObject("id", UUID.class)), rs.getTimestamp("created_at").toInstant()), id, actor);
|
||||
if (chat == null) throw ApiException.notFound();
|
||||
return chat;
|
||||
} catch (EmptyResultDataAccessException exception) { throw ApiException.notFound(); }
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Message send(UUID actor, UUID chatId, String text) {
|
||||
chat(actor, chatId);
|
||||
UUID id = UUID.randomUUID();
|
||||
Instant created = Instant.now();
|
||||
jdbc.update("INSERT INTO messages(id, chat_id, sender_id, text, created_at) VALUES (?, ?, ?, ?, ?)",
|
||||
id, chatId, actor, text, Timestamp.from(created));
|
||||
return findMessage(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Message sendIdempotent(UUID actor, UUID chatId, String text,
|
||||
List<UUID> attachmentIds,
|
||||
String method, String requestPath, String idempotencyKey) {
|
||||
chat(actor, chatId);
|
||||
verifyAttachments(actor, chatId, attachmentIds);
|
||||
String requestHash = hashRequest(text, attachmentIds);
|
||||
Instant created = Instant.now();
|
||||
int inserted = jdbc.update("""
|
||||
INSERT INTO message_idempotency(actor_id, method, request_path, idempotency_key,
|
||||
request_hash, message_id, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, NULL, ?)
|
||||
ON CONFLICT (actor_id, method, request_path, idempotency_key) DO NOTHING
|
||||
""", actor, method, requestPath, idempotencyKey, requestHash, Timestamp.from(created));
|
||||
|
||||
if (inserted == 0) {
|
||||
IdempotencyRecord previous = jdbc.queryForObject("""
|
||||
SELECT request_hash, message_id
|
||||
FROM message_idempotency
|
||||
WHERE actor_id=? AND method=? AND request_path=? AND idempotency_key=?
|
||||
""", (rs, row) -> new IdempotencyRecord(rs.getString("request_hash"),
|
||||
rs.getObject("message_id", UUID.class)), actor, method, requestPath, idempotencyKey);
|
||||
if (!MessageDigest.isEqual(previous.requestHash().getBytes(StandardCharsets.US_ASCII),
|
||||
requestHash.getBytes(StandardCharsets.US_ASCII))) {
|
||||
throw new ApiException(409, "conflict", "Idempotency-Key уже использован для другого сообщения");
|
||||
}
|
||||
if (previous.messageId() == null) {
|
||||
throw new ApiException(503, "unavailable", "Результат идемпотентного запроса ещё не готов");
|
||||
}
|
||||
return findMessage(previous.messageId());
|
||||
}
|
||||
|
||||
UUID id = UUID.randomUUID();
|
||||
jdbc.update("INSERT INTO messages(id, chat_id, sender_id, text, created_at) VALUES (?, ?, ?, ?, ?)",
|
||||
id, chatId, actor, text, Timestamp.from(created));
|
||||
for (UUID attachmentId : attachmentIds) {
|
||||
jdbc.update("INSERT INTO message_attachments(message_id, attachment_id) VALUES (?, ?)", id, attachmentId);
|
||||
}
|
||||
jdbc.update("""
|
||||
UPDATE message_idempotency
|
||||
SET message_id=?
|
||||
WHERE actor_id=? AND method=? AND request_path=? AND idempotency_key=?
|
||||
""", id, actor, method, requestPath, idempotencyKey);
|
||||
return findMessage(id);
|
||||
}
|
||||
|
||||
public Page<Chat> chats(UUID actor, int limit, String cursor) {
|
||||
CursorCodec.Position after = cursors.decode(cursor, "chats:" + actor);
|
||||
String sql = "SELECT c.id, c.title, c.created_at FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE m.user_id=?";
|
||||
List<Object> args = new ArrayList<>(List.of(actor));
|
||||
if (after != null) { sql += " AND (c.created_at, c.id) > (?, ?)"; args.add(Timestamp.from(after.createdAt())); args.add(after.id()); }
|
||||
sql += " ORDER BY c.created_at, c.id LIMIT ?";
|
||||
args.add(limit + 1);
|
||||
List<Chat> rows = jdbc.query(sql, (rs, row) -> {
|
||||
UUID id = rs.getObject("id", UUID.class);
|
||||
return new Chat(id, rs.getString("title"), loadMembers(id), rs.getTimestamp("created_at").toInstant());
|
||||
}, args.toArray());
|
||||
return page(rows, limit, "chats:" + actor, c -> new CursorCodec.Position(c.createdAt(), c.id()));
|
||||
}
|
||||
|
||||
public Page<Message> history(UUID actor, UUID chatId, int limit, String cursor) {
|
||||
chat(actor, chatId);
|
||||
CursorCodec.Position after = cursors.decode(cursor, "messages:" + chatId);
|
||||
String sql = "SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id=?";
|
||||
List<Object> args = new ArrayList<>(List.of(chatId));
|
||||
if (after != null) { sql += " AND (created_at, id) > (?, ?)"; args.add(Timestamp.from(after.createdAt())); args.add(after.id()); }
|
||||
sql += " ORDER BY created_at, id LIMIT ?";
|
||||
args.add(limit + 1);
|
||||
List<Message> rows = jdbc.query(sql, (rs, row) -> new Message(rs.getObject("id", UUID.class),
|
||||
rs.getObject("chat_id", UUID.class), rs.getObject("sender_id", UUID.class), rs.getString("text"),
|
||||
rs.getTimestamp("created_at").toInstant(), loadAttachmentIds(rs.getObject("id", UUID.class))), args.toArray());
|
||||
return page(rows, limit, "messages:" + chatId, m -> new CursorCodec.Position(m.createdAt(), m.id()));
|
||||
}
|
||||
|
||||
private List<UUID> loadMembers(UUID chatId) {
|
||||
return jdbc.query("SELECT user_id FROM chat_members WHERE chat_id=? ORDER BY user_id",
|
||||
(rs, row) -> rs.getObject("user_id", UUID.class), chatId);
|
||||
}
|
||||
|
||||
private Message findMessage(UUID id) {
|
||||
try {
|
||||
return jdbc.queryForObject("SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE id=?",
|
||||
(rs, row) -> new Message(rs.getObject("id", UUID.class), rs.getObject("chat_id", UUID.class),
|
||||
rs.getObject("sender_id", UUID.class), rs.getString("text"), rs.getTimestamp("created_at").toInstant(),
|
||||
loadAttachmentIds(rs.getObject("id", UUID.class))), id);
|
||||
} catch (EmptyResultDataAccessException exception) {
|
||||
throw new ApiException(500, "internal_error", "Созданное сообщение не найдено");
|
||||
}
|
||||
}
|
||||
|
||||
private String hashRequest(String text, List<UUID> attachmentIds) {
|
||||
try {
|
||||
byte[] digest = MessageDigest.getInstance("SHA-256")
|
||||
.digest((text + "|" + attachmentIds.stream().map(UUID::toString).sorted().toList()).getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder result = new StringBuilder(digest.length * 2);
|
||||
for (byte value : digest) result.append(String.format("%02x", value));
|
||||
return result.toString();
|
||||
} catch (java.security.NoSuchAlgorithmException exception) {
|
||||
throw new IllegalStateException("SHA-256 is unavailable", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private void verifyAttachments(UUID actor, UUID chatId, List<UUID> ids) {
|
||||
for (UUID id : ids) {
|
||||
Integer count = jdbc.queryForObject("SELECT count(*) FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id WHERE a.id=? AND a.chat_id=? AND m.user_id=? AND a.status <> 'failed'", Integer.class, id, chatId, actor);
|
||||
if (count == null || count != 1) throw ApiException.notFound();
|
||||
}
|
||||
}
|
||||
|
||||
private List<UUID> loadAttachmentIds(UUID messageId) {
|
||||
return jdbc.query("SELECT attachment_id FROM message_attachments WHERE message_id=? ORDER BY attachment_id",
|
||||
(rs, row) -> rs.getObject("attachment_id", UUID.class), messageId);
|
||||
}
|
||||
|
||||
private record IdempotencyRecord(String requestHash, UUID messageId) {}
|
||||
|
||||
private User user(java.sql.ResultSet rs) throws java.sql.SQLException {
|
||||
return new User(rs.getObject("id", UUID.class), rs.getString("username"), rs.getString("display_name"),
|
||||
rs.getTimestamp("created_at").toInstant());
|
||||
}
|
||||
|
||||
private <T> Page<T> page(List<T> rows, int limit, String scope, java.util.function.Function<T, CursorCodec.Position> position) {
|
||||
boolean more = rows.size() > limit;
|
||||
List<T> result = more ? rows.subList(0, limit) : rows;
|
||||
String next = more ? cursors.encode(scope, position.apply(result.getLast())) : null;
|
||||
return new Page<>(result, next);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class Models {
|
||||
private Models() {}
|
||||
|
||||
public record User(UUID id, String username, String displayName, Instant createdAt) {}
|
||||
public record Chat(UUID id, String title, List<UUID> memberIds, Instant createdAt) {
|
||||
public Chat { memberIds = List.copyOf(memberIds); }
|
||||
}
|
||||
public record Message(UUID id, UUID chatId, UUID senderId, String text, Instant createdAt,
|
||||
List<UUID> attachmentIds) {
|
||||
public Message { attachmentIds = List.copyOf(attachmentIds); }
|
||||
}
|
||||
public record Attachment(UUID id, UUID chatId, UUID ownerId, String filename, String contentType,
|
||||
long sizeBytes, String sha256, String status, List<Variant> variants,
|
||||
String errorCode, Instant createdAt, Instant updatedAt) {
|
||||
public Attachment { variants = List.copyOf(variants); }
|
||||
}
|
||||
public record Variant(String name, String contentType, long sizeBytes, int width, int height, String sha256) {}
|
||||
public record Credentials(User user, String passwordHash) {}
|
||||
public record Session(String token, String tokenType, Instant expiresAt, User user) {}
|
||||
public record Page<T>(List<T> items, String nextCursor) {
|
||||
public Page { items = List.copyOf(items); }
|
||||
}
|
||||
public record ErrorBody(ErrorDetail error) {}
|
||||
public record ErrorDetail(String code, String message, String requestId) {}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
@ConditionalOnProperty(name = "WORKER_ONLY", havingValue = "false", matchIfMissing = true)
|
||||
public class OutboxPublisher {
|
||||
private final JdbcTemplate jdbc;
|
||||
private final JobBroker broker;
|
||||
public OutboxPublisher(JdbcTemplate jdbc, JobBroker broker) { this.jdbc = jdbc; this.broker = broker; }
|
||||
|
||||
@Scheduled(fixedDelayString = "${OUTBOX_PUBLISH_INTERVAL_MS:500}")
|
||||
public void publishPending() {
|
||||
List<Job> jobs = jdbc.query("SELECT job_id FROM attachment_jobs WHERE status='queued' AND published_at IS NULL AND next_attempt_at <= now() ORDER BY created_at LIMIT 20",
|
||||
(rs, row) -> new Job(rs.getObject("job_id", UUID.class)));
|
||||
for (Job job : jobs) {
|
||||
try {
|
||||
broker.publish(job.id().toString());
|
||||
jdbc.update("UPDATE attachment_jobs SET published_at=now(), updated_at=now() WHERE job_id=? AND published_at IS NULL", job.id());
|
||||
} catch (RuntimeException ignored) { /* retry on the next poll */ }
|
||||
}
|
||||
}
|
||||
private record Job(UUID id) {}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import static ru.mtusi.messenger.Models.*;
|
||||
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class RequestFilter extends OncePerRequestFilter {
|
||||
public static final String ACTOR = "messenger.actor";
|
||||
public static final String REQUEST_ID = "messenger.requestId";
|
||||
public static final String TOKEN = "messenger.token";
|
||||
public static final String INSTANCE_ID = "messenger.instanceId";
|
||||
private static final String INSTANCE = instanceId();
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RequestFilter.class);
|
||||
private final SessionService sessions;
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public RequestFilter(SessionService sessions, ObjectMapper mapper) {
|
||||
this.sessions = sessions;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||
FilterChain chain) throws ServletException, IOException {
|
||||
String requestId = UUID.randomUUID().toString();
|
||||
request.setAttribute(REQUEST_ID, requestId);
|
||||
response.setHeader("X-Request-Id", requestId);
|
||||
request.setAttribute(INSTANCE_ID, INSTANCE);
|
||||
response.setHeader("X-Instance-Id", INSTANCE);
|
||||
long start = System.nanoTime();
|
||||
try {
|
||||
String path = request.getRequestURI();
|
||||
boolean publicEndpoint = (path.equals("/api/v1/users") && request.getMethod().equals("POST"))
|
||||
|| (path.equals("/api/v1/auth/sessions") && request.getMethod().equals("POST"));
|
||||
if (path.startsWith("/api/v1/") && !publicEndpoint) {
|
||||
String header = request.getHeader("Authorization");
|
||||
if (header == null || !header.startsWith("Bearer ")) throw ApiException.unauthorized();
|
||||
String token = header.substring("Bearer ".length()).trim();
|
||||
request.setAttribute(TOKEN, token);
|
||||
request.setAttribute(ACTOR, sessions.authenticate(token));
|
||||
}
|
||||
chain.doFilter(request, response);
|
||||
} catch (ApiException exception) {
|
||||
writeError(response, exception.status(), exception.code(), exception.getMessage(), requestId, request);
|
||||
} catch (Exception exception) {
|
||||
if (unavailable(exception)) {
|
||||
writeError(response, 503, "unavailable", "Хранилище данных недоступно", requestId, request);
|
||||
} else if (exception instanceof ServletException servlet) {
|
||||
throw servlet;
|
||||
} else if (exception instanceof IOException io) {
|
||||
throw io;
|
||||
} else {
|
||||
throw new ServletException(exception);
|
||||
}
|
||||
} finally {
|
||||
Object route = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
|
||||
LOG.info("request_id={} method={} route={} status={} duration_ms={}", requestId,
|
||||
request.getMethod(), route == null ? "unmapped" : route, response.getStatus(),
|
||||
(System.nanoTime() - start) / 1_000_000);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeError(HttpServletResponse response, int status, String code, String message,
|
||||
String requestId, HttpServletRequest request) throws IOException {
|
||||
response.setStatus(status);
|
||||
if (status == 401) {
|
||||
String challenge = request.getRequestURI().equals("/api/v1/auth/sessions")
|
||||
? "Basic realm=\"messenger\", charset=\"UTF-8\""
|
||||
: "Bearer realm=\"messenger\"";
|
||||
response.setHeader("WWW-Authenticate", challenge);
|
||||
}
|
||||
response.setContentType("application/json");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
mapper.writeValue(response.getOutputStream(), new ErrorBody(new ErrorDetail(code, message, requestId)));
|
||||
}
|
||||
|
||||
private boolean unavailable(Throwable error) {
|
||||
for (Throwable cause = error; cause != null; cause = cause.getCause()) {
|
||||
if (cause instanceof DataAccessException || cause instanceof SQLException) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String instanceId() {
|
||||
String configured = System.getenv("API_INSTANCE_ID");
|
||||
if (configured != null && !configured.isBlank()) return configured;
|
||||
String hostname = System.getenv("HOSTNAME");
|
||||
return hostname == null || hostname.isBlank() ? UUID.randomUUID().toString() : hostname;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import io.minio.GetObjectArgs;
|
||||
import io.minio.MakeBucketArgs;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import io.minio.RemoveObjectArgs;
|
||||
import io.minio.BucketExistsArgs;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
@Component
|
||||
public class S3Storage {
|
||||
private final MinioClient client;
|
||||
private final String bucket;
|
||||
|
||||
public S3Storage() {
|
||||
String endpoint = env("S3_ENDPOINT", "http://minio:9000");
|
||||
String access = env("S3_ACCESS_KEY_ID", "messenger-api");
|
||||
String secret = env("S3_SECRET_ACCESS_KEY", "messenger-api-secret");
|
||||
this.bucket = env("S3_BUCKET", "messenger-attachments");
|
||||
this.client = MinioClient.builder().endpoint(endpoint).credentials(access, secret).build();
|
||||
try {
|
||||
if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build())) {
|
||||
client.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
throw new IllegalStateException("S3 bucket is unavailable", exception);
|
||||
}
|
||||
}
|
||||
|
||||
public void put(String key, InputStream input, long size, String contentType) {
|
||||
try {
|
||||
client.putObject(PutObjectArgs.builder().bucket(bucket).object(key).stream(input, size, -1)
|
||||
.contentType(contentType).build());
|
||||
} catch (Exception exception) {
|
||||
throw new ApiException(503, "unavailable", "S3-хранилище недоступно");
|
||||
}
|
||||
}
|
||||
|
||||
public void check() {
|
||||
try {
|
||||
if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build())) {
|
||||
throw new IllegalStateException("bucket is missing");
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
throw new ApiException(503, "unavailable", "S3-хранилище недоступно");
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream get(String key) {
|
||||
try {
|
||||
return client.getObject(GetObjectArgs.builder().bucket(bucket).object(key).build());
|
||||
} catch (Exception exception) {
|
||||
throw new ApiException(503, "unavailable", "S3-хранилище недоступно");
|
||||
}
|
||||
}
|
||||
|
||||
public void delete(String key) {
|
||||
try {
|
||||
client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(key).build());
|
||||
} catch (Exception ignored) {
|
||||
// Best-effort compensation; orphan cleanup is documented in REPORT.md.
|
||||
}
|
||||
}
|
||||
|
||||
private static String env(String name, String fallback) {
|
||||
String value = System.getenv(name);
|
||||
return value == null || value.isBlank() ? fallback : value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import static ru.mtusi.messenger.Models.*;
|
||||
|
||||
@Service
|
||||
public class SessionService {
|
||||
private static final Base64.Encoder TOKEN_ENCODER = Base64.getUrlEncoder().withoutPadding();
|
||||
private final MessengerStore store;
|
||||
private final StringRedisTemplate redis;
|
||||
private final SCryptPasswordEncoder passwords = SCryptPasswordEncoder.defaultsForSpringSecurity_v5_8();
|
||||
private final SecureRandom random = new SecureRandom();
|
||||
private final long ttlSeconds;
|
||||
private final long databaseTimeoutMillis;
|
||||
private final ExecutorService databaseExecutor = Executors.newFixedThreadPool(4);
|
||||
|
||||
public SessionService(MessengerStore store, StringRedisTemplate redis,
|
||||
@Value("${SESSION_TTL_SECONDS:3600}") long ttlSeconds,
|
||||
@Value("${DB_REQUEST_TIMEOUT_MS:2000}") long databaseTimeoutMillis) {
|
||||
this.store = store;
|
||||
this.redis = redis;
|
||||
this.ttlSeconds = Math.max(2, ttlSeconds);
|
||||
this.databaseTimeoutMillis = Math.max(250, databaseTimeoutMillis);
|
||||
}
|
||||
|
||||
public String hash(String password) { return passwords.encode(password); }
|
||||
|
||||
public Session login(String username, String password) {
|
||||
Credentials credentials = database(() -> store.credentials(username));
|
||||
if (credentials == null || !passwords.matches(password, credentials.passwordHash())) throw ApiException.unauthorized();
|
||||
byte[] bytes = new byte[32];
|
||||
random.nextBytes(bytes);
|
||||
String token = TOKEN_ENCODER.encodeToString(bytes);
|
||||
Instant expires = Instant.now().plusSeconds(ttlSeconds);
|
||||
try {
|
||||
redis.opsForValue().set(key(token), credentials.user().id().toString(), Duration.ofSeconds(ttlSeconds));
|
||||
} catch (RuntimeException exception) {
|
||||
throw new ApiException(503, "unavailable", "Сервис сессий недоступен");
|
||||
}
|
||||
return new Session(token, "Bearer", expires, credentials.user());
|
||||
}
|
||||
|
||||
public User authenticate(String token) {
|
||||
if (token == null || token.isBlank()) throw ApiException.unauthorized();
|
||||
String value;
|
||||
try { value = redis.opsForValue().get(key(token)); }
|
||||
catch (RuntimeException exception) { throw new ApiException(503, "unavailable", "Сервис сессий недоступен"); }
|
||||
if (value == null) throw ApiException.unauthorized();
|
||||
User user = database(() -> store.findUser(UUID.fromString(value)));
|
||||
if (user == null) throw ApiException.unauthorized();
|
||||
return user;
|
||||
}
|
||||
|
||||
public void revoke(String token) {
|
||||
if (token == null) throw ApiException.unauthorized();
|
||||
try { redis.delete(key(token)); }
|
||||
catch (RuntimeException exception) { throw new ApiException(503, "unavailable", "Сервис сессий недоступен"); }
|
||||
}
|
||||
|
||||
public void check() { redis.opsForValue().get("messenger:readiness"); }
|
||||
|
||||
private String key(String token) { return "messenger:session:" + token; }
|
||||
|
||||
private <T> T database(java.util.concurrent.Callable<T> operation) {
|
||||
Future<T> future = databaseExecutor.submit(operation);
|
||||
try {
|
||||
return future.get(databaseTimeoutMillis, TimeUnit.MILLISECONDS);
|
||||
} catch (TimeoutException exception) {
|
||||
future.cancel(true);
|
||||
throw new ApiException(503, "unavailable", "Хранилище данных недоступно");
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new ApiException(503, "unavailable", "Хранилище данных недоступно");
|
||||
} catch (ExecutionException exception) {
|
||||
Throwable cause = exception.getCause();
|
||||
if (cause instanceof RuntimeException runtime) throw runtime;
|
||||
throw new IllegalStateException(cause);
|
||||
}
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
void shutdownDatabaseExecutor() {
|
||||
databaseExecutor.shutdownNow();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user