只是为了繁荣,因为这将永远存在于“互联网”上,这是我问题的最终解决方案:
首先,我返回我引用的“HTML 模糊”的原因是因为我指向的 URL 正在重定向到另一台服务器。
一旦我弄清楚并指向“真实”服务器,我就开始收到 SSL 证书信任错误。在这种情况下,服务器有一个内部自签名证书。我尝试在我的 PeopleSoft Web 服务器和 PeopleSoft 的数字证书库中简单地安装该证书,但无济于事。然后我继续执行 B 计划:强制我的 PeopleCode/Java 混合使用证书本身。
在大多数情况下,这篇文章帮助我弄清楚我需要做什么。一般来说,这些是我采取的步骤:
- 基于 PEM 格式的证书字符串创建 CertificateFactory
- 从证书工厂获取证书对象(generateCertificate 方法)
- 使用证书(setCertificateEntry 方法)创建 Keystore 对象。
- 创建一个TrustManagerFactory对象,用Keystore对象初始化(init方法,传入Keystore对象)
- 创建一个 SSLContext 对象,用 TrustManagerFactory 对象初始化(init 方法,传入 TrustManagerFactory.getTrustManagers())
- 从 SSLContext 对象中获取一个 SSLSocketFactory 对象(getSocketFactory 方法)
- 创建 URL 对象,指向 ASHX 处理程序
- 从 URL 对象中获取 URLConnection 对象(openConnection 方法)
- 将该 URLConnection 对象转换为 HttpsURLConnection 对象。这可能看起来很普通,但要在 PeopleCode 中做到这一点,它需要创建一个自定义强制转换类和静态方法,因此我在这里引用它(这点归功于Jim 的 PeopleSoft Journal)。
- 从 HttpsURLConnection 获取图片 InputStream 对象(getInputStream 方法)
- 使用 ImageIO 类的 read 方法从 InputStream 创建一个 BufferedImage 对象(传入 InputStream)
- 使用 ImageIO 写入方法将该 BufferedImage 对象写入 File 对象。
- 按照此博文中的一般步骤,将该文件写入 PeopleSoft 的 EMPL_PHOTO 记录。
希望将来有人会发现这很有帮助。这是完整的 PeopleCode 供参考(请注意,这只是我绑定到页面的 FieldChange 事件按钮的测试代码):
Local JavaObject &joCertString, &joUrl, &joUrlConnection, &joInputStream, &joImageIO, &joBufferedImage, &joFile;
Local JavaObject &joCertificateFactoryClass, &joCertificateFactory, &joCertInputStream, &joCertificate;
Local JavaObject &joKeystoreClass, &joKeystore, &joTrustManagerFactoryClass, &joTrustManagerFactory;
Local JavaObject &joSSLContextClass, &joSSLContext, &joSSLFactory, &joCastObjectClass;
Local any &anyFileData;
Local Record &recTest;
Local string &sPhotosCert;
Local number &nPutResult;
/*Setup CertificateFactory*/
&sPhotosCert = GetHTMLText(HTML.PHOTOS_CERT);
&joCertString = CreateJavaObject("java.lang.String", &sPhotosCert);
&joCertificateFactoryClass = GetJavaClass("java.security.cert.CertificateFactory");
&joCertificateFactory = &joCertificateFactoryClass.getInstance("X.509");
&joCertInputStream = CreateJavaObject("java.io.ByteArrayInputStream", &joCertString.getBytes("UTF-8"));
&joCertificate = &joCertificateFactory.generateCertificate(&joCertInputStream);
/*Create Keystore*/
&joKeystoreClass = GetJavaClass("java.security.KeyStore");
&joKeystore = &joKeystoreClass.getInstance("JKS");
&joKeystore.load( Null, Null);
&joKeystore.setCertificateEntry("photos", &joCertificate);
/*Create TrustManagerFactory*/
&joTrustManagerFactoryClass = GetJavaClass("javax.net.ssl.TrustManagerFactory");
&joTrustManagerFactory = &joTrustManagerFactoryClass.getInstance(&joTrustManagerFactoryClass.getDefaultAlgorithm());
&joTrustManagerFactory.init(&joKeystore);
/*Create SSLContext and get SSLSocketFactory*/
&joSSLContextClass = GetJavaClass("javax.net.ssl.SSLContext");
&joSSLContext = &joSSLContextClass.getInstance("TLS");
&joSSLContext.init( Null, &joTrustManagerFactory.getTrustManagers(), Null);
&joSSLFactory = &joSSLContext.getSocketFactory();
/*Create URL*/
&joUrl = CreateJavaObject("java.net.URL", "https://domain.com/handler.ashx?id=123456");
/*Get URLConnection*/
&joUrlConnection = &joUrl.openConnection();
/*Cast to HttpsURLConnection*/
&joCastObjectClass = GetJavaClass("com.domain.CastObject");
&joUrlConnection = &joCastObjectClass.objectToHttpsURLConnection(&joUrlConnection);
/*Set connection properties*/
&joUrlConnection.setSSLSocketFactory(&joSSLFactory);
&joUrlConnection.setRequestMethod("GET");
/*Get InputStream*/
&joInputStream = GetJavaClass("java.io.InputStream");
&joInputStream = &joUrlConnection.getInputStream();
/*Get Image From Stream*/
&joImageIO = GetJavaClass("javax.imageio.ImageIO");
&joBufferedImage = CreateJavaObject("java.awt.image.BufferedImage", 1, 1, 1);
&joBufferedImage = &joImageIO.read(&joInputStream);
/*Write Image to File*/
&joFile = CreateJavaObject("java.io.File", "/temp/URLImage2.jpg");
&joImageIO.write(&joBufferedImage, "jpg", &joFile);
/*Put the File into a PS record so we can get a BLOB of it*/
&nPutResult = PutAttachment("record://PSFILE_ATTDET", "013505.jpg", "/temp/URLImage2.jpg");
/*Grab that BLOB*/
SQLExec("SELECT FILE_DATA FROM PSFILE_ATTDET WHERE ATTACHSYSFILENAME = :1 AND VERSION = 1", "013505.jpg", &anyFileData);
/*Clean up the temp row we inserted*/
SQLExec("DELETE FROM PSFILE_ATTDET WHERE ATTACHSYSFILENAME = :1 AND VERSION = 1", "013505.jpg");
/*Setup an employee photo record*/
&recTest = CreateRecord(Record.EMPL_PHOTO);
&recTest.EMPLID.Value = "013505";
&recTest.PSIMAGEVER.Value = (Days365(Date3(1999, 12, 31), %Date) * 86400) + (%Time - Time3(0, 0, 0));
&recTest.EMPLOYEE_PHOTO.Value = &anyFileData;
/*Insert/Update the employee photo record*/
&recTest.Insert();
&recTest.Update();