Search in sources :

Example 1 with RecordWriterFactory

use of com.revolsys.record.io.RecordWriterFactory in project com.revolsys.open by revolsys.

the class RecordWriterPerformanceTest method main.

public static void main(final String[] args) {
    final Path basePath = Paths.getPath("target/test/performance/recordWriter");
    Paths.createDirectories(basePath);
    final RecordDefinition recordDefinition = newRecordDefinition(DataTypes.POINT);
    for (final RecordWriterFactory writerFactory : IoFactory.factories(RecordWriterFactory.class)) {
        writeRecords(basePath, recordDefinition, writerFactory);
    }
// writeRecords(basePath, recordDefinition, new Kml());
}
Also used : Path(java.nio.file.Path) RecordWriterFactory(com.revolsys.record.io.RecordWriterFactory) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 2 with RecordWriterFactory

use of com.revolsys.record.io.RecordWriterFactory in project com.revolsys.open by revolsys.

the class RecordIoTestSuite method addWriteReadTest.

public static void addWriteReadTest(final TestSuite suite, final String prefix, final String fileExtension) {
    addGeometryTestSuites(suite, prefix, (geometryFactory, geometry, geometryDataType) -> {
        final String geometryTypeString = geometryDataType.toString();
        final File directory = new File("/tmp/revolsystest/io/" + fileExtension);
        final File file = new File(directory, geometryTypeString + "_" + geometryFactory.getAxisCount() + "_" + geometry.getVertexCount() + "." + fileExtension);
        file.delete();
        file.getParentFile().mkdirs();
        final PathResource resource = new PathResource(file);
        final RecordWriterFactory recordWriterFactory = IoFactory.factory(RecordWriterFactory.class, resource);
        final RecordDefinitionImpl recordDefinition = new RecordDefinitionImpl(PathName.newPathName(geometryTypeString));
        if (recordWriterFactory.isCustomFieldsSupported()) {
            recordDefinition.addField("BOOLEAN", DataTypes.BOOLEAN, true);
            recordDefinition.addField("BYTE", DataTypes.BYTE, true);
            recordDefinition.addField("SHORT", DataTypes.SHORT, true);
            recordDefinition.addField("INT", DataTypes.INT, true);
            recordDefinition.addField("LONG", DataTypes.LONG, true);
            recordDefinition.addField("FLOAT", DataTypes.FLOAT, 4, 3, true);
            recordDefinition.addField("DOUBLE", DataTypes.DOUBLE, 10, 9, true);
            recordDefinition.addField("STRING", DataTypes.STRING, true);
        }
        if (recordWriterFactory.isGeometrySupported()) {
            recordDefinition.addField("GEOMETRY", geometryDataType, true);
        }
        recordDefinition.setGeometryFactory(geometryFactory);
        final ArrayRecord record = new ArrayRecord(recordDefinition);
        record.setValue("BOOLEAN", true);
        record.setValue("BYTE", Byte.MAX_VALUE);
        record.setValue("SHORT", Short.MAX_VALUE);
        record.setValue("INT", Integer.MAX_VALUE);
        record.setValue("LONG", 999999999999999999L);
        record.setValue("FLOAT", 6.789);
        record.setValue("DOUBLE", 1.234567890);
        record.setValue("STRING", "test");
        record.setGeometryValue(geometry);
        doRecordWriteTest(resource, record);
        doRecordReadTest(resource, record);
        doGeometryReadTest(resource, record);
    });
}
Also used : ArrayRecord(com.revolsys.record.ArrayRecord) PathResource(com.revolsys.spring.resource.PathResource) RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) RecordWriterFactory(com.revolsys.record.io.RecordWriterFactory) File(java.io.File)

Example 3 with RecordWriterFactory

use of com.revolsys.record.io.RecordWriterFactory in project com.revolsys.open by revolsys.

the class RecordReaderHttpMessageConverter method write.

@Override
public void write(final RecordReader reader, final MediaType mediaType, final HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    if (!HttpServletUtils.getResponse().isCommitted()) {
        MediaType actualMediaType;
        if (mediaType == null) {
            actualMediaType = getDefaultMediaType();
        } else {
            actualMediaType = mediaType;
        }
        if (actualMediaType != null) {
            final Charset charset = HttpServletUtils.setContentTypeWithCharset(outputMessage, actualMediaType);
            final String mediaTypeString = actualMediaType.getType() + "/" + actualMediaType.getSubtype();
            final RecordWriterFactory writerFactory = IoFactory.factoryByMediaType(RecordWriterFactory.class, mediaTypeString);
            if (writerFactory == null) {
                throw new IllegalArgumentException("Media type " + actualMediaType + " not supported");
            } else {
                final RecordDefinition recordDefinition = reader.getRecordDefinition();
                final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
                String baseName = (String) requestAttributes.getAttribute("contentDispositionFileName", RequestAttributes.SCOPE_REQUEST);
                if (baseName == null) {
                    baseName = HttpServletUtils.getRequestBaseFileName();
                }
                String contentDisposition = (String) requestAttributes.getAttribute("contentDisposition", RequestAttributes.SCOPE_REQUEST);
                if (contentDisposition == null) {
                    contentDisposition = "attachment";
                }
                final String fileName = baseName + "." + writerFactory.getFileExtension(mediaTypeString);
                final HttpHeaders headers = outputMessage.getHeaders();
                headers.set("Content-Disposition", contentDisposition + "; filename=" + fileName);
                final OutputStream body = outputMessage.getBody();
                final Writer<Record> writer = writerFactory.newRecordWriter(baseName, recordDefinition, body, charset);
                if (Boolean.FALSE.equals(requestAttributes.getAttribute("wrapHtml", RequestAttributes.SCOPE_REQUEST))) {
                    writer.setProperty(IoConstants.WRAP_PROPERTY, false);
                }
                final HttpServletRequest request = HttpServletUtils.getRequest();
                String callback = request.getParameter("jsonp");
                if (callback == null) {
                    callback = request.getParameter("callback");
                }
                if (callback != null) {
                    writer.setProperty(IoConstants.JSONP_PROPERTY, callback);
                }
                for (final String attributeName : requestAttributes.getAttributeNames(RequestAttributes.SCOPE_REQUEST)) {
                    final Object value = requestAttributes.getAttribute(attributeName, RequestAttributes.SCOPE_REQUEST);
                    if (value != null && attributeName.startsWith("java:") || this.requestAttributeNames.contains(attributeName)) {
                        writer.setProperty(attributeName, value);
                    }
                }
                final Iterator<Record> iterator = reader.iterator();
                if (iterator.hasNext()) {
                    Record record = iterator.next();
                    final Geometry geometry = record.getGeometry();
                    if (geometry != null) {
                        final GeometryFactory geometryFactory = geometry.getGeometryFactory();
                        writer.setProperty(IoConstants.GEOMETRY_FACTORY, geometryFactory);
                    }
                    writer.write(record);
                    while (iterator.hasNext()) {
                        record = iterator.next();
                        writer.write(record);
                    }
                }
                writer.close();
            }
        }
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) RequestAttributes(org.springframework.web.context.request.RequestAttributes) RecordWriterFactory(com.revolsys.record.io.RecordWriterFactory) RecordDefinition(com.revolsys.record.schema.RecordDefinition) HttpServletRequest(javax.servlet.http.HttpServletRequest) Geometry(com.revolsys.geometry.model.Geometry) MediaType(org.springframework.http.MediaType) Record(com.revolsys.record.Record)

Aggregations

RecordWriterFactory (com.revolsys.record.io.RecordWriterFactory)3 RecordDefinition (com.revolsys.record.schema.RecordDefinition)2 Geometry (com.revolsys.geometry.model.Geometry)1 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 ArrayRecord (com.revolsys.record.ArrayRecord)1 Record (com.revolsys.record.Record)1 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)1 PathResource (com.revolsys.spring.resource.PathResource)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1 Charset (java.nio.charset.Charset)1 Path (java.nio.file.Path)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpHeaders (org.springframework.http.HttpHeaders)1 MediaType (org.springframework.http.MediaType)1 RequestAttributes (org.springframework.web.context.request.RequestAttributes)1