org.ow2.petals.binding.soap.util.SUPropertiesHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of petals-bc-soap Show documentation
Show all versions of petals-bc-soap Show documentation
The PEtALS SOAP JBI binding component based on Axis2 and Jetty.
The newest version!
/**
* Copyright (c) 2007-2012 EBM WebSourcing, 2012-2023 Linagora
*
* This program/library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or (at your
* option) any later version.
*
* This program/library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program/library; If not, see http://www.gnu.org/licenses/
* for the GNU Lesser General Public License version 2.1.
*/
package org.ow2.petals.binding.soap.util;
import static org.ow2.petals.binding.soap.SoapConstants.SOAP.SOAP_VERSION_12;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.ADDRESS;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.BASIC_AUTH_PASSWORD_PARAM;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.BASIC_AUTH_USERNAME;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.CHUNKED_MODE;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.COMPATIBILITY;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.ENABLE_HTTPS_TRANSPORT;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.ENABLE_HTTP_TRANSPORT;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.ENABLE_JMS_TRANSPORT;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.ENABLE_WSA;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.HEADERS_FILTER;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.HEADERS_TO_INJECT;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.HTTP_SERVICES_REDIRECTION;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.INJECT_HEADERS;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.MODULES;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.PROXY_DOMAIN;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.PROXY_HOST;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.PROXY_PASSWORD_PARAM;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.PROXY_PORT;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.PROXY_USER;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.SERVICE_NAME;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.SERVICE_PARAMETERS;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.SOAP_ACTION;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.SOAP_VERSION;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.WSS_POLICY_FILE;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.COMPATIBILITY.AXIS1;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.HTTPS.KEYSTORE_FILE;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.HTTPS.KEYSTORE_PASSWORD_PARAM;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.HTTPS.TRUSTSTORE_FILE;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.HTTPS.TRUSTSTORE_PASSWORD_PARAM;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.WSA.FAULT_TO;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.WSA.FROM;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.WSA.REPLY_TO;
import static org.ow2.petals.binding.soap.SoapConstants.ServiceUnit.WSA.TO;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMXMLBuilderFactory;
import org.apache.axiom.om.OMXMLParserWrapper;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axis2.Constants;
import org.apache.axis2.client.Options;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.apache.axis2.transport.http.impl.httpclient4.HttpTransportPropertiesImpl;
import org.apache.neethi.Policy;
import org.apache.neethi.PolicyEngine;
import org.ow2.petals.component.framework.api.configuration.SuConfigurationParameters;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
/**
* Helper for Service Unit properties values.
*
* @author chamerling - EBM WebSourcing
*/
public class SUPropertiesHelper {
/**
* Check if the given parameter is present
*
* @param parameterName
* the parameter name
* @param extensions
* the SU extensions
* @return
*/
protected static final boolean checkPresent(final String parameterName, final SuConfigurationParameters extensions) {
final String tmp = extensions.get(parameterName);
return tmp != null && tmp.trim().length() > 0;
}
/**
* Get the address
*
* @return the address
* @deprecated use {@link #getWSATo(SuConfigurationParameters)} instead
*/
@Deprecated
public static String getAddress(final SuConfigurationParameters extensions) {
return getString(ADDRESS, extensions);
}
/**
* Get the boolean value of the parameter
*
* @param parameterName
* the parameter name
* @param extensions
* the SU extensions
* @return the boolean value of the parameter according to the Boolean.parseBoolean method, true if the parameter
* has not been found.
*/
protected static final boolean getBoolean(final String parameterName, final SuConfigurationParameters extensions) {
boolean result = true;
final String tmp = extensions.get(parameterName);
if (tmp != null) {
result = Boolean.parseBoolean(tmp.trim());
}
return result;
}
protected static final boolean getBoolean(final String parameterName, final SuConfigurationParameters extensions,
final boolean defaultValue) {
boolean result = true;
final String tmp = extensions.get(parameterName);
if (tmp == null) {
result = defaultValue;
} else {
result = Boolean.parseBoolean(tmp.trim());
}
return result;
}
public static final String getHttpRedirection(final SuConfigurationParameters extensions) {
return getString(HTTP_SERVICES_REDIRECTION, extensions);
}
/**
* Get the service name. This name will be used to create the web service
*
* @param extensions
* the SU extensions
* @return
*/
public static String getServiceName(final SuConfigurationParameters extensions) {
return getString(SERVICE_NAME, extensions);
}
/**
* Get the parameter value or return null if not found
*
* @param parameterName
* @param extensions
* the SU extensions
* @return
*/
protected static final String getString(final String parameterName, final SuConfigurationParameters extensions) {
String result = extensions.get(parameterName);
if (result != null) {
result = result.trim();
}
return result;
}
/**
* @param parameterName
* @param defaultValue
* @param extensions
* the SU extensions
* @return
*/
protected static final String getString(final String parameterName, final String defaultValue,
final SuConfigurationParameters extensions) {
String result = getString(parameterName, extensions);
if (result == null) {
result = defaultValue;
}
return result;
}
/**
* @param extensions
* the SU extensions
* @return
*/
public static String getWSAFaultTo(final SuConfigurationParameters extensions) {
return getString(FAULT_TO, extensions);
}
/**
* @param extensions
* the SU extensions
* @return
*/
public static String getWSAFrom(final SuConfigurationParameters extensions) {
return getString(FROM, extensions);
}
/**
* @param extensions
* the SU extensions
* @return
*/
public static String getWSAReplyTo(final SuConfigurationParameters extensions) {
return getString(REPLY_TO, extensions);
}
/**
* Get the wsa-to
*
* @return the wsa-to
*/
public static String getWSATo(final SuConfigurationParameters extensions) {
return getString(TO, extensions);
}
/**
* Get the flag enabling the HTTP transport layer
*
* @param extensions
* the SU extensions
* @return
*/
public static boolean isHttpsTransportEnabled(final SuConfigurationParameters extensions) {
return getBoolean(ENABLE_HTTPS_TRANSPORT, extensions, false);
}
/**
* Get the flag enabling the HTTP transport layer
*
* @param extensions
* the SU extensions
* @return
*/
public static boolean isHttpTransportEnabled(final SuConfigurationParameters extensions) {
return getBoolean(ENABLE_HTTP_TRANSPORT, extensions);
}
/**
* Get the flag enabling the JMS transport layer
*
* @param extensions
* the SU extensions
* @return
*/
public static boolean isJmsTransportEnabled(final SuConfigurationParameters extensions) {
return getBoolean(ENABLE_JMS_TRANSPORT, extensions, false);
}
/**
* @param extensions
* the SU extensions
* @return
*/
public static List retrieveHeaderList(final SuConfigurationParameters extensions) {
List result = null;
final String tmp = extensions.get(HEADERS_FILTER);
if (tmp != null) {
result = new ArrayList();
final StringTokenizer st = new StringTokenizer(tmp, ",");
while (st.hasMoreTokens()) {
result.add(st.nextToken().trim());
}
}
return result;
}
/**
* We inject the headers by default
*
* @param extensions
* the SU extensions
* @return
*/
public static boolean retrieveInjectHeader(final SuConfigurationParameters extensions) {
return getBoolean(INJECT_HEADERS, extensions);
}
/**
* Creates a new instance if {@link SUPropertiesHelper}
*/
protected SUPropertiesHelper() {
super();
}
public static List retrieveHeaderToInject(final SuConfigurationParameters extensions, final Logger logger) {
final List result = new ArrayList();
final String toInject = getString(HEADERS_TO_INJECT, extensions);
if (toInject == null)
return result;
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final Document doc;
try {
// The list of headers to inject is not necessarily an XML tree
// eg.
// So, add a root node...
doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader("" + toInject + " ")));
} catch (Exception e) {
logger.log(Level.WARNING, "Error while parsing headers to inject, skipping them: '" + toInject + "'", e);
return result;
}
final Element root = doc.getDocumentElement();
while (root.hasChildNodes()) { // root element ignored (added above...)
DocumentFragment docfrag = doc.createDocumentFragment();
docfrag.appendChild(root.removeChild(root.getFirstChild()));
result.add(docfrag);
}
return result;
}
/**
* Is Axis 1 compatibility enabled
*
* @param extensions
* the SU extensions
* @return true if the Axis 1 compatibility is enabled, otherwise false
*/
public static boolean isAxis1CompatibilityEnabled(final SuConfigurationParameters extensions) {
String compatibility = extensions.get(COMPATIBILITY);
return compatibility != null && compatibility.equals(AXIS1);
}
/**
* Get the keystore file location (HTTPS)
*
* @param extensions
* the SU extensions
* @return the keystore file location
*/
public static String getKeystoreFile(final SuConfigurationParameters extensions) {
return extensions.get(KEYSTORE_FILE);
}
/**
* Get the keystore password (HTTPS)
*
* @param extensions
* the SU extensions the SU extensions
* @return the keystore password
*/
public static String getKeystorePassword(final SuConfigurationParameters extensions) {
return extensions.get(KEYSTORE_PASSWORD_PARAM);
}
/**
* Get the truststore file location (HTTPS)
*
* @param extensions
* the SU extensions
* @return the truststore file location
*/
public static String getTruststoreFile(final SuConfigurationParameters extensions) {
return extensions.get(TRUSTSTORE_FILE);
}
/**
* Get the truststore password (HTTPS)
*
* @param extensions
* the SU extensions
* @return the truststore password
*/
public static String getTruststorePassword(final SuConfigurationParameters extensions) {
return extensions.get(TRUSTSTORE_PASSWORD_PARAM);
}
/**
* Return if the WSA-Addressing is enabled
*
* @param extensions
* the SU extensions
* @return true if the WSA-Addressing is enabled, otherwise false
*/
public static boolean isWSAEnabled(final SuConfigurationParameters extensions) {
return getBoolean(ENABLE_WSA, extensions, false);
}
/**
* Return the Basic Authentication username
*
* @param extensions
* the SU parameters
* @return the Basic Authentication username
*/
public static String getBasicAuthUser(final SuConfigurationParameters extensions) {
return extensions.get(BASIC_AUTH_USERNAME);
}
/**
* Return the Basic Authentication password
*
* @param extensions
* the SU parameters
* @return the Basic Authentication password
*/
public static String getBasicAuthPwd(final SuConfigurationParameters extensions) {
return extensions.get(BASIC_AUTH_PASSWORD_PARAM);
}
/**
* Get the modules
*
* @param extensions
* the SU parameters
* @return the list of modules
*/
public static final List getModules(final SuConfigurationParameters extensions) {
final List result = new ArrayList();
// get modules from extension
final String token = extensions.get(MODULES);
if (token != null) {
// get individual modules values
final StringTokenizer st = new StringTokenizer(token, ",");
while (st.hasMoreTokens()) {
result.add(st.nextToken().trim());
}
if (!result.contains(Constants.MODULE_ADDRESSING)) {
result.add(Constants.MODULE_ADDRESSING);
}
}
return result;
}
/**
* Get the service parameters for the associated service
*
* @param extensions
* the SU parameters
* @return the services parameters (XML)
*/
public static final String getServiceParameters(final SuConfigurationParameters extensions) {
return extensions.get(SERVICE_PARAMETERS);
}
/**
* Get the WS-Policy for WS-Security
*
* @param extensions
* the SU parameters
* @return the WS-Policy or {@code null} if no policy is defined or policy file is not found
*/
public static final Policy getWSSPolicy(final SuConfigurationParameters extensions, final ClassLoader suClassloader,
final Logger logger) {
final String policyFileStr = extensions.get(WSS_POLICY_FILE);
if (policyFileStr != null && !policyFileStr.trim().isEmpty()) {
try (final InputStream isPolicy = suClassloader.getResourceAsStream(policyFileStr);) {
final OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(isPolicy);
final OMElement policyRootElt = builder.getDocumentElement();
return PolicyEngine.getPolicy(policyRootElt);
} catch (final IOException e) {
logger.log(Level.WARNING,
String.format("An error occurs about the WSS Policy file '%s'.", policyFileStr), e);
return null;
}
} else {
return null;
}
}
/**
* Retrieve, in the jbi.xml descriptor, the SOAP version to use for SoapEnvelopeNamespaceURI Search a
* soap-version tag.
* Legal values are 1.1 and 1.2. 1.1 is used if no tag is found.
*
* @param extensions
* the SU parameters
* @return the Axis2 soapEnvelopeNamespaceURI constant.
* @see org.apache.axiom.soap.SOAP11Constants#SOAP_ENVELOPE_NAMESPACE_URI
* @see org.apache.axiom.soap.SOAP12Constants#SOAP_ENVELOPE_NAMESPACE_URI
*/
public static final String retrieveSOAPEnvelopeNamespaceURI(final SuConfigurationParameters extensions) {
String soapEnvelopeNamespaceURI = SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI;
final String soapVersion = extensions.get(SOAP_VERSION);
if (SOAP_VERSION_12.equals(soapVersion)) {
soapEnvelopeNamespaceURI = SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI;
}
return soapEnvelopeNamespaceURI;
}
/**
* Retrieve the chunked mode property value
*
* @param extensions
* the SU parameters
*
* @return the chunked mode property value
*/
public final static String retrieveChunkedMode(final SuConfigurationParameters extensions) {
return extensions.get(CHUNKED_MODE);
}
/**
* Retrieve the default SOAP action from the extensions
*
* @param extensions
* the SU parameters
*
* @return the default SOAP action
*/
public final static String retrieveDefaultSOAPAction(final SuConfigurationParameters extensions) {
final String soapAction = extensions.get(SOAP_ACTION);
return soapAction;
}
/**
* Retrieve the proxy settings from the extensions. The proxy-host value is required. The other ones will be set to
* default values by Axis if they have not been setted by the SU.
*
* @param extensions
* the SU parameters
* @return the proxy settings if they are present in the extensions, null otherwise.
*/
public static final HttpTransportProperties.ProxyProperties retrieveProxySettings(
final SuConfigurationParameters extensions, final Logger logger) {
HttpTransportProperties.ProxyProperties proxyProperties = null;
if (extensions.get(PROXY_HOST) != null) {
// proxy host is required, if it is not set the proxy mode is not
// activated
proxyProperties = new HttpTransportProperties.ProxyProperties();
final String domain = extensions.get(PROXY_DOMAIN);
final String password = extensions.get(PROXY_PASSWORD_PARAM);
final String proxyHost = extensions.get(PROXY_HOST);
int proxyPort = -1;
final String tmp = extensions.get(PROXY_PORT);
if (tmp != null) {
try {
proxyPort = Integer.parseInt(tmp);
} catch (final NumberFormatException e) {
logger.log(Level.WARNING, "Error while parsing " + PROXY_PORT + ", skipping it: '" + tmp + '"', e);
}
}
final String proxyUserName = extensions.get(PROXY_USER);
proxyProperties.setDomain(domain);
proxyProperties.setPassWord(password);
proxyProperties.setProxyName(proxyHost);
proxyProperties.setProxyPort(proxyPort);
proxyProperties.setUserName(proxyUserName);
}
return proxyProperties;
}
/**
* Set the basic authentication parameters for the outgoing message request.
*
* @param extensions
* the SU parameters
* @param options
* the Axis2 option
*/
public static final void setBasicAuthentication(final SuConfigurationParameters extensions, final Options options) {
HttpTransportPropertiesImpl.Authenticator basicAuthentication = null;
final String username = SUPropertiesHelper.getBasicAuthUser(extensions);
String password = SUPropertiesHelper.getBasicAuthPwd(extensions);
if (username != null) {
if (password == null) {
password = "";
}
basicAuthentication = new HttpTransportPropertiesImpl.Authenticator();
basicAuthentication.setUsername(username);
basicAuthentication.setPassword(password);
basicAuthentication.setPreemptiveAuthentication(true);
options.setProperty(org.apache.axis2.kernel.http.HTTPConstants.AUTHENTICATE, basicAuthentication);
}
}
}