Search in sources :

Example 1 with ProcessorResponse

use of org.polymap.core.data.pipeline.ProcessorResponse in project polymap4-core by Polymap4.

the class PipelineFeatureSource method getCountInternal.

@Override
@SuppressWarnings("hiding")
protected int getCountInternal(Query query) throws IOException {
    GetFeaturesSizeRequest request = new GetFeaturesSizeRequest(query);
    try {
        final int[] result = new int[1];
        newExecutor().execute(pipeline, request, (ProcessorResponse r) -> {
            GetFeaturesSizeResponse response = (GetFeaturesSizeResponse) r;
            result[0] = response.getSize();
        });
        return result[0];
    } catch (Exception e) {
        Throwables.propagateIfInstanceOf(e, IOException.class);
        throw Throwables.propagate(e);
    }
}
Also used : GetFeaturesSizeRequest(org.polymap.core.data.feature.GetFeaturesSizeRequest) ProcessorResponse(org.polymap.core.data.pipeline.ProcessorResponse) GetFeaturesSizeResponse(org.polymap.core.data.feature.GetFeaturesSizeResponse) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with ProcessorResponse

use of org.polymap.core.data.pipeline.ProcessorResponse in project polymap4-core by Polymap4.

the class PipelineFeatureSource method fetchFeatures.

/**
 * Called by {@link SyncPipelineFeatureCollection} to fetch feature chunks.
 */
@SuppressWarnings("hiding")
protected void fetchFeatures(Query query, final FeatureResponseHandler handler) throws Exception {
    try {
        log.debug("fetchFeatures(): maxFeatures= " + query.getMaxFeatures());
        GetFeaturesRequest request = new GetFeaturesRequest(query);
        newExecutor().execute(pipeline, request, new ResponseHandler() {

            public void handle(ProcessorResponse r) throws Exception {
                GetFeaturesResponse response = (GetFeaturesResponse) r;
                handler.handle(response.getFeatures());
            }
        });
    } finally {
        handler.endOfResponse();
    }
}
Also used : GetFeaturesResponse(org.polymap.core.data.feature.GetFeaturesResponse) GetFeaturesRequest(org.polymap.core.data.feature.GetFeaturesRequest) ProcessorResponse(org.polymap.core.data.pipeline.ProcessorResponse) ResponseHandler(org.polymap.core.data.pipeline.ResponseHandler) IOException(java.io.IOException)

Example 3 with ProcessorResponse

use of org.polymap.core.data.pipeline.ProcessorResponse in project polymap4-core by Polymap4.

the class SimpleWmsServer method doGet.

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    log.debug("Request: " + request.getQueryString());
    try {
        final Map<String, String> kvp = parseKvpSet(request.getQueryString());
        sessionContext.execute(() -> {
            // LAYERS
            final String layer = kvp.get("LAYERS");
            assert !layer.contains(INNER_DELIMETER);
            final String style = kvp.get("STYLES");
            assert style == null || !style.contains(INNER_DELIMETER);
            log.info("layers=" + layer + ", style=" + style);
            // WIDTH/HEIGHT
            int width = Integer.parseInt(kvp.get("WIDTH"));
            int height = Integer.parseInt(kvp.get("HEIGHT"));
            // BBOX
            ReferencedEnvelope bbox = parseBBox(kvp.get("BBOX"));
            String srsCode = kvp.get("SRS");
            CoordinateReferenceSystem crs = CRS.decode(srsCode);
            bbox = new ReferencedEnvelope(bbox, crs);
            // FORMAT
            String format = kvp.get("FORMAT");
            format = format != null ? format : "image/png";
            log.debug("    --layers= " + layer);
            log.debug("    --imageSize= " + width + "x" + height);
            log.debug("    --bbox= " + bbox);
            crs = bbox.getCoordinateReferenceSystem();
            log.debug("    --CRS= " + bbox.getCoordinateReferenceSystem().getName());
            // find/create pipeline
            final Pipeline pipeline = pipelines.get(layer, key -> createPipeline(key));
            long modifiedSince = request.getDateHeader("If-Modified-Since");
            final ProcessorRequest pr = new GetMapRequest(Collections.singletonList(layer), Collections.singletonList(style), srsCode, bbox, format, width, height, modifiedSince);
            // process
            Lazy<ServletOutputStream> out = new PlainLazyInit(() -> {
                try {
                    return response.getOutputStream();
                } catch (Exception e) {
                    log.warn("Pipeline exception: " + e, e);
                    response.setStatus(502);
                    return null;
                }
            });
            try {
                createPipelineExecutor().execute(pipeline, pr, new ResponseHandler() {

                    @Override
                    public void handle(ProcessorResponse pipeResponse) throws Exception {
                        if (pipeResponse == EncodedImageResponse.NOT_MODIFIED) {
                            response.setStatus(304);
                        } else {
                            long lastModified = ((EncodedImageResponse) pipeResponse).getLastModified();
                            // allow the browser to use a cached tile for max-age without a request
                            if (lastModified > 0) {
                                long maxAge = ((EncodedImageResponse) pipeResponse).getExpires() - System.currentTimeMillis() / 1000;
                                response.setDateHeader("Last-Modified", lastModified);
                                response.setHeader("Cache-Control", "public,must-revalidate");
                            } else // disable browser cache if there is no internal Cache for this layer
                            {
                                response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
                                response.setDateHeader("Expires", 0);
                                response.setHeader("Pragma", "no-cache");
                            }
                            byte[] chunk = ((EncodedImageResponse) pipeResponse).getChunk();
                            int len = ((EncodedImageResponse) pipeResponse).getChunkSize();
                            out.get().write(chunk, 0, len);
                        }
                    }
                });
            } catch (Throwable e) {
                log.warn("Pipeline exception: " + e, e);
                response.setStatus(502);
            }
            return null;
        });
    } catch (IOException e) {
        // assuming that this is an EOF exception
        log.info("Exception: " + e);
    } catch (Exception e) {
        log.warn(e.toString(), e);
    } finally {
    // XXX do I have to close out?
    // out.close();
    }
}
Also used : ProcessorResponse(org.polymap.core.data.pipeline.ProcessorResponse) ResponseHandler(org.polymap.core.data.pipeline.ResponseHandler) ServletOutputStream(javax.servlet.ServletOutputStream) IOException(java.io.IOException) PlainLazyInit(org.polymap.core.runtime.PlainLazyInit) FactoryException(org.opengis.referencing.FactoryException) ServletException(javax.servlet.ServletException) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) ServiceException(org.osgi.framework.ServiceException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Pipeline(org.polymap.core.data.pipeline.Pipeline) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) EncodedImageResponse(org.polymap.core.data.image.EncodedImageResponse) ProcessorRequest(org.polymap.core.data.pipeline.ProcessorRequest) GetMapRequest(org.polymap.core.data.image.GetMapRequest) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 4 with ProcessorResponse

use of org.polymap.core.data.pipeline.ProcessorResponse in project polymap4-core by Polymap4.

the class PipelineFeatureSource method buildFeatureType.

@Override
protected SimpleFeatureType buildFeatureType() throws IOException {
    // FIXME caching schema is not allowed since the pipeline and/or processors
    // may change; maybe we could add a listener API to the pipeline
    // avoid synchronize; doing this in parallel is ok
    GetFeatureTypeRequest request = new GetFeatureTypeRequest();
    try {
        final FeatureType[] type = new FeatureType[1];
        newExecutor().execute(pipeline, request, (ProcessorResponse r) -> {
            GetFeatureTypeResponse response = (GetFeatureTypeResponse) r;
            type[0] = response.getFeatureType();
        });
        return (SimpleFeatureType) type[0];
    } catch (Exception e) {
        Throwables.propagateIfInstanceOf(e, IOException.class);
        throw Throwables.propagate(e);
    }
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) FeatureType(org.opengis.feature.type.FeatureType) ProcessorResponse(org.polymap.core.data.pipeline.ProcessorResponse) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) GetFeatureTypeRequest(org.polymap.core.data.feature.GetFeatureTypeRequest) GetFeatureTypeResponse(org.polymap.core.data.feature.GetFeatureTypeResponse) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)4 ProcessorResponse (org.polymap.core.data.pipeline.ProcessorResponse)4 ResponseHandler (org.polymap.core.data.pipeline.ResponseHandler)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ServletException (javax.servlet.ServletException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)1 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)1 FeatureType (org.opengis.feature.type.FeatureType)1 FactoryException (org.opengis.referencing.FactoryException)1 NoSuchAuthorityCodeException (org.opengis.referencing.NoSuchAuthorityCodeException)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1 ServiceException (org.osgi.framework.ServiceException)1 GetFeatureTypeRequest (org.polymap.core.data.feature.GetFeatureTypeRequest)1 GetFeatureTypeResponse (org.polymap.core.data.feature.GetFeatureTypeResponse)1 GetFeaturesRequest (org.polymap.core.data.feature.GetFeaturesRequest)1 GetFeaturesResponse (org.polymap.core.data.feature.GetFeaturesResponse)1 GetFeaturesSizeRequest (org.polymap.core.data.feature.GetFeaturesSizeRequest)1 GetFeaturesSizeResponse (org.polymap.core.data.feature.GetFeaturesSizeResponse)1 EncodedImageResponse (org.polymap.core.data.image.EncodedImageResponse)1