Search in sources :

Example 1 with Base64Encoder

use of de.janrufmonitor.util.io.Base64Encoder in project janrufmonitor by tbrandt77.

the class CompressBase64 method compressBase64Encode.

public static synchronized String compressBase64Encode(String raw) throws IOException {
    // create ByteArrayInputStream from String
    ByteArrayInputStream rawIn = new ByteArrayInputStream(raw.getBytes());
    // create a buffer for output
    ByteArrayOutputStream zippedOut = new ByteArrayOutputStream();
    ZipOutputStream out = new ZipOutputStream(zippedOut);
    out.setMethod(ZipOutputStream.DEFLATED);
    out.setLevel(Deflater.BEST_COMPRESSION);
    // write an data entry
    ZipEntry data = new ZipEntry("data");
    out.putNextEntry(data);
    transform(rawIn, out);
    out.closeEntry();
    out.close();
    // System.out.println(zippedOut.toString());
    // create a base64 encoded string
    ByteArrayInputStream zippedIn = new ByteArrayInputStream(zippedOut.toByteArray());
    ByteArrayOutputStream encodedOut = new ByteArrayOutputStream();
    Base64Encoder b64 = new Base64Encoder(encodedOut);
    transform(zippedIn, b64);
    b64.write(13);
    b64.flush();
    return new String(encodedOut.toByteArray());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Base64Encoder(de.janrufmonitor.util.io.Base64Encoder)

Example 2 with Base64Encoder

use of de.janrufmonitor.util.io.Base64Encoder in project janrufmonitor by tbrandt77.

the class TR064FritzBoxFirmware method doHttpCall.

private StringBuffer doHttpCall(String u, String method, String body, String[][] headers, String user, String pw, boolean isBase64Encoded) throws IOException {
    if (this.m_logger.isLoggable(Level.INFO))
        this.m_logger.info("HTTP call: " + u + ", method " + method);
    URL url = new URL(u);
    URLConnection connection = null;
    if (url.getProtocol().equalsIgnoreCase("https")) {
        connection = (HttpsURLConnection) url.openConnection();
        ((HttpsURLConnection) connection).setRequestMethod(method);
    } else {
        connection = (HttpURLConnection) url.openConnection();
        ((HttpURLConnection) connection).setRequestMethod(method);
    }
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    if (this.m_logger.isLoggable(Level.INFO))
        this.m_logger.info("HTTP headers...");
    for (int i = 0; i < headers.length; i++) {
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info(headers[i][0] + ": " + headers[i][1]);
        connection.setRequestProperty(headers[i][0], headers[i][1]);
    }
    if (user != null && pw != null) {
        connection.setRequestProperty("Authorization", "Basic " + Base64Encoder.encode(user + ":" + pw));
    }
    if (body != null) {
        if (this.m_logger.isLoggable(Level.INFO)) {
            this.m_logger.info("HTTP body...");
            this.m_logger.info(body);
        }
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(body);
        writer.flush();
        writer.close();
    }
    StringBuffer response = new StringBuffer();
    if (!isBase64Encoded) {
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info("HTTP response is plain/text.");
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        for (String line; (line = reader.readLine()) != null; ) {
            response.append(line);
            response.append(IJAMConst.CRLF);
        }
        reader.close();
    } else {
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info("HTTP response is base64 encoded.");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Base64Encoder b64 = new Base64Encoder(bos);
        Stream.copy(new BufferedInputStream(connection.getInputStream()), b64);
        b64.flush();
        b64.close();
        response.append(new String(bos.toByteArray()));
    }
    if (this.m_logger.isLoggable(Level.INFO)) {
        this.m_logger.info("HTTP response...");
        this.m_logger.info(response.toString());
    }
    return response;
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Base64Encoder(de.janrufmonitor.util.io.Base64Encoder) HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 3 with Base64Encoder

use of de.janrufmonitor.util.io.Base64Encoder in project janrufmonitor by tbrandt77.

the class HsqldbMultiPhoneCallerDatabaseHandler method internalUpdate.

protected void internalUpdate(ICaller c) throws SQLException {
    // check 1) image already exists and gets exchanged
    if (internalHasImage(c)) {
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info("Caller " + c.toString() + " has already an image. Updating image.");
        PreparedStatement ps = this.getStatement("UPDATE_IMAGE");
        if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH)) {
            if (this.m_logger.isLoggable(Level.INFO))
                this.m_logger.info("Updating image from ATTRIBUTE_NAME_IMAGEPATH.");
            String imgpath = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH).getValue();
            File img = new File(PathResolver.getInstance(getRuntime()).resolve(imgpath));
            if (img.exists()) {
                if (this.m_logger.isLoggable(Level.INFO))
                    this.m_logger.info("Updating image from ATTRIBUTE_NAME_IMAGEPATH: " + img.getAbsolutePath());
                try {
                    FileInputStream fim = new FileInputStream(img);
                    ByteArrayOutputStream encodedOut = new ByteArrayOutputStream();
                    Base64Encoder b64 = new Base64Encoder(encodedOut);
                    Stream.copy(fim, b64);
                    b64.flush();
                    b64.close();
                    IAttribute img_ref = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEREF);
                    String uuid = null;
                    if (img_ref != null && img_ref.getValue().length() > 0) {
                        uuid = img_ref.getValue();
                    } else
                        uuid = c.getUUID();
                    updateImage(ps, uuid, encodedOut.toByteArray());
                    if (uuid != null)
                        c.getAttributes().add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEREF, uuid));
                } catch (FileNotFoundException e) {
                    this.m_logger.log(Level.SEVERE, e.getMessage(), e);
                } catch (IOException e) {
                    this.m_logger.log(Level.SEVERE, e.getMessage(), e);
                }
                c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH);
            } else {
                // file does not exist so remove image
                internalDelete(c);
                c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEREF);
                c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH);
            }
        } else if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY)) {
            if (this.m_logger.isLoggable(Level.INFO))
                this.m_logger.info("Updating image from ATTRIBUTE_NAME_IMAGEBINARY.");
            String imgdata = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY).getValue();
            IAttribute img_ref = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEREF);
            String uuid = null;
            if (img_ref != null && img_ref.getValue().length() > 0) {
                uuid = img_ref.getValue();
            } else
                uuid = c.getUUID();
            updateImage(ps, uuid, imgdata.getBytes());
            if (uuid != null)
                c.getAttributes().add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEREF, uuid));
            c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY);
        } else if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEURL)) {
            if (this.m_logger.isLoggable(Level.INFO))
                this.m_logger.info("Updating image from ATTRIBUTE_NAME_IMAGEURL.");
            // TODO update from URL
            c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEURL);
        }
        ps.executeBatch();
    } else // check 2) image does not exist and has to be insert
    if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH) || c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEURL) || c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY)) {
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info("Caller " + c.toString() + " has not yet an image. Inserting image.");
        PreparedStatement ps = this.getStatement("INSERT_IMAGE");
        if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH)) {
            String imgpath = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH).getValue();
            File img = new File(PathResolver.getInstance(getRuntime()).resolve(imgpath));
            if (img.exists()) {
                if (this.m_logger.isLoggable(Level.INFO))
                    this.m_logger.info("Inserting image from ATTRIBUTE_NAME_IMAGEPATH: " + img.getAbsolutePath());
                try {
                    FileInputStream fim = new FileInputStream(img);
                    ByteArrayOutputStream encodedOut = new ByteArrayOutputStream();
                    Base64Encoder b64 = new Base64Encoder(encodedOut);
                    Stream.copy(fim, b64);
                    b64.flush();
                    b64.close();
                    createImage(ps, c.getUUID(), encodedOut.toByteArray());
                    c.getAttributes().add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEREF, c.getUUID()));
                } catch (FileNotFoundException e) {
                    this.m_logger.log(Level.SEVERE, e.getMessage(), e);
                } catch (IOException e) {
                    this.m_logger.log(Level.SEVERE, e.getMessage(), e);
                }
            }
            c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH);
        } else if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY)) {
            if (this.m_logger.isLoggable(Level.INFO))
                this.m_logger.info("Inserting image from ATTRIBUTE_NAME_IMAGEBINARY.");
            String imgdata = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY).getValue();
            createImage(ps, c.getUUID(), imgdata.getBytes());
            c.getAttributes().add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEREF, c.getUUID()));
            c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY);
        } else if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEURL)) {
            if (this.m_logger.isLoggable(Level.INFO))
                this.m_logger.info("Inserting image from ATTRIBUTE_NAME_IMAGEURL.");
            // TODO insert from URL
            c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEURL);
        }
        ps.executeBatch();
    } else // check 3) image gets deleted
    {
        internalDelete(c);
        c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEREF);
    }
}
Also used : IAttribute(de.janrufmonitor.framework.IAttribute) FileNotFoundException(java.io.FileNotFoundException) PreparedStatement(java.sql.PreparedStatement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) Base64Encoder(de.janrufmonitor.util.io.Base64Encoder)

Example 4 with Base64Encoder

use of de.janrufmonitor.util.io.Base64Encoder in project janrufmonitor by tbrandt77.

the class ImageBase64 method renderAsText.

public String renderAsText() {
    if (this.m_o != null) {
        if (this.m_o instanceof ICall) {
            this.m_o = ((ICall) this.m_o).getCaller();
        }
        if (this.m_o instanceof ICaller) {
            if (ImageHandler.getInstance().hasImage((ICaller) this.m_o)) {
                try {
                    InputStream fim = ImageHandler.getInstance().getImageStream((ICaller) this.m_o);
                    ByteArrayOutputStream encodedOut = new ByteArrayOutputStream();
                    Base64Encoder b64 = new Base64Encoder(encodedOut);
                    Stream.copy(fim, b64);
                    b64.flush();
                    b64.close();
                    return new String(encodedOut.toByteArray());
                } catch (IOException e) {
                }
            }
        }
    }
    return "";
}
Also used : ICaller(de.janrufmonitor.framework.ICaller) ICall(de.janrufmonitor.framework.ICall) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Base64Encoder(de.janrufmonitor.util.io.Base64Encoder)

Example 5 with Base64Encoder

use of de.janrufmonitor.util.io.Base64Encoder in project janrufmonitor by tbrandt77.

the class JournalBuilder method parseCallerImage.

/**
 * Introduced for console version of html callmanager. Since no renderer are available on console,
 * a mechanism is needed for rendering caller images.
 *
 * @param text
 * @param call
 * @return
 * @throws IOException
 */
private static String parseCallerImage(String text, ICall call) throws IOException {
    String prefix = "<!-- start_caller_image:";
    String postfix = ":end_caller_image-->";
    while (text.indexOf(prefix) >= 0) {
        String token = text.substring(text.indexOf(prefix) + prefix.length(), text.indexOf(postfix));
        String[] elements = token.split(";");
        if (elements.length > 2) {
            continue;
        }
        ICaller c = call.getCaller();
        if (ImageHandler.getInstance().hasImage(c)) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Base64Encoder b64 = new Base64Encoder(bos);
            InputStream fin = ImageHandler.getInstance().getImageStream(c);
            Stream.copy(new BufferedInputStream(fin), b64, true);
            text = StringUtils.replaceString(text, prefix + token + postfix, "<img src=\"data:image/jpeg;base64," + bos.toString() + "\" " + (elements.length == 2 ? "width=\"" + elements[0] + "\" height=\"" + elements[1] + "\" " : "") + "/>");
        } else {
            text = StringUtils.replaceString(text, prefix + token + postfix, "");
        }
    }
    return text;
}
Also used : ICaller(de.janrufmonitor.framework.ICaller) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Base64Encoder(de.janrufmonitor.util.io.Base64Encoder)

Aggregations

Base64Encoder (de.janrufmonitor.util.io.Base64Encoder)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 FileInputStream (java.io.FileInputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 File (java.io.File)5 InputStream (java.io.InputStream)4 ICaller (de.janrufmonitor.framework.ICaller)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 FileNotFoundException (java.io.FileNotFoundException)2 IAttribute (de.janrufmonitor.framework.IAttribute)1 ICall (de.janrufmonitor.framework.ICall)1 ICallerList (de.janrufmonitor.framework.ICallerList)1 IMultiPhoneCaller (de.janrufmonitor.framework.IMultiPhoneCaller)1 IPhonenumber (de.janrufmonitor.framework.IPhonenumber)1 Formatter (de.janrufmonitor.util.formatter.Formatter)1 BufferedReader (java.io.BufferedReader)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1