Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package web;
- import java.io.IOException;
- import java.util.LinkedList;
- import java.util.concurrent.TimeUnit;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.Produces;
- import javax.ws.rs.QueryParam;
- import javax.ws.rs.core.Response;
- import org.geotools.data.ows.HTTPResponse;
- import org.glassfish.jersey.media.sse.EventOutput;
- import org.glassfish.jersey.media.sse.OutboundEvent;
- import org.glassfish.jersey.media.sse.SseFeature;
- import org.joda.time.DateTime;
- import com.vividsolutions.jts.geom.Geometry;
- import com.vividsolutions.jts.io.ParseException;
- import com.vividsolutions.jts.io.WKTReader;
- import model.ImageData;
- import web.config.RestTimestampParam;
- import workflows.ChangeDetectionWorkflow;
- @Path("/changes")
- public class ChangeDetectionService {
- // @POST
- // @Path("/detect")
- // @Produces(MediaType.APPLICATION_JSON)
- // @Consumes(MediaType.APPLICATION_JSON)
- // public Response simpleChangeDetection(ImageData imageData) {
- // ResponseMessage respMessage=new ResponseMessage();
- // if (imageData == null) {
- // respMessage.setMessage("request body should not be empty.");
- // respMessage.setCode(400);
- // throw new WebApplicationException(Response.status(HttpURLConnection.HTTP_BAD_REQUEST).entity(respMessage).build());
- // }
- // try {
- // ChangeDetectionWorkflow changeDetectionWorkflow = new ChangeDetectionWorkflow();
- // //changeDetectionWorkflow.runWorkflow(imageData);
- // } catch (Exception e1) {
- // respMessage.setMessage("internal server error: " + e1.getMessage());
- // respMessage.setCode(500);
- // throw new WebApplicationException(
- // Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR).entity(respMessage).build());
- // }
- // respMessage.setMessage("change detection was performed successfully.");
- // respMessage.setCode(200);
- // return Response.status(200).entity(respMessage).build();
- // }
- @GET
- @Path("/progress")
- @Produces(SseFeature.SERVER_SENT_EVENTS)
- public EventOutput changeDetectionwithProgress(@QueryParam("extent") String extent,@QueryParam("event_date") RestTimestampParam eventDate,
- @QueryParam("reference_date") RestTimestampParam referenceDate,@QueryParam("polarization") String selectedPolarisations,@QueryParam("username") String username,@QueryParam("password") String password) throws Exception {
- Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Query parameters are: "+ extent+" "+selectedPolarisations+" "+eventDate+" "+referenceDate);
- // System.out.println(username+" "+password);
- final EventOutput eventOutput = new EventOutput();
- if (extent == null) {
- handleServerException(eventOutput,"extent should not be null.");
- }
- DateTime eventDate2=null;
- DateTime referenceDate2=null;
- if(eventDate==null)
- eventDate2=new DateTime();
- else
- eventDate2=eventDate.getDate();
- if(referenceDate==null)
- referenceDate2=eventDate2.minusDays(30);
- else
- referenceDate2=referenceDate.getDate();
- WKTReader wktReader = new WKTReader();
- ImageData imageData = new ImageData(eventDate2,referenceDate2,null,username,password,new String[]{"ff"});
- try {
- Geometry geometry = wktReader.read(extent);
- imageData.setArea(geometry);
- ChangeDetectionWorkflow changeDetectionWorkflow = new ChangeDetectionWorkflow();
- try {
- try {
- changeDetectionWorkflow.detectChanges(imageData).subscribe((value)->{
- try {
- notifyProgress(eventOutput, value);
- } catch (Exception e1) {
- handleServerException(eventOutput, e1.getMessage());
- }},
- e -> handleServerException(eventOutput, e.getMessage()),
- ()->{
- try {
- if(!eventOutput.isClosed())
- eventOutput.close();
- } catch (Exception e1) {
- Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,e1.getMessage());
- }
- });
- System.out.println("EDWWWWW11111progress");
- } catch (Exception e) {
- if(!eventOutput.isClosed())
- eventOutput.close();
- Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,e.getMessage());
- }
- } catch (Exception e) {
- if(!eventOutput.isClosed())
- eventOutput.close();
- Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,e.getMessage());
- }
- } catch (ParseException e1) {
- handleServerException(eventOutput,"bounding_box is not a valid WKT polygon");
- }
- return eventOutput;
- }
- ////////////////////////////////////////BEGIN after event detection//////////////////////////////////////////////////////////
- static ChangeDetectionService cds = new ChangeDetectionService();
- @GET
- @Path("/progressAfterED")
- @Produces(SseFeature.SERVER_SENT_EVENTS)
- public Response changeDetectionwithProgressAfterED(@QueryParam("extent") String extent,@QueryParam("event_date") RestTimestampParam eventDate,
- @QueryParam("reference_date") RestTimestampParam referenceDate,@QueryParam("polarization") String selectedPolarisations,@QueryParam("username") String username,@QueryParam("password") String password) throws Exception {
- Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Query parameters are: "+ extent+" "+selectedPolarisations+" "+eventDate+" "+referenceDate);
- // System.out.println(username+" "+password);
- final EventOutput eventOutput = new EventOutput();
- System.out.println("EDWWWWWprinprin");
- ChangeDetectionParams cdp1 = new ChangeDetectionParams(extent, eventDate, referenceDate, selectedPolarisations, username, password);
- System.out.println("event::"+cdp1.extent+" "+cdp1.eventDate+" "+cdp1.referenceDate+" "+cdp1.username+" "+cdp1.password);
- try {
- EDQ.enqueue(cdp1); // submit to the queue
- System.out.println("EDWWWWWprogressAfterED");
- System.out.println("size2 "+EDQ.theList.size());
- } catch (Exception e) {
- if(!eventOutput.isClosed())
- eventOutput.close();
- Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,e.getMessage());
- }
- return Response.status(200).entity("OK").build();
- }
- //////////////////dequeue/////////////////////////////////////////////////////////////////////////////////////////////////
- @GET
- @Path("/progressDequeue")
- @Produces(SseFeature.SERVER_SENT_EVENTS)
- public Response changeDetectionDequeue() throws Exception {
- Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Dequeue ");
- // System.out.println(username+" "+password);
- final EventOutput eventOutput = new EventOutput();
- System.out.println("EDWWWWWprinprin");
- try {
- while (true)
- {
- System.out.println("dequeue SIZE "+EDQ.theList.size());
- if (EDQ.theList.size()<1)
- {
- System.out.println("sleep");
- TimeUnit.MINUTES.sleep(60);
- }
- else
- {
- ChangeDetectionParams cdp = EDQ.dequeue();
- String extent = cdp.extent;
- RestTimestampParam eventDate = cdp.eventDate;
- RestTimestampParam referenceDate = cdp.referenceDate;
- String selectedPolarisations = cdp.selectedPolarisations;
- String username = cdp.username;
- String password = cdp.password;
- System.out.println("finished");
- System.out.println("dequeue SIZE "+EDQ.theList.size());
- EventOutput ep = changeDetectionwithProgress(extent, eventDate, referenceDate, selectedPolarisations, username, password);
- System.out.println("finished"+ep.toString());
- }
- }
- } catch (Exception e) {
- if(!eventOutput.isClosed())
- eventOutput.close();
- Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,e.getMessage());
- }
- return Response.status(200).entity("OK").build(); }
- ///////////////////endequeue/////////////////////////////////////////////////////////////////////////////////////////////
- private class EventDetectionQueue
- {
- LinkedList<ChangeDetectionParams> theList = new LinkedList<>();
- public synchronized void enqueue(ChangeDetectionParams cdp)
- {
- theList.add(cdp);
- while (theList.size()>1000)
- {
- theList.remove();
- }
- notifyAll();
- }
- public synchronized ChangeDetectionParams dequeue() throws InterruptedException // reader thread gets this
- {
- return theList.remove();
- }
- }
- public class ChangeDetectionParams
- {
- String extent;
- RestTimestampParam eventDate;
- RestTimestampParam referenceDate;
- String selectedPolarisations;
- String username;
- String password;
- public ChangeDetectionParams(String extent, RestTimestampParam eventDate,
- RestTimestampParam referenceDate, String selectedPolarisations, String username, String password) {
- this.extent = extent;
- this.eventDate = eventDate;
- this.referenceDate = referenceDate;
- this.selectedPolarisations = selectedPolarisations;
- this.username = username;
- this.password = password;
- }
- }
- // private class ReaderThread implements Runnable
- // {
- // EventDetectionQueue EDQ;
- // ReaderThread(EventDetectionQueue EDQ){ this.EDQ=EDQ;}
- // public void run()
- // {
- // try {
- // ChangeDetectionParams cdp = EDQ.fetch();
- // } catch (InterruptedException e) {
- // // TODO Auto-generated catch block
- // e.printStackTrace();
- // }
- //
- //
- // // here perform a HTTP-GET on the existing changeDetectionwithProgress service,
- // // with the arguments in CDCall
- // }
- // }
- final static EventDetectionQueue EDQ = cds.new EventDetectionQueue();
- // ReaderThread RT = new ReaderThread(EDQ);
- ////////////////////////////////////////END after event detection///////////////////////////////////////////////////////////////
- private void notifyProgress(EventOutput eventOutput, String value) throws IOException {
- final OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
- eventBuilder.data(String.class, value);
- final OutboundEvent event = eventBuilder.build();
- try {
- eventOutput.write(event);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- if(!eventOutput.isClosed())
- eventOutput.close();
- }
- }
- private void handleServerException(EventOutput eventOutput, String message) {
- Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,message);
- final OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
- eventBuilder.data(String.class, message);
- final OutboundEvent event = eventBuilder.build();
- try {
- eventOutput.write(event);
- if(!eventOutput.isClosed())
- eventOutput.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement