📄 requesthelper.java
字号:
break; } } out.println(); } // ieCertFormat /** * Reads template and inserts cert to send back to IE for installation of cert * * @param b64cert cert to be installed in IE-client * @param out utput stream to send to * @param sc serveltcontext * @param responseTemplate path to responseTemplate * @param classid replace * * @throws Exception on error */ public static void sendNewCertToIEClient(byte[] b64cert, OutputStream out, ServletContext sc, String responseTemplate, String classid) throws Exception { if (b64cert.length == 0) { log.error("0 length certificate can not be sent to IE client!"); return; } PrintStream ps = new PrintStream(out); BufferedReader br = new BufferedReader(new InputStreamReader(sc.getResourceAsStream( responseTemplate))); while (true) { String line = br.readLine(); if (line == null) { break; } if (line.indexOf("cert =") < 0) { ps.println(CLASSID.matcher(line).replaceFirst(classid)); } else { RequestHelper.ieCertFormat(b64cert, ps); } } ps.close(); log.debug("Sent reply to IE client"); log.debug(new String(b64cert)); } // sendNewCertToIEClient /** * Sends back cert to NS/Mozilla for installation of cert * * @param certs DER encoded certificates to be installed in browser * @param out output stream to send to * * @throws Exception on error */ public static void sendNewCertToNSClient(byte[] certs, HttpServletResponse out) throws Exception { if (certs.length == 0) { log.error("0 length certificate can not be sent to NS client!"); return; } // Set content-type to what NS wants out.setContentType("application/x-x509-user-cert"); out.setContentLength(certs.length); // Print the certificate out.getOutputStream().write(certs); log.debug("Sent reply to NS client"); log.debug(new String(Base64.encode(certs))); } // sendNewCertToNSClient /** * Sends back certificate as binary file (application/octet-stream) * * @param b64cert base64 encoded certificate to be returned * @param out output stream to send to * @param beginKey, String contaitning key information, ie BEGIN_CERTIFICATE_WITH_NL or BEGIN_PKCS7_WITH_NL * @param beginKey, String contaitning key information, ie END_CERTIFICATE_WITH_NL or END_PKCS7_WITH_NL * @throws Exception on error */ public static void sendNewB64Cert(byte[] b64cert, HttpServletResponse out, String beginKey, String endKey) throws Exception { if (b64cert.length == 0) { log.error("0 length certificate can not be sent to client!"); return; } // We must remove cache headers for IE ServletUtils.removeCacheHeaders(out); // Set content-type to general file out.setContentType("application/octet-stream"); out.setHeader("Content-disposition", "filename=cert.pem"); out.setContentLength(b64cert.length + beginKey.length() + endKey.length()); // Write the certificate ServletOutputStream os = out.getOutputStream(); os.write(beginKey.getBytes()); os.write(b64cert); os.write(endKey.getBytes()); out.flushBuffer(); log.debug("Sent reply to client"); log.debug(new String(b64cert)); } // sendNewB64Cert /** * Sends back CA-certificate as binary file (application/x-x509-ca-cert) * * @param cert DER encoded certificate to be returned * @param out output stream to send to * * @throws Exception on error */ public static void sendNewX509CaCert(byte[] cert, HttpServletResponse out) throws Exception { // Set content-type to CA-cert sendBinaryBytes(cert, out, "application/x-x509-ca-cert"); } // sendNewX509CaCert /** * Sends back a number of bytes * * @param bytes DER encoded certificate to be returned * @param out output stream to send to * @param contentType mime type to send back bytes as * * @throws Exception on error */ public static void sendBinaryBytes(byte[] bytes, HttpServletResponse out, String contentType) throws Exception { if (bytes.length == 0) { log.error("0 length can not be sent to client!"); return; } // Set content-type to general file out.setContentType(contentType); out.setContentLength(bytes.length); // Write the certificate ServletOutputStream os = out.getOutputStream(); os.write(bytes); out.flushBuffer(); log.debug("Sent " + bytes.length + " bytes to client"); } // sendBinaryBytes public static PKCS10RequestMessage genPKCS10RequestMessageFromPEM(byte[] b64Encoded){ byte[] buffer = null; try { // A real PKCS10 PEM request String beginKey = BEGIN_CERTIFICATE_REQUEST; String endKey = END_CERTIFICATE_REQUEST; buffer = FileTools.getBytesFromPEM(b64Encoded, beginKey, endKey); } catch (IOException e) { try { // Keytool PKCS10 PEM request String beginKey = "-----BEGIN NEW CERTIFICATE REQUEST-----"; String endKey = "-----END NEW CERTIFICATE REQUEST-----"; buffer = FileTools.getBytesFromPEM(b64Encoded, beginKey, endKey); } catch (IOException ioe) { // IE PKCS10 Base64 coded request buffer = Base64.decode(b64Encoded); } } if (buffer == null) { return null; } return new PKCS10RequestMessage(buffer); } // PKCS10RequestMessage /** Returns the default content encoding used in JSPs. Reads the env-entry contentEncoding from web.xml. * * @return The content encoding set in the webs env-entry java:comp/env/contentEncoding, or ISO-8859-1 (default), never returns null. */ public static String getDefaultContentEncoding() { String ret = null; try { ret = ServiceLocator.getInstance().getString("java:comp/env/contentEncoding"); } catch (ServiceLocatorException e) { log.debug("Can not find any default content encoding, using hard default ISO-8859-1."); ret = "ISO-8859-1"; } if (ret == null) { log.debug("Can not find any default content encoding, using hard default ISO-8859-1."); ret = "ISO-8859-1"; } return ret; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -