Advertisement
Guest User

Untitled

a guest
Nov 21st, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.52 KB | None | 0 0
  1. package web;
  2.  
  3. import java.io.IOException;
  4. import java.util.LinkedList;
  5. import java.util.concurrent.TimeUnit;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8.  
  9. import javax.ws.rs.GET;
  10. import javax.ws.rs.Path;
  11. import javax.ws.rs.Produces;
  12. import javax.ws.rs.QueryParam;
  13. import javax.ws.rs.core.Response;
  14.  
  15. import org.geotools.data.ows.HTTPResponse;
  16. import org.glassfish.jersey.media.sse.EventOutput;
  17. import org.glassfish.jersey.media.sse.OutboundEvent;
  18. import org.glassfish.jersey.media.sse.SseFeature;
  19. import org.joda.time.DateTime;
  20.  
  21. import com.vividsolutions.jts.geom.Geometry;
  22. import com.vividsolutions.jts.io.ParseException;
  23. import com.vividsolutions.jts.io.WKTReader;
  24.  
  25. import model.ImageData;
  26. import web.config.RestTimestampParam;
  27. import workflows.ChangeDetectionWorkflow;
  28.  
  29. @Path("/changes")
  30. public class ChangeDetectionService {
  31.  
  32. // @POST
  33. // @Path("/detect")
  34. // @Produces(MediaType.APPLICATION_JSON)
  35. // @Consumes(MediaType.APPLICATION_JSON)
  36. // public Response simpleChangeDetection(ImageData imageData) {
  37. // ResponseMessage respMessage=new ResponseMessage();
  38. // if (imageData == null) {
  39. // respMessage.setMessage("request body should not be empty.");
  40. // respMessage.setCode(400);
  41. // throw new WebApplicationException(Response.status(HttpURLConnection.HTTP_BAD_REQUEST).entity(respMessage).build());
  42. // }
  43. // try {
  44. // ChangeDetectionWorkflow changeDetectionWorkflow = new ChangeDetectionWorkflow();
  45. // //changeDetectionWorkflow.runWorkflow(imageData);
  46. // } catch (Exception e1) {
  47. // respMessage.setMessage("internal server error: " + e1.getMessage());
  48. // respMessage.setCode(500);
  49. // throw new WebApplicationException(
  50. // Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR).entity(respMessage).build());
  51. // }
  52. // respMessage.setMessage("change detection was performed successfully.");
  53. // respMessage.setCode(200);
  54. // return Response.status(200).entity(respMessage).build();
  55. // }
  56.  
  57. @GET
  58. @Path("/progress")
  59. @Produces(SseFeature.SERVER_SENT_EVENTS)
  60. public EventOutput changeDetectionwithProgress(@QueryParam("extent") String extent,@QueryParam("event_date") RestTimestampParam eventDate,
  61. @QueryParam("reference_date") RestTimestampParam referenceDate,@QueryParam("polarization") String selectedPolarisations,@QueryParam("username") String username,@QueryParam("password") String password) throws Exception {
  62. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Query parameters are: "+ extent+" "+selectedPolarisations+" "+eventDate+" "+referenceDate);
  63. // System.out.println(username+" "+password);
  64. final EventOutput eventOutput = new EventOutput();
  65. if (extent == null) {
  66. handleServerException(eventOutput,"extent should not be null.");
  67. }
  68. DateTime eventDate2=null;
  69. DateTime referenceDate2=null;
  70. if(eventDate==null)
  71. eventDate2=new DateTime();
  72. else
  73. eventDate2=eventDate.getDate();
  74. if(referenceDate==null)
  75. referenceDate2=eventDate2.minusDays(30);
  76. else
  77. referenceDate2=referenceDate.getDate();
  78.  
  79. WKTReader wktReader = new WKTReader();
  80.  
  81. ImageData imageData = new ImageData(eventDate2,referenceDate2,null,username,password,new String[]{"ff"});
  82. try {
  83. Geometry geometry = wktReader.read(extent);
  84. imageData.setArea(geometry);
  85. ChangeDetectionWorkflow changeDetectionWorkflow = new ChangeDetectionWorkflow();
  86. try {
  87. try {
  88. changeDetectionWorkflow.detectChanges(imageData).subscribe((value)->{
  89. try {
  90. notifyProgress(eventOutput, value);
  91. } catch (Exception e1) {
  92. handleServerException(eventOutput, e1.getMessage());
  93. }},
  94. e -> handleServerException(eventOutput, e.getMessage()),
  95. ()->{
  96. try {
  97. if(!eventOutput.isClosed())
  98. eventOutput.close();
  99. } catch (Exception e1) {
  100. Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,e1.getMessage());
  101. }
  102. });
  103. System.out.println("EDWWWWW11111progress");
  104. } catch (Exception e) {
  105. if(!eventOutput.isClosed())
  106. eventOutput.close();
  107. Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,e.getMessage());
  108. }
  109. } catch (Exception e) {
  110. if(!eventOutput.isClosed())
  111. eventOutput.close();
  112. Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,e.getMessage());
  113. }
  114.  
  115. } catch (ParseException e1) {
  116. handleServerException(eventOutput,"bounding_box is not a valid WKT polygon");
  117. }
  118. return eventOutput;
  119. }
  120.  
  121. ////////////////////////////////////////BEGIN after event detection//////////////////////////////////////////////////////////
  122.  
  123.  
  124. static ChangeDetectionService cds = new ChangeDetectionService();
  125.  
  126. @GET
  127. @Path("/progressAfterED")
  128. @Produces(SseFeature.SERVER_SENT_EVENTS)
  129. public Response changeDetectionwithProgressAfterED(@QueryParam("extent") String extent,@QueryParam("event_date") RestTimestampParam eventDate,
  130. @QueryParam("reference_date") RestTimestampParam referenceDate,@QueryParam("polarization") String selectedPolarisations,@QueryParam("username") String username,@QueryParam("password") String password) throws Exception {
  131. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Query parameters are: "+ extent+" "+selectedPolarisations+" "+eventDate+" "+referenceDate);
  132. // System.out.println(username+" "+password);
  133. final EventOutput eventOutput = new EventOutput();
  134. System.out.println("EDWWWWWprinprin");
  135.  
  136. ChangeDetectionParams cdp1 = new ChangeDetectionParams(extent, eventDate, referenceDate, selectedPolarisations, username, password);
  137. System.out.println("event::"+cdp1.extent+" "+cdp1.eventDate+" "+cdp1.referenceDate+" "+cdp1.username+" "+cdp1.password);
  138.  
  139. try {
  140.  
  141. EDQ.enqueue(cdp1); // submit to the queue
  142. System.out.println("EDWWWWWprogressAfterED");
  143. System.out.println("size2 "+EDQ.theList.size());
  144.  
  145. } catch (Exception e) {
  146. if(!eventOutput.isClosed())
  147. eventOutput.close();
  148. Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,e.getMessage());
  149. }
  150. return Response.status(200).entity("OK").build();
  151. }
  152.  
  153. //////////////////dequeue/////////////////////////////////////////////////////////////////////////////////////////////////
  154. @GET
  155. @Path("/progressDequeue")
  156. @Produces(SseFeature.SERVER_SENT_EVENTS)
  157. public Response changeDetectionDequeue() throws Exception {
  158. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Dequeue ");
  159. // System.out.println(username+" "+password);
  160. final EventOutput eventOutput = new EventOutput();
  161. System.out.println("EDWWWWWprinprin");
  162.  
  163. try {
  164.  
  165. while (true)
  166. {
  167. System.out.println("dequeue SIZE "+EDQ.theList.size());
  168. if (EDQ.theList.size()<1)
  169. {
  170. System.out.println("sleep");
  171. TimeUnit.MINUTES.sleep(60);
  172. }
  173. else
  174. {
  175. ChangeDetectionParams cdp = EDQ.dequeue();
  176.  
  177. String extent = cdp.extent;
  178. RestTimestampParam eventDate = cdp.eventDate;
  179. RestTimestampParam referenceDate = cdp.referenceDate;
  180. String selectedPolarisations = cdp.selectedPolarisations;
  181. String username = cdp.username;
  182. String password = cdp.password;
  183.  
  184. System.out.println("finished");
  185. System.out.println("dequeue SIZE "+EDQ.theList.size());
  186.  
  187. EventOutput ep = changeDetectionwithProgress(extent, eventDate, referenceDate, selectedPolarisations, username, password);
  188. System.out.println("finished"+ep.toString());
  189.  
  190. }
  191. }
  192. } catch (Exception e) {
  193. if(!eventOutput.isClosed())
  194. eventOutput.close();
  195. Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,e.getMessage());
  196. }
  197. return Response.status(200).entity("OK").build(); }
  198. ///////////////////endequeue/////////////////////////////////////////////////////////////////////////////////////////////
  199.  
  200. private class EventDetectionQueue
  201. {
  202. LinkedList<ChangeDetectionParams> theList = new LinkedList<>();
  203.  
  204. public synchronized void enqueue(ChangeDetectionParams cdp)
  205. {
  206. theList.add(cdp);
  207. while (theList.size()>1000)
  208. {
  209. theList.remove();
  210. }
  211. notifyAll();
  212. }
  213. public synchronized ChangeDetectionParams dequeue() throws InterruptedException // reader thread gets this
  214. {
  215. return theList.remove();
  216.  
  217. }
  218. }
  219.  
  220. public class ChangeDetectionParams
  221. {
  222. String extent;
  223. RestTimestampParam eventDate;
  224. RestTimestampParam referenceDate;
  225. String selectedPolarisations;
  226. String username;
  227. String password;
  228.  
  229. public ChangeDetectionParams(String extent, RestTimestampParam eventDate,
  230. RestTimestampParam referenceDate, String selectedPolarisations, String username, String password) {
  231. this.extent = extent;
  232. this.eventDate = eventDate;
  233. this.referenceDate = referenceDate;
  234. this.selectedPolarisations = selectedPolarisations;
  235. this.username = username;
  236. this.password = password;
  237. }
  238.  
  239. }
  240.  
  241. // private class ReaderThread implements Runnable
  242. // {
  243. // EventDetectionQueue EDQ;
  244. // ReaderThread(EventDetectionQueue EDQ){ this.EDQ=EDQ;}
  245. // public void run()
  246. // {
  247. // try {
  248. // ChangeDetectionParams cdp = EDQ.fetch();
  249. // } catch (InterruptedException e) {
  250. // // TODO Auto-generated catch block
  251. // e.printStackTrace();
  252. // }
  253. //
  254. //
  255. // // here perform a HTTP-GET on the existing changeDetectionwithProgress service,
  256. // // with the arguments in CDCall
  257. // }
  258. // }
  259.  
  260. final static EventDetectionQueue EDQ = cds.new EventDetectionQueue();
  261. // ReaderThread RT = new ReaderThread(EDQ);
  262.  
  263.  
  264. ////////////////////////////////////////END after event detection///////////////////////////////////////////////////////////////
  265.  
  266. private void notifyProgress(EventOutput eventOutput, String value) throws IOException {
  267. final OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
  268. eventBuilder.data(String.class, value);
  269. final OutboundEvent event = eventBuilder.build();
  270. try {
  271. eventOutput.write(event);
  272.  
  273. } catch (IOException e) {
  274. // TODO Auto-generated catch block
  275. if(!eventOutput.isClosed())
  276. eventOutput.close();
  277. }
  278. }
  279.  
  280. private void handleServerException(EventOutput eventOutput, String message) {
  281. Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,message);
  282. final OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
  283. eventBuilder.data(String.class, message);
  284. final OutboundEvent event = eventBuilder.build();
  285. try {
  286. eventOutput.write(event);
  287. if(!eventOutput.isClosed())
  288. eventOutput.close();
  289. } catch (IOException e) {
  290. // TODO Auto-generated catch block
  291. e.printStackTrace();
  292. }
  293. }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement