diff --git a/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/EpcQrCodeResource.kt b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/EpcQrCodeResource.kt index 47dcfdb..5103fc3 100644 --- a/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/EpcQrCodeResource.kt +++ b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/EpcQrCodeResource.kt @@ -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) diff --git a/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/service/EpcQrCodeService.kt b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/service/EpcQrCodeService.kt index fd6f613..346db7a 100644 --- a/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/service/EpcQrCodeService.kt +++ b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/service/EpcQrCodeService.kt @@ -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)) } diff --git a/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/service/HtmlService.kt b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/service/HtmlService.kt new file mode 100644 index 0000000..1e2e97c --- /dev/null +++ b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/service/HtmlService.kt @@ -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("") + .appendLine("") + .appendLine("") + .appendLine("") + .appendLine("EPC QR code (also marketed as GiroCode, scan2Code, ...)") + .appendLine("") + .appendLine("") + .appendLine("
") + .appendLine("\"EPC") + .appendLine("
") + .appendLine("") + .appendLine("") + .toString() + } + +} \ No newline at end of file