Search in sources :

Example 1 with InputStreamResource

use of com.revolsys.spring.resource.InputStreamResource in project com.revolsys.open by revolsys.

the class UsgsGriddedElevationReader method getChannel.

protected ReadableByteChannel getChannel() {
    final String fileExtension = this.resource.getFileNameExtension();
    try {
        if (fileExtension.equals("zip")) {
            final ZipInputStream in = this.resource.newBufferedInputStream(ZipInputStream::new);
            final String fileName = this.resource.getBaseName();
            final String baseName = FileUtil.getBaseName(fileName);
            final String projName = baseName + ".prj";
            for (ZipEntry zipEntry = in.getNextEntry(); zipEntry != null; zipEntry = in.getNextEntry()) {
                final String name = zipEntry.getName();
                if (name.equals(projName)) {
                    final String wkt = FileUtil.getString(new InputStreamReader(in, StandardCharsets.UTF_8), false);
                    final GeometryFactory geometryFactory = GeometryFactory.floating3d(wkt);
                    if (geometryFactory.isHasCoordinateSystem()) {
                        this.geometryFactory = geometryFactory;
                    }
                } else if (name.equals(fileName)) {
                    return new InputStreamResource(in).newReadableByteChannel();
                }
            }
            throw new IllegalArgumentException("Cannot find " + fileName + " in " + this.resource);
        } else if (fileExtension.equals("gz")) {
            final String baseName = this.resource.getBaseName();
            setGeometryFactory(this.resource.getParent().newChildResource(baseName));
            final InputStream in = this.resource.newBufferedInputStream();
            final GZIPInputStream gzIn = new GZIPInputStream(in);
            return new InputStreamResource(gzIn).newReadableByteChannel();
        } else {
            setGeometryFactory(this.resource);
            return this.resource.newReadableByteChannel();
        }
    } catch (final IOException e) {
        throw Exceptions.wrap("Unable to open: " + this.resource, e);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) InputStreamResource(com.revolsys.spring.resource.InputStreamResource)

Example 2 with InputStreamResource

use of com.revolsys.spring.resource.InputStreamResource in project com.revolsys.open by revolsys.

the class RecordReaderHttpMessageConverter method read.

@Override
public RecordReader read(final Class<? extends RecordReader> clazz, final HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    try {
        final HttpHeaders headers = inputMessage.getHeaders();
        final MediaType mediaType = headers.getContentType();
        Charset charset = mediaType.getCharSet();
        if (charset == null) {
            charset = StandardCharsets.UTF_8;
        }
        final InputStream body = inputMessage.getBody();
        final String mediaTypeString = mediaType.getType() + "/" + mediaType.getSubtype();
        final RecordReaderFactory readerFactory = IoFactory.factoryByMediaType(RecordReaderFactory.class, mediaTypeString);
        if (readerFactory == null) {
            throw new HttpMessageNotReadableException("Cannot read data in format" + mediaType);
        } else {
            final Reader<Record> reader = readerFactory.newRecordReader(new InputStreamResource("recordInput", body));
            GeometryFactory factory = this.geometryFactory;
            final ServletWebRequest requestAttributes = (ServletWebRequest) RequestContextHolder.getRequestAttributes();
            final String srid = requestAttributes.getParameter("srid");
            if (srid != null && srid.trim().length() > 0) {
                factory = GeometryFactory.floating3d(Integer.parseInt(srid));
            }
            reader.setProperty(IoConstants.GEOMETRY_FACTORY, factory);
            return (RecordReader) reader;
        }
    } catch (final IOException e) {
        throw new HttpMessageNotReadableException("Error reading data", e);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) InputStream(java.io.InputStream) RecordReader(com.revolsys.record.io.RecordReader) Charset(java.nio.charset.Charset) IOException(java.io.IOException) RecordReaderFactory(com.revolsys.record.io.RecordReaderFactory) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MediaType(org.springframework.http.MediaType) Record(com.revolsys.record.Record) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) InputStreamResource(com.revolsys.spring.resource.InputStreamResource)

Example 3 with InputStreamResource

use of com.revolsys.spring.resource.InputStreamResource in project com.revolsys.open by revolsys.

the class GeometryHttpMessageConverter method read.

@Override
public Geometry read(final Class<? extends Geometry> clazz, final HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    final HttpHeaders headers = inputMessage.getHeaders();
    final MediaType mediaType = headers.getContentType();
    try {
        Charset charset = mediaType.getCharSet();
        if (charset == null) {
            charset = StandardCharsets.UTF_8;
        }
        final InputStream body = inputMessage.getBody();
        final String mediaTypeString = mediaType.getType() + "/" + mediaType.getSubtype();
        final GeometryReaderFactory readerFactory = IoFactory.factoryByMediaType(GeometryReaderFactory.class, mediaTypeString);
        if (readerFactory == null) {
            throw new HttpMessageNotReadableException("Cannot read data in format" + mediaType);
        } else {
            final Reader<Geometry> reader = readerFactory.newGeometryReader(new InputStreamResource("geometryUpload", body));
            GeometryFactory factory = this.geometryFactory;
            final ServletWebRequest requestAttributes = (ServletWebRequest) RequestContextHolder.getRequestAttributes();
            final String srid = requestAttributes.getParameter("srid");
            if (srid != null && srid.trim().length() > 0) {
                factory = GeometryFactory.floating3d(Integer.parseInt(srid));
            }
            reader.setProperty(IoConstants.GEOMETRY_FACTORY, factory);
            for (final Geometry geometry : reader) {
                if (clazz.isAssignableFrom(geometry.getClass())) {
                    return geometry;
                }
            }
            return null;
        }
    } catch (final Throwable e) {
        Logs.error(this, "Error reading data using " + mediaType, e);
        throw new HttpMessageNotReadableException("Error reading data using" + mediaType);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) GeometryReaderFactory(com.revolsys.geometry.io.GeometryReaderFactory) Geometry(com.revolsys.geometry.model.Geometry) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MediaType(org.springframework.http.MediaType) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) InputStreamResource(com.revolsys.spring.resource.InputStreamResource)

Example 4 with InputStreamResource

use of com.revolsys.spring.resource.InputStreamResource in project com.revolsys.open by revolsys.

the class GeometryReaderHttpMessageConverter method read.

@Override
public GeometryReader read(final Class<? extends GeometryReader> clazz, final HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    try {
        final HttpHeaders headers = inputMessage.getHeaders();
        final MediaType mediaType = headers.getContentType();
        Charset charset = mediaType.getCharSet();
        if (charset == null) {
            charset = StandardCharsets.UTF_8;
        }
        final InputStream body = inputMessage.getBody();
        final String mediaTypeString = mediaType.getType() + "/" + mediaType.getSubtype();
        final GeometryReaderFactory readerFactory = IoFactory.factoryByMediaType(GeometryReaderFactory.class, mediaTypeString);
        if (readerFactory == null) {
            throw new HttpMessageNotReadableException("Cannot read data in format" + mediaType);
        } else {
            final InputStreamResource resource = new InputStreamResource("geometryInput", body);
            final GeometryReader reader = readerFactory.newGeometryReader(resource);
            GeometryFactory factory = this.geometryFactory;
            final ServletWebRequest requestAttributes = (ServletWebRequest) RequestContextHolder.getRequestAttributes();
            final String srid = requestAttributes.getParameter("srid");
            if (srid != null && srid.trim().length() > 0) {
                factory = GeometryFactory.floating3d(Integer.parseInt(srid));
            }
            reader.setProperty(IoConstants.GEOMETRY_FACTORY, factory);
            return reader;
        }
    } catch (final IOException e) {
        throw new HttpMessageNotReadableException("Error reading data", e);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) InputStream(java.io.InputStream) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset) IOException(java.io.IOException) GeometryReaderFactory(com.revolsys.geometry.io.GeometryReaderFactory) GeometryReader(com.revolsys.geometry.io.GeometryReader) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) InputStreamResource(com.revolsys.spring.resource.InputStreamResource)

Aggregations

GeometryFactory (com.revolsys.geometry.model.GeometryFactory)4 InputStreamResource (com.revolsys.spring.resource.InputStreamResource)4 InputStream (java.io.InputStream)4 IOException (java.io.IOException)3 Charset (java.nio.charset.Charset)3 HttpHeaders (org.springframework.http.HttpHeaders)3 MediaType (org.springframework.http.MediaType)3 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)3 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)3 GeometryReaderFactory (com.revolsys.geometry.io.GeometryReaderFactory)2 GeometryReader (com.revolsys.geometry.io.GeometryReader)1 Geometry (com.revolsys.geometry.model.Geometry)1 Record (com.revolsys.record.Record)1 RecordReader (com.revolsys.record.io.RecordReader)1 RecordReaderFactory (com.revolsys.record.io.RecordReaderFactory)1 InputStreamReader (java.io.InputStreamReader)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1