Search in sources :

Example 1 with IHttpRequest

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;
}
Also used : IHttpRequest(de.janrufmonitor.service.commons.http.IHttpRequest) MalformedURLException(java.net.MalformedURLException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) GetImageHandler(de.janrufmonitor.service.client.request.handler.GetImageHandler) IRequester(de.janrufmonitor.service.commons.http.IRequester) File(java.io.File) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with IHttpRequest

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;
}
Also used : IHttpRequest(de.janrufmonitor.service.commons.http.IHttpRequest) UnknownHostException(java.net.UnknownHostException) SimplePortListener(de.janrufmonitor.service.commons.http.simple.SimplePortListener) ClientHandler(de.janrufmonitor.service.client.http.simple.ClientHandler) IRequester(de.janrufmonitor.service.commons.http.IRequester) UnknownHostException(java.net.UnknownHostException)

Example 3 with IHttpRequest

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.");
    }
}
Also used : IHttpRequest(de.janrufmonitor.service.commons.http.IHttpRequest) ICall(de.janrufmonitor.framework.ICall)

Example 4 with IHttpRequest

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 "";
}
Also used : IHttpRequest(de.janrufmonitor.service.commons.http.IHttpRequest) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) GetImageHandler(de.janrufmonitor.service.client.request.handler.GetImageHandler) IRequester(de.janrufmonitor.service.commons.http.IRequester) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 5 with IHttpRequest

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);
    }
}
Also used : IHttpRequest(de.janrufmonitor.service.commons.http.IHttpRequest) ApplicationReadyHandler(de.janrufmonitor.service.server.request.handler.ApplicationReadyHandler) IdentifiedCallHandler(de.janrufmonitor.service.server.request.handler.IdentifiedCallHandler) ClearHandler(de.janrufmonitor.service.server.request.handler.ClearHandler) IncomingCallHandler(de.janrufmonitor.service.server.request.handler.IncomingCallHandler) OutgoingCallHandler(de.janrufmonitor.service.server.request.handler.OutgoingCallHandler) IdentifiedOutgoingCallHandler(de.janrufmonitor.service.server.request.handler.IdentifiedOutgoingCallHandler) AcceptHandler(de.janrufmonitor.service.server.request.handler.AcceptHandler) RejectedHandler(de.janrufmonitor.service.server.request.handler.RejectedHandler) IdentifiedOutgoingCallHandler(de.janrufmonitor.service.server.request.handler.IdentifiedOutgoingCallHandler)

Aggregations

IHttpRequest (de.janrufmonitor.service.commons.http.IHttpRequest)6 IRequester (de.janrufmonitor.service.commons.http.IRequester)3 GetImageHandler (de.janrufmonitor.service.client.request.handler.GetImageHandler)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 ICall (de.janrufmonitor.framework.ICall)1 IEventBroker (de.janrufmonitor.framework.event.IEventBroker)1 ClientHandler (de.janrufmonitor.service.client.http.simple.ClientHandler)1 SimplePortListener (de.janrufmonitor.service.commons.http.simple.SimplePortListener)1 AcceptHandler (de.janrufmonitor.service.server.request.handler.AcceptHandler)1 ApplicationReadyHandler (de.janrufmonitor.service.server.request.handler.ApplicationReadyHandler)1 ClearHandler (de.janrufmonitor.service.server.request.handler.ClearHandler)1 IdentifiedCallHandler (de.janrufmonitor.service.server.request.handler.IdentifiedCallHandler)1 IdentifiedOutgoingCallHandler (de.janrufmonitor.service.server.request.handler.IdentifiedOutgoingCallHandler)1 IncomingCallHandler (de.janrufmonitor.service.server.request.handler.IncomingCallHandler)1 OutgoingCallHandler (de.janrufmonitor.service.server.request.handler.OutgoingCallHandler)1 RejectedHandler (de.janrufmonitor.service.server.request.handler.RejectedHandler)1 ShutdownHandler (de.janrufmonitor.service.server.request.handler.ShutdownHandler)1 BufferedInputStream (java.io.BufferedInputStream)1