Example usage for com.fasterxml.jackson.core Base64Variants MIME

List of usage examples for com.fasterxml.jackson.core Base64Variants MIME

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core Base64Variants MIME.

Prototype

Base64Variant MIME

To view the source code for com.fasterxml.jackson.core Base64Variants MIME.

Click Source Link

Document

This variant is what most people would think of "the standard" Base64 encoding.

Usage

From source file:org.mitre.secretsharing.server.FormJoinServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Writer w = new HtmlXSSWriter(resp.getWriter());

    try {// w ww .ja  va2  s  .c o  m
        String parts = req.getParameter("parts");
        if (parts == null)
            throw new RuntimeException("No secret parts provided");

        boolean base64 = false;
        if (req.getParameter("base64") != null)
            base64 = Boolean.parseBoolean(req.getParameter("base64"));

        List<Part> partsBytes = new ArrayList<Part>();
        for (String s : parts.split("\n")) {
            s = s.trim();
            if (s.isEmpty())
                continue;
            try {
                partsBytes.add(PartFormats.parse(s));
            } catch (Exception e) {
                throw new RuntimeException("Corrupt key part \"" + s + "\""
                        + (e.getMessage() == null ? ": Improper encoding of secret parts"
                                : ": " + e.getMessage()),
                        e);
            }
        }

        Part[] p = partsBytes.toArray(new Part[0]);

        byte[] secret = p[0].join(Arrays.copyOfRange(p, 1, p.length));

        if (base64)
            w.write(Base64Variants.MIME.encode(secret));
        else
            w.write(new String(secret, "UTF-8"));
    } catch (Throwable t) {
        if (t.getMessage() != null)
            w.write("error: " + t.getMessage());
        else
            w.write("error");
    }
}

From source file:org.mitre.secretsharing.server.FormSplitServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    Writer w = new HtmlXSSWriter(resp.getWriter());

    try {//w ww . j a  va  2  s .c o  m
        String secret = req.getParameter("secret");
        if (secret == null)
            throw new RuntimeException("No secret parameter");
        int totalParts;
        try {
            totalParts = Integer.parseInt(req.getParameter("total_parts"));
            if (totalParts < 1)
                throw new RuntimeException();
        } catch (Exception e) {
            throw new RuntimeException("Total parts not an integer at least 1.");
        }
        int requiredParts;
        try {
            requiredParts = Integer.parseInt(req.getParameter("required_parts"));
            if (requiredParts < 1 || requiredParts > totalParts)
                throw new RuntimeException();
        } catch (Exception e) {
            throw new RuntimeException(
                    "Required parts not an integer at least 1 and not more than total parts.");
        }
        boolean base64 = false;
        if (req.getParameter("base64") != null)
            base64 = Boolean.parseBoolean(req.getParameter("base64"));

        byte[] secretBytes;

        if (base64) {
            try {
                secretBytes = Base64Variants.MIME.decode(secret);
            } catch (Exception e) {
                throw new RuntimeException("Improper encoding of base64 secret");
            }
        } else
            secretBytes = secret.getBytes("UTF-8");

        Part[] parts = Secrets.splitPerByte(secretBytes, totalParts, requiredParts, rnd);

        for (Part part : parts) {
            w.write(part + "\n");
        }
    } catch (Throwable t) {
        if (t.getMessage() != null)
            w.write(t.getMessage());
        else
            w.write("error");
    }
}