Skip to content

Commit

Permalink
GH-1474 Adapt to the latest snapshot of Javalin 5.x & plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Aug 9, 2022
1 parent ddb8669 commit 0ae83e6
Show file tree
Hide file tree
Showing 31 changed files with 119 additions and 117 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ scmVersion {
commit { version -> "Release $version" }
}

localOnly = true
scmVersion {
checks.isSnapshotDependencies = false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.reposilite

const val VERSION = "3.0.0-SNAPSHOT"
const val VERSION = "3.0.1-SNAPSHOT"
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.reposilite.configuration

import com.reposilite.specification.LocalSpecificationJunitExtension
import com.reposilite.specification.RemoteSpecificationJunitExtension
import com.reposilite.ReposiliteObjectMapper
import com.reposilite.auth.application.AuthenticationSettings
import com.reposilite.auth.application.LdapSettings
import com.reposilite.frontend.application.FrontendSettings
import com.reposilite.maven.application.MavenSettings
import com.reposilite.configuration.shared.SharedConfigurationFacade
import com.reposilite.configuration.specification.SettingsIntegrationSpecification
import com.reposilite.frontend.application.FrontendSettings
import com.reposilite.maven.application.MavenSettings
import com.reposilite.specification.LocalSpecificationJunitExtension
import com.reposilite.specification.RemoteSpecificationJunitExtension
import com.reposilite.statistics.api.ResolvedRequestsInterval.YEARLY
import com.reposilite.statistics.application.StatisticsSettings
import com.reposilite.web.application.WebSettings
import com.reposilite.web.http.ErrorResponse
import io.javalin.http.HttpCode.OK
import io.javalin.http.HttpCode.UNAUTHORIZED
import io.javalin.http.HttpStatus.OK
import io.javalin.http.HttpStatus.UNAUTHORIZED
import kong.unirest.Unirest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

package com.reposilite.maven

import com.reposilite.maven.specification.MavenIntegrationSpecification
import com.reposilite.specification.LocalSpecificationJunitExtension
import com.reposilite.specification.RemoteSpecificationJunitExtension
import com.reposilite.maven.specification.MavenIntegrationSpecification
import com.reposilite.token.RoutePermission.READ
import io.javalin.http.HttpCode.UNAUTHORIZED
import io.javalin.http.HttpStatus.UNAUTHORIZED
import kong.unirest.Unirest.get
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
Expand Down Expand Up @@ -60,7 +60,7 @@ internal abstract class MavenApiIntegrationTest : MavenIntegrationSpecification(
.asString()

// then: service rejects request
assertEquals(UNAUTHORIZED.status, unauthorizedResponse.status)
assertEquals(UNAUTHORIZED.code, unauthorizedResponse.status)

// given: valid credentials
val (name, secret) = useAuth("name", "secret", routes = mapOf("/$artifactPath" to READ))
Expand Down Expand Up @@ -90,7 +90,7 @@ internal abstract class MavenApiIntegrationTest : MavenIntegrationSpecification(
.asString()

// then: service rejects request
assertEquals(UNAUTHORIZED.status, unauthorizedResponse.status)
assertEquals(UNAUTHORIZED.code, unauthorizedResponse.status)

// given: valid credentials
val (name, secret) = useAuth("name", "secret", routes = mapOf(endpoint.replace("/api/maven/details", "") to READ))
Expand Down Expand Up @@ -130,7 +130,7 @@ internal abstract class MavenApiIntegrationTest : MavenIntegrationSpecification(
.asString()

// then: service rejects request
assertEquals(UNAUTHORIZED.status, unauthorizedResponse.status)
assertEquals(UNAUTHORIZED.code, unauthorizedResponse.status)

// given: valid credentials
val (name, secret) = useAuth("name", "secret", routes = mapOf("/private" to READ))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

package com.reposilite.maven

import com.reposilite.configuration.local.LocalConfiguration
import com.reposilite.maven.specification.MavenIntegrationSpecification
import com.reposilite.specification.LocalSpecificationJunitExtension
import com.reposilite.specification.RemoteSpecificationJunitExtension
import com.reposilite.maven.specification.MavenIntegrationSpecification
import com.reposilite.configuration.local.LocalConfiguration
import com.reposilite.storage.api.DocumentInfo
import com.reposilite.web.http.ErrorResponse
import io.javalin.http.HttpCode.NOT_FOUND
import io.javalin.http.HttpCode.UNAUTHORIZED
import io.javalin.http.HttpStatus.NOT_FOUND
import io.javalin.http.HttpStatus.UNAUTHORIZED
import kong.unirest.HeaderNames.CONTENT_LENGTH
import kong.unirest.Unirest.delete
import kong.unirest.Unirest.get
Expand Down Expand Up @@ -86,7 +86,7 @@ internal abstract class MavenIntegrationTest : MavenIntegrationSpecification() {
.asObject(ErrorResponse::class.java)

// then: service should reject the request
assertEquals(UNAUTHORIZED.status, response.status)
assertEquals(UNAUTHORIZED.code, response.status)
}

@Test
Expand Down Expand Up @@ -164,7 +164,7 @@ internal abstract class MavenIntegrationTest : MavenIntegrationSpecification() {
val response = get(address).asString()

// then: service responds with custom 404 page
assertEquals(NOT_FOUND.status, response.status)
assertEquals(NOT_FOUND.code, response.status)
assertTrue(response.body.contains("Reposilite - 404 Not Found"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import com.reposilite.token.Route
import com.reposilite.token.RoutePermission
import com.reposilite.token.api.CreateAccessTokenRequest
import com.reposilite.web.http.ErrorResponse
import io.javalin.http.HttpCode
import io.javalin.http.HttpCode.UNAUTHORIZED
import io.javalin.http.HttpStatus
import io.javalin.http.HttpStatus.UNAUTHORIZED
import kong.unirest.HttpRequest
import kong.unirest.HttpResponse
import kong.unirest.Unirest
Expand Down Expand Up @@ -64,16 +64,16 @@ internal abstract class ReposiliteSpecification : ReposiliteRunner() {
fun <T : Any> HttpRequest<*>.asJacksonObject(type: KClass<T>): HttpResponse<T> =
this.asObject { ReposiliteObjectMapper.DEFAULT_OBJECT_MAPPER.readValue(it.contentAsString, type.java) }

fun assertStatus(expectedCode: HttpCode, value: Int) {
assertEquals(expectedCode.status, value)
fun assertStatus(expectedCode: HttpStatus, value: Int) {
assertEquals(expectedCode.code, value)
}

fun assertErrorResponse(expectedCode: HttpCode, response: HttpResponse<*>) {
fun assertErrorResponse(expectedCode: HttpStatus, response: HttpResponse<*>) {
assertStatus(expectedCode, response.status)
assertFalse(response.isSuccess)
}

fun <T> assertSuccessResponse(expectedCode: HttpCode, response: HttpResponse<T>, block: (T) -> Unit = {}): T {
fun <T> assertSuccessResponse(expectedCode: HttpStatus, response: HttpResponse<T>, block: (T) -> Unit = {}): T {
assertStatus(expectedCode, response.status)
assertTrue(response.isSuccess)
return response.body.also { block(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.reposilite.statistics.api.ResolvedCountResponse
import com.reposilite.statistics.specification.StatisticsIntegrationSpecification
import com.reposilite.token.AccessTokenPermission.MANAGER
import com.reposilite.token.RoutePermission.READ
import io.javalin.http.HttpCode.UNAUTHORIZED
import io.javalin.http.HttpStatus.UNAUTHORIZED
import kong.unirest.Unirest.get
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -57,7 +57,7 @@ internal abstract class StatisticsIntegrationTest : StatisticsIntegrationSpecifi
val unauthorizedResponse = get(endpoint).asString()

// then: service rejects request
assertEquals(UNAUTHORIZED.status, unauthorizedResponse.status)
assertEquals(UNAUTHORIZED.code, unauthorizedResponse.status)

// given: a valid credentials
val (name, secret) = useAuth("name", "secret", listOf(MANAGER))
Expand All @@ -82,7 +82,7 @@ internal abstract class StatisticsIntegrationTest : StatisticsIntegrationSpecifi
val unauthorizedResponse = get(endpoint).asString()

// then: service rejects request
assertEquals(UNAUTHORIZED.status, unauthorizedResponse.status)
assertEquals(UNAUTHORIZED.code, unauthorizedResponse.status)

// given: a valid credentials
val (name, secret) = useAuth("name", "secret", emptyList(), mapOf(identifier.toString() to READ))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import com.reposilite.token.api.CreateAccessTokenResponse
import com.reposilite.token.api.CreateAccessTokenWithNoNameRequest
import com.reposilite.token.specification.AccessTokenIntegrationSpecification
import com.reposilite.web.http.ErrorResponse
import io.javalin.http.HttpCode.OK
import io.javalin.http.HttpCode.UNAUTHORIZED
import io.javalin.http.HttpCode.FORBIDDEN
import io.javalin.http.HttpStatus.FORBIDDEN
import io.javalin.http.HttpStatus.OK
import io.javalin.http.HttpStatus.UNAUTHORIZED
import kong.unirest.Unirest.delete
import kong.unirest.Unirest.get
import kong.unirest.Unirest.put
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.reposilite.token.AccessTokenFacade
import com.reposilite.token.api.AccessTokenDto
import com.reposilite.web.http.ErrorResponse
import com.reposilite.web.http.errorResponse
import io.javalin.http.HttpCode.UNAUTHORIZED
import io.javalin.http.HttpStatus.UNAUTHORIZED
import panda.std.Result
import panda.std.asSuccess

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ import com.reposilite.token.AccessTokenFacade
import com.reposilite.token.api.AccessTokenDto
import com.reposilite.token.api.CreateAccessTokenRequest
import com.reposilite.web.http.ErrorResponse
import com.reposilite.web.http.errorResponse
import com.reposilite.web.http.badRequest
import com.reposilite.web.http.badRequestError
import com.reposilite.web.http.internalServerError
import com.reposilite.web.http.notFoundError
import com.reposilite.web.http.unauthorized
import io.javalin.http.HttpCode.BAD_REQUEST
import io.javalin.http.HttpCode.INTERNAL_SERVER_ERROR
import io.javalin.http.HttpCode.UNAUTHORIZED
import panda.std.Result
import panda.std.asSuccess
import panda.std.reactive.Reference
Expand Down Expand Up @@ -65,7 +64,7 @@ internal class LdapAuthenticator(
userAttribute
)
}
.filter({ it.size == 1 }, { ErrorResponse(BAD_REQUEST, "Could not identify one specific result") }) // only one search result allowed
.filter({ it.size == 1 }, { badRequest("Could not identify one specific result") }) // only one search result allowed
.map { it.first() }
.flatMap { createContext(user = it.first, password = credentials.secret) } // try to authenticate user with matched domain namespace
.flatMap {
Expand All @@ -74,10 +73,10 @@ internal class LdapAuthenticator(
userAttribute
)
}
.filter({ it.size == 1 }, { ErrorResponse(BAD_REQUEST, "Could not identify one specific result as user") }) // only one search result allowed
.filter({ it.size == 1 }, { badRequest("Could not identify one specific result as user") }) // only one search result allowed
.map { it.first() }
.map { (_, attributes) -> attributes[userAttribute]!! } // search returns only lists with values
.filter({ it.size == 1 }, { ErrorResponse(BAD_REQUEST, "Could not identify one specific attribute") }) // only one attribute value is allowed
.filter({ it.size == 1 }, { badRequest("Could not identify one specific attribute") }) // only one attribute value is allowed
.map { it.first() }
.filter(
{ credentials.name == it }, // make sure requested name matches required attribute
Expand Down Expand Up @@ -109,7 +108,7 @@ internal class LdapAuthenticator(
.let { Result.attempt { InitialDirContext(it) } }
.mapErr {
accessTokenFacade.logger.exception(DEBUG, it)
ErrorResponse(UNAUTHORIZED, "Unauthorized LDAP access")
unauthorized("Unauthorized LDAP access")
}

fun search(ldapFilterQuery: String, vararg requestedAttributes: String): Result<List<SearchEntry>, ErrorResponse> =
Expand All @@ -134,10 +133,10 @@ internal class LdapAuthenticator(
notFoundError(nameNotFoundException.toString())
} catch (invalidSearchFilterException: InvalidSearchFilterException) {
failureFacade.throwException("Bad search request in LDAP", invalidSearchFilterException)
errorResponse(BAD_REQUEST, invalidSearchFilterException.toString())
badRequestError(invalidSearchFilterException.toString())
} catch (exception: Exception) {
failureFacade.throwException("Unknown LDAP search exception", exception)
errorResponse(INTERNAL_SERVER_ERROR, exception.toString())
internalServerError(exception.toString())
}

private fun SearchResult.attributesMap(vararg requestedAttributes: String): AttributesMap =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ package com.reposilite.auth.infrastructure
import com.reposilite.web.api.ReposiliteRoute
import com.reposilite.web.api.ReposiliteRoutes
import com.reposilite.web.routing.RouteMethod.AFTER
import io.javalin.http.HttpCode.UNAUTHORIZED
import io.javalin.http.HttpStatus.UNAUTHORIZED

private const val WWW_AUTHENTICATE = "www-authenticate"
private const val WWW_BASIC_REALM = """Basic realm="Reposilite", charset="UTF-8""""

internal class PostAuthHandler : ReposiliteRoutes() {

private val realmDescription = ReposiliteRoute<Unit>("/{repository}/<*>", AFTER) {
if (ctx.status() == UNAUTHORIZED.status && !uri.startsWith("/api")) {
if (ctx.status() == UNAUTHORIZED && !uri.startsWith("/api")) {
ctx.header(WWW_AUTHENTICATE, WWW_BASIC_REALM)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ import com.reposilite.configuration.shared.SharedConfigurationFacade
import com.reposilite.configuration.shared.api.SharedSettings
import com.reposilite.web.api.ReposiliteRoute
import com.reposilite.web.api.ReposiliteRoutes
import com.reposilite.web.http.ErrorResponse
import com.reposilite.web.http.errorResponse
import com.reposilite.web.http.badRequest
import com.reposilite.web.http.badRequestError
import com.reposilite.web.routing.RouteMethod.GET
import com.reposilite.web.routing.RouteMethod.PUT
import io.javalin.http.HttpCode.BAD_REQUEST
import io.javalin.openapi.HttpMethod
import io.javalin.openapi.OpenApi
import io.javalin.openapi.OpenApiContent
Expand Down Expand Up @@ -120,8 +119,8 @@ internal class SettingsEndpoints(private val sharedConfigurationFacade: SharedCo
.takeIf { it.isNotEmpty() }
?.let { sharedConfigurationFacade.getSettingsReference<SharedSettings>(this)?.type }
?.let { sharedConfigurationFacade.updateSharedSettings(this, ctx.bodyAsClass(it.java)) }
?.mapErr { ErrorResponse(BAD_REQUEST, it.toString()) }
?: errorResponse(BAD_REQUEST, "Body is empty")
?.mapErr { badRequest(it.toString()) }
?: badRequestError("Body is empty")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import com.reposilite.journalist.Journalist
import com.reposilite.journalist.Logger
import com.reposilite.plugin.api.Facade
import com.reposilite.web.http.ErrorResponse
import com.reposilite.web.http.errorResponse
import io.javalin.http.HttpCode.BAD_REQUEST
import com.reposilite.web.http.badRequestError
import panda.std.Result
import panda.std.asSuccess
import panda.utilities.StringUtils
Expand All @@ -38,11 +37,11 @@ class ConsoleFacade internal constructor(

fun executeCommand(command: String): Result<ExecutionResponse, ErrorResponse> {
if (StringUtils.isEmpty(command)) {
return errorResponse(BAD_REQUEST, "Missing command")
return badRequestError("Missing command")
}

if (command.length > MAX_COMMAND_LENGTH) {
return errorResponse(BAD_REQUEST, "The given command exceeds allowed length (${command.length} > $MAX_COMMAND_LENGTH)")
return badRequestError("The given command exceeds allowed length (${command.length} > $MAX_COMMAND_LENGTH)")
}

return commandExecutor.execute(command).asSuccess()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.reposilite.frontend.FrontendFacade
import io.javalin.http.Context
import io.javalin.http.ExceptionHandler
import io.javalin.http.Handler
import io.javalin.http.HttpCode.NOT_FOUND
import io.javalin.http.HttpStatus.NOT_FOUND
import io.javalin.http.NotFoundResponse

internal class NotFoundHandler(private val frontendFacade: FrontendFacade) : Handler, ExceptionHandler<NotFoundResponse> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ import com.reposilite.storage.api.FileType.DIRECTORY
import com.reposilite.storage.api.Location
import com.reposilite.token.AccessTokenIdentifier
import com.reposilite.web.http.ErrorResponse
import com.reposilite.web.http.errorResponse
import com.reposilite.web.http.badRequestError
import com.reposilite.web.http.notFound
import com.reposilite.web.http.notFoundError
import com.reposilite.web.http.unauthorizedError
import io.javalin.http.HttpCode.BAD_REQUEST
import io.javalin.http.HttpCode.NOT_FOUND
import panda.std.Result
import panda.std.asSuccess
import java.io.InputStream
Expand Down Expand Up @@ -67,7 +65,7 @@ internal class MavenService(
val (repository, path) = deployRequest

if (repository.redeployment.not() && !path.getSimpleName().contains(METADATA_FILE) && repository.exists(path)) {
return errorResponse(BAD_REQUEST, "Redeployment is not allowed")
return badRequestError("Redeployment is not allowed")
}

return repository.putFile(path, deployRequest.content).peek {
Expand All @@ -90,7 +88,7 @@ internal class MavenService(

private fun findFile(accessToken: AccessTokenIdentifier?, repository: Repository, gav: Location): Result<Pair<DocumentInfo, InputStream>, ErrorResponse> =
findDetails(accessToken, repository, gav)
.`is`(DocumentInfo::class.java) { ErrorResponse(NOT_FOUND, "Requested file is a directory") }
.`is`(DocumentInfo::class.java) { notFound("Requested file is a directory") }
.flatMap { details -> findInputStream(repository, gav).map { details to it } }
.let { extensions.emitEvent(ResolvedFileEvent(accessToken, repository, gav, it)).result }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import com.reposilite.maven.api.VersionsResponse
import com.reposilite.storage.VersionComparator
import com.reposilite.storage.api.Location
import com.reposilite.web.http.ErrorResponse
import com.reposilite.web.http.internalServer
import com.reposilite.web.http.notFound
import com.reposilite.web.http.unauthorizedError
import io.javalin.http.HttpCode.INTERNAL_SERVER_ERROR
import panda.std.Result
import panda.std.letIf
import panda.std.mapToUnit
Expand All @@ -59,7 +59,7 @@ internal class MetadataService(private val repositorySecurityProvider: Repositor
fun saveMetadata(saveMetadataRequest: SaveMetadataRequest): Result<Metadata, ErrorResponse> =
with (saveMetadataRequest) {
Result.attempt { xml.writeValueAsBytes(metadata) }
.mapErr { ErrorResponse(INTERNAL_SERVER_ERROR, "Cannot parse metadata file") }
.mapErr { internalServer("Cannot parse metadata file") }
.flatMap { repository.putFile(gav.resolveMetadataFile(), it.inputStream()).map { _ -> it } }
.flatMap { repository.writeFileChecksums(gav.resolveMetadataFile(), it) }
.map { metadata }
Expand Down
Loading

0 comments on commit 0ae83e6

Please sign in to comment.