-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make it possible to return data from /details as CSV #284
- Loading branch information
1 parent
a5cd5a4
commit b0721c4
Showing
18 changed files
with
595 additions
and
228 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
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
34 changes: 34 additions & 0 deletions
34
lapis2/src/main/kotlin/org/genspectrum/lapis/controller/CsvWriter.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,34 @@ | ||
package org.genspectrum.lapis.controller | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import org.apache.commons.csv.CSVFormat | ||
import org.apache.commons.csv.CSVPrinter | ||
import org.genspectrum.lapis.silo.DetailsData | ||
import org.springframework.stereotype.Component | ||
import java.io.StringWriter | ||
|
||
interface CsvRecord { | ||
fun asArray(): Array<String> | ||
} | ||
|
||
@Component | ||
class CsvWriter { | ||
fun write(headers: Array<String>, data: List<CsvRecord>): String { | ||
val stringWriter = StringWriter() | ||
CSVPrinter( | ||
stringWriter, | ||
CSVFormat.DEFAULT.builder().setRecordSeparator("\n").setHeader(*headers).build(), | ||
).use { | ||
for (datum in data) { | ||
it.printRecord(*datum.asArray()) | ||
} | ||
} | ||
return stringWriter.toString().trim() | ||
} | ||
} | ||
|
||
fun DetailsData.asCsvRecord() = JsonValuesCsvRecord(this.values) | ||
|
||
data class JsonValuesCsvRecord(val values: Collection<JsonNode>) : CsvRecord { | ||
override fun asArray() = values.map { it.asText() }.toTypedArray() | ||
} |
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
64 changes: 64 additions & 0 deletions
64
lapis2/src/main/kotlin/org/genspectrum/lapis/controller/FormatParameterFilter.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,64 @@ | ||
package org.genspectrum.lapis.controller | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import jakarta.servlet.FilterChain | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletRequestWrapper | ||
import jakarta.servlet.http.HttpServletResponse | ||
import mu.KotlinLogging | ||
import org.genspectrum.lapis.util.CachedBodyHttpServletRequest | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.filter.OncePerRequestFilter | ||
import java.util.Collections | ||
import java.util.Enumeration | ||
|
||
private val log = KotlinLogging.logger {} | ||
|
||
@Component | ||
class FormatParameterFilter(val objectMapper: ObjectMapper) : OncePerRequestFilter() { | ||
|
||
override fun doFilterInternal( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
filterChain: FilterChain, | ||
) { | ||
val reReadableRequest = CachedBodyHttpServletRequest(request, objectMapper) | ||
|
||
filterChain.doFilter(AcceptHeaderModifyingRequestWrapper(reReadableRequest), response) | ||
} | ||
} | ||
|
||
class AcceptHeaderModifyingRequestWrapper( | ||
private val reReadableRequest: CachedBodyHttpServletRequest, | ||
) : HttpServletRequestWrapper(reReadableRequest) { | ||
|
||
override fun getHeader(name: String): String? { | ||
if (name.equals("Accept", ignoreCase = true)) { | ||
when (reReadableRequest.getRequestFields()[FORMAT_PROPERTY]?.textValue()?.uppercase()) { | ||
"CSV" -> { | ||
log.debug { "Overwriting Accept header to text/csv due to format property" } | ||
return "text/csv" | ||
} | ||
|
||
else -> {} | ||
} | ||
} | ||
|
||
return super.getHeader(name) | ||
} | ||
|
||
override fun getHeaders(name: String): Enumeration<String>? { | ||
if (name.equals("Accept", ignoreCase = true)) { | ||
when (reReadableRequest.getRequestFields()[FORMAT_PROPERTY]?.textValue()?.uppercase()) { | ||
"CSV" -> { | ||
log.debug { "Overwriting Accept header to text/csv due to format property" } | ||
return Collections.enumeration(listOf("text/csv")) | ||
} | ||
|
||
else -> {} | ||
} | ||
} | ||
|
||
return super.getHeaders(name) | ||
} | ||
} |
Oops, something went wrong.