Skip to content

Instantly share code, notes, and snippets.

@fernandor777
Created May 24, 2017 20:59
Show Gist options
  • Save fernandor777/c4072c066c3a93f67a56974d2ca3f937 to your computer and use it in GitHub Desktop.
Save fernandor777/c4072c066c3a93f67a56974d2ca3f937 to your computer and use it in GitHub Desktop.
JAX-RS Streaming of Web Request file from geoserver WMS
package gob.libertad.geoapi.rest;
import gob.libertad.geoapi.predios.CatastroGeoConfigs;
import gob.libertad.geoapi.predios.PrediosService;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
/**
*
* @author Fernando
*/
@RequestScoped
@Path("/predio")
public class PredioRs {
@Inject
protected CatastroGeoConfigs catConf;
@Inject
protected PrediosService predioSrv;
@GET
@Path("/croquis/{cod}")
@Produces("image/png")
public Response croquisFicha(@PathParam("cod") String codCatas){
Client client = ClientBuilder.newClient();
WebTarget wt = client.target(catConf.getWmsUrl())
.queryParam("service", "WMS")
.queryParam("version", "1.1.0")
.queryParam("request", "GetMap")
.queryParam("layers", "catastro:geo_predio")
.queryParam("bbox", "508509.4375,9750475.0,514450.53125,9755929.0")
.queryParam("width", "768")
.queryParam("height", "705")
.queryParam("srs", "EPSG:32717")
.queryParam("format", "image/png");
final InputStream is = wt.request().get(InputStream.class);
StreamingOutput so = new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
byte[] buffer = new byte[1024]; // Adjust if you want
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1)
{
output.write(buffer, 0, bytesRead);
output.flush();
}
}
};
return Response.ok(so).build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment