-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathJavadocFacade.kt
135 lines (120 loc) · 5.67 KB
/
JavadocFacade.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (c) 2023 dzikoysk
*
* 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
*
* http://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.
*/
package com.reposilite.javadocs
import com.reposilite.javadocs.api.JavadocPageRequest
import com.reposilite.javadocs.api.JavadocRawRequest
import com.reposilite.javadocs.api.JavadocRawResponse
import com.reposilite.javadocs.api.JavadocResponse
import com.reposilite.journalist.Journalist
import com.reposilite.journalist.Logger
import com.reposilite.maven.MavenFacade
import com.reposilite.maven.Repository
import com.reposilite.maven.api.VersionLookupRequest
import com.reposilite.plugin.api.Facade
import com.reposilite.shared.ErrorResponse
import com.reposilite.shared.notFound
import com.reposilite.shared.notFoundError
import com.reposilite.storage.api.Location
import com.reposilite.token.AccessTokenIdentifier
import io.javalin.http.ContentType
import java.nio.file.Files
import java.nio.file.Path
import panda.std.Result
import panda.std.Result.supplyThrowing
import panda.std.asSuccess
import panda.utilities.StringUtils
private const val LATEST_PATTERN = "/latest"
class JavadocFacade internal constructor(
private val journalist: Journalist,
val mavenFacade: MavenFacade,
private val javadocFolder: Path,
private val javadocContainerService: JavadocContainerService
) : Journalist, Facade {
private val supportedExtensions = mapOf(
"html" to ContentType.TEXT_HTML,
"css" to ContentType.TEXT_CSS,
"js" to ContentType.TEXT_JS,
"json" to ContentType.TEXT_JS,
"png" to ContentType.IMAGE_PNG,
"svg" to ContentType.IMAGE_SVG,
)
private data class JavadocPlainFile(
val targetPath: Path,
val extension: String,
val contentType: ContentType
)
fun findJavadocPage(request: JavadocPageRequest): Result<JavadocResponse, ErrorResponse> =
with (request) {
mavenFacade.canAccessResource(accessToken, repository, gav)
.flatMap { createPage(accessToken, repository, resolveGav(request)) }
.onError { logger.error("Cannot extract javadoc: ${it.message} (${it.status})}") }
}
fun findRawJavadocResource(request: JavadocRawRequest): Result<JavadocRawResponse, ErrorResponse> =
with (request) {
mavenFacade.canAccessResource(accessToken, repository, gav)
.flatMap { javadocContainerService.loadContainer(accessToken, repository, gav) }
.filter({ Files.exists(it.javadocUnpackPath.resolve(resource.toString())) }, { notFound("Resource $resource not found") })
.map {
JavadocRawResponse(
contentType = supportedExtensions[resource.getExtension()] ?: ContentType.APPLICATION_OCTET_STREAM,
content = Files.newInputStream(it.javadocUnpackPath.resolve(resource.toString()))
)
}
}
private fun createPage(accessToken: AccessTokenIdentifier?, repository: Repository, gav: Location): Result<JavadocResponse, ErrorResponse> {
val resourcesFile = createPlainFile(javadocFolder, repository, gav)
return when {
/* File not found */
resourcesFile != null && !Files.exists(resourcesFile.targetPath) ->
JavadocResponse(resourcesFile.contentType.mimeType, StringUtils.EMPTY).asSuccess()
/* File exists */
resourcesFile != null ->
supplyThrowing {
JavadocResponse(resourcesFile.contentType.mimeType, readFile(resourcesFile.targetPath))
}.mapErr {
notFound("Resource not found!")
}
/* Premature resources request */
gav.contains("/resources/") ->
notFoundError("Resources are unavailable before extraction")
/* Load resource */
else ->
javadocContainerService
.loadContainer(accessToken, repository, gav)
.map { JavadocResponse(ContentType.HTML, readFile(it.javadocContainerIndex)) }
}
}
private fun createPlainFile(javadocFolder: Path, repository: Repository, gav: Location): JavadocPlainFile? =
javadocFolder
.resolve(repository.name)
.resolve(gav.toString())
.let { targetPath -> targetPath to supportedExtensions[gav.getExtension()] }
.takeIf { (_, contentType) -> contentType != null }
?.let { (targetPath, contentType) -> JavadocPlainFile(targetPath, gav.getExtension(), contentType!!) }
private fun readFile(indexFile: Path): String =
Files.readAllLines(indexFile).joinToString(separator = "\n")
private fun resolveGav(request: JavadocPageRequest): Location =
request.gav
.takeIf { it.contains("/latest") }
?.let { request.gav.locationBeforeLast("/latest") }
?.let { gavWithoutVersion -> VersionLookupRequest(request.accessToken, request.repository, gavWithoutVersion) }
?.let { mavenFacade.findLatestVersion(it) }
?.map { request.gav.replace(LATEST_PATTERN, "/%s".format(it.version)) }
?.orNull()
?: request.gav
override fun getLogger(): Logger =
journalist.logger
}