📄 requesthelper.java
字号:
is = sc.getResourceAsStream("/"+responseTemplate); } if (is == null) { throw new IOException("Template '(/)"+responseTemplate+"' can not be found or read."); } BufferedReader br = new BufferedReader(new InputStreamReader(is)); String baseURL = request.getRequestURL().toString().substring(0, request.getRequestURL().toString().lastIndexOf( request.getRequestURI().toString()) ) + request.getContextPath() + "/"; // If we would like to parse the jsp stuff instead so we could use "include" etc, we could use the below code // unfortunately if we are using https this will not work correctly, because we can not make a https connection here. /* String responseURL = baseURL + responseTemplate; BufferedReader br = new BufferedReader(new InputStreamReader( (new URL(responseURL)).openStream() )); */ PrintWriter pw = new PrintWriter(sw); while (true) { String line = br.readLine(); if (line == null) { break; } line = line.replaceAll("\\x2E\\x2E/", baseURL); // This line should be removed when headers are properly configured with absolute paths line = line.replaceAll("TAG_cert",new String(certificate)); line = CLASSID.matcher(line).replaceFirst(classid); pw.println(line); } pw.close(); sw.flush(); } PrintWriter pw = new PrintWriter(out); if (log.isDebugEnabled()) { log.debug(sw); } pw.print(sw); pw.close(); out.flush(); log.debug("<sendNewCertToIidClient"); } // sendCertificates /** * 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); log.debug("Response template is: "+responseTemplate); InputStream is = sc.getResourceAsStream(responseTemplate); if (is == null) { // Some app servers (oracle) require a / first... log.debug("Trying to read responseTemplate with / first"); is = sc.getResourceAsStream("/"+responseTemplate); } if (is == null) { throw new IOException("Template '(/)"+responseTemplate+"' can not be found or read."); } BufferedReader br = new BufferedReader(new InputStreamReader(is)); 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 { log.debug(">nsCertRequest"); 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))); log.debug("<nsCertRequest"); } // 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 filename filename sent as 'Content-disposition' header * @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 IOException * @throws Exception on error */ public static void sendNewB64File(byte[] b64cert, HttpServletResponse out, String filename, String beginKey, String endKey) throws IOException { 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=\""+filename+"\""); 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)); } /** * 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 IOException { RequestHelper.sendNewB64File(b64cert, out, "cert.pem", beginKey, endKey); } // 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", null); } // 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 * @param fileName to call the file in a Content-disposition, can be null to leave out this header * * @throws Exception on error */ public static void sendBinaryBytes(byte[] bytes, HttpServletResponse out, String contentType, String filename) throws Exception { if ( (bytes == null) || (bytes.length == 0) ) { log.error("0 length can not be sent to client!"); return; } if (filename != null) { // We must remove cache headers for IE ServletUtils.removeCacheHeaders(out); out.setHeader("Content-disposition", "filename=\""+filename+"\""); } // 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 /** 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; } /** Sets the default character encoding for decoding post and get parameters. * First tries to get the character encoding from the request, if the browser is so kind to tell us which it is using, which it never does... * Otherwise, when the browser is silent, it sets the character encoding to the same encoding that we use to display the pages. * * @param request HttpServletRequest * @throws UnsupportedEncodingException * */ public static void setDefaultCharacterEncoding(HttpServletRequest request) throws UnsupportedEncodingException { String encoding = request.getCharacterEncoding(); if(StringUtils.isEmpty(encoding)) { encoding = RequestHelper.getDefaultContentEncoding(); log.debug("Setting encoding to default value: "+encoding); request.setCharacterEncoding(encoding); } else { log.debug("Setting encoding to value from request: "+encoding); request.setCharacterEncoding(encoding); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -