Author: rsearls
Date: 2014-05-29 16:24:44 -0400 (Thu, 29 May 2014)
New Revision: 18704
Added:
stack/cxf/branches/rsearls/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/WSDLSoapAddressRewriteInterceptor.java
stack/cxf/branches/rsearls/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/util/
stack/cxf/branches/rsearls/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/util/WSDLSoapAddressRewriteUtils.java
Log:
returning new classes
Added:
stack/cxf/branches/rsearls/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/WSDLSoapAddressRewriteInterceptor.java
===================================================================
---
stack/cxf/branches/rsearls/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/WSDLSoapAddressRewriteInterceptor.java
(rev 0)
+++
stack/cxf/branches/rsearls/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/WSDLSoapAddressRewriteInterceptor.java 2014-05-29
20:24:44 UTC (rev 18704)
@@ -0,0 +1,120 @@
+package org.jboss.wsf.stack.cxf.interceptor;
+
+import org.apache.cxf.binding.soap.interceptor.EndpointSelectionInterceptor;
+import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.common.util.UrlUtils;
+import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.frontend.WSDLGetInterceptor;
+import org.apache.cxf.frontend.WSDLGetOutInterceptor;
+import org.apache.cxf.interceptor.*;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+import org.apache.cxf.phase.AbstractPhaseInterceptor;
+import org.apache.cxf.phase.Phase;
+import org.apache.cxf.service.model.EndpointInfo;
+import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;
+import org.jboss.wsf.stack.cxf.interceptor.util.WSDLSoapAddressRewriteUtils;
+import org.w3c.dom.Document;
+
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * This is a customization of org.apache.cxf.frontend.WSDLGetInterceptor. It
+ * enables the handling of rewriting the path of a URL.
+ * Date: 5/19/14
+ */
+public class WSDLSoapAddressRewriteInterceptor extends
AbstractPhaseInterceptor<Message> {
+ public static final WSDLSoapAddressRewriteInterceptor INSTANCE =
+ new WSDLSoapAddressRewriteInterceptor();
+ private static final String TRANSFORM_SKIP = "transform.skip";
+ private Interceptor<Message> wsdlGetOutInterceptor =
WSDLGetOutInterceptor.INSTANCE;
+
+ public WSDLSoapAddressRewriteInterceptor() {
+ super(Phase.READ);
+ getAfter().add(EndpointSelectionInterceptor.class.getName());
+ }
+
+ public WSDLSoapAddressRewriteInterceptor(Interceptor<Message> outInterceptor) {
+ this();
+ // Let people override the wsdlGetOutInterceptor
+ wsdlGetOutInterceptor = outInterceptor;
+ }
+
+ public void handleMessage(Message message) throws Fault {
+ String method = (String)message.get(Message.HTTP_REQUEST_METHOD);
+ String query = (String)message.get(Message.QUERY_STRING);
+
+ if (!"GET".equals(method) || StringUtils.isEmpty(query)) {
+ return;
+ }
+
+ String baseUri = (String)message.get(Message.REQUEST_URL);
+ String ctx = (String)message.get(Message.PATH_INFO);
+
+ Map<String, String> map = UrlUtils.parseQueryString(query);
+ if (isRecognizedQuery(map, baseUri, ctx,
message.getExchange().getEndpoint().getEndpointInfo())) {
+ Document doc = getDocument(message, baseUri, map, ctx);
+
+ Endpoint e = message.getExchange().get(Endpoint.class);
+ Message mout = new MessageImpl();
+ mout.setExchange(message.getExchange());
+ mout = e.getBinding().createMessage(mout);
+
mout.setInterceptorChain(OutgoingChainInterceptor.getOutInterceptorChain(message.getExchange()));
+ message.getExchange().setOutMessage(mout);
+
+ mout.put(WSDLGetInterceptor.DOCUMENT_HOLDER, doc);
+ mout.put(Message.CONTENT_TYPE, "text/xml");
+
+ // just remove the interceptor which should not be used
+ cleanUpOutInterceptors(mout);
+
+ // notice this is being added after the purge above, don't swap the order!
+ mout.getInterceptorChain().add(wsdlGetOutInterceptor);
+
+ message.getExchange().put(TRANSFORM_SKIP, Boolean.TRUE);
+ // skip the service executor and goto the end of the chain.
+ message.getInterceptorChain().doInterceptStartingAt(
+ message,
+ OutgoingChainInterceptor.class.getName());
+ }
+ }
+
+ protected void cleanUpOutInterceptors(Message outMessage) {
+ // TODO - how can I improve this to provide a specific interceptor chain that just
has the
+ // stax, gzip and message sender components, while also ensuring that GZIP is only
provided
+ // if its already configured for the endpoint.
+ Iterator<Interceptor<? extends Message>> iterator =
outMessage.getInterceptorChain().iterator();
+ while (iterator.hasNext()) {
+ Interceptor<? extends Message> inInterceptor = iterator.next();
+ if (!inInterceptor.getClass().equals(StaxOutInterceptor.class)
+ && !inInterceptor.getClass().equals(GZIPOutInterceptor.class)
+ && !inInterceptor.getClass().equals(MessageSenderInterceptor.class))
{
+ outMessage.getInterceptorChain().remove(inInterceptor);
+ }
+ }
+
+ }
+
+ private Document getDocument(Message message, String base, Map<String, String>
params, String ctxUri) {
+ // cannot have two wsdl's being generated for the same endpoint at the same
+ // time as the addresses may get mixed up
+ // For WSDL's the WSDLWriter does not share any state between documents.
+ // For XSD's, the WSDLSoapAddressRewriteUtils makes a copy of any XSD schema
documents before updating
+ // any addresses and returning them, so for both WSDL and XSD this is the only part
that needs
+ // to be synchronized.
+ synchronized (message.getExchange().getEndpoint()) {
+ return new WSDLSoapAddressRewriteUtils().getDocument(message, base, params,
ctxUri,
+ message.getExchange().getEndpoint().getEndpointInfo());
+ }
+ }
+
+ private boolean isRecognizedQuery(Map<String, String> map, String baseUri,
String ctx,
+ EndpointInfo endpointInfo) {
+ if (map.containsKey("wsdl") || map.containsKey("xsd")) {
+ return true;
+ }
+ return false;
+ }
+
+}
Added:
stack/cxf/branches/rsearls/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/util/WSDLSoapAddressRewriteUtils.java
===================================================================
---
stack/cxf/branches/rsearls/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/util/WSDLSoapAddressRewriteUtils.java
(rev 0)
+++
stack/cxf/branches/rsearls/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/util/WSDLSoapAddressRewriteUtils.java 2014-05-29
20:24:44 UTC (rev 18704)
@@ -0,0 +1,245 @@
+package org.jboss.wsf.stack.cxf.interceptor.util;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.catalog.OASISCatalogManager;
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.frontend.WSDLGetUtils;
+import org.apache.cxf.frontend.WSDLQueryException;
+import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageUtils;
+import org.apache.cxf.service.model.EndpointInfo;
+import org.apache.cxf.service.model.SchemaInfo;
+import org.apache.cxf.staxutils.StaxUtils;
+import org.apache.cxf.wsdl.WSDLManager;
+import org.apache.cxf.wsdl11.ResourceManagerWSDLLocator;
+import org.apache.cxf.wsdl11.ServiceWSDLBuilder;
+import org.jboss.ws.common.management.AbstractServerConfig;
+import org.jboss.wsf.spi.management.ServerConfig;
+import org.jboss.wsf.stack.cxf.addressRewrite.SEDProcessor;
+import org.jboss.wsf.stack.cxf.addressRewrite.SoapAddressRewriteHelper;
+import org.jboss.wsf.stack.cxf.interceptor.WSDLSoapAddressRewriteInterceptor;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+
+import javax.wsdl.Definition;
+import javax.wsdl.extensions.schema.SchemaReference;
+import javax.wsdl.xml.WSDLWriter;
+import javax.xml.transform.dom.DOMSource;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.security.AccessController;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.logging.Logger;
+import org.apache.cxf.common.util.StringUtils;
+
+
+/**
+ * User: rsearls
+ * Date: 5/19/14
+ */
+public class WSDLSoapAddressRewriteUtils extends WSDLGetUtils {
+
+ private static final String WSDLS_KEY = WSDLSoapAddressRewriteUtils.class.getName() +
".WSDLs";
+ private static final String SCHEMAS_KEY = WSDLSoapAddressRewriteUtils.class.getName()
+ ".Schemas";
+
+ private static final Logger LOG =
LogUtils.getL7dLogger(WSDLSoapAddressRewriteInterceptor.class);
+
+ public WSDLSoapAddressRewriteUtils() {
+
+ }
+
+ @Override
+ public Document getDocument(Message message,
+ String base,
+ Map<String, String> params,
+ String ctxUri,
+ EndpointInfo endpointInfo) {
+ try {
+ Bus bus = message.getExchange().getBus();
+ Object prop = message.getContextualProperty(PUBLISHED_ENDPOINT_URL);
+ if (prop == null) {
+ prop = endpointInfo.getProperty(PUBLISHED_ENDPOINT_URL);
+ }
+ if (prop != null) {
+ base = String.valueOf(prop);
+ }
+ String wsdl = params.get("wsdl");
+ if (wsdl != null) {
+ // Always use the URL decoded version to ensure that we have a
+ // canonical representation of the import URL for lookup.
+ wsdl = URLDecoder.decode(wsdl, "utf-8");
+ }
+
+ String xsd = params.get("xsd");
+ if (xsd != null) {
+ // Always use the URL decoded version to ensure that we have a
+ // canonical representation of the import URL for lookup.
+ xsd = URLDecoder.decode(xsd, "utf-8");
+ }
+
+ Map<String, Definition> mp = CastUtils.cast((Map<?,
?>)endpointInfo.getService()
+ .getProperty(WSDLS_KEY));
+ Map<String, SchemaReference> smp = CastUtils.cast((Map<?,
?>)endpointInfo.getService()
+ .getProperty(SCHEMAS_KEY));
+
+ if (mp == null) {
+ endpointInfo.getService().setProperty(WSDLS_KEY,
+ new ConcurrentHashMap<String, Definition>(8, 0.75f, 4));
+ mp = CastUtils.cast((Map<?, ?>)endpointInfo.getService()
+ .getProperty(WSDLS_KEY));
+ }
+ if (smp == null) {
+ endpointInfo.getService().setProperty(SCHEMAS_KEY,
+ new ConcurrentHashMap<String, SchemaReference>(8, 0.75f, 4));
+ smp = CastUtils.cast((Map<?, ?>)endpointInfo.getService()
+ .getProperty(SCHEMAS_KEY));
+ }
+
+ if (!mp.containsKey("")) {
+ ServiceWSDLBuilder builder =
+ new ServiceWSDLBuilder(bus, endpointInfo.getService());
+
+ builder.setUseSchemaImports(
+ MessageUtils.getContextualBoolean(message, WSDL_CREATE_IMPORTS, false));
+
+ // base file name is ignored if createSchemaImports == false!
+ builder.setBaseFileName(endpointInfo.getService().getName().getLocalPart());
+
+ Definition def = builder.build(new HashMap<String, SchemaInfo>());
+
+ mp.put("", def);
+ updateDefinition(bus, def, mp, smp, base, endpointInfo, "");
+ }
+
+
+ Document doc;
+ if (xsd == null) {
+ Definition def = mp.get(wsdl);
+ if (def == null) {
+ String wsdl2 =
resolveWithCatalogs(OASISCatalogManager.getCatalogManager(bus),
+ wsdl,
+ base);
+ if (wsdl2 != null) {
+ def = mp.get(wsdl2);
+ }
+ }
+ if (def == null) {
+ throw new WSDLQueryException(new
org.apache.cxf.common.i18n.Message("WSDL_NOT_FOUND",
+ LOG, wsdl), null);
+ }
+
+ synchronized (def) {
+ //writing a def is not threadsafe. Sync on it to make sure
+ //we don't get any ConcurrentModificationExceptions
+ if (endpointInfo.getProperty(PUBLISHED_ENDPOINT_URL) != null) {
+ String epurl =
+ String.valueOf(endpointInfo.getProperty(PUBLISHED_ENDPOINT_URL));
+ updatePublishedEndpointUrl(epurl, def, endpointInfo.getName());
+ base = epurl;
+ } else {
+ // When using replacement path, must set replacement path in the active
url.
+ ServerConfig sc = getServerConfig();
+ if (SoapAddressRewriteHelper.isSoapAddressRewrite(getServerConfig())
+ &&
endpointInfo.getAddress().contains(ServerConfig.UNDEFINED_HOSTNAME)) {
+ String epurl = SoapAddressRewriteHelper.rewriteSoapAddress(sc,
base);
+ updatePublishedEndpointUrl(epurl, def, endpointInfo.getName());
+ base = epurl;
+ }
+ }
+
+ WSDLWriter wsdlWriter = bus.getExtension(WSDLManager.class)
+ .getWSDLFactory().newWSDLWriter();
+
def.setExtensionRegistry(bus.getExtension(WSDLManager.class).getExtensionRegistry());
+ doc = wsdlWriter.getDocument(def);
+ }
+ } else {
+ SchemaReference si = smp.get(xsd);
+ if (si == null) {
+ String xsd2 =
resolveWithCatalogs(OASISCatalogManager.getCatalogManager(bus),
+ xsd,
+ base);
+ if (xsd2 != null) {
+ si = smp.get(xsd2);
+ }
+ }
+ if (si == null) {
+ throw new WSDLQueryException(new
org.apache.cxf.common.i18n.Message("SCHEMA_NOT_FOUND",
+ LOG, wsdl), null);
+ }
+
+ String uri = si.getReferencedSchema().getDocumentBaseURI();
+ uri = resolveWithCatalogs(OASISCatalogManager.getCatalogManager(bus),
+ uri,
+ si.getReferencedSchema().getDocumentBaseURI());
+ if (uri == null) {
+ uri = si.getReferencedSchema().getDocumentBaseURI();
+ }
+ ResourceManagerWSDLLocator rml = new ResourceManagerWSDLLocator(uri,
+ bus);
+
+ InputSource src = rml.getBaseInputSource();
+ if (src.getByteStream() != null || src.getCharacterStream() != null) {
+ doc = StaxUtils.read(src);
+ } else { // last resort lets try for the referenced schema itself.
+ // its not thread safe if we use the same document
+ doc = StaxUtils.read(
+ new
DOMSource(si.getReferencedSchema().getElement().getOwnerDocument()));
+ }
+ }
+
+ updateDoc(doc, base, mp, smp, message, xsd, wsdl);
+
+ return doc;
+ } catch (WSDLQueryException wex) {
+ throw wex;
+ } catch (Exception wex) {
+ throw new WSDLQueryException(new
org.apache.cxf.common.i18n.Message("COULD_NOT_PROVIDE_WSDL",
+ LOG,
+ base), wex);
+ }
+ }
+
+ @Override
+ public void updateWSDLPublishedEndpointAddress(Definition def, EndpointInfo
endpointInfo)
+ {
+ synchronized (def) {
+ //writing a def is not threadsafe. Sync on it to make sure
+ //we don't get any ConcurrentModificationExceptions
+ if (endpointInfo.getProperty(PUBLISHED_ENDPOINT_URL) != null) {
+ String epurl =
+ String.valueOf(endpointInfo.getProperty(PUBLISHED_ENDPOINT_URL));
+ updatePublishedEndpointUrl(epurl, def, endpointInfo.getName());
+ }
+ /** rls test else {
+ // When using replacement path must set replacement path in active url.
+ ServerConfig sc = getServerConfig();
+ if (sc.isModifySOAPAddress() &&
!StringUtils.isEmpty(sc.getWebServicePath()
+ &&
endpointInfo.getAddress().contains(ServerConfig.UNDEFINED_HOSTNAME)) {
+ try {
+ String base = endpointInfo.getAddress();
+ URL baseUrl = new URL(base);
+ //String tmpPath =
Unix4j.builder().echo(baseUrl.getPath()).sed(sc.getWebServicePath()).toStringResult();
+ String tmpPath =
SEDProcessor.newInstance(sc.getWebServicePath()).processLine(baseUrl.getPath());
+ String epurl = base.replace(baseUrl.getPath(), tmpPath);
+ updatePublishedEndpointUrl(epurl, def, endpointInfo.getName());
+ //base = epurl;
+ } catch (Exception e){
+ // TODO fix
+ LOG.info(e.toString());
+ }
+ }
+ }
+ **/
+ }
+ }
+
+ private static ServerConfig getServerConfig() {
+ if(System.getSecurityManager() == null) {
+ return AbstractServerConfig.getServerIntegrationServerConfig();
+ }
+ return
AccessController.doPrivileged(AbstractServerConfig.GET_SERVER_INTEGRATION_SERVER_CONFIG);
+ }
+}