Implemented HTML endpoint that shows generated EPC QR Code in a simple web site
This commit is contained in:
parent
b0f118cfad
commit
a167e8eeac
|
@ -3,6 +3,7 @@ package net.codinux.banking.epcqrcode.rest
|
|||
import net.codinux.banking.epcqrcode.rest.dto.GenerateEpcQrCodeRequest
|
||||
import net.codinux.banking.epcqrcode.rest.dto.GenerateEpcQrCodeResponse
|
||||
import net.codinux.banking.epcqrcode.rest.service.EpcQrCodeService
|
||||
import net.codinux.banking.epcqrcode.rest.service.HtmlService
|
||||
import org.eclipse.microprofile.openapi.annotations.Operation
|
||||
import org.eclipse.microprofile.openapi.annotations.media.Content
|
||||
import org.eclipse.microprofile.openapi.annotations.media.Schema
|
||||
|
@ -21,8 +22,8 @@ import javax.ws.rs.core.Response
|
|||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Tag(name = "EPC QR Code")
|
||||
class EpcQrCodeResource(
|
||||
@Inject
|
||||
private val service: EpcQrCodeService
|
||||
@Inject private val service: EpcQrCodeService,
|
||||
@Inject private val htmlService: HtmlService
|
||||
) {
|
||||
|
||||
private val log = LoggerFactory.getLogger(EpcQrCodeResource::class.java)
|
||||
|
@ -33,7 +34,7 @@ class EpcQrCodeResource(
|
|||
content = [ Content(mediaType = "application/json", schema = Schema(implementation = GenerateEpcQrCodeResponse::class)) ])
|
||||
@APIResponse(responseCode = "500", description = "An internal error occurred")
|
||||
@GET
|
||||
fun createEpcQrCodeGet(@BeanParam request: GenerateEpcQrCodeRequest): Response {
|
||||
fun createEpcQrCodeJsonGet(@BeanParam request: GenerateEpcQrCodeRequest): Response {
|
||||
return handleJsonRequest(request)
|
||||
}
|
||||
|
||||
|
@ -43,13 +44,34 @@ class EpcQrCodeResource(
|
|||
@APIResponse(responseCode = "500", description = "An internal error occurred")
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
fun createEpcQrCodePost(
|
||||
fun createEpcQrCodeJsonPost(
|
||||
@RequestBody(content = [ Content(schema = Schema(implementation = GenerateEpcQrCodeRequest::class)) ])
|
||||
request: GenerateEpcQrCodeRequest
|
||||
): Response {
|
||||
return handleJsonRequest(request)
|
||||
}
|
||||
|
||||
@Operation(summary = "Creates a EPC QR code (also marketed as GiroCode, scan2Code, ...)")
|
||||
@APIResponse(responseCode = "200", description = "The generated EPC QR Code")
|
||||
@APIResponse(responseCode = "500", description = "An internal error occurred")
|
||||
@GET
|
||||
@Produces(MediaType.TEXT_HTML)
|
||||
fun createEpcQrCodeHtml(@BeanParam request: GenerateEpcQrCodeRequest): Response {
|
||||
return try {
|
||||
val qrCodeBytes = service.generateEpcQrCodeBase64EncodedString(request)
|
||||
|
||||
Response.ok(htmlService.createHtmlPage(request.imageHeightAndWidth, qrCodeBytes))
|
||||
.header("Access-Control-Allow-Origin", "*")
|
||||
.build()
|
||||
} catch (e: Exception) {
|
||||
log.error("Could not create QR code for $request", e)
|
||||
|
||||
Response.status(Response.Status.INTERNAL_SERVER_ERROR.statusCode, e.localizedMessage) // TODO: it's almost always a bad idea to pass exception message to the outside
|
||||
.header("Access-Control-Allow-Origin", "*")
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleJsonRequest(request: GenerateEpcQrCodeRequest): Response {
|
||||
return try {
|
||||
val qrCodeBytes = service.generateEpcQrCodeBase64Encoded(request)
|
||||
|
|
|
@ -20,6 +20,12 @@ class EpcQrCodeService {
|
|||
return base64Encoder.encode(epcQrCode.bytes)
|
||||
}
|
||||
|
||||
fun generateEpcQrCodeBase64EncodedString(request: GenerateEpcQrCodeRequest): String {
|
||||
val epcQrCode = generateEpcQrCode(request)
|
||||
|
||||
return base64Encoder.encodeToString(epcQrCode.bytes)
|
||||
}
|
||||
|
||||
private fun generateEpcQrCode(request: GenerateEpcQrCodeRequest): EpcQrCode {
|
||||
return epcQrCodeGenerator.generateEpcQrCode(mapToEpcQrCodeConfig(request))
|
||||
}
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package net.codinux.banking.epcqrcode.rest.service
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped
|
||||
|
||||
@ApplicationScoped
|
||||
class HtmlService {
|
||||
|
||||
fun createHtmlPage(heightAndWidth: Int, qrCodeBase64Encoded: String): String {
|
||||
return StringBuilder()
|
||||
.appendLine("<!DOCTYPE html>")
|
||||
.appendLine("<html>")
|
||||
.appendLine("<head>")
|
||||
.appendLine("<meta charset=\"UTF-8\">")
|
||||
.appendLine("<title>EPC QR code (also marketed as GiroCode, scan2Code, ...)</title>")
|
||||
.appendLine("</head>")
|
||||
.appendLine("<body>")
|
||||
.appendLine("<div style=\"height: 100vh; width: 100%; display: flex; justify-content: center; align-items: center;\">")
|
||||
.appendLine("<img width=\"$heightAndWidth\" height=\"$heightAndWidth\" alt=\"EPC QR code (also marketed as GiroCode, scan2Code, ...)\" " +
|
||||
"src=\"data:image/png;base64,$qrCodeBase64Encoded\" />")
|
||||
.appendLine("</div>")
|
||||
.appendLine("</body>")
|
||||
.appendLine("</html>")
|
||||
.toString()
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue