use of de.janrufmonitor.service.commons.http.IHttpRequest in project janrufmonitor by tbrandt77.
the class HttpImageProvider method getImage.
public File getImage(ICaller caller) {
if (caller.getPhoneNumber().isClired())
return null;
File cacheFile = (File) ImageCache.getInstance().get(caller.getPhoneNumber().getTelephoneNumber());
if (cacheFile != null) {
this.m_logger.info("Taking image file from cache: " + cacheFile.getName());
return cacheFile;
}
try {
IHttpRequest cgh = new GetImageHandler(caller.getPhoneNumber(), this.m_cm);
IRequester r = this.getRequester(cgh);
InputStream in = new BufferedInputStream(r.request().getContentStreamForRead());
File tmpOut = new File(PathResolver.getInstance().getTempDirectory() + "~images" + File.separator + caller.getPhoneNumber().getTelephoneNumber() + this.getExtension("image/jpeg"));
if (!tmpOut.exists()) {
tmpOut.getParentFile().mkdirs();
tmpOut.createNewFile();
}
FileOutputStream fos = new FileOutputStream(tmpOut);
Stream.copy(in, fos);
in.close();
fos.flush();
fos.close();
ImageCache.getInstance().add(caller.getPhoneNumber().getTelephoneNumber(), tmpOut);
return tmpOut;
} catch (MalformedURLException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
} catch (IOException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
} catch (NullPointerException e) {
this.m_logger.log(Level.WARNING, e.getMessage(), e);
} catch (Exception e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
}
return null;
}
use of de.janrufmonitor.service.commons.http.IHttpRequest in project janrufmonitor by tbrandt77.
the class Client method connect.
public synchronized boolean connect() {
if (this.isConnected)
return true;
this.m_httpSrv = new SimplePortListener(new ClientHandler(), this.getListenPort());
this.m_httpSrv.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
this.m_logger.severe(e.getMessage());
}
// register client at server
if (this.m_httpSrv != null) {
try {
IHttpRequest rq = new RegisterGetHandler(InetAddress.getLocalHost().getHostName(), InetAddress.getLocalHost().getHostAddress(), this.m_httpSrv.getPort(), this.getRegisterEvents());
try {
this.m_logger.info("Connecting to server with URI: " + rq.getURI());
} catch (Exception e1) {
this.m_logger.log(Level.SEVERE, e1.getMessage(), e1);
}
IRequester r = RequesterFactory.getInstance().createRequester(rq);
r.request();
int responseCode = r.request().getCode();
if (responseCode == 200) {
this.isConnected = true;
this.m_logger.info("Registration of client " + this.m_httpSrv.getServerIP() + " successfull.");
this.m_logger.info("Client is started and connected with server ...");
ClientStateManager.getInstance().fireState(IClientStateMonitor.CONNECTION_OK, "");
return true;
}
if (responseCode == 0) {
this.m_logger.severe("Registration of client " + this.m_httpSrv.getServerIP() + " failed. Server not reachable.");
ClientStateManager.getInstance().fireState(IClientStateMonitor.SERVER_NOT_FOUND, "Server: " + r.getServer() + "\nPort: " + r.getPort());
}
if (responseCode == 403) {
this.m_logger.severe("Client " + InetAddress.getLocalHost().getHostName() + " (" + InetAddress.getLocalHost().getHostAddress() + ") is not authorized to connect to server: " + r.getServer());
ClientStateManager.getInstance().fireState(IClientStateMonitor.SERVER_NOT_AUTHORIZED, "Client " + InetAddress.getLocalHost().getHostName() + " (" + InetAddress.getLocalHost().getHostAddress() + ") is not authorized to connect to server: " + r.getServer());
}
this.m_logger.severe("Response code from server was not OK: " + responseCode);
} catch (UnknownHostException e) {
this.m_logger.severe(e.getMessage());
ClientStateManager.getInstance().fireState(IClientStateMonitor.SERVER_NOT_FOUND, e.getMessage());
}
this.m_httpSrv.stop();
this.m_httpSrv = null;
}
return false;
}
use of de.janrufmonitor.service.commons.http.IHttpRequest in project janrufmonitor by tbrandt77.
the class Client method received.
public void received(IEvent event) {
if (event.getType() == IEventConst.EVENT_TYPE_CALLREJECTED) {
this.m_logger.info("Sending EVENT_TYPE_CALLREJECTED event to server.");
IHttpRequest request = new RejectHandler((ICall) event.getData());
this.sendRequest(IEventConst.EVENT_TYPE_CALLREJECTED, request);
}
if (event.getType() == IEventConst.EVENT_TYPE_IDENTIFIED_CALL) {
ICall c = (ICall) event.getData();
if (c != null) {
ClientCallMap.getInstance().setCall(c.getCaller().getPhoneNumber().getTelephoneNumber() + "/" + c.getMSN().getMSN(), c);
this.m_logger.info("Updating call in ClienTCallMap: " + c);
}
}
if (event.getType() == IEventConst.EVENT_TYPE_RETURNED_HIBERNATE) {
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Client detected hibernate return mode.");
if (this.isConnected()) {
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Client detected hibernate return mode, and client is connected.");
this.disconnect();
if (this.connect()) {
ClientStateManager.getInstance().fireState(IClientStateMonitor.CONNECTION_OK, "");
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Client reconnected after hibernate return mode.");
} else {
ClientStateManager.getInstance().fireState(IClientStateMonitor.CONNECTION_CLOSED, "");
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Client not reconnected after hibernate return mode.");
}
} else if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Client detected hibernate return mode, but is not connected. No re-connect.");
}
}
use of de.janrufmonitor.service.commons.http.IHttpRequest in project janrufmonitor by tbrandt77.
the class HttpImageProvider method getImagePath.
public String getImagePath(ICaller caller) {
if (caller.getPhoneNumber().isClired())
return "";
if (!hasImage(caller))
return "";
try {
IHttpRequest cgh = new GetImageHandler(caller.getPhoneNumber(), this.m_cm);
IRequester r = this.getRequester(cgh);
return "http://" + r.getServer() + ":" + r.getPort() + cgh.getURI().toString();
} catch (MalformedURLException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
} catch (IOException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
} catch (Exception e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
}
return "";
}
use of de.janrufmonitor.service.commons.http.IHttpRequest in project janrufmonitor by tbrandt77.
the class Server method received.
public void received(IEvent event) {
if (event.getType() == IEventConst.EVENT_TYPE_INCOMINGCALL) {
this.m_logger.info("Sending INCOMING_CALL event to clients.");
IHttpRequest request = new IncomingCallHandler((ICall) event.getData());
this.sendRequest(event, request);
}
if (event.getType() == IEventConst.EVENT_TYPE_OUTGOINGCALL) {
this.m_logger.info("Sending OUTGOING_CALL event to clients.");
IHttpRequest request = new OutgoingCallHandler((ICall) event.getData());
this.sendRequest(event, request);
}
if (event.getType() == IEventConst.EVENT_TYPE_APPLICATION_READY) {
this.m_logger.info("Sending APPLICATION_READY event to clients.");
IHttpRequest request = new ApplicationReadyHandler();
this.sendRequest(event, request);
}
if (event.getType() == IEventConst.EVENT_TYPE_CALLACCEPTED) {
this.m_logger.info("Sending CALLACCEPTED event to clients.");
IHttpRequest request = new AcceptHandler((ICall) event.getData());
this.sendRequest(event, request);
}
// added: 19/06/2004: Reject flag on client was not set
if (event.getType() == IEventConst.EVENT_TYPE_CALLREJECTED) {
this.m_logger.info("Sending CALLREJECTED event to clients.");
IHttpRequest request = new RejectedHandler((ICall) event.getData());
this.sendRequest(event, request);
}
if (event.getType() == IEventConst.EVENT_TYPE_CALLCLEARED) {
this.m_logger.info("Sending CALLCLEARED event to clients.");
IHttpRequest request = new ClearHandler((ICall) event.getData());
this.sendRequest(event, request);
}
if (event.getType() == IEventConst.EVENT_TYPE_IDENTIFIED_CALL) {
this.m_logger.info("Sending IDENTIFIED_CALL event to clients.");
IHttpRequest request = new IdentifiedCallHandler((ICall) event.getData());
this.sendRequest(event, request);
}
if (event.getType() == IEventConst.EVENT_TYPE_IDENTIFIED_OUTGOING_CALL) {
this.m_logger.info("Sending IDENTIFIED_OUTGOING_CALL event to clients.");
IHttpRequest request = new IdentifiedOutgoingCallHandler((ICall) event.getData());
this.sendRequest(event, request);
}
}
Aggregations