From 404bf98e50216e2de1054a94957569dd7e47320a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20Sta=CC=88rk?= Date: Sat, 18 Apr 2020 11:45:44 +0200 Subject: [PATCH] updates to make it compileable again --- .../org/mustangproject/commandline/Main.java | 2 +- .../main/java/mustang/MustangResource.java | 110 +++++++++++------- 2 files changed, 66 insertions(+), 46 deletions(-) diff --git a/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java b/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java index 50b85129..d5e7ad06 100755 --- a/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java +++ b/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java @@ -35,7 +35,7 @@ import org.mustangproject.ZUGFeRD.ZUGFeRDExporter; import org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromA1Factory; import org.mustangproject.ZUGFeRD.ZUGFeRDImporter; import org.mustangproject.ZUGFeRD.ZUGFeRDMigrator; -import ZUV.ZUGFeRDValidator; +import org.mustangproject.library.extended.ZUGFeRDValidator; /*** * This is the command line interface to mustangproject diff --git a/Mustang-Server/src/main/java/mustang/MustangResource.java b/Mustang-Server/src/main/java/mustang/MustangResource.java index 73f970ab..d1eb218e 100644 --- a/Mustang-Server/src/main/java/mustang/MustangResource.java +++ b/Mustang-Server/src/main/java/mustang/MustangResource.java @@ -7,6 +7,8 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.File; +import java.nio.file.*; import javax.annotation.security.RolesAllowed; import javax.ws.rs.Consumes; @@ -23,6 +25,8 @@ import org.mustangproject.ZUGFeRD.ZUGFeRDConformanceLevel; import org.mustangproject.ZUGFeRD.ZUGFeRDExporter; import org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromA1Factory; import org.mustangproject.ZUGFeRD.ZUGFeRDImporter; + +import org.mustangproject.library.extended.ZUGFeRDValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,15 +45,9 @@ import io.swagger.annotations.SwaggerDefinition; @Path("/mustang") @Produces(MediaType.APPLICATION_JSON) @Api(value = "Mustang") -@SwaggerDefinition(info=@Info( - description = "Rest API to read/write hybrid (ZUGFeRD/Factur-X) e-invoices with a Mustang Server", - version = "V0.0.2", - title = "Mustangproject API", - license = @License(name = "Apache 2.0", url = "http://www.apache.org") +@SwaggerDefinition(info = @Info(description = "Rest API to read/write hybrid (ZUGFeRD/Factur-X) e-invoices with a Mustang Server", version = "V0.0.2", title = "Mustangproject API", license = @License(name = "Apache 2.0", url = "http://www.apache.org") - ), - schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS} - ) +), schemes = { SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS }) public class MustangResource { Logger logger = LoggerFactory.getLogger(MustangResource.class.getName()); @@ -59,88 +57,110 @@ public class MustangResource { @POST @Path("/extract") - //@RolesAllowed("ADMIN") + // @RolesAllowed("ADMIN") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_XML) - @ApiOperation(value="Extracts XML from a zf/fx PDF",notes="Input PDF must be ZUGFeRD or Factur-X") - public String extractFile(@ApiParam(required=true,value="Input ZUGFeRD/Factur-X file") @FormDataParam("file") final InputStream fileInputStream, + @ApiOperation(value = "Extracts XML from a zf/fx PDF", notes = "Input PDF must be ZUGFeRD or Factur-X") + public String extractFile( + @ApiParam(required = true, value = "Input ZUGFeRD/Factur-X file") @FormDataParam("file") final InputStream fileInputStream, @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) { ZUGFeRDImporter zi; logger.debug("Reading..."); zi = new ZUGFeRDImporter(fileInputStream); - return ""+zi.getUTF8()+""; + return "" + zi.getUTF8() + ""; } + @POST + @Path("/validateFX") + // @RolesAllowed("ADMIN") + @Consumes(MediaType.MULTIPART_FORM_DATA) + @Produces(MediaType.APPLICATION_XML) + @ApiOperation(value = "Validates a ZUGFeRD PDF file", notes = "Input PDF must be ZUGFeRD or Factur-X") + public String validateFX( + @ApiParam(required = true, value = "Input ZUGFeRD/Factur-X file") @FormDataParam("file") final InputStream fileInputStream, + @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) { + ZUGFeRDValidator zfv = new ZUGFeRDValidator(); + String validationResult = ""; + try { + + final File tempFile = File.createTempFile("tovalidate", ".pdf"); + tempFile.deleteOnExit(); + + Files.copy(fileInputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + validationResult = zfv.validate(tempFile.getAbsolutePath()); + } catch (Exception ex) { + logger.error(ex.getMessage(),ex); + } + + logger.debug("Validating..."); + return validationResult; + } @POST @Path("/combine") @RolesAllowed("ADMIN") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_OCTET_STREAM) - @ApiOperation(value="Combine PDF file and custom XML to zf/fx PDF",notes="Input PDF must be PDF/A-1, output PDF will be a ZUGFeRD/Factur-X PDF/A-3 file called invoice.pdf. Demo authentication good-guy:secret") - public Response combineFile(@ApiParam(required=true,value="Input PDF/A-1 file") @FormDataParam("file") final InputStream fileInputStream, - @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader, - @ApiParam(required=true, value="The zf/fx XML to add to the file") @FormDataParam("xml") String XML, - @ApiParam(required=true, defaultValue="zf", value="zf|fx:zf for ZUGFeRD, fx for Factur-X") @FormDataParam("format") String format, - @ApiParam(required=true, defaultValue="2", value="version, i.e. fx 1 or zf 1 or 2") @FormDataParam("version") int version, - @ApiParam(required=true, defaultValue="EN16931", value="Profile: BASIC|COMFORT|EXTENDED for zf1, MINIMUM|BASICWL|BASIC|CIUS|EN16931|EXTENDED for zf2/fx1") @FormDataParam("profile") String profile - ) { + @ApiOperation(value = "Combine PDF file and custom XML to zf/fx PDF", notes = "Input PDF must be PDF/A-1, output PDF will be a ZUGFeRD/Factur-X PDF/A-3 file called invoice.pdf. Demo authentication good-guy:secret") + public Response combineFile( + @ApiParam(required = true, value = "Input PDF/A-1 file") @FormDataParam("file") final InputStream fileInputStream, + @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader, + @ApiParam(required = true, value = "The zf/fx XML to add to the file") @FormDataParam("xml") String XML, + @ApiParam(required = true, defaultValue = "zf", value = "zf|fx:zf for ZUGFeRD, fx for Factur-X") @FormDataParam("format") String format, + @ApiParam(required = true, defaultValue = "2", value = "version, i.e. fx 1 or zf 1 or 2") @FormDataParam("version") int version, + @ApiParam(required = true, defaultValue = "EN16931", value = "Profile: BASIC|COMFORT|EXTENDED for zf1, MINIMUM|BASICWL|BASIC|CIUS|EN16931|EXTENDED for zf2/fx1") @FormDataParam("profile") String profile) { ZUGFeRDExporter ze; - ByteArrayOutputStream output=new ByteArrayOutputStream(); - + ByteArrayOutputStream output = new ByteArrayOutputStream(); + try { logger.debug("Converting to PDF/A-3u"); - - if ((version<1)||(version>2)) { + + if ((version < 1) || (version > 2)) { // this should be checked with annotations but I did not get it to work throw new IllegalArgumentException("invalid version"); } - // this should be restricted to the enum with annotations but I did not get it to work - ZUGFeRDConformanceLevel prof=ZUGFeRDConformanceLevel.valueOf(profile); + // this should be restricted to the enum with annotations but I did not get it + // to work + ZUGFeRDConformanceLevel prof = ZUGFeRDConformanceLevel.valueOf(profile); /* * Add .setZUGFeRDVersion and .setZUGFeRDConformanceLevel in the next lines to * set the ZUGFeRD version respective profile of the XML you are inserting. */ ze = new ZUGFeRDExporterFromA1Factory().setProducer("Mustang API") - .setCreator(System.getProperty("user.name")) - .setZUGFeRDVersion(version) - .setZUGFeRDConformanceLevel(prof) - .load(fileInputStream); + .setCreator(System.getProperty("user.name")).setZUGFeRDVersion(version) + .setZUGFeRDConformanceLevel(prof).load(fileInputStream); logger.debug("Attaching ZUGFeRD-Data"); if (format.equalsIgnoreCase("fx")) { ze.setFacturX(); } ze.setZUGFeRDXMLData(XML.getBytes("UTF-8")); logger.debug("Writing ZUGFeRD-PDF"); - + ze.export(output); } catch (IOException e) { e.printStackTrace(); - } + } byte[] bytes = output.toByteArray(); InputStream inputStream = new ByteArrayInputStream(bytes); - - return Response.ok(inputStream) - .header(HttpHeaders.CONTENT_DISPOSITION, - "attachment; filename=\"invoice.pdf\"") - .build(); - + + return Response.ok(inputStream).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"invoice.pdf\"") + .build(); + } - + @POST @Path("/test") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value="Returns a hello world",notes="Just a test") + @ApiOperation(value = "Returns a hello world", notes = "Just a test") public Response test() { - - return Response.status(Response.Status.OK) - .entity("{\"payload\":\"test\"}") - .build(); - + + return Response.status(Response.Status.OK).entity("{\"payload\":\"test\"}").build(); + } } \ No newline at end of file