PrintStream ps = new PrintStream(res.getOutputStream()); ps.println("-----BEGIN CERTIFICATE-----"); ps.println(new String(b64cert)); ps.println("-----END CERTIFICATE-----"); ps.close();
ServletOutputStream os = res.getOutputStream(); os.write(beg.getBytes()); os.write(b64cert); os.write(end.getBytes()); res.flushBuffer();
    public void doGet(HttpServletRequest req,  HttpServletResponse res) throws java.io.IOException, ServletException {        cat.debug(">doGet()");        String command;        command = req.getParameter(COMMAND_PROPERTY_NAME);        if (command == null)            command = "";        // If we don't ask for any level of CA, assume root         String lev = req.getParameter(LEVEL_PROPERTY_NAME);        int level = 0;        if (lev != null)            level = Integer.parseInt(lev);        // Root CA is level 0, next below root level 1 etc etc        try {            ISignSessionRemote ss = home.create();            Certificate[] chain = ss.getCertificateChain();            // chain.length-1 is last cert in chain (root CA)            if ( (chain.length-1-level) < 0 ) {                PrintStream ps = new PrintStream(res.getOutputStream());                ps.println("No CA certificate of level "+level+"exist.");                cat.error("No CA certificate of level "+level+"exist.");                return;            }                            X509Certificate cacert = (X509Certificate)chain[chain.length-1-level];            byte[] enccert = cacert.getEncoded();            if (command.equalsIgnoreCase(COMMAND_NSCACERT)) {                    res.setContentType("application/x-x509-ca-cert");                    res.setContentLength(enccert.length);                    res.getOutputStream().write(enccert);                    cat.debug("Sent CA cert to NS client.");            } else if (command.equalsIgnoreCase(COMMAND_IECACERT)) {                    res.setHeader("Content-disposition", "attachment; filename=ca.crt");                    res.setContentType("application/octet-stream");                    res.setContentLength(enccert.length);                    res.getOutputStream().write(enccert);                    cat.debug("Sent CA cert to IE client.");            } else if (command.equalsIgnoreCase(COMMAND_CACERT)) {                    byte[] b64cert = Base64.encode(enccert);                    res.setHeader("Content-disposition", "attachment; filename=ca.pem");                    res.setContentType("application/octet-stream");                    res.setContentLength(b64cert.length+52);                    // Print the certificate                    PrintStream ps = new PrintStream(res.getOutputStream());                    ps.println("-----BEGIN CERTIFICATE-----");                    ps.println(new String(b64cert));                    ps.println("-----END CERTIFICATE-----");                    ps.close();                    cat.debug("Sent CA cert to client.");            } else {                res.setContentType("text/plain");                res.getOutputStream().println("Commands="+COMMAND_NSCACERT+" || "+COMMAND_IECACERT+" || "+COMMAND_CACERT);                return;            }        } catch (Exception e) {            PrintStream ps = new PrintStream(res.getOutputStream());            ps.println("Error sending CA certificate:");            e.printStackTrace(ps);            cat.error("Error sending CA cert.");            cat.debug(e);            return;        }    } // doGet