Search in sources :

Example 1 with GeometryReaderFactory

use of com.revolsys.geometry.io.GeometryReaderFactory 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 2 with GeometryReaderFactory

use of com.revolsys.geometry.io.GeometryReaderFactory 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

GeometryReaderFactory (com.revolsys.geometry.io.GeometryReaderFactory)2 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)2 InputStreamResource (com.revolsys.spring.resource.InputStreamResource)2 InputStream (java.io.InputStream)2 Charset (java.nio.charset.Charset)2 HttpHeaders (org.springframework.http.HttpHeaders)2 MediaType (org.springframework.http.MediaType)2 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)2 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)2 GeometryReader (com.revolsys.geometry.io.GeometryReader)1 Geometry (com.revolsys.geometry.model.Geometry)1 IOException (java.io.IOException)1