commit 017b235f2c3d417df472422b8738256a2af2bf1a Author: Jose Conde Date: Wed Sep 24 16:17:57 2025 +0200 new version diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..8af972c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +/gradlew text eol=lf +*.bat text eol=crlf +*.jar binary diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5a979af --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Kotlin ### +.kotlin diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..b0f20e6 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,60 @@ +plugins { + kotlin("jvm") version "1.9.25" + kotlin("plugin.spring") version "1.9.25" + id("org.springframework.boot") version "3.5.6" + id("io.spring.dependency-management") version "1.1.7" +} + +group = "net.xintanalabs.rssotto" +version = "0.0.1-SNAPSHOT" +description = "Project for Spring Boot" + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(17) + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation("com.fasterxml.jackson.module:jackson-module-kotlin") + implementation("org.jetbrains.kotlin:kotlin-reflect") + + implementation("io.jsonwebtoken:jjwt-api:0.12.3") + implementation("io.jsonwebtoken:jjwt-impl:0.12.3") + implementation("io.jsonwebtoken:jjwt-jackson:0.12.3") + + implementation("org.springframework.boot:spring-boot-starter-security") + implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.boot:spring-boot-starter-webflux") + implementation("org.springframework.boot:spring-boot-starter-data-mongodb:3.2.5") + + implementation("org.jetbrains.kotlin:kotlin-reflect") + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.8.1") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3") + implementation("org.jsoup:jsoup:1.21.2") + implementation("jakarta.servlet:jakarta.servlet-api:6.0.0") + implementation("org.mongodb:mongodb-driver-kotlin-coroutine:5.5.0") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin") + implementation("org.seleniumhq.selenium:selenium-java:4.35.0") + + testImplementation("org.springframework.security:spring-security-test") + testImplementation("org.springframework.boot:spring-boot-starter-test") + testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") + testRuntimeOnly("org.junit.platform:junit-platform-launcher") +} + +kotlin { + compilerOptions { + freeCompilerArgs.addAll("-Xjsr305=strict") + } +} + +tasks.withType { + useJUnitPlatform() +} diff --git a/chromedriver.log b/chromedriver.log new file mode 100644 index 0000000..e69de29 diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..1b33c55 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..d4081da --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..23d15a9 --- /dev/null +++ b/gradlew @@ -0,0 +1,251 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH="\\\"\\\"" + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..db3a6ac --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH= + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..5b91591 --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = "rssotto" diff --git a/src/main/kotlin/net/xintanalabs/rssotto/RssottoApplication.kt b/src/main/kotlin/net/xintanalabs/rssotto/RssottoApplication.kt new file mode 100644 index 0000000..22b6847 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/RssottoApplication.kt @@ -0,0 +1,11 @@ +package net.xintanalabs.rssotto + +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication + +@SpringBootApplication +class RssottoApplication + +fun main(args: Array) { + runApplication(*args) +} diff --git a/src/main/kotlin/net/xintanalabs/rssotto/components/checker/CheckerFactory.kt b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/CheckerFactory.kt new file mode 100644 index 0000000..923796d --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/CheckerFactory.kt @@ -0,0 +1,20 @@ +package net.xintanalabs.rssotto.components.checkers + +import net.xintanalabs.rssotto.components.checker.api.ApiChecker +import net.xintanalabs.rssotto.components.checkers.scrape.ScrapeChecker +import org.springframework.stereotype.Component + +@Component +class CheckerFactory( + private val apiChecker: ApiChecker, + private val scrapeChecker: ScrapeChecker +) { + fun createChecker(type: String): IVersionChecker { + val parts = type.split(":") + return when (parts[0].lowercase()) { + "scrape" -> scrapeChecker + "api" -> apiChecker + else -> throw IllegalArgumentException("Unknown checker type: $type") + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/components/checker/IVersionChecker.kt b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/IVersionChecker.kt new file mode 100644 index 0000000..4f688a5 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/IVersionChecker.kt @@ -0,0 +1,7 @@ +package net.xintanalabs.rssotto.components.checkers + +import net.xintanalabs.rssotto.model.Source + +interface IVersionChecker { + suspend fun getLatestVersion(paramsDict: Map): String +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/components/checker/api/ApiChecker.kt b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/api/ApiChecker.kt new file mode 100644 index 0000000..8c0ce54 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/api/ApiChecker.kt @@ -0,0 +1,38 @@ +package net.xintanalabs.rssotto.components.checker.api + +import kotlinx.serialization.json.* +import net.xintanalabs.rssotto.components.checkers.IVersionChecker +import org.springframework.stereotype.Component +import org.springframework.web.reactive.function.client.WebClient +import org.springframework.web.reactive.function.client.awaitBody +import org.springframework.web.reactive.function.client.awaitExchange +import kotlin.text.get + +@Component +class ApiChecker(private val webClient: WebClient) : IVersionChecker { + override suspend fun getLatestVersion(paramsDict: Map): String { + val url = paramsDict["url"]?.takeIf { it.isNotEmpty() } + ?: throw IllegalArgumentException("API URL required") + val jsonPath = paramsDict["jsonPath"]?.takeIf { it.isNotEmpty() } + ?: throw IllegalArgumentException("jsonPath required") + + val response = webClient.get() + .uri(url) + .awaitExchange { it.awaitBody() } + ?: throw Exception("Empty API response") + + val jsonDoc = Json.parseToJsonElement(response) + + var element: JsonElement = jsonDoc + for (key in jsonPath.split('.')) { + element = (element as? JsonObject)?.get(key) + ?: throw Exception("JSON key '$key' not found in response") + } + + if (element is JsonPrimitive && element.isString) { + return element.content.trim() + } else { + throw Exception("JSON value for '$jsonPath' is not a string") + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/DefaultScrapeFetcher.kt b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/DefaultScrapeFetcher.kt new file mode 100644 index 0000000..dc0debe --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/DefaultScrapeFetcher.kt @@ -0,0 +1,13 @@ +package net.xintanalabs.rssotto.components.checkers.scrape + +import org.springframework.beans.factory.annotation.Qualifier +import org.springframework.stereotype.Component +import org.springframework.web.client.RestTemplate + +@Component("default") +class DefaultScrapeFetcher(private val restTemplate: RestTemplate) : IScrapeFetcher{ + override suspend fun fetch(url: String): String { + return restTemplate.getForObject(url, String::class.java) + ?: throw Exception("Empty response from URL") + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/IScrapeFetcher.kt b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/IScrapeFetcher.kt new file mode 100644 index 0000000..d35f262 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/IScrapeFetcher.kt @@ -0,0 +1,5 @@ +package net.xintanalabs.rssotto.components.checkers.scrape + +interface IScrapeFetcher { + suspend fun fetch(url: String): String +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/JSoupFetcher.kt b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/JSoupFetcher.kt new file mode 100644 index 0000000..c4731e2 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/JSoupFetcher.kt @@ -0,0 +1,25 @@ +package net.xintanalabs.rssotto.components.checkers.scrape + +import kotlinx.coroutines.delay +import org.jsoup.Jsoup +import org.springframework.stereotype.Component + +@Component("jsoup") +class JSoupFetcher: IScrapeFetcher { + override suspend fun fetch(url: String): String { + delay(1000) // 1-second delay to avoid rate limiting + return try { + Jsoup.connect(url) + .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36") + .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") + .header("Accept-Language", "en-US,en;q=0.5") + .header("Accept-Encoding", "gzip, deflate") + //.header("Connection", "keep-alive") + .header("Upgrade-Insecure-Requests", "1") + .get() + .html() + } catch (e: Exception) { + throw RuntimeException("Error fetching ${url}", e) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/ScrapeChecker.kt b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/ScrapeChecker.kt new file mode 100644 index 0000000..012f5c2 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/ScrapeChecker.kt @@ -0,0 +1,43 @@ +package net.xintanalabs.rssotto.components.checkers.scrape + +import kotlinx.coroutines.delay +import net.xintanalabs.rssotto.components.checkers.IVersionChecker +import net.xintanalabs.rssotto.model.Source +import net.xintanalabs.rssotto.tasks.ScheduledTasks +import org.openqa.selenium.chrome.ChromeDriver +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Component +import org.springframework.web.client.RestTemplate +import java.util.regex.Pattern + +@Component +class ScrapeChecker( + private val restTemplate: RestTemplate, + private val chromeDriver: ChromeDriver +) : IVersionChecker { + + private val log = LoggerFactory.getLogger(ScrapeChecker::class.java) + + override suspend fun getLatestVersion(paramsDict: Map): String { + val url = paramsDict["url"]?.takeIf { it.isNotEmpty() } + ?: throw IllegalArgumentException("URL required") + log.info("Url : {}", url) + val mode = paramsDict["mode"] + log.info("Mode : {}", mode) + val fetcher: IScrapeFetcher = when (mode) { + "selenium" -> SeleniumFetcher(chromeDriver) + "jsoup" -> JSoupFetcher() + else -> DefaultScrapeFetcher(restTemplate) + } + val response = fetcher.fetch(url) + val cleanedResponse = response.replace(">\\s+<".toRegex(), "><") + + val regex: String = paramsDict["regex"] as String + log.info("Regex : {}", regex) + val match = Pattern.compile(regex).matcher(cleanedResponse) + if (!match.find() || match.groupCount() < 1) { + throw Exception("No match with regex in response") + } + return match.group(1) + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/SeleniumFetcher.kt b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/SeleniumFetcher.kt new file mode 100644 index 0000000..04274fc --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/components/checker/scrape/SeleniumFetcher.kt @@ -0,0 +1,19 @@ +package net.xintanalabs.rssotto.components.checkers.scrape + +import kotlinx.coroutines.delay +import org.openqa.selenium.chrome.ChromeDriver +import org.springframework.stereotype.Component + +@Component("selenium") +class SeleniumFetcher(private val chromeDriver: ChromeDriver): IScrapeFetcher { + override suspend fun fetch(url: String): String { + try { + + chromeDriver.get(url) + delay(2000) // Consider WebDriverWait for robustness + return chromeDriver.pageSource + } finally { + // Note: Don't quit driver here since it's Spring-managed + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/config/Configuration.kt b/src/main/kotlin/net/xintanalabs/rssotto/config/Configuration.kt new file mode 100644 index 0000000..ed9f405 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/config/Configuration.kt @@ -0,0 +1,76 @@ +package net.xintanalabs.rssotto.config + +import net.xintanalabs.rssotto.repository.UserRepository +import net.xintanalabs.rssotto.service.CustomUserDetailsService +import org.openqa.selenium.chrome.ChromeDriver +import org.openqa.selenium.chrome.ChromeDriverService +import org.openqa.selenium.chrome.ChromeOptions +import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.security.authentication.AuthenticationManager +import org.springframework.security.authentication.AuthenticationProvider +import org.springframework.security.authentication.dao.DaoAuthenticationProvider +import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration +import org.springframework.security.core.userdetails.UserDetailsService +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder +import org.springframework.security.crypto.password.PasswordEncoder +import org.springframework.web.client.RestTemplate +import org.springframework.web.reactive.function.client.WebClient +import java.io.File + +@Configuration +@EnableConfigurationProperties(JwtProperties::class) +class Configuration{ + @Bean + fun userDetailsService(userRepository: UserRepository) : UserDetailsService = + CustomUserDetailsService(userRepository) + + @Bean + fun encoder(): PasswordEncoder = BCryptPasswordEncoder() + + @Bean + fun authenticationProvider(userRepository: UserRepository): AuthenticationProvider = + DaoAuthenticationProvider() + .also { + it.setUserDetailsService(userDetailsService(userRepository)) + it.setPasswordEncoder(encoder()) + } + @Bean + fun authenticationManager(config: AuthenticationConfiguration): AuthenticationManager = + config.authenticationManager + + @Bean + fun webClient(builder: WebClient.Builder): WebClient { + return builder.build() + } + + @Bean + fun restTemplate(): RestTemplate { + return RestTemplate() + } + + @Bean(destroyMethod = "quit") + fun chromeDriver(): ChromeDriver { + // Set log file to suppress console output (optional) + val logFile = File("chromedriver.log") + val service = ChromeDriverService.Builder() + .withLogFile(logFile) // Redirect logs to a file + .withSilent(true) // Suppress console output + .withVerbose(false) // Explicitly disable verbose logging + .build() + + // Optional: Set hideCommandPromptWindow for Windows + System.setProperty("webdriver.chrome.hideCommandPromptWindow", "true") + + val options = ChromeOptions().apply { + addArguments("--headless") + addArguments("--disable-gpu") + addArguments("--no-sandbox") + addArguments("--disable-dev-shm-usage") + addArguments("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124") + } + return ChromeDriver(service, options) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/config/JwtAuthenticationFilter.kt b/src/main/kotlin/net/xintanalabs/rssotto/config/JwtAuthenticationFilter.kt new file mode 100644 index 0000000..5b5be75 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/config/JwtAuthenticationFilter.kt @@ -0,0 +1,60 @@ +package net.xintanalabs.rssotto.config + +import jakarta.servlet.FilterChain +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpServletResponse +import net.xintanalabs.rssotto.service.CustomUserDetailsService +import net.xintanalabs.rssotto.service.TokenService +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken +import org.springframework.security.core.context.SecurityContextHolder +import org.springframework.security.core.userdetails.UserDetails +import org.springframework.security.web.authentication.WebAuthenticationDetailsSource +import org.springframework.stereotype.Component +import org.springframework.web.filter.OncePerRequestFilter + +@Component +class JwtAuthenticationFilter( + private val userDetailService: CustomUserDetailsService, + private val tokenService: TokenService +) : OncePerRequestFilter() { + + override fun doFilterInternal( + request: HttpServletRequest, + response: HttpServletResponse, + filterChain: FilterChain + ) { + val authHeader: String? = request.getHeader("Authorization") + + if (authHeader.doesNotContainBearerToken()) { + filterChain.doFilter(request, response) + return + } + + val jwtToken = authHeader!!.extractTokenValue() + val username = tokenService.extractUsername(jwtToken) + + if (username != null && SecurityContextHolder.getContext().authentication == null){ + val foundUser = userDetailService.loadUserByUsername(username) + if (tokenService.isValid(jwtToken, foundUser)) { + updateContext(foundUser, request) + } + filterChain.doFilter(request, response) + } + } + + private fun updateContext( + foundUser: UserDetails, + request: HttpServletRequest + ) { + val authToken = UsernamePasswordAuthenticationToken(foundUser, null, foundUser.authorities) + authToken.details = WebAuthenticationDetailsSource().buildDetails(request) + SecurityContextHolder.getContext().authentication = authToken + } + + + private fun String?.doesNotContainBearerToken(): Boolean = + this == null || !this.startsWith("Bearer ") + + private fun String.extractTokenValue() = + this.substringAfter("Bearer ").trim() +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/config/JwtProperties.kt b/src/main/kotlin/net/xintanalabs/rssotto/config/JwtProperties.kt new file mode 100644 index 0000000..b55f976 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/config/JwtProperties.kt @@ -0,0 +1,10 @@ +package net.xintanalabs.rssotto.config + +import org.springframework.boot.context.properties.ConfigurationProperties + +@ConfigurationProperties("jwt") +data class JwtProperties( + val key: String, + val accessTokenExpiration: Long, + val refreshTokenExpiration: Long +) \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/config/SecurityConfiguration.kt b/src/main/kotlin/net/xintanalabs/rssotto/config/SecurityConfiguration.kt new file mode 100644 index 0000000..4d19816 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/config/SecurityConfiguration.kt @@ -0,0 +1,43 @@ +package net.xintanalabs.rssotto.config + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.http.HttpMethod +import org.springframework.security.authentication.AuthenticationProvider +import org.springframework.security.config.annotation.web.builders.HttpSecurity +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity +import org.springframework.security.config.http.SessionCreationPolicy +import org.springframework.security.web.DefaultSecurityFilterChain +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter + +@Configuration +@EnableWebSecurity +class SecurityConfiguration( + private val authenticationProvider: AuthenticationProvider +) { + + @Bean + fun securityFilterChain( + http: HttpSecurity, + jwtAuthenticationFilter: JwtAuthenticationFilter + ): DefaultSecurityFilterChain = + http + .csrf { it.disable() } + .authorizeHttpRequests { + it + .requestMatchers("/api/auth", "/api/auth/refresh", "/error") + .permitAll() + .requestMatchers(HttpMethod.POST, "/api/user") + .permitAll() + .requestMatchers( "/api/user**") + .hasRole("ADMIN") + .anyRequest() + .fullyAuthenticated() + } + .sessionManagement { + it.sessionCreationPolicy(SessionCreationPolicy.STATELESS) + } + .authenticationProvider(authenticationProvider) + .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter::class.java) + .build() +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/constants/Constants.kt b/src/main/kotlin/net/xintanalabs/rssotto/constants/Constants.kt new file mode 100644 index 0000000..f087f27 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/constants/Constants.kt @@ -0,0 +1,9 @@ +package net.xintanalabs.rssotto.constants + +object Constants { + const val API_BASE_PATH: String = "/api" + const val COLLECTION_TYPES: String = "types" + const val COLLECTION_APPS: String = "apps" + const val COLLECTION_SOURCES: String = "sources" + const val COLLECTION_CHECKER_TYPES: String = "checkerTypes" +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/controller/article/ArticleController.kt b/src/main/kotlin/net/xintanalabs/rssotto/controller/article/ArticleController.kt new file mode 100644 index 0000000..39c2f21 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/controller/article/ArticleController.kt @@ -0,0 +1,27 @@ +package net.xintanalabs.rssotto.controller.article + +import net.xintanalabs.rssotto.constants.Constants +import net.xintanalabs.rssotto.model.Article +import net.xintanalabs.rssotto.service.ArticleService +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +@RequestMapping("${Constants.API_BASE_PATH}/article") +class ArticleController( + private val articleService: ArticleService +) { + + @GetMapping + fun listAll(): List = + articleService.findAll() + .map { it.toResponse() } + + fun Article.toResponse() = + ArticleResponse( + id = this.id, + title = this.title, + content = this.content + ) +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/controller/article/ArticleResponse.kt b/src/main/kotlin/net/xintanalabs/rssotto/controller/article/ArticleResponse.kt new file mode 100644 index 0000000..6edb75a --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/controller/article/ArticleResponse.kt @@ -0,0 +1,9 @@ +package net.xintanalabs.rssotto.controller.article + +import java.util.UUID + +data class ArticleResponse( + val id: UUID, + val title: String, + val content: String +) diff --git a/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/AuthController.kt b/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/AuthController.kt new file mode 100644 index 0000000..66bdd99 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/AuthController.kt @@ -0,0 +1,28 @@ +package net.xintanalabs.rssotto.controller.auth + +import net.xintanalabs.rssotto.service.AuthenticationService +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController +import org.springframework.web.server.ResponseStatusException + +@RestController +@RequestMapping("/api/auth") +class AuthController( + private val authenticationService: AuthenticationService +) { + + @PostMapping + suspend fun authenticate(@RequestBody authRequest: AuthenticationRequest) : AuthenticationResponse = + authenticationService.authenticate(authRequest) + + @PostMapping("/refresh") + suspend fun refresh(@RequestBody request: RefreshTokenRequest) : TokenResponse = + authenticationService.refreshAccessToken(request.token) + ?.mapToTokenResponse() + ?: throw ResponseStatusException(HttpStatus.FORBIDDEN,"Invalid refresh token") + + private fun String.mapToTokenResponse(): TokenResponse = TokenResponse(this) +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/AuthenticationRequest.kt b/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/AuthenticationRequest.kt new file mode 100644 index 0000000..2edcde1 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/AuthenticationRequest.kt @@ -0,0 +1,6 @@ +package net.xintanalabs.rssotto.controller.auth + +data class AuthenticationRequest ( + val username: String, + val password: String +) diff --git a/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/AuthenticationResponse.kt b/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/AuthenticationResponse.kt new file mode 100644 index 0000000..407e86a --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/AuthenticationResponse.kt @@ -0,0 +1,6 @@ +package net.xintanalabs.rssotto.controller.auth + +data class AuthenticationResponse ( + val accessToken: String, + val refreshToken: String +) diff --git a/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/RefreshTokenRequest.kt b/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/RefreshTokenRequest.kt new file mode 100644 index 0000000..573739d --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/RefreshTokenRequest.kt @@ -0,0 +1,5 @@ +package net.xintanalabs.rssotto.controller.auth + +data class RefreshTokenRequest( + val token: String, +) diff --git a/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/TokenResponse.kt b/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/TokenResponse.kt new file mode 100644 index 0000000..ecb24cc --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/controller/auth/TokenResponse.kt @@ -0,0 +1,5 @@ +package net.xintanalabs.rssotto.controller.auth + +data class TokenResponse ( + val token: String +) \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/controller/source/SourceController.kt b/src/main/kotlin/net/xintanalabs/rssotto/controller/source/SourceController.kt new file mode 100644 index 0000000..3c4cc90 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/controller/source/SourceController.kt @@ -0,0 +1,49 @@ +package net.xintanalabs.rssotto.controller.source + +import net.xintanalabs.rssotto.constants.Constants +import net.xintanalabs.rssotto.model.Source +import net.xintanalabs.rssotto.service.SourceService +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController +import org.springframework.web.server.ResponseStatusException + +@RestController +@RequestMapping("${Constants.API_BASE_PATH}/source") +class SourceController(private val sourceService: SourceService) { + @PostMapping + suspend fun create(@RequestBody sourceRequest: SourceRequest): SourceResponse { + return try { + sourceService.create(sourceRequest.toModel()) + .toResponse() + } catch (e: Exception) { + throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Cannot create source: ${e.message}", e) + } + } + + @GetMapping + fun getAll(): List { + return try { + sourceService.findALl() + } catch (e: Exception) { + throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Cannot retrieve sources: ${e.message}", e) + } + } + + private fun Source.toResponse(): SourceResponse = SourceResponse( + id = this.id, + name = this.name, + checkerType = this.checkerType, + defaults = this.defaults, + ) + + private fun SourceRequest.toModel(): Source = Source( + id = null, + name = this.name, + checkerType = this.checkerType, + defaults = this.defaults, + ) +} diff --git a/src/main/kotlin/net/xintanalabs/rssotto/controller/source/SourceRequest.kt b/src/main/kotlin/net/xintanalabs/rssotto/controller/source/SourceRequest.kt new file mode 100644 index 0000000..f8e7242 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/controller/source/SourceRequest.kt @@ -0,0 +1,7 @@ +package net.xintanalabs.rssotto.controller.source + +data class SourceRequest( + val name: String, + val checkerType: String, + val defaults: Map = mapOf() +) diff --git a/src/main/kotlin/net/xintanalabs/rssotto/controller/source/SourceResponse.kt b/src/main/kotlin/net/xintanalabs/rssotto/controller/source/SourceResponse.kt new file mode 100644 index 0000000..54afc91 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/controller/source/SourceResponse.kt @@ -0,0 +1,8 @@ +package net.xintanalabs.rssotto.controller.source + +data class SourceResponse( + val id: String?, + val name: String, + val checkerType: String, + val defaults: Map = mapOf() +) \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/controller/user/UserController.kt b/src/main/kotlin/net/xintanalabs/rssotto/controller/user/UserController.kt new file mode 100644 index 0000000..33747ef --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/controller/user/UserController.kt @@ -0,0 +1,62 @@ +package net.xintanalabs.rssotto.controller.user + +import net.xintanalabs.rssotto.model.Role +import net.xintanalabs.rssotto.model.User +import net.xintanalabs.rssotto.service.UserService +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.DeleteMapping +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController +import org.springframework.web.server.ResponseStatusException +import java.util.UUID + +@RestController +@RequestMapping("/api/user") +class UserController(private val userService: UserService) { + + @PostMapping + fun create(@RequestBody userRequest: UserRequest): UserResponse? = + userService.createUser(userRequest.toModel()) + ?.toResponse() + ?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Cannot create user") + + @GetMapping + fun listAll(): List = listOf() + //userService.findAll().map { it.toResponse() } + + @GetMapping("/{uuid}") + fun findById(@PathVariable uuid: UUID): UserResponse { + val user = userService.findById(uuid); + return user + ?.toResponse() + ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "User not found") + } + + @DeleteMapping("/{uuid}") + fun deleteById(@PathVariable uuid: UUID): ResponseEntity { + val success = userService.deleteById(uuid) + return if (success) + ResponseEntity.noContent().build() + else + throw ResponseStatusException(HttpStatus.NOT_FOUND, "User not found") + } + + + private fun UserRequest.toModel() = User( + id = UUID.randomUUID(), + username = this.username, + password = this.password, + role = Role.USER + ) + + private fun User.toResponse(): UserResponse = UserResponse( + id = this.id, + username = this.username + ) + +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/controller/user/UserRequest.kt b/src/main/kotlin/net/xintanalabs/rssotto/controller/user/UserRequest.kt new file mode 100644 index 0000000..62e348c --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/controller/user/UserRequest.kt @@ -0,0 +1,6 @@ +package net.xintanalabs.rssotto.controller.user + +data class UserRequest( + val username: String, + val password: String +) diff --git a/src/main/kotlin/net/xintanalabs/rssotto/controller/user/UserResponse.kt b/src/main/kotlin/net/xintanalabs/rssotto/controller/user/UserResponse.kt new file mode 100644 index 0000000..78e8025 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/controller/user/UserResponse.kt @@ -0,0 +1,8 @@ +package net.xintanalabs.rssotto.controller.user + +import java.util.UUID + +data class UserResponse( + val id: UUID, + val username: String +) diff --git a/src/main/kotlin/net/xintanalabs/rssotto/db/mongodb/MongoDBAbstract.kt b/src/main/kotlin/net/xintanalabs/rssotto/db/mongodb/MongoDBAbstract.kt new file mode 100644 index 0000000..1cdcdb7 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/db/mongodb/MongoDBAbstract.kt @@ -0,0 +1,31 @@ +package net.xintanalabs.rssotto.db.mongodb + +import org.springframework.beans.factory.annotation.Autowired + +abstract class MongoDBAbstract ( + private val mongoDBClient: MongoDBClient +) { + protected abstract val collection: String + protected abstract val entityClass: Class + protected val idField: String = "id" + + protected fun create(entity: T): T { + return mongoDBClient.insert(collection, entity, entityClass) + } + + protected fun getAll(): List { + return mongoDBClient.findAll(collection, entityClass) + } + + protected fun getById(id: String): T? { + return mongoDBClient.findOne(collection, idField, id, entityClass) + } + + protected fun delete(id: String): Long { + return mongoDBClient.deleteOne(collection, idField, id, entityClass) + } + + protected fun update(id: String, updateFields: Map): Long { + return mongoDBClient.updateOne(collection, idField, id, updateFields, entityClass) + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/db/mongodb/MongoDBClient.kt b/src/main/kotlin/net/xintanalabs/rssotto/db/mongodb/MongoDBClient.kt new file mode 100644 index 0000000..0ff3a77 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/db/mongodb/MongoDBClient.kt @@ -0,0 +1,70 @@ +package net.xintanalabs.rssotto.db.mongodb + +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.asFlow +import org.springframework.data.mongodb.core.MongoTemplate +import org.springframework.data.mongodb.core.aggregation.Aggregation +import org.springframework.data.mongodb.core.query.Criteria +import org.springframework.data.mongodb.core.query.Query +import org.springframework.data.mongodb.core.query.Update +import org.springframework.stereotype.Component + +@Component +class MongoDBClient(private val mongoTemplate: MongoTemplate) { + + // Insert a single document + fun insert(collectionName: String, document: T, clazz: Class): T { + return mongoTemplate.insert(document, collectionName) + } + + // Insert multiple documents + fun insertMany(collectionName: String, documents: List, clazz: Class): List { + return mongoTemplate.insert(documents, collectionName).toList() + } + + // Find one document by a field + fun findOne(collectionName: String, field: String, value: Any, clazz: Class): T? { + val query = Query(Criteria.where(field).`is`(value)) + return mongoTemplate.findOne(query, clazz, collectionName) + } + + // Find all documents in a collection + fun findAll(collectionName: String, clazz: Class): List { + return mongoTemplate.findAll(clazz, collectionName) + } + + // Find documents with a custom filter + fun findByFilter(collectionName: String, criteria: Criteria, clazz: Class): List { + val query = Query(criteria) + return mongoTemplate.find(query, clazz, collectionName) + } + + // Update one document + fun updateOne(collectionName: String, field: String, value: Any, updateFields: Map, clazz: Class): Long { + val query = Query(Criteria.where(field).`is`(value)) + val update = Update().apply { + updateFields.forEach { (k, v) -> set(k, v) } + } + return mongoTemplate.updateFirst(query, update, clazz, collectionName).modifiedCount + } + + // Delete one document + fun deleteOne(collectionName: String, field: String, value: Any, clazz: Class): Long { + val query = Query(Criteria.where(field).`is`(value)) + return mongoTemplate.remove(query, clazz, collectionName).deletedCount + } + + fun aggregate( + collectionName: String, + aggregation: Aggregation, + inputType: Class, + outputType: Class + ): Flow { + return try { + val results = mongoTemplate.aggregate(aggregation, collectionName, outputType) + results.asFlow() + } catch (e: Exception) { + throw RuntimeException("Aggregation failed: ${e.message}", e) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/model/Article.kt b/src/main/kotlin/net/xintanalabs/rssotto/model/Article.kt new file mode 100644 index 0000000..b076b53 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/model/Article.kt @@ -0,0 +1,9 @@ +package net.xintanalabs.rssotto.model + +import java.util.UUID + +data class Article( + val id: UUID, + val title: String, + val content: String +) diff --git a/src/main/kotlin/net/xintanalabs/rssotto/model/Field.kt b/src/main/kotlin/net/xintanalabs/rssotto/model/Field.kt new file mode 100644 index 0000000..9194c11 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/model/Field.kt @@ -0,0 +1,16 @@ +package net.xintanalabs.rssotto.model + +import com.fasterxml.jackson.annotation.JsonInclude +import org.springframework.data.annotation.Id + +@JsonInclude(JsonInclude.Include.NON_NULL) +data class Field( + @Id val id: String? = null, + val name: String, + val label: String, + val description: String? = null, + val type: String = "string", + val required: Boolean = false, + val controlType: String = "text", + val defaultValue: String? = null +) diff --git a/src/main/kotlin/net/xintanalabs/rssotto/model/Source.kt b/src/main/kotlin/net/xintanalabs/rssotto/model/Source.kt new file mode 100644 index 0000000..3c7d8fc --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/model/Source.kt @@ -0,0 +1,14 @@ +package net.xintanalabs.rssotto.model + +import com.fasterxml.jackson.annotation.JsonInclude +import org.springframework.data.annotation.Id +import org.springframework.data.mongodb.core.mapping.Field + +@JsonInclude(JsonInclude.Include.NON_NULL) +data class Source( + @Id val id: String? = null, + val name: String, + @Field("type") + val checkerType: String, + val defaults: Map = mapOf() +) diff --git a/src/main/kotlin/net/xintanalabs/rssotto/model/Type.kt b/src/main/kotlin/net/xintanalabs/rssotto/model/Type.kt new file mode 100644 index 0000000..253b5e8 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/model/Type.kt @@ -0,0 +1,13 @@ +package net.xintanalabs.rssotto.model + +import com.fasterxml.jackson.annotation.JsonInclude +import org.springframework.data.annotation.Id + +@JsonInclude(JsonInclude.Include.NON_NULL) +data class Type( + @Id val id: String? = null, + val shortName: String, + val name: String, + val configFields: List, + val appFields: List +) \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/model/User.kt b/src/main/kotlin/net/xintanalabs/rssotto/model/User.kt new file mode 100644 index 0000000..42a3bb2 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/model/User.kt @@ -0,0 +1,15 @@ +package net.xintanalabs.rssotto.model + +import java.util.UUID + +data class User( + val id: UUID, + val username: String, + val password: String, + val role: Role +) + + +enum class Role { + USER, ADMIN +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/repository/ArticleRepository.kt b/src/main/kotlin/net/xintanalabs/rssotto/repository/ArticleRepository.kt new file mode 100644 index 0000000..78be7d2 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/repository/ArticleRepository.kt @@ -0,0 +1,16 @@ +package net.xintanalabs.rssotto.repository + +import net.xintanalabs.rssotto.model.Article +import org.springframework.stereotype.Repository +import java.util.UUID + +@Repository +class ArticleRepository { + private val articles = mutableListOf
( + Article(UUID.randomUUID(), "Title 1", "Content 1"), + Article(UUID.randomUUID(), "Title 2", "Content 2"), + Article(UUID.randomUUID(), "Title 3", "Content 3") + ) + + fun findAll(): List
= articles +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/repository/RefreshTokenRepository.kt b/src/main/kotlin/net/xintanalabs/rssotto/repository/RefreshTokenRepository.kt new file mode 100644 index 0000000..dc2d406 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/repository/RefreshTokenRepository.kt @@ -0,0 +1,16 @@ +package net.xintanalabs.rssotto.repository + +import org.springframework.security.core.userdetails.UserDetails +import org.springframework.stereotype.Repository + +@Repository +class RefreshTokenRepository { + private val tokens = mutableMapOf() + + fun findUserDetailsByToken(token: String): UserDetails? = + tokens[token] + + fun save(token: String, userDetails: UserDetails) { + tokens[token] = userDetails + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/repository/SourceRepository.kt b/src/main/kotlin/net/xintanalabs/rssotto/repository/SourceRepository.kt new file mode 100644 index 0000000..6265ac4 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/repository/SourceRepository.kt @@ -0,0 +1,28 @@ +package net.xintanalabs.rssotto.repository + +import net.xintanalabs.rssotto.constants.Constants +import net.xintanalabs.rssotto.db.mongodb.MongoDBAbstract +import net.xintanalabs.rssotto.db.mongodb.MongoDBClient +import net.xintanalabs.rssotto.model.Source +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.stereotype.Repository + +@Repository +class SourceRepository ( + mongoDBClient: MongoDBClient +): MongoDBAbstract(mongoDBClient) { + override val collection: String = Constants.COLLECTION_SOURCES + override val entityClass: Class = Source::class.java + + fun createSource(source: Source): Source { + return create(source) + } + + fun getAllSources(): List { + return getAll() + } + + fun getSourceById(id: String): Source? { + return getById(id) + } +} diff --git a/src/main/kotlin/net/xintanalabs/rssotto/repository/TypeRepository.kt b/src/main/kotlin/net/xintanalabs/rssotto/repository/TypeRepository.kt new file mode 100644 index 0000000..ef8e43d --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/repository/TypeRepository.kt @@ -0,0 +1,25 @@ +package net.xintanalabs.rssotto.repository + +import net.xintanalabs.rssotto.constants.Constants +import net.xintanalabs.rssotto.db.mongodb.MongoDBAbstract +import net.xintanalabs.rssotto.db.mongodb.MongoDBClient +import net.xintanalabs.rssotto.model.Type +import org.springframework.stereotype.Repository + +@Repository +class TypeRepository(mongoDBClient: MongoDBClient): MongoDBAbstract(mongoDBClient) { + override val collection: String = Constants.COLLECTION_TYPES + override val entityClass: Class = Type::class.java + + suspend fun createType(type: Type): Type { + return create(type) + } + + suspend fun getAllTypes(): List { + return getAll() + } + + suspend fun getTypeById(id: String): Type? { + return getById(id) + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/repository/UserRepository.kt b/src/main/kotlin/net/xintanalabs/rssotto/repository/UserRepository.kt new file mode 100644 index 0000000..b99895c --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/repository/UserRepository.kt @@ -0,0 +1,48 @@ +package net.xintanalabs.rssotto.repository + +import net.xintanalabs.rssotto.model.Role +import net.xintanalabs.rssotto.model.User +import org.springframework.security.crypto.password.PasswordEncoder +import org.springframework.stereotype.Repository +import java.util.UUID + +@Repository +class UserRepository( + private val encoder: PasswordEncoder +) { + private val users = mutableListOf( + User( + UUID.randomUUID(), + "admin", + encoder.encode("admin"), + Role.ADMIN), + User( + UUID.randomUUID(), + "user", + encoder.encode("user"), + Role.USER), + User( + UUID.randomUUID(), + "test", + encoder.encode("test"), + Role.USER) + ) + + fun save(user: User): Boolean { + val updated = user.copy(password = encoder.encode(user.password)) + return users.add(updated) + } + + fun findByUsername(username: String): User? = + users.find { it.username == username } + + fun findAll(): List = + users.toList() + + fun findById(id: UUID): User? = + users.find { it.id == id } + + fun deleteById(id: UUID): Boolean = + users.removeIf { it.id == id } + +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/service/ArticleService.kt b/src/main/kotlin/net/xintanalabs/rssotto/service/ArticleService.kt new file mode 100644 index 0000000..a20cb1a --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/service/ArticleService.kt @@ -0,0 +1,14 @@ +package net.xintanalabs.rssotto.service + +import net.xintanalabs.rssotto.model.Article +import net.xintanalabs.rssotto.repository.ArticleRepository +import org.springframework.stereotype.Service + +@Service +class ArticleService( + private val articleRepository: ArticleRepository +) { + + fun findAll(): List
= + articleRepository.findAll() +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/service/AuthenticationService.kt b/src/main/kotlin/net/xintanalabs/rssotto/service/AuthenticationService.kt new file mode 100644 index 0000000..9cb4e8c --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/service/AuthenticationService.kt @@ -0,0 +1,61 @@ +package net.xintanalabs.rssotto.service + +import net.xintanalabs.rssotto.config.JwtProperties +import net.xintanalabs.rssotto.controller.auth.AuthenticationRequest +import net.xintanalabs.rssotto.controller.auth.AuthenticationResponse +import net.xintanalabs.rssotto.repository.RefreshTokenRepository +import org.springframework.security.authentication.AuthenticationManager +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken +import org.springframework.security.core.userdetails.UserDetails +import org.springframework.stereotype.Service +import java.util.Date + +@Service +class AuthenticationService( + private val authManager: AuthenticationManager, + private val userDetailsService: CustomUserDetailsService, + private val tokenService: TokenService, + private val jwtProperties: JwtProperties, + private val refreshTokenRepository: RefreshTokenRepository +) { + fun authenticate(authRequest: AuthenticationRequest): AuthenticationResponse { + authManager.authenticate( + UsernamePasswordAuthenticationToken(authRequest.username, authRequest.password) + ) + + val user = userDetailsService.loadUserByUsername(authRequest.username) + val accessToken = generateAccessToken(user) + val refreshToken = generateRefreshToken(user) + + refreshTokenRepository.save(refreshToken, user) + + return AuthenticationResponse( + accessToken = accessToken, + refreshToken = refreshToken + ) + } + + private fun generateRefreshToken(user: UserDetails): String = tokenService.generate( + user, + Date(System.currentTimeMillis() + jwtProperties.refreshTokenExpiration) + ) + + private fun generateAccessToken(user: UserDetails): String = tokenService.generate( + user, + Date(System.currentTimeMillis() + jwtProperties.accessTokenExpiration) + ) + + fun refreshAccessToken(token: String): String? { + val extractedUsername = tokenService.extractUsername(token) + return extractedUsername?.let { + val currentUserDetails = userDetailsService.loadUserByUsername(it) + val refreshTokenUserDetails = refreshTokenRepository.findUserDetailsByToken(token) + if (!tokenService.isExpired(token) && currentUserDetails.username == refreshTokenUserDetails?.username) { + generateAccessToken(currentUserDetails) + } else + null + + } + } + +} diff --git a/src/main/kotlin/net/xintanalabs/rssotto/service/CustomUserDetailsService.kt b/src/main/kotlin/net/xintanalabs/rssotto/service/CustomUserDetailsService.kt new file mode 100644 index 0000000..ae9eee7 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/service/CustomUserDetailsService.kt @@ -0,0 +1,29 @@ +package net.xintanalabs.rssotto.service + + +import net.xintanalabs.rssotto.repository.UserRepository +import org.springframework.security.core.userdetails.User +import org.springframework.security.core.userdetails.UserDetails +import org.springframework.security.core.userdetails.UserDetailsService +import org.springframework.security.core.userdetails.UsernameNotFoundException +import org.springframework.stereotype.Service + +typealias ApplicationUser = net.xintanalabs.rssotto.model.User + +@Service +class CustomUserDetailsService( + private val userRepository: UserRepository +): UserDetailsService { + + override fun loadUserByUsername(username: String): UserDetails = + userRepository.findByUsername(username) + ?.mapToUserDetails() + ?: throw UsernameNotFoundException("User not found: $username") + + fun ApplicationUser.mapToUserDetails(): UserDetails = + User.builder() + .username(this.username) + .password(this.password) + .roles(this.role.name) + .build() +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/service/SourceService.kt b/src/main/kotlin/net/xintanalabs/rssotto/service/SourceService.kt new file mode 100644 index 0000000..785f02d --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/service/SourceService.kt @@ -0,0 +1,22 @@ +package net.xintanalabs.rssotto.service + +import net.xintanalabs.rssotto.model.Source +import net.xintanalabs.rssotto.repository.SourceRepository +import org.springframework.stereotype.Service + +@Service +class SourceService( + private val sourceRepository: SourceRepository +) { + fun findALl(): List { + return sourceRepository.getAllSources() + } + + fun create(source: Source): Source { + return sourceRepository.createSource(source) + } + + fun findById(id: String): Source? { + return sourceRepository.getSourceById(id) + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/service/TokenService.kt b/src/main/kotlin/net/xintanalabs/rssotto/service/TokenService.kt new file mode 100644 index 0000000..7c011a1 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/service/TokenService.kt @@ -0,0 +1,54 @@ +package net.xintanalabs.rssotto.service + +import io.jsonwebtoken.Claims +import io.jsonwebtoken.Jwts +import io.jsonwebtoken.security.Keys +import net.xintanalabs.rssotto.config.JwtProperties +import org.springframework.security.core.userdetails.UserDetails +import org.springframework.stereotype.Service +import java.util.Date + +@Service +class TokenService( + jwtProperties: JwtProperties +) { + private val secretKey = Keys.hmacShaKeyFor( + jwtProperties.key.toByteArray() + ) + + fun generate( + userDetails: UserDetails, + expirationDate: Date, + additionalClaims: Map = emptyMap() + ): String { + return Jwts.builder() + .claims() + .subject(userDetails.username) + .issuedAt(Date(System.currentTimeMillis())) + .expiration(expirationDate) + .add(additionalClaims) + .and() + .signWith(secretKey) + .compact() + } + + fun extractUsername(token: String): String? { + return getAllClaims(token).subject + } + + fun isExpired(token: String): Boolean { + return getAllClaims(token).expiration.before(Date(System.currentTimeMillis())) + } + + fun isValid(token: String, userDetails: UserDetails): Boolean { + val username = extractUsername(token) + return (username == userDetails.username) && !isExpired(token) + } + + private fun getAllClaims(token: String): Claims { + val parser = Jwts.parser() + .verifyWith(secretKey) + .build() + return parser.parseSignedClaims(token).payload + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/service/UserService.kt b/src/main/kotlin/net/xintanalabs/rssotto/service/UserService.kt new file mode 100644 index 0000000..f3fd46a --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/service/UserService.kt @@ -0,0 +1,26 @@ +package net.xintanalabs.rssotto.service + +import net.xintanalabs.rssotto.model.User +import net.xintanalabs.rssotto.repository.UserRepository +import org.springframework.stereotype.Service +import java.util.UUID + +@Service +class UserService(private val userRepository: UserRepository) { + fun createUser(user: User): User? { + val found = userRepository.findByUsername(user.username) + return if (found == null && userRepository.save(user)) user else null + } + + fun findById(id: UUID): User? = + userRepository.findById(id) + + fun findAll(): List = + userRepository.findAll() + + fun deleteById(id: UUID): Boolean = + userRepository.deleteById(id) + + fun findByUsername(username: String): User? = + userRepository.findByUsername(username) +} \ No newline at end of file diff --git a/src/main/kotlin/net/xintanalabs/rssotto/tasks/ScheduledTasks.kt b/src/main/kotlin/net/xintanalabs/rssotto/tasks/ScheduledTasks.kt new file mode 100644 index 0000000..96fa197 --- /dev/null +++ b/src/main/kotlin/net/xintanalabs/rssotto/tasks/ScheduledTasks.kt @@ -0,0 +1,18 @@ +package net.xintanalabs.rssotto.tasks + +import org.slf4j.LoggerFactory +import org.springframework.scheduling.annotation.Scheduled +import org.springframework.stereotype.Component +import java.text.SimpleDateFormat +import java.util.Date + +@Component +class ScheduledTasks { + private val log = LoggerFactory.getLogger(ScheduledTasks::class.java) + private val dateFormat = SimpleDateFormat("HH:mm:ss") + + @Scheduled(fixedRate = 500000) + fun reportCurrentTime() { + log.info("The time is now {}", dateFormat.format(Date())) + } +} \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml new file mode 100644 index 0000000..5ba63c2 --- /dev/null +++ b/src/main/resources/application.yaml @@ -0,0 +1,31 @@ +server: + port: 8080 + +spring: + application: + name: rssotto + data: + mongodb: + uri: mongodb://root:A%3Ay(nW%3C06Gu%5D*Q8%5DA%40j)@192.168.1.115:27017/ + database: rssotto + +jwt: + key: ${JWT_KEY} + # QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234== + refresh-key: ${JWT_REFRESH_KEY} + # ZXCVBNMASDFGHJKLQWERTYUIOPzxcvbnmasdfghjklqwertyuiop5678== + access-token-expiration: 3600000 # 1 hour + refresh-token-expiration: 604800000 # 7 days + +logging: + level: + org: + springframework: + data: + mongodb: DEBUG + security: DEBUG + +version-checker: + interval-minutes: 5 + + diff --git a/src/test/kotlin/net/xintanalabs/rssotto/RssottoApplicationTests.kt b/src/test/kotlin/net/xintanalabs/rssotto/RssottoApplicationTests.kt new file mode 100644 index 0000000..228c797 --- /dev/null +++ b/src/test/kotlin/net/xintanalabs/rssotto/RssottoApplicationTests.kt @@ -0,0 +1,13 @@ +package net.xintanalabs.rssotto + +import org.junit.jupiter.api.Test +import org.springframework.boot.test.context.SpringBootTest + +@SpringBootTest +class RssottoApplicationTests { + + @Test + fun contextLoads() { + } + +}