Author: jaz
Date: Mon May 28 14:57:01 2007 New Revision: 542332 URL: http://svn.apache.org/viewvc?view=rev&rev=542332 Log: added Apache XML-RPC client extension to support passing specific aliases certificates to RPC servers Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/xmlrpc/ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/xmlrpc/AliasSupportedTransportFactory.java (with props) Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/xmlrpc/AliasSupportedTransportFactory.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/xmlrpc/AliasSupportedTransportFactory.java?view=auto&rev=542332 ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/xmlrpc/AliasSupportedTransportFactory.java (added) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/xmlrpc/AliasSupportedTransportFactory.java Mon May 28 14:57:01 2007 @@ -0,0 +1,132 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +package org.ofbiz.webapp.xmlrpc; + +import org.apache.xmlrpc.client.*; +import org.apache.xmlrpc.XmlRpcRequest; +import org.apache.xmlrpc.XmlRpcException; +import org.apache.xmlrpc.util.HttpUtil; +import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig; +import org.ofbiz.base.util.SSLUtil; +import org.ofbiz.base.util.GeneralException; +import org.xml.sax.SAXException; + +import javax.net.ssl.HttpsURLConnection; +import java.security.KeyStore; +import java.security.GeneralSecurityException; +import java.net.URLConnection; +import java.net.URL; +import java.net.HttpURLConnection; +import java.io.IOException; +import java.io.InputStream; + +/** + * AliasSupportedTransportFactory + */ +public class AliasSupportedTransportFactory extends XmlRpcTransportFactoryImpl { + + private final AliasSupportedTransport transport; + + public AliasSupportedTransportFactory(XmlRpcClient client, KeyStore ks, String password, String alias) { + super(client); + transport = new AliasSupportedTransport(client, ks, password, alias); + } + + public XmlRpcTransport getTransport() { + return transport; + } + + class AliasSupportedTransport extends XmlRpcHttpTransport { + + protected static final String userAgent = USER_AGENT + " (Sun HTTP Transport)"; + private URLConnection con; + private String password; + private String alias; + private KeyStore ks; + + protected AliasSupportedTransport(XmlRpcClient client, KeyStore ks, String password, String alias) { + super(client, userAgent); + this.password = password; + this.alias = alias; + this.ks = ks; + } + + public Object sendRequest(XmlRpcRequest req) throws XmlRpcException { + XmlRpcHttpClientConfig config = (XmlRpcHttpClientConfig) req.getConfig(); + URL serverUrl = config.getServerURL(); + if (serverUrl == null) { + throw new XmlRpcException("Invalid server URL"); + } + + try { + con = openConnection(serverUrl); + con.setUseCaches(false); + con.setDoInput(true); + con.setDoOutput(true); + } catch (IOException e) { + throw new XmlRpcException("Failed to create URLConnection: " + e.getMessage(), e); + } + return super.sendRequest(req); + } + + protected URLConnection openConnection(URL url) throws IOException { + URLConnection con = url.openConnection(); + if ("HTTPS".equalsIgnoreCase(url.getProtocol())) { + HttpsURLConnection scon = (HttpsURLConnection) con; + try { + scon.setSSLSocketFactory(SSLUtil.getSSLSocketFactory(ks, password, alias)); + scon.setHostnameVerifier(SSLUtil.getHostnameVerifier(SSLUtil.HOSTCERT_MIN_CHECK)); + } catch (GeneralException e) { + throw new IOException(e.getMessage()); + } catch (GeneralSecurityException e) { + throw new IOException(e.getMessage()); + } + } + + return con; + } + + protected void setRequestHeader(String header, String value) { + con.setRequestProperty(header, value); + } + + protected void close() throws XmlRpcClientException { + if (con instanceof HttpURLConnection) { + ((HttpURLConnection) con).disconnect(); + } + } + + protected boolean isResponseGzipCompressed(XmlRpcStreamRequestConfig config) { + return HttpUtil.isUsingGzipEncoding(con.getHeaderField("Content-Encoding")); + } + + protected InputStream getInputStream() throws XmlRpcException { + try { + return con.getInputStream(); + } catch (IOException e) { + throw new XmlRpcException("Failed to create input stream: " + e.getMessage(), e); + } + } + + protected void writeRequest(ReqWriter pWriter) throws IOException, XmlRpcException, SAXException { + pWriter.write(con.getOutputStream()); + } + } +} Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/xmlrpc/AliasSupportedTransportFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/xmlrpc/AliasSupportedTransportFactory.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/xmlrpc/AliasSupportedTransportFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain |
Free forum by Nabble | Edit this page |