Skip to content

Commit

Permalink
Fix #121: Classloading issues in Dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Oct 14, 2024
1 parent 488be70 commit a2491e3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleWriterExporterOutput;
import net.sf.jasperreports.export.SimpleXmlExporterOutput;
import net.sf.jasperreports.pdf.JRPdfExporter;

public abstract class AbstractJasperResource {

Expand Down Expand Up @@ -92,4 +93,15 @@ protected ByteArrayOutputStream exportOds(JasperPrint jasperPrint) throws JRExce
return outputStream;
}

}
protected ByteArrayOutputStream exportPdf(JasperPrint jasperPrint) throws JRException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JRPdfExporter exporter = new JRPdfExporter();

exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));

exporter.exportReport();
return outputStream;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package io.quarkiverse.jasperreports.it;

import static io.quarkiverse.jasperreports.it.ExtendedMediaType.APPLICATION_OPENDOCUMENT_TEXT;
import static io.quarkiverse.jasperreports.it.ExtendedMediaType.APPLICATION_RTF;
import static io.quarkiverse.jasperreports.it.ExtendedMediaType.TEXT_CSV;
import static io.restassured.RestAssured.given;
import static jakarta.ws.rs.core.MediaType.APPLICATION_XML;
import static jakarta.ws.rs.core.MediaType.TEXT_HTML;
import static org.hamcrest.Matchers.containsString;

import org.junit.jupiter.api.Test;
Expand All @@ -11,12 +16,51 @@
class JasperReportsStylesResourceTest {

@Test
void testStyleDatasource() {
void testStyleCsv() {
given()
.when().header("accept", "text/csv").get("jasper/style")
.when().header("accept", TEXT_CSV).get("/jasper/style")
.then()
.statusCode(200)
.body(containsString("Regular (default): font size = 12, centered, border"));
}

}
@Test
void testExportXML() {
given()
.when().header("accept", APPLICATION_XML).get("/jasper/style")
.then()
.statusCode(200);
}

@Test
void testExportHTML() {
given()
.when().header("accept", TEXT_HTML).get("/jasper/style")
.then()
.statusCode(200);
}

@Test
void testExportRTF() {
given()
.when().header("accept", APPLICATION_RTF).get("/jasper/style")
.then()
.statusCode(200);
}

@Test
void testExportODT() {
given()
.when().header("accept", APPLICATION_OPENDOCUMENT_TEXT).get("/jasper/style")
.then()
.statusCode(200);
}

@Test
void testExportPDF() {
given()
.when().header("accept", ExtendedMediaType.APPLICATION_PDF).get("/jasper/style")
.then()
.statusCode(200);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.jboss.logging.Logger;

import io.quarkus.runtime.util.ClassPathUtils;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperReportsContext;
import net.sf.jasperreports.engine.SimpleJasperReportsContext;
Expand Down Expand Up @@ -68,20 +69,22 @@ public JasperReportsContext getContext() {
*/
@Override
public InputStream getInputStream(String uri) {
LOG.debugf("Loading getInputStream %s", uri);
String logType = null;
String filePath = uri;
String filePath = null;

if (uri.endsWith(EXT_COMPILED) || uri.endsWith(EXT_STYLE)) {
logType = uri.endsWith(EXT_COMPILED) ? "report" : "style";
filePath = Path.of(this.destinationPath.toString(), uri).toString();
filePath = ClassPathUtils.toResourceName(Path.of(this.destinationPath.toString(), uri));
} else if (uri.endsWith(EXT_DATA_ADAPTER)) {
logType = "data adapter";
}

if (logType != null) {
try {
LOG.debugf("Loading %s file %s", logType, filePath);
return JRLoader.getLocationInputStream(filePath);
InputStream is = JRLoader.getLocationInputStream(filePath);
LOG.debugf("Loading %s file %s %s", logType, filePath, is);
return is;
} catch (JRException ex) {
LOG.warnf("Failed to load %s - %s", logType, ex.getMessage());
LOG.debug(ex);
Expand All @@ -100,6 +103,7 @@ public InputStream getInputStream(String uri) {
*/
@Override
public OutputStream getOutputStream(String uri) {
LOG.warnf("Read-Only repository called getOutputStream: %s", uri);
throw new IllegalStateException("This repository is read only");
}

Expand All @@ -112,6 +116,7 @@ public OutputStream getOutputStream(String uri) {
*/
@Override
public Resource getResource(String uri) {
LOG.warnf("Read-Only repository called getResource: %s", uri);
throw new IllegalStateException("Can only return an InputStream");
}

Expand All @@ -124,6 +129,7 @@ public Resource getResource(String uri) {
*/
@Override
public void saveResource(String uri, Resource resource) {
LOG.warnf("Read-Only repository called saveResource: %s", uri);
throw new IllegalStateException("This repository is read only");
}

Expand All @@ -138,6 +144,7 @@ public void saveResource(String uri, Resource resource) {
@Override
@SuppressWarnings("unchecked")
public <T extends Resource> T getResource(String uri, Class<T> resourceType) {
LOG.debugf("getResource %s type %s", uri, resourceType);
final PersistenceService persistenceService = PersistenceUtil.getInstance(context).getService(
FileRepositoryService.class,
resourceType);
Expand Down

0 comments on commit a2491e3

Please sign in to comment.