|
| 1 | +/* |
| 2 | + * Copyright 2011-2012, 2014 De Bortoli Wines Pty Limited (Australia) |
| 3 | + * |
| 4 | + * This file is part of OdooJavaAPI. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +package com.debortoliwines.odoo.api; |
| 21 | + |
| 22 | +import java.net.InetSocketAddress; |
| 23 | +import java.net.MalformedURLException; |
| 24 | +import java.net.Proxy; |
| 25 | +import java.net.URL; |
| 26 | + |
| 27 | +import org.apache.xmlrpc.XmlRpcException; |
| 28 | +import org.apache.xmlrpc.client.XmlRpcClient; |
| 29 | +import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; |
| 30 | +import org.apache.xmlrpc.client.XmlRpcSun15HttpTransportFactory; |
| 31 | +import org.apache.xmlrpc.client.XmlRpcTransportFactory; |
| 32 | + |
| 33 | +/** |
| 34 | + * An XMLRRPC Client that connects to Odoo |
| 35 | + * |
| 36 | + * @author Pieter van der Merwe |
| 37 | + * @author fpoyer |
| 38 | + */ |
| 39 | +public class OdooXmlRpcProxy extends XmlRpcClient { |
| 40 | + |
| 41 | + /** |
| 42 | + * Enum for the main RPC services that Odoo expose |
| 43 | + */ |
| 44 | + public enum RPCServices { |
| 45 | + RPC_COMMON, |
| 46 | + RPC_OBJECT, |
| 47 | + RPC_DATABASE |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Enum for the RPC protocol used to connect to Odoo |
| 52 | + */ |
| 53 | + public enum RPCProtocol { |
| 54 | + RPC_HTTP, |
| 55 | + RPC_HTTPS |
| 56 | + } |
| 57 | + |
| 58 | + private final String RPC_COMMON_URL = "/xmlrpc/2/common"; |
| 59 | + private final String RPC_OBJECT_URL = "/xmlrpc/2/object"; |
| 60 | + private final String RPC_DATABASE_URL = "/xmlrpc/2/db"; |
| 61 | + |
| 62 | + /** |
| 63 | + * Proxy object to handle calls to and from the Odoo server |
| 64 | + * |
| 65 | + * @param protocol |
| 66 | + * Protocol to use when connecting to the RPC service ex. |
| 67 | + * http/https |
| 68 | + * @param host |
| 69 | + * Host name or IP address where the Odoo server is hosted |
| 70 | + * @param port |
| 71 | + * XML-RPC port number to connect to. Typically 8069. |
| 72 | + * @param service |
| 73 | + * Odoo webservice to call (db, common or object) |
| 74 | + */ |
| 75 | + public OdooXmlRpcProxy(RPCProtocol protocol, String host, int port, RPCServices service) { |
| 76 | + super(); |
| 77 | + |
| 78 | + String URL = ""; |
| 79 | + |
| 80 | + switch (service) { |
| 81 | + case RPC_COMMON: |
| 82 | + URL = this.RPC_COMMON_URL; |
| 83 | + break; |
| 84 | + case RPC_OBJECT: |
| 85 | + URL = this.RPC_OBJECT_URL; |
| 86 | + break; |
| 87 | + case RPC_DATABASE: |
| 88 | + URL = this.RPC_DATABASE_URL; |
| 89 | + break; |
| 90 | + } |
| 91 | + |
| 92 | + String protocol_str = ""; |
| 93 | + switch (protocol) { |
| 94 | + case RPC_HTTP: |
| 95 | + protocol_str = "http"; |
| 96 | + break; |
| 97 | + |
| 98 | + default: |
| 99 | + protocol_str = "https"; |
| 100 | + break; |
| 101 | + } |
| 102 | + |
| 103 | + useProxyIfAvailable(protocol); |
| 104 | + |
| 105 | + XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl(); |
| 106 | + |
| 107 | + // Odoo does not support extensions |
| 108 | + xmlrpcConfigLogin.setEnabledForExtensions(false); |
| 109 | + |
| 110 | + // The URL is hardcoded and can not be malformed |
| 111 | + try { |
| 112 | + xmlrpcConfigLogin.setServerURL(new URL(protocol_str, host, port, URL)); |
| 113 | + } catch (MalformedURLException e) { |
| 114 | + } |
| 115 | + |
| 116 | + this.setConfig(xmlrpcConfigLogin); |
| 117 | + } |
| 118 | + |
| 119 | + void useProxyIfAvailable(RPCProtocol protocol) { |
| 120 | + // If a proxy is defined, use it: |
| 121 | + XmlRpcTransportFactory factory = this.getTransportFactory(); |
| 122 | + if (factory != null && factory instanceof XmlRpcSun15HttpTransportFactory) { |
| 123 | + if (protocol == RPCProtocol.RPC_HTTP) { |
| 124 | + String proxyHost = System.getProperty("http.proxyHost"); |
| 125 | + String proxyPortString = System.getProperty("http.proxyPort"); |
| 126 | + if (proxyHost != null && !proxyHost.isEmpty()) { |
| 127 | + int proxyPort = 80; |
| 128 | + if (proxyPortString != null && !proxyPortString.isEmpty()) { |
| 129 | + try { |
| 130 | + proxyPort = Integer.parseInt(proxyPortString); |
| 131 | + } catch (NumberFormatException e) { |
| 132 | + // Port badly defined, keep the default |
| 133 | + } |
| 134 | + } |
| 135 | + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); |
| 136 | + ((XmlRpcSun15HttpTransportFactory) factory).setProxy(proxy); |
| 137 | + } |
| 138 | + } else { |
| 139 | + String proxyHost = System.getProperty("https.proxyHost"); |
| 140 | + if (proxyHost == null || proxyHost.isEmpty()) { |
| 141 | + proxyHost = System.getProperty("http.proxyHost"); |
| 142 | + } |
| 143 | + String proxyPortString = System.getProperty("https.proxyPort"); |
| 144 | + if (proxyPortString == null || proxyPortString.isEmpty()) { |
| 145 | + proxyPortString = System.getProperty("http.proxyPort"); |
| 146 | + } |
| 147 | + |
| 148 | + if (proxyHost != null && !proxyHost.isEmpty()) { |
| 149 | + int proxyPort = 443; |
| 150 | + if (proxyPortString != null && !proxyPortString.isEmpty()) { |
| 151 | + try { |
| 152 | + proxyPort = Integer.parseInt(proxyPortString); |
| 153 | + } catch (NumberFormatException e) { |
| 154 | + // Port badly defined, keep the default |
| 155 | + } |
| 156 | + } |
| 157 | + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); |
| 158 | + ((XmlRpcSun15HttpTransportFactory) factory).setProxy(proxy); |
| 159 | + } |
| 160 | + } |
| 161 | + } else { |
| 162 | + System.err.println("No transport factory or not compatible with Proxy support!"); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /*** |
| 167 | + * Returns the Odoo server version. For example 7.0-20130216-002451 or 6.1-1 |
| 168 | + * |
| 169 | + * @param protocol |
| 170 | + * Protocol to use when connecting to the RPC service ex. |
| 171 | + * http/https |
| 172 | + * @param host |
| 173 | + * Host name or IP address where the Odoo server is hosted |
| 174 | + * @param port |
| 175 | + * XML-RPC port number to connect to |
| 176 | + * @return The version number as a String |
| 177 | + * @throws XmlRpcException |
| 178 | + */ |
| 179 | + public static Version getServerVersion(RPCProtocol protocol, String host, int port) throws XmlRpcException |
| 180 | + { |
| 181 | + OdooXmlRpcProxy client = new OdooXmlRpcProxy(protocol, host, port, RPCServices.RPC_DATABASE); |
| 182 | + |
| 183 | + return new Version(client.execute("server_version", new Object[] {}).toString()); |
| 184 | + } |
| 185 | +} |
0 commit comments