Search in sources :

Example 1 with IoParseException

use of org.n52.io.IoParseException in project series-rest-api by 52North.

the class ChartIoHandler method encodeAndWriteTo.

@Override
public void encodeAndWriteTo(DataCollection<QuantityData> data, OutputStream stream) throws IoParseException {
    try {
        writeDataToChart(data);
        ImageIO.write(createImage(), mimeType.getFormatName(), stream);
    } catch (IOException e) {
        throw new IoParseException("Could not write image to output stream.", e);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) IOException(java.io.IOException)

Example 2 with IoParseException

use of org.n52.io.IoParseException in project series-rest-api by 52North.

the class PDFReportGenerator method encodeAndWriteTo.

@Override
public void encodeAndWriteTo(DataCollection<QuantityData> data, OutputStream stream) throws IoParseException {
    try {
        generateOutput(data);
        DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
        Configuration cfg = cfgBuilder.build(document.newInputStream());
        FopFactory fopFactory = new FopFactoryBuilder(baseURI).setConfiguration(cfg).build();
        final String mimeType = MimeType.APPLICATION_PDF.getMimeType();
        Fop fop = fopFactory.newFop(mimeType, stream);
        //FopFactory fopFactory = FopFactory.newInstance(cfg);
        //Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
        //FopFactory fopFactory = fopFactoryBuilder.build();
        //Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
        // Create PDF via XSLT transformation
        TransformerFactory transFact = TransformerFactory.newInstance();
        StreamSource transformationRule = getTransforamtionRule();
        Transformer transformer = transFact.newTransformer(transformationRule);
        Source source = new StreamSource(document.newInputStream());
        Result result = new SAXResult(fop.getDefaultHandler());
        if (LOGGER.isDebugEnabled()) {
            try {
                File tempFile = File.createTempFile(TEMP_FILE_PREFIX, ".xml");
                StreamResult debugResult = new StreamResult(tempFile);
                transformer.transform(source, debugResult);
                String xslResult = XmlObject.Factory.parse(tempFile).xmlText();
                LOGGER.debug("xsl-fo input (locale '{}'): {}", i18n.getTwoDigitsLanguageCode(), xslResult);
            } 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) {
        throw new IoParseException("Failed to create Formatting Object Processor (FOP)", e);
    } catch (SAXException | ConfigurationException | IOException e) {
        throw new IoParseException("Failed to read config for Formatting Object Processor (FOP)", e);
    } catch (TransformerConfigurationException e) {
        throw new IoParseException("Invalid transform configuration. Inspect xslt!", e);
    } catch (TransformerException e) {
        throw new IoParseException("Could not generate PDF report!", e);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) DefaultConfigurationBuilder(org.apache.avalon.framework.configuration.DefaultConfigurationBuilder) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Configuration(org.apache.avalon.framework.configuration.Configuration) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) FopFactory(org.apache.fop.apps.FopFactory) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXException(org.xml.sax.SAXException) FopFactoryBuilder(org.apache.fop.apps.FopFactoryBuilder) FOPException(org.apache.fop.apps.FOPException) SAXResult(javax.xml.transform.sax.SAXResult) ConfigurationException(org.apache.avalon.framework.configuration.ConfigurationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) XmlException(org.apache.xmlbeans.XmlException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 3 with IoParseException

use of org.n52.io.IoParseException in project series-rest-api by 52North.

the class PDFReportGenerator method generateOutput.

public void generateOutput(DataCollection<QuantityData> data) throws IoParseException {
    try {
        generateTimeseriesChart(data);
        generateTimeseriesMetadata();
    } catch (IOException e) {
        throw new IoParseException("Error handling (temp) file!", e);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) IOException(java.io.IOException)

Example 4 with IoParseException

use of org.n52.io.IoParseException in project series-rest-api by 52North.

the class IoParameters method getSpatialFilter.

/**
 * Creates a {@link BoundingBox} instance from given spatial request parameters. The resulting bounding
 * box is the merged extent of all spatial filters given. For example if {@value #NEAR} and {@value #BBOX}
 * exist, the returned bounding box includes both extents.
 *
 * @return a spatial filter created from given spatial parameters.
 * @throws IoParseException
 *         if parsing parameters fails, or if a requested {@value #CRS} object could not be created.
 */
public BoundingBox getSpatialFilter() {
    if (!containsParameter(NEAR) && !containsParameter(BBOX)) {
        return null;
    }
    BoundingBox bboxBounds = createBbox();
    BoundingBox bounds = parseBoundsFromVicinity();
    return mergeBounds(bounds, bboxBounds);
}
Also used : BoundingBox(org.n52.io.crs.BoundingBox)

Example 5 with IoParseException

use of org.n52.io.IoParseException in project series-rest-api by 52North.

the class IoParameters method createBbox.

/**
 * @return a {@link BBox} instance or <code>null</code> if no {@link #BBOX} parameter is present.
 * @throws IoParseException
 *         if parsing parameter fails.
 * @throws IoParseException
 *         if a requested {@value #CRS} object could not be created
 */
private BoundingBox createBbox() {
    if (!containsParameter(BBOX)) {
        return null;
    }
    String bboxValue = getAsString(BBOX);
    CRSUtils crsUtils = CRSUtils.createEpsgForcedXYAxisOrder();
    // Check if supplied in minx,miny,maxx,maxy format - else assume json
    if (bboxMatching(bboxValue, 3)) {
        String[] coordArray = bboxValue.split(SPLIT_REGEX);
        Point lowerLeft = crsUtils.createPoint(Double.valueOf(coordArray[0].trim()), Double.valueOf(coordArray[1].trim()), CRSUtils.DEFAULT_CRS);
        Point upperRight = crsUtils.createPoint(Double.valueOf(coordArray[2].trim()), Double.valueOf(coordArray[3].trim()), CRSUtils.DEFAULT_CRS);
        return new BoundingBox(lowerLeft, upperRight, CRSUtils.DEFAULT_CRS);
    }
    try {
        BBox bbox = handleJsonValueParseException(BBOX, BBox.class, this::parseJson);
        return new BoundingBox(bbox.getLl(), bbox.getUr(), CRSUtils.DEFAULT_CRS);
    } catch (IoParseException e) {
        throw e.addHint(createInvalidParameterMessage(Parameters.BBOX)).addHint("Check http://epsg-registry.org for EPSG CRS definitions and codes.").addHint("(alternate format of 'llLon,llLat,urLon,urLat' couldn't be detected)");
    }
}
Also used : IoParseException(org.n52.io.IoParseException) BoundingBox(org.n52.io.crs.BoundingBox) CRSUtils(org.n52.io.crs.CRSUtils) Point(org.locationtech.jts.geom.Point)

Aggregations

IoParseException (org.n52.io.IoParseException)7 IOException (java.io.IOException)5 File (java.io.File)2 Result (javax.xml.transform.Result)2 Source (javax.xml.transform.Source)2 Transformer (javax.xml.transform.Transformer)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 TransformerFactory (javax.xml.transform.TransformerFactory)2 SAXResult (javax.xml.transform.sax.SAXResult)2 StreamResult (javax.xml.transform.stream.StreamResult)2 StreamSource (javax.xml.transform.stream.StreamSource)2 FOPException (org.apache.fop.apps.FOPException)2 Fop (org.apache.fop.apps.Fop)2 FopFactory (org.apache.fop.apps.FopFactory)2 FopFactoryBuilder (org.apache.fop.apps.FopFactoryBuilder)2 XmlException (org.apache.xmlbeans.XmlException)2 BoundingBox (org.n52.io.crs.BoundingBox)2 Point (com.vividsolutions.jts.geom.Point)1 URI (java.net.URI)1