This is a question about implementing a RESTful listener service, but I will provide some background first to provide context.
I have a existing java application & swing UI which takes a directory and database file as input. After these are set a button imports the PDF forms within that directory into the database. There is a small JFrame window in the UI for the buttons and some logging output. This works very well.
I now want this java app to connect to our Exchange Mail Server, listen for new emails with the PDFs attached, save them to a directory and import using my application above.
To connect to our exchange server (IMAP, POP etc are not enabled) I was going to use the ews-java API https://github.com/OfficeDev/ews-java-api
However this requires that the client who wants to get push notifications from the exchange mail server (via REST) does three things:
1) Connect to the exchange server (I can do this)
ExchangeService newService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
newService.setUrl("https://myurl/ews/Exchange.asmx");
ExchangeCredentials credentials = new WebCredentials("user","password");
newService.setCredentials(credentials);
2) Subscribe to push notifications and setup a callback URI (I can do this)
@Override
public PushSubscription subscribeToPushNotifications(){
URI callback = null;
PushSubscription pushSubscription = null;
try{
logger.info("Subscribing to push notifications.. Endpoint Listener URI = " + config.getNotificationListenerURI());
callback = new URI(config.getNotificationListenerURI());
pushSubscription = service.subscribeToPushNotifications(
getFoldersForSubscription(), callback , 5, null,
EventType.NewMail, EventType.Created, EventType.Deleted, EventType.Modified, EventType.Moved);
logger.info("Done! --> PushSubscription ID= " + pushSubscription.getId());
}catch(URISyntaxException e){
logger.error("EWS ==> Invalid Notification Listener URL = " + config.getNotificationListenerURI() +". Please, verify <integration-modules>");
Throwables.propagate(e);
}catch (Exception e){
logger.error("EWS ==> Error subscribing to push notifications", e);
Throwables.propagate(e);
}
return pushSubscription;
}
3) Setup a listener service to get new-email notifications (help needed here)
Similar to this : Exchange Web Services Java APi + RESTful Push Notifications Listener
@Path("/emailnotification")
public class ExchangeNotificationListener {
private static final Log logger = LogFactory.getLog(ExchangeNotificationListener.class);
@Path("/incomingevent")
@POST()
@Produces(MediaType.TEXT_XML)
public Response onNotificationReceived() throws Exception {
logger.info("Received EWS notification !!!");
return Response.ok().build();
}
@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {
String output = "Jersey say : " + msg;
return Response.status(200).entity(output).build();
}
}
It appears that my listener and jax-rs needs to run within a container like tomcat for step 3 and these web services that run within tomcat and are started by tomcat.
Is there a way to have my existing application start / stop the listener or do I have to convert the whole thing to be a web service project? So far I have only managed to do step 3 as a standalone web project but would like to be able to start stop it from my existing app.
Ideally I would like to have the ExchangeNotificationListener started after the subscription to email notifications in step two.
Could the class that starts the subscription extend the ExchangeNotificationListener class to do this?
Or should I be using something other than tomcat?
Thanks