-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check IANA media types in the Rule 172
* Add a build task to generate media types configuration from IANA list * Extend default rules configuration to support the generated configuration. * Add a step to the release process about generating the configuration.
- Loading branch information
Showing
8 changed files
with
2,090 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import org.gradle.kotlin.dsl.`kotlin-dsl` | ||
|
||
plugins { | ||
`kotlin-dsl` | ||
} | ||
|
||
dependencies { | ||
implementation(platform("com.fasterxml.jackson:jackson-bom:2.12.2")) | ||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin") | ||
implementation("com.fasterxml.jackson.core:jackson-annotations") | ||
implementation("com.fasterxml.jackson.core:jackson-core") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} |
74 changes: 74 additions & 0 deletions
74
server/buildSrc/src/main/kotlin/org/zalando/zally/task/MediaTypesConfigurationTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.zalando.zally.task | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies | ||
import com.fasterxml.jackson.databind.SerializationFeature | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.provider.ListProperty | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.OutputFile | ||
import org.gradle.api.tasks.TaskAction | ||
import java.io.StringReader | ||
import java.net.URI | ||
import java.net.http.HttpClient | ||
import java.net.http.HttpRequest | ||
import java.net.http.HttpResponse | ||
|
||
abstract class MediaTypesConfigurationTask : DefaultTask() { | ||
|
||
private val host = "https://www.iana.org/assignments/media-types" | ||
private val jsonMapper = ObjectMapper() | ||
.enable(SerializationFeature.INDENT_OUTPUT) | ||
.setPropertyNamingStrategy(PropertyNamingStrategies.UPPER_CAMEL_CASE) | ||
|
||
|
||
@get:Input | ||
val mediaTypes: ListProperty<String> = project.objects.listProperty(String::class.java) | ||
|
||
@get:OutputFile | ||
val outputFile: RegularFileProperty = project.objects.fileProperty() | ||
|
||
|
||
@TaskAction | ||
fun generate() { | ||
val httpClient = HttpClient.newHttpClient() | ||
|
||
logger.info("Generate media types configuration from IANA media types list ($host)") | ||
val resultMediaTypes = ArrayList<String>() | ||
|
||
for (mediaType in mediaTypes.get()) { | ||
logger.debug("Download: ${host}/$mediaType.csv") | ||
val request = HttpRequest.newBuilder(URI.create("${host}/$mediaType.csv")).GET().build() | ||
val response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body() | ||
|
||
var counter = 0; | ||
StringReader(response).forEachLine { | ||
if (!isHeader(it)) { | ||
try { | ||
val registeredMediaType = it.split(",")[1].trim() | ||
if (registeredMediaType.isNotBlank()) { | ||
resultMediaTypes.add(registeredMediaType) | ||
counter++ | ||
} | ||
} catch (e: Exception) { | ||
logger.error("Failed to get media type from $it", e) | ||
} | ||
|
||
} | ||
} | ||
logger.info("Downloaded $counter media types from ${host}/$mediaType.csv") | ||
} | ||
|
||
|
||
jsonMapper.writeValue(outputFile.get().asFile, Configuration(MediaTypesRuleConfiguration(resultMediaTypes))) | ||
} | ||
|
||
private fun isHeader(str: String) = str == "Name,Template,Reference" | ||
} | ||
|
||
class MediaTypesRuleConfiguration(@get:JsonProperty("standard_media_types") val standardMediaTypes: List<String>) | ||
|
||
class Configuration(@get:JsonProperty("MediaTypesRule") val mediaTypesRuleConfiguration: MediaTypesRuleConfiguration) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.