# martiandreamer/chat_application architecture

> Architecture diagram of the GitHub repository martiandreamer/chat_application, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/martiandreamer/chat_application
- Repository: https://github.com/martiandreamer/chat_application

## Overview

- Frontend Client(s): Although the primary focus is the backend, there is a “tests/simple-frontend” folder that shows an example chat client (chat.html and chat.js). Represent this as the Client that interacts with REST APIs and WebSocket endpoints.
- Chat Application (Backend Server): This is the core application, bootstrapped by ChatApplicationApplication.java.
- API Layer:
- REST Controllers: Under “controller/rest” (e.g., LoginController, UserController, NotificationController, etc.) – these handle HTTP requests for operations like login, admin tasks, and other user management functions.
- WebSocket Controllers: Folder “controller/websocket” (e.g., MessageController) – these manage real-time messaging.
- Security & Authentication Components:
- Security Configurations: Files such as WebSecurityConfig, WebSocketAuthenticationConfig, and WebSocketAuthorizationSecurityConfig that configure HTTP and WebSocket security.
- JWT and Token Management: Classes like WebSocketJwtChannelInterceptor and JwtService indicate the use of JWT tokens.
- Authorization: The “authentication/authorization” package includes classes to enforce permissions for attachments, conversations, messages, and relationships.
- Service Layer: Multiple services exist (e.g., UserService, MessageService, ConversationService, AttachmentService, NotificationService, etc.) that contain the business logic.
- Data Access Layer (Repository):
- Repositories for each Entity (e.g., UserRepository, MessageRepository, ConversationRepository, etc.) that likely interface with a relational database.
- Domain Model (Entities):
- Entities such as User, Message, Conversation, Attachment, Notification, etc., define the data structure and are found in the “entity” package.
- Aspect-Oriented Programming:
- Components under the “aspect” folder (with annotation and processor packages) indicate that some cross-cutting concerns like encryption for passwords are addressed via AOP.
- Utility and Configuration Support:
- “util” classes (like CommonUtils and PrincipalUtils), configuration classes (e.g., WebSocketConfig), constants, and validation components that add additional functional layers.

##### Out the Relationships and Interactions
- From the Services, indicate interactions with the Repository layer to perform CRUD operations on the database.
- Security flows and filters: Indicate how HTTP and WebSocket requests pass through JWT and CORS filters (e.g., HttpJwtFilter, CustomWsCorsFilter) before being processed by controllers.

##### Architectural Patterns and Design Principles
- Layered Architecture: Clearly show the separation into Controller, Service, Repository, and Database layers.
- MVC/MVCS for REST endpoints: Controllers handle inputs, services perform business logic, and repositories manage persistence.
- AOP for Cross-Cutting Concerns: Emphasize the role of aspects in managing encryption or other concerns.
- Security Concepts: Distinguish authentication and authorization responsibilities across dedicated configuration files and interceptor patterns.
- Modular Design: Identify how each functionality (chat messaging, user management, file attachments) resides in dedicated packages with clear separation of concerns.

##### Relevant Technologies and Frameworks
- Java and Maven for the build and dependency management.
- Spring Boot (assumed from the structure) for overall framework management.
- Spring Security (given the multiple security configuration classes) for authentication/authorization.
- WebSocket support for real-time messaging.
- AspectJ or Spring AOP for encryption and other cross-cutting concerns.
- Database integration (likely using Spring Data JPA with a relational database given the repository pattern).

- Each component must have a clear label. For example, “WebSocket Controller (MessageController)” or “Service Layer (AttachmentService)”.
Client → REST Controller (LoginController) → Authentication Service → JWT Token Generation → Client.
Client → WebSocket Controller (MessageController) → Message Service → Message Repository → Database.
- Annotations such as “AOP encryption applied here” at the points where aspects interact with services.

· REST API Layer: Comprising all REST controllers.
· WebSocket API Layer: Comprising WebSocket controllers.
- Have a separate branch illustrating security components:
· Show a “Security & Authentication” module that intercepts incoming requests (display JWT filters, Csrf filters, etc.), which then directs valid requests onward.
- An “AOP Aspects” module detailing cross-cutting concerns such as encryption, attached via connectors to where password handling occurs.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend and Backend
    Client["Frontend Client<br>(chat.html, chat.js)"]:::client
    click Client "https://github.com/martiandreamer/chat_application/tree/main/tests/simple-frontend"
    
    Backend["Chat Application (Backend Server)"]:::backend
    click Backend "https://github.com/martiandreamer/chat_application/blob/main/src/main/java/vn/edu/uit/chat_application/ChatApplicationApplication.java"
    
    Client -->|"HTTP/WebSocket Request"| Backend

    %% API Layer Subgraph
    subgraph "API Layer"
        REST_API["REST API Controllers<br>(e.g., LoginController, UserController)"]:::rest
        click REST_API "https://github.com/martiandreamer/chat_application/tree/main/src/main/java/vn/edu/uit/chat_application/controller/rest"
        WS_API["WebSocket Controller<br>(MessageController)"]:::websocket
        click WS_API "https://github.com/martiandreamer/chat_application/blob/main/src/main/java/vn/edu/uit/chat_application/controller/websocket/MessageController.java"
    end

    Backend --> REST_API
    Backend --> WS_API

    %% Service Layer
    Service["Service Layer<br>(UserService, MessageService, etc.)"]:::service
    click Service "https://github.com/martiandreamer/chat_application/tree/main/src/main/java/vn/edu/uit/chat_application/service"
    REST_API -->|"Process Request"| Service
    WS_API -->|"Process Request"| Service

    %% Repository and Domain Model
    Repository["Repository Layer<br>(UserRepository, MessageRepository, etc.)"]:::repository
    click Repository "https://github.com/martiandreamer/chat_application/tree/main/src/main/java/vn/edu/uit/chat_application/repository"
    Service -->|"CRUD Operations"| Repository

    Domain["Domain Model<br>(Entities: User, Message, etc.)"]:::domain
    click Domain "https://github.com/martiandreamer/chat_application/tree/main/src/main/java/vn/edu/uit/chat_application/entity"
    Repository -->|"Maps to"| Domain

    %% Database
    Database["Database"]:::database
    Repository -->|"Persists Data"| Database

    %% Security & Authentication Subgraph
    subgraph "Security & Authentication"
        HTTP_Security["HTTP Security<br>(WebSecurityConfig)"]:::security
        click HTTP_Security "https://github.com/martiandreamer/chat_application/blob/main/src/main/java/vn/edu/uit/chat_application/authentication/WebSecurityConfig.java"
        WS_Auth["WebSocket Authentication<br>(WebSocketAuthenticationConfig)"]:::security
        click WS_Auth "https://github.com/martiandreamer/chat_application/blob/main/src/main/java/vn/edu/uit/chat_application/authentication/WebSocketAuthenticationConfig.java"
        WS_Authorization["WebSocket Authorization<br>(WebSocketAuthorizationSecurityConfig)"]:::security
        click WS_Authorization "https://github.com/martiandreamer/chat_application/blob/main/src/main/java/vn/edu/uit/chat_application/authentication/WebSocketAuthorizationSecurityConfig.java"
        JWT_Interceptor["JWT Interceptor<br>(WebSocketJwtChannelInterceptor)"]:::security
        click JWT_Interceptor "https://github.com/martiandreamer/chat_application/blob/main/src/main/java/vn/edu/uit/chat_application/authentication/WebSocketJwtChannelInterceptor.java"
        HTTP_JWT_Filter["HTTP JWT Filter"]:::security
        click HTTP_JWT_Filter "https://github.com/martiandreamer/chat_application/blob/main/src/main/java/vn/edu/uit/chat_application/filter/HttpJwtFilter.java"
        WS_CORS_Filter["Custom WS CORS Filter"]:::security
        click WS_CORS_Filter "https://github.com/martiandreamer/chat_application/blob/main/src/main/java/vn/edu/uit/chat_application/filter/CustomWsCorsFilter.java"
        Auth_Components["Authorization Components"]:::security
        click Auth_Components "https://github.com/martiandreamer/chat_application/tree/main/src/main/java/vn/edu/uit/chat_application/authentication/authorization"
    end

    %% Security connections (dotted lines)
    HTTP_Security --- REST_API
    WS_Auth --- WS_API
    WS_Authorization --- WS_API
    HTTP_JWT_Filter --- REST_API
    WS_CORS_Filter --- WS_API
    JWT_Interceptor --- WS_API
    Auth_Components --- Service

    %% AOP Aspects Subgraph
    subgraph "AOP Aspects"
        Encrypt_Annotation["EncryptPassword Annotation"]:::aop
        click Encrypt_Annotation "https://github.com/martiandreamer/chat_application/blob/main/src/main/java/vn/edu/uit/chat_application/aspect/annotation/EncryptPassword.java"
        Encrypt_Processor["EncryptPassword Processor"]:::aop
        click Encrypt_Processor "https://github.com/martiandreamer/chat_application/blob/main/src/main/java/vn/edu/uit/chat_application/aspect/processor/EncryptPasswordProcessor.java"
    end

    %% AOP connection (dashed line)
    Service -.->|"AOP Encryption Applied"| Encrypt_Annotation
    Service -.->|"AOP Encryption Applied"| Encrypt_Processor

    %% Configuration & Utilities Subgraph
    subgraph "Configuration & Utilities"
        WS_Config["WebSocket Config"]:::config
        click WS_Config "https://github.com/martiandreamer/chat_application/blob/main/src/main/java/vn/edu/uit/chat_application/config/WebSocketConfig.java"
        Common_Utils["Common Utilities"]:::config
        click Common_Utils "https://github.com/martiandreamer/chat_application/blob/main/src/main/java/vn/edu/uit/chat_application/util/CommonUtils.java"
        Principal_Utils["Principal Utils"]:::config
        click Principal_Utils "https://github.com/martiandreamer/chat_application/blob/main/src/main/java/vn/edu/uit/chat_application/util/PrincipalUtils.java"
        Constants["Constants & Roles"]:::config
        click Constants "https://github.com/martiandreamer/chat_application/tree/main/src/main/java/vn/edu/uit/chat_application/constants"
    end

    %% Config connection (dashed line)
    WS_Config -.-> WS_API

    %% Styles
    classDef client fill:#A2D9CE,stroke:#333,stroke-width:2px;
    classDef backend fill:#F9E79F,stroke:#333,stroke-width:2px;
    classDef rest fill:#FAD7A0,stroke:#333,stroke-width:2px;
    classDef websocket fill:#F5B7B1,stroke:#333,stroke-width:2px;
    classDef service fill:#D7BDE2,stroke:#333,stroke-width:2px;
    classDef repository fill:#AED6F1,stroke:#333,stroke-width:2px;
    classDef domain fill:#E8F8F5,stroke:#333,stroke-width:2px;
    classDef database fill:#FBEEC1,stroke:#333,stroke-width:2px,stroke-dasharray: 5 5;
    classDef security fill:#F7DC6F,stroke:#333,stroke-width:2px;
    classDef aop fill:#D5F5E3,stroke:#333,stroke-width:2px;
    classDef config fill:#E8DAEF,stroke:#333,stroke-width:2px;
```
