Search in sources :

Example 1 with GetImageHandler

use of de.janrufmonitor.service.client.request.handler.GetImageHandler 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 GetImageHandler

use of de.janrufmonitor.service.client.request.handler.GetImageHandler 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)

Aggregations

GetImageHandler (de.janrufmonitor.service.client.request.handler.GetImageHandler)2 IHttpRequest (de.janrufmonitor.service.commons.http.IHttpRequest)2 IRequester (de.janrufmonitor.service.commons.http.IRequester)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1