Skip to content

Commit

Permalink
PDF creation does not need a baseURI
Browse files Browse the repository at this point in the history
Resolving internal URIs is not needed within the PDF creation,
as rendered image can be included via absolute URI as well.

This makes configuration much easier, as no root URI as to
be provided which has been taken from the servlet context.
So, no BaseController has not to be ServletContextAware
anymore :)
  • Loading branch information
ridoo committed May 30, 2018
1 parent 3430e62 commit 116f4b1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 63 deletions.
12 changes: 0 additions & 12 deletions io/src/main/java/org/n52/io/IoFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
*/
package org.n52.io;

import java.net.URI;
import java.util.List;
import java.util.Set;

Expand All @@ -49,8 +48,6 @@ public abstract class IoFactory<P extends DatasetOutput<V>, V extends AbstractVa

private ParameterService<P> datasetService;

private URI basePath;

public IoFactory() {
this.parameters = IoParameters.createDefaults();
}
Expand All @@ -60,11 +57,6 @@ public IoFactory<P, V> setParameters(IoParameters parameters) {
return this;
}

public IoFactory<P, V> setBasePath(URI basePath) {
this.basePath = basePath;
return this;
}

public IoFactory<P, V> setDataService(DataService<Data<V>> dataService) {
this.dataService = dataService;
return this;
Expand Down Expand Up @@ -124,8 +116,4 @@ protected DataService<Data<V>> getDataService() {
return dataService;
}

public URI getBasePath() {
return basePath;
}

}
17 changes: 6 additions & 11 deletions io/src/main/java/org/n52/io/quantity/QuantityIoFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,12 @@ public IoHandler<Data<QuantityValue>> createHandler(String outputMimeType) {
return createMultiChartRenderer(mimeType);
} else if (mimeType == Constants.MimeType.APPLICATION_PDF) {
ChartIoHandler imgRenderer = createMultiChartRenderer(mimeType);
PDFReportGenerator reportGenerator = new PDFReportGenerator(
parameters,
createProcessChain(),
imgRenderer);
reportGenerator.setBaseURI(getBasePath());
return reportGenerator;
} else if (mimeType == Constants.MimeType.TEXT_CSV || mimeType == Constants.MimeType.APPLICATION_ZIP) {
QuantityCsvIoHandler handler = new QuantityCsvIoHandler(
parameters,
createProcessChain(),
getMetadatas());
return new PDFReportGenerator(parameters, createProcessChain(), imgRenderer);
}
else if (mimeType == Constants.MimeType.TEXT_CSV || mimeType == Constants.MimeType.APPLICATION_ZIP) {
QuantityCsvIoHandler handler = new QuantityCsvIoHandler(parameters,
createProcessChain(),
getMetadatas());
handler.setTokenSeparator(parameters.getOther(Parameters.TOKEN_SEPARATOR));

boolean zipOutput = parameters.getAsBoolean(Parameters.ZIP, false);
Expand Down
37 changes: 17 additions & 20 deletions io/src/main/java/org/n52/io/report/quantity/PDFReportGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/

package org.n52.io.report.quantity;

import java.io.File;
Expand Down Expand Up @@ -92,8 +93,6 @@ public class PDFReportGenerator extends ReportGenerator<Data<QuantityValue>> {

private final ChartIoHandler renderer;

private URI baseURI;

public PDFReportGenerator(IoParameters parameters,
IoProcessChain<Data<QuantityValue>> processChain,
ChartIoHandler renderer) {
Expand All @@ -104,15 +103,12 @@ public PDFReportGenerator(IoParameters parameters,
this.renderer = renderer;
}

public void setBaseURI(URI baseURI) {
this.baseURI = baseURI;
}

public void generateOutput(DataCollection<Data<QuantityValue>> data) throws IoHandlerException {
try {
generateTimeseriesChart(data);
generateTimeseriesMetadata();
} catch (IOException e) {
}
catch (IOException e) {
throw new IoHandlerException("Error handling (temp) file!", e);
}
}
Expand All @@ -123,18 +119,13 @@ private void generateTimeseriesChart(DataCollection<Data<QuantityValue>> data) t
try (FileOutputStream stream = new FileOutputStream(tmpFile)) {
renderer.encodeAndWriteTo(data, stream);
document.getDocumentStructure()
.setDiagramURL(tmpFile.getAbsolutePath());
.setDiagramURL(tmpFile.toURI().toString());
// String absoluteFilePath = getFoAbsoluteFilepath(tmpFile);
// document.getDocumentStructure().setDiagramURL(absoluteFilePath);
stream.flush();
}
}

private String getFoAbsoluteFilepath(File tmpFile) {
return tmpFile.toURI()
.toString();
}

private void generateTimeseriesMetadata() {
for (DatasetOutput metadata : getAllDatasetMetadatas()) {
TimeSeries timeseries = addTimeseries(metadata);
Expand All @@ -150,8 +141,9 @@ public void encodeAndWriteTo(DataCollection<Data<QuantityValue>> data, OutputStr
generateOutput(data);
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.build(document.newInputStream());
FopFactory fopFactory = new FopFactoryBuilder(baseURI)
.setConfiguration(cfg)

URI baseURI = new File(".").toURI();
FopFactory fopFactory = new FopFactoryBuilder(baseURI).setConfiguration(cfg)
.build();
final String mimeType = Constants.APPLICATION_PDF;
Fop fop = fopFactory.newFop(mimeType, stream);
Expand All @@ -176,20 +168,25 @@ public void encodeAndWriteTo(DataCollection<Data<QuantityValue>> data, OutputStr
String xslResult = XmlObject.Factory.parse(tempFile)
.xmlText();
LOGGER.debug("xsl-fo input (locale '{}'): {}", i18n.getTwoDigitsLanguageCode(), xslResult);
} catch (IOException | TransformerException | XmlException e) {
}
catch (IOException | TransformerException | XmlException e) {
LOGGER.error("Could not debug XSL result output!", e);
}
}

// XXX debug, diagram is not embedded
transformer.transform(source, result);
} catch (FOPException e) {
}
catch (FOPException e) {
throw new IoParseException("Failed to create Formatting Object Processor (FOP)", e);
} catch (SAXException | ConfigurationException | IOException e) {
}
catch (SAXException | ConfigurationException | IOException e) {
throw new IoParseException("Failed to read config for Formatting Object Processor (FOP)", e);
} catch (TransformerConfigurationException e) {
}
catch (TransformerConfigurationException e) {
throw new IoParseException("Invalid transform configuration. Inspect xslt!", e);
} catch (TransformerException e) {
}
catch (TransformerException e) {
throw new IoParseException("Could not generate PDF report!", e);
}
}
Expand Down
11 changes: 1 addition & 10 deletions rest/src/main/java/org/n52/web/ctrl/BaseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
* </p>
*/
@RestController
public abstract class BaseController implements ServletConfigAware {
public abstract class BaseController {

private static final Logger LOGGER = LoggerFactory.getLogger(ResourcesController.class);

Expand Down Expand Up @@ -131,15 +131,6 @@ private IoParameters createParameters(IoParameters parameters, String locale) {
.setParseExceptionHandle(getExceptionHandle());
}

@Override
public void setServletConfig(ServletConfig servletConfig) {
this.servletConfig = servletConfig;
}

public ServletConfig getServletConfig() {
return servletConfig;
}

protected boolean isRequestingJsonData(HttpServletRequest request) {
return Constants.MimeType.APPLICATION_JSON.getMimeType()
.equals(getAcceptHeader(request));
Expand Down
9 changes: 0 additions & 9 deletions rest/src/main/java/org/n52/web/ctrl/DataController.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -446,12 +443,6 @@ private void checkForUnknownDatasetIds(IoParameters parameters, Set<String> seri
.setDatasetService(datasetService);
}

private URI getRootResource() throws URISyntaxException, MalformedURLException {
return getServletConfig().getServletContext()
.getResource("/")
.toURI();
}

// TODO set preredering config instead of task

public PreRenderingJob getPreRenderingTask() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ QuantityValue> createIoFactory(IoParameters parameters)
throws DatasetFactoryException, URISyntaxException, MalformedURLException {
return createDefaultIoFactory().create(QuantityValue.TYPE)
.setParameters(parameters)
.setBasePath(getRootResource())
.setDataService(timeseriesDataService)
.setDatasetService(timeseriesMetadataService);
}
Expand Down

0 comments on commit 116f4b1

Please sign in to comment.