From 64427fe27e624587f8654a923efa2181aaaf7585 Mon Sep 17 00:00:00 2001 From: dankito Date: Mon, 10 Apr 2023 19:20:44 +0200 Subject: [PATCH] Added GET endpoint --- .../epcqrcode/rest/EpcQrCodeResource.kt | 20 +++++++++++++------ .../rest/dto/GenerateEpcQrCodeRequestDto.kt | 14 +++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) 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 d9737fb..345d910 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 @@ -5,9 +5,7 @@ import net.codinux.banking.epcqrcode.EpcQrCodeGenerator import net.codinux.banking.epcqrcode.rest.dto.GenerateEpcQrCodeRequestDto import net.codinux.banking.epcqrcode.rest.dto.GenerateEpcQrCodeResponseDto import org.slf4j.LoggerFactory -import javax.ws.rs.POST -import javax.ws.rs.Path -import javax.ws.rs.Produces +import javax.ws.rs.* import javax.ws.rs.core.MediaType import javax.ws.rs.core.Response @@ -23,18 +21,28 @@ class EpcQrCodeResource { protected val epcQrCodeGenerator = EpcQrCodeGenerator() + @GET + @Produces(MediaType.APPLICATION_JSON) + fun createEpcQrCodeGet(@BeanParam request: GenerateEpcQrCodeRequestDto): Response { + return handleJsonRequest(request) + } + @POST @Produces(MediaType.APPLICATION_JSON) - fun createEpcQrCode(requestDto: GenerateEpcQrCodeRequestDto): Response { + fun createEpcQrCodePost(request: GenerateEpcQrCodeRequestDto): Response { + return handleJsonRequest(request) + } + + private fun handleJsonRequest(request: GenerateEpcQrCodeRequestDto): Response { try { - val epcQrCode = epcQrCodeGenerator.generateEpcQrCode(mapToEpcQrCodeConfig(requestDto), requestDto.imageHeightAndWidth) + val epcQrCode = epcQrCodeGenerator.generateEpcQrCode(mapToEpcQrCodeConfig(request), request.imageHeightAndWidth) val qrCodeBytes = epcQrCode.bytes // TODO: convert to Base64 return Response.ok(GenerateEpcQrCodeResponseDto(qrCodeBytes)) .header("Access-Control-Allow-Origin", "*") .build() } catch (e: Exception) { - log.error("Could not create QR code for $requestDto", e) + log.error("Could not create QR code for $request", e) return 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", "*") diff --git a/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/dto/GenerateEpcQrCodeRequestDto.kt b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/dto/GenerateEpcQrCodeRequestDto.kt index 819a42f..10b7197 100644 --- a/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/dto/GenerateEpcQrCodeRequestDto.kt +++ b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/dto/GenerateEpcQrCodeRequestDto.kt @@ -3,26 +3,40 @@ package net.codinux.banking.epcqrcode.rest.dto import net.codinux.banking.epcqrcode.EpcQrCode import net.codinux.banking.epcqrcode.EpcQrCodeCharacterSet import net.codinux.banking.epcqrcode.ImageFormat +import javax.ws.rs.DefaultValue +import javax.ws.rs.QueryParam class GenerateEpcQrCodeRequestDto { + @QueryParam("receiverName") var receiverName: String = "" + @QueryParam("bic") var bic: String? = null + @QueryParam("iban") var iban: String = "" + @QueryParam("amount") var amount: String? = null + @QueryParam("reference") var reference: String? = null + @QueryParam("noteToUser") var noteToUser: String? = null + @DefaultValue("" + EpcQrCode.DefaultHeightAndWidth) + @QueryParam("imageHeightAndWidth") var imageHeightAndWidth: Int = EpcQrCode.DefaultHeightAndWidth + @DefaultValue("PNG") + @QueryParam("imageFormat") var imageFormat: ImageFormat = ImageFormat.PNG + @DefaultValue("UTF_8") + @QueryParam("encoding") var encoding: EpcQrCodeCharacterSet = EpcQrCodeCharacterSet.UTF_8 } \ No newline at end of file