Search in sources :

Example 1 with EmptyJRDataSource

use of org.opennms.netmgt.jasper.measurement.EmptyJRDataSource in project opennms by OpenNMS.

the class LocalMeasurementDataSourceWrapper method createDataSource.

@Override
public JRRewindableDataSource createDataSource(String query) throws JRException {
    Objects.requireNonNull(query);
    QueryRequest queryRequest = unmarshal(query);
    Objects.requireNonNull(queryRequest);
    // Enforce relaxed mode
    queryRequest.setRelaxed(true);
    try {
        QueryResponse response = fetchService.query(queryRequest);
        return new MeasurementDataSource(response);
    } catch (ResourceNotFoundException rnfe) {
        LOG.warn("A attribute or resource was not found", rnfe);
        return new EmptyJRDataSource();
    } catch (Exception e) {
        LOG.error("An error occurred while fetching the measurement results", e);
        throw new JRException(e);
    }
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) JRException(net.sf.jasperreports.engine.JRException) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) MeasurementDataSource(org.opennms.netmgt.jasper.measurement.MeasurementDataSource) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) JRException(net.sf.jasperreports.engine.JRException) EmptyJRDataSource(org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)

Example 2 with EmptyJRDataSource

use of org.opennms.netmgt.jasper.measurement.EmptyJRDataSource in project opennms by OpenNMS.

the class RemoteMeasurementDataSourceWrapper method createDataSource.

@Override
public JRRewindableDataSource createDataSource(String query) throws JRException {
    try {
        QueryRequest queryRequest = unmarshal(query);
        // enforce relaxed mode
        queryRequest.setRelaxed(true);
        query = marshal(queryRequest);
        Result result = connector.execute(useSsl, url, username, password, query);
        // therefore do not allow redirection at all.
        if (result.wasRedirection()) {
            throw new IOException("Request was redirected. This is not supported.");
        }
        // if there is no data
        if (404 == result.getResponseCode()) {
            LOG.warn("Got a 404 (Not Found) response. This might be due to a wrong url or the resource does not exist. Requested URL was: '{}'", url);
            return new EmptyJRDataSource();
        }
        // OK
        if (result.wasSuccessful() && result.getInputStream() != null) {
            return new MeasurementDataSource(result.getInputStream());
        }
        // Error
        ByteArrayOutputStream errorMessageStream = new ByteArrayOutputStream();
        if (result.getErrorStream() != null) {
            ByteStreams.copy(result.getErrorStream(), errorMessageStream);
        }
        throw new JRException("Invalid request. Response was : " + result.getResponseCode() + " (" + result.getResponseMessage() + ")\n" + errorMessageStream.toString());
    } catch (IOException ioException) {
        throw new JRException(ioException);
    }
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) JRException(net.sf.jasperreports.engine.JRException) MeasurementDataSource(org.opennms.netmgt.jasper.measurement.MeasurementDataSource) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EmptyJRDataSource(org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)

Aggregations

JRException (net.sf.jasperreports.engine.JRException)2 EmptyJRDataSource (org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)2 MeasurementDataSource (org.opennms.netmgt.jasper.measurement.MeasurementDataSource)2 QueryRequest (org.opennms.netmgt.measurements.model.QueryRequest)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ResourceNotFoundException (org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException)1 QueryResponse (org.opennms.netmgt.measurements.model.QueryResponse)1