Skip to content

Commit

Permalink
barcode lib 2.7, update swagger doc
Browse files Browse the repository at this point in the history
  • Loading branch information
barnhill committed Jun 30, 2024
1 parent e444401 commit d105f84
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 33 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
#libs
barcode = "2.6.4"
barcode = "2.7.0"
openapi-ui = "2.5.0"

#plugins
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@OpenAPIDefinition(servers = {
@Server(url = "https://barcode.pnuema.com/", description = "Remote"),
@Server(url = "https://barcodeapi.link/", description = "Remote"),
@Server(url = "http://localhost:8443/", description = "Local")
@Server(url = "http://localhost:8080/", description = "Local")
})
@SpringBootApplication
@EnableCaching
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/com/pnuema/java/barcode/barcodeapi/BarcodeBody.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,75 @@
package com.pnuema.java.barcode.barcodeapi;

import com.pnuema.java.barcode.EncodingType;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.Optional;

@SuppressWarnings("unused")
@Schema(name = "BarcodeBody", requiredMode = Schema.RequiredMode.REQUIRED)
public class BarcodeBody {
@Schema(
description = "Symbology type",
type = "String",
example = "upca",
implementation = EncodingType.class,
requiredMode = Schema.RequiredMode.REQUIRED
)
private String type;

@Schema(
description = "Barcode data to be encoded",
type = "string",
example = "123456789012",
requiredMode = Schema.RequiredMode.REQUIRED
)
private String data;

@Schema(
example = "png",
allowableValues = { "png", "jpg", "gif" },
defaultValue = "png",
requiredMode = Schema.RequiredMode.NOT_REQUIRED
)
private String imageFormat;

@Schema(
description = "Desired image width",
type = "int",
example = "400",
requiredMode = Schema.RequiredMode.NOT_REQUIRED
)
private Integer w;

@Schema(
description = "Desired image height",
type = "int",
example = "200",
requiredMode = Schema.RequiredMode.NOT_REQUIRED
)
private Integer h;

@Schema(
allowableValues = { "false", "true" },
defaultValue = "false",
requiredMode = Schema.RequiredMode.NOT_REQUIRED
)
private boolean label;

@Schema(
description = "Hex color code for bars",
type = "string",
example = "000000",
requiredMode = Schema.RequiredMode.NOT_REQUIRED
)
private String barcolor;

@Schema(
description = "Hex color code for spaces and background",
type = "string",
example = "ffffff",
requiredMode = Schema.RequiredMode.NOT_REQUIRED
)
private String background;

public String getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("/v1")
public abstract class AbstractV1Resource {
}
public abstract class AbstractV1Resource {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.pnuema.java.barcode.Barcode;
import com.pnuema.java.barcode.EncodingType;
import com.pnuema.java.barcode.barcodeapi.BarcodeBody;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand All @@ -22,36 +23,12 @@
@RestController
public class BarcodeController extends AbstractV1Resource {

@GetMapping(value = "/barcode/{type}/data/{data}")
@Cacheable("barcodes")
public ResponseEntity<byte[]> getBarcodeImage(
@PathVariable(name = "type") String type,
@PathVariable(name = "data") String data,
@RequestParam(name = "imageFormat") Optional<String> imageFormat,
@RequestParam(name = "w") Optional<Integer> width,
@RequestParam(name = "h") Optional<Integer> height,
@RequestParam(name = "label") Optional<Boolean> includeLabel,
@RequestParam(name = "barcolor") Optional<String> barColor,
@RequestParam(name = "background") Optional<String> background) throws IOException {

return generateBarcode(
type,
data,
imageFormat,
width,
height,
includeLabel,
barColor,
background
);
}

@PostMapping(value = "/barcode/",
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
consumes = { MediaType.APPLICATION_JSON_VALUE },
produces = { MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_GIF_VALUE }
)
@Cacheable("barcodes")
public ResponseEntity<byte[]> getBarcodeImage(@RequestBody BarcodeBody body) throws IOException {

return generateBarcode(
body.getType(),
body.getData(),
Expand All @@ -64,6 +41,58 @@ public ResponseEntity<byte[]> getBarcodeImage(@RequestBody BarcodeBody body) thr
);
}

@GetMapping(
name = "barcode",
value = "/barcode/{type}/data/{data}",
produces = { MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_GIF_VALUE }
)
@Cacheable("barcodes")
public ResponseEntity<byte[]> getBarcodeImage(
@Schema(example = "upca", implementation = EncodingType.class, defaultValue = "upca")
@PathVariable(name = "type")
String type,

@Schema(example = "123456789012")
@PathVariable(name = "data")
String data,

@Schema(example = "png", allowableValues = { "png", "jpg", "gif" }, defaultValue = "png")
@RequestParam(name = "imageFormat")
Optional<String> imageFormat,

@Schema(example = "400")
@RequestParam(name = "w")
Optional<Integer> width,

@Schema(example = "200")
@RequestParam(name = "h")
Optional<Integer> height,

@Schema(allowableValues = { "false", "true" }, defaultValue = "false")
@RequestParam(name = "label", defaultValue = "false")
Optional<Boolean> includeLabel,

@Schema(example = "000000")
@RequestParam(name = "barcolor")
Optional<String> barColor,

@Schema(example = "ffffff")
@RequestParam(name = "background")
Optional<String> background

) throws IOException {
return generateBarcode(
type,
data,
imageFormat,
width,
height,
includeLabel,
barColor,
background
);
}

private ResponseEntity<byte[]> generateBarcode(
String type,
String data,
Expand All @@ -72,7 +101,8 @@ private ResponseEntity<byte[]> generateBarcode(
Optional<Integer> height,
Optional<Boolean> includeLabel,
Optional<String> barColor,
Optional<String> background) throws IOException {
Optional<String> background
) throws IOException {

Barcode barcode = new Barcode();

Expand Down Expand Up @@ -119,7 +149,11 @@ private ResponseEntity<byte[]> generateBarcode(
//attach debug info to header
responseHeaders.set("x-barcode-version", barcode.getTitle() + " " + barcode.getVersion());
responseHeaders.set("x-raw-value", barcode.getRawData());
responseHeaders.set("x-label-font", barcode.getLabelFont().getName());

if (barcode.isIncludeLabel()) {
responseHeaders.set("x-label-font", barcode.getLabelFont().getName());
}

responseHeaders.set("x-served-by", getMachineName());

if (exception != null || image == null) {
Expand Down

0 comments on commit d105f84

Please sign in to comment.