You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@synapse.apache.org by as...@apache.org on 2007/03/17 17:18:47 UTC
svn commit: r519363 [11/16] - in /webservices/synapse/trunk/java:
modules/core/ modules/core/src/main/java/org/apache/synapse/
modules/core/src/main/java/org/apache/synapse/config/
modules/core/src/main/java/org/apache/synapse/config/xml/ modules/core/...
Modified: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/spring/SpringMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/spring/SpringMediator.java?view=diff&rev=519363&r1=519362&r2=519363
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/spring/SpringMediator.java (original)
+++ webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/spring/SpringMediator.java Sat Mar 17 09:18:32 2007
@@ -1,152 +1,152 @@
-/*
- * 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.apache.synapse.mediators.spring;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.synapse.MessageContext;
-import org.apache.synapse.SynapseException;
-import org.apache.synapse.Mediator;
-import org.apache.synapse.Constants;
-import org.apache.synapse.mediators.AbstractMediator;
-import org.apache.synapse.config.Util;
-import org.apache.synapse.config.Entry;
-import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.support.GenericApplicationContext;
-import org.springframework.core.io.InputStreamResource;
-
-/**
- * This mediator allows Spring beans implementing the org.apache.synapse.Mediator
- * interface to mediate messages passing through Synapse.
- *
- * A Spring mediator is instantiated by Spring (see www.springframework.org). The mediator
- * refers to a Spring bean name, and also either a Spring configuration defined to Synapse
- * or an inlined Spring configuration.
- */
-public class SpringMediator extends AbstractMediator {
-
- private static final Log log = LogFactory.getLog(SpringMediator.class);
- private static final Log trace = LogFactory.getLog(Constants.TRACE_LOGGER);
-
- /**
- * The Spring bean ref to be used
- */
- private String beanName = null;
- /**
- * The named Spring config to be used
- */
- private String configKey = null;
- /**
- * The Spring ApplicationContext to be used
- */
- private ApplicationContext appContext = null;
-
- public boolean mediate(MessageContext synCtx) {
-
- boolean shouldTrace = shouldTrace(synCtx.getTracingState());
- if (shouldTrace) {
- trace.trace("Start : Spring mediator");
- }
- Entry entry = synCtx.getConfiguration().getEntryDefinition(configKey);
-
- // if the configKey refers to a dynamic property
- if (entry != null && entry.isDynamic()) {
- if (!entry.isCached() || entry.isExpired()) {
- buildAppContext(synCtx);
- }
- // if the property is not a DynamicProperty, we will create an ApplicationContext only once
- } else {
- if (appContext == null) {
- buildAppContext(synCtx);
- }
- }
-
- if (appContext != null) {
-
- Object o = appContext.getBean(beanName);
- if (o != null && Mediator.class.isAssignableFrom(o.getClass())) {
- Mediator m = (Mediator) o;
- if (shouldTrace) {
- trace.trace("Loaded mediator from bean : " + beanName + " executing...");
- }
- return m.mediate(synCtx);
-
- } else {
- if (shouldTrace) {
- trace.trace("Unable to load mediator from bean : " + beanName);
- }
- handleException("Could not load bean named : " + beanName +
- " from the Spring configuration with key : " + configKey);
- }
- } else {
- handleException("Cannot reference Spring application context with key : " + configKey);
- }
- if (shouldTrace) {
- trace.trace("End : Spring mediator");
- }
- return true;
- }
-
- private synchronized void buildAppContext(MessageContext synCtx) {
- log.debug("Creating Spring ApplicationContext from property key : " + configKey);
- GenericApplicationContext appContext = new GenericApplicationContext();
- XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(appContext);
- xbdr.setValidating(false);
- xbdr.loadBeanDefinitions(new InputStreamResource(
- Util.getStreamSource(
- synCtx.getConfiguration().getEntry(configKey)).getInputStream()));
- appContext.refresh();
- this.appContext = appContext;
- }
-
- private void handleException(String msg) {
- log.error(msg);
- throw new SynapseException(msg);
- }
-
- public void setBeanName(String beanName) {
- this.beanName = beanName;
- }
-
- public String getBeanName() {
- return beanName;
- }
-
- public String getConfigKey() {
- return configKey;
- }
-
- public void setConfigKey(String configKey) {
- this.configKey = configKey;
- }
-
- public ApplicationContext getAppContext() {
- return appContext;
- }
-
- public void setAppContext(ApplicationContext appContext) {
- this.appContext = appContext;
- }
-
- public String getType() {
- return "SpringMediator";
- }
-}
+/*
+ * 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.apache.synapse.mediators.spring;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.Constants;
+import org.apache.synapse.mediators.AbstractMediator;
+import org.apache.synapse.config.Util;
+import org.apache.synapse.config.Entry;
+import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.core.io.InputStreamResource;
+
+/**
+ * This mediator allows Spring beans implementing the org.apache.synapse.Mediator
+ * interface to mediate messages passing through Synapse.
+ *
+ * A Spring mediator is instantiated by Spring (see www.springframework.org). The mediator
+ * refers to a Spring bean name, and also either a Spring configuration defined to Synapse
+ * or an inlined Spring configuration.
+ */
+public class SpringMediator extends AbstractMediator {
+
+ private static final Log log = LogFactory.getLog(SpringMediator.class);
+ private static final Log trace = LogFactory.getLog(Constants.TRACE_LOGGER);
+
+ /**
+ * The Spring bean ref to be used
+ */
+ private String beanName = null;
+ /**
+ * The named Spring config to be used
+ */
+ private String configKey = null;
+ /**
+ * The Spring ApplicationContext to be used
+ */
+ private ApplicationContext appContext = null;
+
+ public boolean mediate(MessageContext synCtx) {
+
+ boolean shouldTrace = shouldTrace(synCtx.getTracingState());
+ if (shouldTrace) {
+ trace.trace("Start : Spring mediator");
+ }
+ Entry entry = synCtx.getConfiguration().getEntryDefinition(configKey);
+
+ // if the configKey refers to a dynamic property
+ if (entry != null && entry.isDynamic()) {
+ if (!entry.isCached() || entry.isExpired()) {
+ buildAppContext(synCtx);
+ }
+ // if the property is not a DynamicProperty, we will create an ApplicationContext only once
+ } else {
+ if (appContext == null) {
+ buildAppContext(synCtx);
+ }
+ }
+
+ if (appContext != null) {
+
+ Object o = appContext.getBean(beanName);
+ if (o != null && Mediator.class.isAssignableFrom(o.getClass())) {
+ Mediator m = (Mediator) o;
+ if (shouldTrace) {
+ trace.trace("Loaded mediator from bean : " + beanName + " executing...");
+ }
+ return m.mediate(synCtx);
+
+ } else {
+ if (shouldTrace) {
+ trace.trace("Unable to load mediator from bean : " + beanName);
+ }
+ handleException("Could not load bean named : " + beanName +
+ " from the Spring configuration with key : " + configKey);
+ }
+ } else {
+ handleException("Cannot reference Spring application context with key : " + configKey);
+ }
+ if (shouldTrace) {
+ trace.trace("End : Spring mediator");
+ }
+ return true;
+ }
+
+ private synchronized void buildAppContext(MessageContext synCtx) {
+ log.debug("Creating Spring ApplicationContext from property key : " + configKey);
+ GenericApplicationContext appContext = new GenericApplicationContext();
+ XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(appContext);
+ xbdr.setValidating(false);
+ xbdr.loadBeanDefinitions(new InputStreamResource(
+ Util.getStreamSource(
+ synCtx.getConfiguration().getEntry(configKey)).getInputStream()));
+ appContext.refresh();
+ this.appContext = appContext;
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+ public void setBeanName(String beanName) {
+ this.beanName = beanName;
+ }
+
+ public String getBeanName() {
+ return beanName;
+ }
+
+ public String getConfigKey() {
+ return configKey;
+ }
+
+ public void setConfigKey(String configKey) {
+ this.configKey = configKey;
+ }
+
+ public ApplicationContext getAppContext() {
+ return appContext;
+ }
+
+ public void setAppContext(ApplicationContext appContext) {
+ this.appContext = appContext;
+ }
+
+ public String getType() {
+ return "SpringMediator";
+ }
+}
Propchange: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/spring/SpringMediator.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/spring/SpringMediatorFactory.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/spring/SpringMediatorFactory.java?view=diff&rev=519363&r1=519362&r2=519363
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/spring/SpringMediatorFactory.java (original)
+++ webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/spring/SpringMediatorFactory.java Sat Mar 17 09:18:32 2007
@@ -1,84 +1,84 @@
-/*
- * 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.apache.synapse.mediators.spring;
-
-import org.apache.axiom.om.OMAttribute;
-import org.apache.axiom.om.OMElement;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.synapse.SynapseException;
-import org.apache.synapse.Mediator;
-import org.apache.synapse.config.xml.Constants;
-import org.apache.synapse.config.xml.MediatorFactory;
-import org.apache.synapse.config.xml.AbstractMediatorFactory;
-
-import javax.xml.namespace.QName;
-
-/**
- * Creates an instance of a Spring mediator that refers to the given Spring
- * configuration and bean. Optionally, one could specify an inlined Spring
- * configuration as opposed to a globally defined Spring configuration
- * <p/>
- * <spring bean="exampleBean1" key="string""/>
- */
-public class SpringMediatorFactory extends AbstractMediatorFactory {
-
- private static final Log log = LogFactory.getLog(SpringMediatorFactory.class);
-
- private static final QName TAG_NAME = new QName(Constants.SYNAPSE_NAMESPACE + "/spring", "spring");
-
- /**
- * Create a Spring mediator instance referring to the bean and configuration given
- * by the OMElement declaration
- *
- * @param elem the OMElement that specifies the Spring mediator configuration
- * @return the Spring mediator instance created
- */
- public Mediator createMediator(OMElement elem) {
-
- SpringMediator sm = new SpringMediator();
- OMAttribute bean = elem.getAttribute(new QName(Constants.NULL_NAMESPACE, "bean"));
- OMAttribute key = elem.getAttribute(new QName(Constants.NULL_NAMESPACE, "key"));
-
- if (bean == null) {
- handleException("The 'bean' attribute is required for a Spring mediator definition");
- } else if (key == null) {
- handleException("A 'key' attribute is required for a Spring mediator definition");
- } else {
-
- // after successfully creating the mediator
- // set its common attributes such as tracing etc
- initMediator(sm,elem);
- sm.setBeanName(bean.getAttributeValue());
- sm.setConfigKey(key.getAttributeValue());
- return sm;
- }
- return null;
- }
-
- private void handleException(String msg) {
- log.error(msg);
- throw new SynapseException(msg);
- }
-
- public QName getTagQName() {
- return TAG_NAME;
- }
-}
+/*
+ * 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.apache.synapse.mediators.spring;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.config.xml.Constants;
+import org.apache.synapse.config.xml.MediatorFactory;
+import org.apache.synapse.config.xml.AbstractMediatorFactory;
+
+import javax.xml.namespace.QName;
+
+/**
+ * Creates an instance of a Spring mediator that refers to the given Spring
+ * configuration and bean. Optionally, one could specify an inlined Spring
+ * configuration as opposed to a globally defined Spring configuration
+ * <p/>
+ * <spring bean="exampleBean1" key="string""/>
+ */
+public class SpringMediatorFactory extends AbstractMediatorFactory {
+
+ private static final Log log = LogFactory.getLog(SpringMediatorFactory.class);
+
+ private static final QName TAG_NAME = new QName(Constants.SYNAPSE_NAMESPACE + "/spring", "spring");
+
+ /**
+ * Create a Spring mediator instance referring to the bean and configuration given
+ * by the OMElement declaration
+ *
+ * @param elem the OMElement that specifies the Spring mediator configuration
+ * @return the Spring mediator instance created
+ */
+ public Mediator createMediator(OMElement elem) {
+
+ SpringMediator sm = new SpringMediator();
+ OMAttribute bean = elem.getAttribute(new QName(Constants.NULL_NAMESPACE, "bean"));
+ OMAttribute key = elem.getAttribute(new QName(Constants.NULL_NAMESPACE, "key"));
+
+ if (bean == null) {
+ handleException("The 'bean' attribute is required for a Spring mediator definition");
+ } else if (key == null) {
+ handleException("A 'key' attribute is required for a Spring mediator definition");
+ } else {
+
+ // after successfully creating the mediator
+ // set its common attributes such as tracing etc
+ initMediator(sm,elem);
+ sm.setBeanName(bean.getAttributeValue());
+ sm.setConfigKey(key.getAttributeValue());
+ return sm;
+ }
+ return null;
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+ public QName getTagQName() {
+ return TAG_NAME;
+ }
+}
Propchange: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/spring/SpringMediatorFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/spring/SpringMediatorSerializer.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediator.java?view=diff&rev=519363&r1=519362&r2=519363
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediator.java (original)
+++ webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediator.java Sat Mar 17 09:18:32 2007
@@ -1,248 +1,248 @@
-/*
- * 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.apache.synapse.mediators.throttle;
-
-import org.apache.axiom.om.OMElement;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.neethi.PolicyEngine;
-import org.apache.synapse.MessageContext;
-import org.apache.synapse.SynapseException;
-import org.apache.synapse.Constants;
-import org.apache.synapse.config.Entry;
-import org.apache.synapse.core.axis2.Axis2MessageContext;
-import org.apache.synapse.mediators.AbstractMediator;
-import org.wso2.throttle.factory.AccessControllerFactory;
-import org.wso2.throttle.*;
-
-
-/**
- * The Mediator for the throttling - Throtting will occur according to the ws-policy which is specified as
- * the key for lookup from the registry or the inline policy
- * Only support IP based throttling- Throotling can manage per IP using the throttle policy
- */
-
-public class ThrottleMediator extends AbstractMediator {
-
- private static final Log log = LogFactory.getLog(ThrottleMediator.class);
- private static final Log trace = LogFactory.getLog(Constants.TRACE_LOGGER);
- /** The key for getting policy value - key refer to registry entry */
- private String policyKey = null;
- /** InLine policy object - XML */
- private OMElement inLinePolicy = null;
- /** The throttle - hold runtime + configuration data of throttle */
- Throttle throttle = null;
-
- public boolean mediate(MessageContext synCtx) {
- boolean shouldTrace = shouldTrace(synCtx.getTracingState());
- try {
- if (shouldTrace) {
- trace.trace("Start : Throttle mediator");
- }
- //init method to init throttle
- init(synCtx, shouldTrace);
- // check access allow or not
- return canAccess(synCtx, shouldTrace);
- } finally {
- if (shouldTrace) {
- trace.trace("End : Throttle mediator");
- }
- }
- }
-
- /**
- * To check whether allow access or not for caller
- * Current Implementaion only support IP Based Throttling
- *
- * @param synContext
- * @return boolean which indicate whether this caller can or not access
- */
- protected boolean canAccess(MessageContext synContext, boolean shouldTrace) {
-
- if (throttle == null) {
- log.info("Can not find a throttle");
- return true;
- }
- org.apache.axis2.context.MessageContext axis2MessageContext
- = ((Axis2MessageContext) synContext).getAxis2MessageContext();
- //IP based throttling
- Object remoteIP = axis2MessageContext.getProperty(
- org.apache.axis2.context.MessageContext.REMOTE_ADDR);
- if (remoteIP == null) {
- if (shouldTrace) {
- trace.trace("The IP Address of the caller is cannnot find- The Throttling will" +
- "not occur");
- }
- log.info("The IP address of the caller can not find - Currently only support caller-IP base"
- + "access control - Thottling will not happen ");
- return true;
- } else {
- if (shouldTrace) {
- trace.trace("The IP Address of the caller :" + remoteIP);
- }
- ThrottleContext throttleContext
- = throttle.getThrottleContext(ThrottleConstants.IP_BASED_THROTTLE_KEY);
- if (throttleContext == null) {
- log.info("Can not find a configuartion for the IP Based Throttle");
- return true;
- }
- try {
- AccessController accessControler = AccessControllerFactory.createAccessControler(
- ThrottleConstants.IP_BASE);
- boolean canAccess = accessControler.canAccess(throttleContext, remoteIP);
- if (!canAccess) {
- String msg = "Access has currently been denied by" +
- " the IP_BASE throttle for the IP :\t" + remoteIP;
- if (shouldTrace) {
- trace.trace(msg);
- }
- log.info(msg);
- }
- return canAccess;
- }
- catch (ThrottleException e) {
- handleException("Error occur during throttling ",e);
- return true;
- }
- }
- }
-
-
- /**
- * To init throttle with the policy
- * If the policy is defined as a Registry key ,then Policy will only process after it has expired
- * Any runtime changes to the policy will take effect
- * If the policy is defined as a Inline XML ,then only one time policy will process and any runtime
- * changes to the policy will not reflect
- *
- * @param synContext
- */
- protected void init(MessageContext synContext, boolean shouldTrace) {
-
- boolean reCreate = false; // It is not need to recreate ,if property is not dyanamic
- OMElement policyOmElement = null;
- if (policyKey != null) {
- Entry entry = synContext.getConfiguration().getEntryDefinition(policyKey);
- if (entry == null) {
- log.warn("Cant not find a Entry from the Entry key " + policyKey);
- return;
- }
- Object entryValue = entry.getValue();
- if (entryValue == null) {
- log.warn("Cant not find a Policy(Enrty value) from the Entry key " + policyKey);
- return;
- }
- if (!(entryValue instanceof OMElement)) {
- log.warn("Entry value which is refered from the key " + policyKey + " is Incompatible " +
- "for the policy element");
- return;
- }
- // if entry is dynamic, need to check wheather updated or not
- if ((!entry.isCached() || entry.isExpired())) {
- reCreate = true;
- }
- policyOmElement = (OMElement) entryValue;
- } else if (inLinePolicy != null) {
- policyOmElement = inLinePolicy;
- }
- if (policyOmElement == null) {
- log.warn("Cant not find a Policy - Throttling will not occur");
- return;
- }
- if (shouldTrace) {
- trace.trace("The Throttle Policy :" + policyOmElement.toString());
- }
- if (!reCreate) {
- //The first time creation
- if (throttle == null) {
- createThrottleMetaData(policyOmElement);
- }
- } else {
- createThrottleMetaData(policyOmElement);
- }
-
- }
-
- /**
- * To create the Throttle from the policy element
- *
- * @param policyOmElement - valid throttle policy
- */
- protected void createThrottleMetaData(OMElement policyOmElement) {
- try {
- log.info("Creating a new throttle configuration by parsing the Policy");
- throttle = ThrottlePolicyProcessor
- .processPoclicy(PolicyEngine.getPolicy(policyOmElement));
- }
- catch (ThrottleException e) {
- handleException("Error during processing the thorttle policy " + e.getMessage());
- }
- }
-
- private void handleException(String msg) {
- log.error(msg);
- throw new SynapseException(msg);
- }
-
- private void handleException(String msg, Exception e) {
- log.info(msg);
- log.error(e);
- throw new SynapseException(msg);
- }
-
- public String getType() {
- return ThrottleMediator.class.getName();
- }
-
- /**
- * To get the policy key - The key for which lookup from the registry
- *
- * @return String
- */
- public String getPolicyKey() {
- return policyKey;
- }
-
- /**
- * To set the policy key - The key for which lookup from the registry
- *
- * @param policyKey
- */
- public void setPolicyKey(String policyKey) {
- this.policyKey = policyKey;
- }
-
- /**
- * getting throttle policy which has defined as InLineXML
- *
- * @return InLine Throttle Policy
- */
- public OMElement getInLinePolicy() {
- return inLinePolicy;
- }
-
- /**
- * setting throttle policy which has defined as InLineXML
- *
- * @param inLinePolicy
- */
- public void setInLinePolicy(OMElement inLinePolicy) {
- this.inLinePolicy = inLinePolicy;
- }
-}
+/*
+ * 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.apache.synapse.mediators.throttle;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.neethi.PolicyEngine;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.Constants;
+import org.apache.synapse.config.Entry;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.mediators.AbstractMediator;
+import org.wso2.throttle.factory.AccessControllerFactory;
+import org.wso2.throttle.*;
+
+
+/**
+ * The Mediator for the throttling - Throtting will occur according to the ws-policy which is specified as
+ * the key for lookup from the registry or the inline policy
+ * Only support IP based throttling- Throotling can manage per IP using the throttle policy
+ */
+
+public class ThrottleMediator extends AbstractMediator {
+
+ private static final Log log = LogFactory.getLog(ThrottleMediator.class);
+ private static final Log trace = LogFactory.getLog(Constants.TRACE_LOGGER);
+ /** The key for getting policy value - key refer to registry entry */
+ private String policyKey = null;
+ /** InLine policy object - XML */
+ private OMElement inLinePolicy = null;
+ /** The throttle - hold runtime + configuration data of throttle */
+ Throttle throttle = null;
+
+ public boolean mediate(MessageContext synCtx) {
+ boolean shouldTrace = shouldTrace(synCtx.getTracingState());
+ try {
+ if (shouldTrace) {
+ trace.trace("Start : Throttle mediator");
+ }
+ //init method to init throttle
+ init(synCtx, shouldTrace);
+ // check access allow or not
+ return canAccess(synCtx, shouldTrace);
+ } finally {
+ if (shouldTrace) {
+ trace.trace("End : Throttle mediator");
+ }
+ }
+ }
+
+ /**
+ * To check whether allow access or not for caller
+ * Current Implementaion only support IP Based Throttling
+ *
+ * @param synContext
+ * @return boolean which indicate whether this caller can or not access
+ */
+ protected boolean canAccess(MessageContext synContext, boolean shouldTrace) {
+
+ if (throttle == null) {
+ log.info("Can not find a throttle");
+ return true;
+ }
+ org.apache.axis2.context.MessageContext axis2MessageContext
+ = ((Axis2MessageContext) synContext).getAxis2MessageContext();
+ //IP based throttling
+ Object remoteIP = axis2MessageContext.getProperty(
+ org.apache.axis2.context.MessageContext.REMOTE_ADDR);
+ if (remoteIP == null) {
+ if (shouldTrace) {
+ trace.trace("The IP Address of the caller is cannnot find- The Throttling will" +
+ "not occur");
+ }
+ log.info("The IP address of the caller can not find - Currently only support caller-IP base"
+ + "access control - Thottling will not happen ");
+ return true;
+ } else {
+ if (shouldTrace) {
+ trace.trace("The IP Address of the caller :" + remoteIP);
+ }
+ ThrottleContext throttleContext
+ = throttle.getThrottleContext(ThrottleConstants.IP_BASED_THROTTLE_KEY);
+ if (throttleContext == null) {
+ log.info("Can not find a configuartion for the IP Based Throttle");
+ return true;
+ }
+ try {
+ AccessController accessControler = AccessControllerFactory.createAccessControler(
+ ThrottleConstants.IP_BASE);
+ boolean canAccess = accessControler.canAccess(throttleContext, remoteIP);
+ if (!canAccess) {
+ String msg = "Access has currently been denied by" +
+ " the IP_BASE throttle for the IP :\t" + remoteIP;
+ if (shouldTrace) {
+ trace.trace(msg);
+ }
+ log.info(msg);
+ }
+ return canAccess;
+ }
+ catch (ThrottleException e) {
+ handleException("Error occur during throttling ",e);
+ return true;
+ }
+ }
+ }
+
+
+ /**
+ * To init throttle with the policy
+ * If the policy is defined as a Registry key ,then Policy will only process after it has expired
+ * Any runtime changes to the policy will take effect
+ * If the policy is defined as a Inline XML ,then only one time policy will process and any runtime
+ * changes to the policy will not reflect
+ *
+ * @param synContext
+ */
+ protected void init(MessageContext synContext, boolean shouldTrace) {
+
+ boolean reCreate = false; // It is not need to recreate ,if property is not dyanamic
+ OMElement policyOmElement = null;
+ if (policyKey != null) {
+ Entry entry = synContext.getConfiguration().getEntryDefinition(policyKey);
+ if (entry == null) {
+ log.warn("Cant not find a Entry from the Entry key " + policyKey);
+ return;
+ }
+ Object entryValue = entry.getValue();
+ if (entryValue == null) {
+ log.warn("Cant not find a Policy(Enrty value) from the Entry key " + policyKey);
+ return;
+ }
+ if (!(entryValue instanceof OMElement)) {
+ log.warn("Entry value which is refered from the key " + policyKey + " is Incompatible " +
+ "for the policy element");
+ return;
+ }
+ // if entry is dynamic, need to check wheather updated or not
+ if ((!entry.isCached() || entry.isExpired())) {
+ reCreate = true;
+ }
+ policyOmElement = (OMElement) entryValue;
+ } else if (inLinePolicy != null) {
+ policyOmElement = inLinePolicy;
+ }
+ if (policyOmElement == null) {
+ log.warn("Cant not find a Policy - Throttling will not occur");
+ return;
+ }
+ if (shouldTrace) {
+ trace.trace("The Throttle Policy :" + policyOmElement.toString());
+ }
+ if (!reCreate) {
+ //The first time creation
+ if (throttle == null) {
+ createThrottleMetaData(policyOmElement);
+ }
+ } else {
+ createThrottleMetaData(policyOmElement);
+ }
+
+ }
+
+ /**
+ * To create the Throttle from the policy element
+ *
+ * @param policyOmElement - valid throttle policy
+ */
+ protected void createThrottleMetaData(OMElement policyOmElement) {
+ try {
+ log.info("Creating a new throttle configuration by parsing the Policy");
+ throttle = ThrottlePolicyProcessor
+ .processPoclicy(PolicyEngine.getPolicy(policyOmElement));
+ }
+ catch (ThrottleException e) {
+ handleException("Error during processing the thorttle policy " + e.getMessage());
+ }
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+ private void handleException(String msg, Exception e) {
+ log.info(msg);
+ log.error(e);
+ throw new SynapseException(msg);
+ }
+
+ public String getType() {
+ return ThrottleMediator.class.getName();
+ }
+
+ /**
+ * To get the policy key - The key for which lookup from the registry
+ *
+ * @return String
+ */
+ public String getPolicyKey() {
+ return policyKey;
+ }
+
+ /**
+ * To set the policy key - The key for which lookup from the registry
+ *
+ * @param policyKey
+ */
+ public void setPolicyKey(String policyKey) {
+ this.policyKey = policyKey;
+ }
+
+ /**
+ * getting throttle policy which has defined as InLineXML
+ *
+ * @return InLine Throttle Policy
+ */
+ public OMElement getInLinePolicy() {
+ return inLinePolicy;
+ }
+
+ /**
+ * setting throttle policy which has defined as InLineXML
+ *
+ * @param inLinePolicy
+ */
+ public void setInLinePolicy(OMElement inLinePolicy) {
+ this.inLinePolicy = inLinePolicy;
+ }
+}
Propchange: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediator.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorFactory.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorFactory.java?view=diff&rev=519363&r1=519362&r2=519363
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorFactory.java (original)
+++ webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorFactory.java Sat Mar 17 09:18:32 2007
@@ -1,83 +1,83 @@
-/*
- * 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.apache.synapse.mediators.throttle;
-
-import org.apache.axiom.om.OMAttribute;
-import org.apache.axiom.om.OMElement;
-import org.apache.synapse.Mediator;
-import org.apache.synapse.SynapseException;
-import org.apache.synapse.config.xml.AbstractMediatorFactory;
-import org.apache.synapse.config.xml.Constants;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import javax.xml.namespace.QName;
-
-
-/**
- * The Factory for create throttle mediator- key or InLine XMl need to provide
- */
-
-public class ThrottleMediatorFactory extends AbstractMediatorFactory {
-
- private static final Log log = LogFactory.getLog(ThrottleMediatorFactory.class);
-
- /**
- * The Tag Name for throttle
- */
- private static final QName TAG_NAME
- = new QName(Constants.SYNAPSE_NAMESPACE + "/throttle", "throttle");
-
- public Mediator createMediator(OMElement elem) {
-
- ThrottleMediator throttleMediator = new ThrottleMediator();
- OMElement policy = elem.getFirstChildWithName(
- new QName(Constants.SYNAPSE_NAMESPACE, "policy"));
- if (policy != null) {
- OMAttribute key = policy.getAttribute(new QName(Constants.NULL_NAMESPACE, "key"));
- if (key != null) {
- String keyValue = key.getAttributeValue();
- if (keyValue != null && !"".equals(keyValue)) {
- throttleMediator.setPolicyKey(keyValue);
- } else {
- handleException("key attribute should have a value ");
- }
- } else {
- OMElement inLine = policy.getFirstElement();
- if (inLine != null) {
- throttleMediator.setInLinePolicy(inLine);
- }
- }
- } else {
- handleException("Throttle Mediator must have a policy");
- }
- // after successfully creating the mediator
- // set its common attributes such as tracing etc
- initMediator(throttleMediator,elem);
- return throttleMediator;
- }
-
- private void handleException(String msg) {
- log.error(msg);
- throw new SynapseException(msg);
- }
-
- public QName getTagQName() {
- return TAG_NAME;
- }
-}
+/*
+ * 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.apache.synapse.mediators.throttle;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.config.xml.AbstractMediatorFactory;
+import org.apache.synapse.config.xml.Constants;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import javax.xml.namespace.QName;
+
+
+/**
+ * The Factory for create throttle mediator- key or InLine XMl need to provide
+ */
+
+public class ThrottleMediatorFactory extends AbstractMediatorFactory {
+
+ private static final Log log = LogFactory.getLog(ThrottleMediatorFactory.class);
+
+ /**
+ * The Tag Name for throttle
+ */
+ private static final QName TAG_NAME
+ = new QName(Constants.SYNAPSE_NAMESPACE + "/throttle", "throttle");
+
+ public Mediator createMediator(OMElement elem) {
+
+ ThrottleMediator throttleMediator = new ThrottleMediator();
+ OMElement policy = elem.getFirstChildWithName(
+ new QName(Constants.SYNAPSE_NAMESPACE, "policy"));
+ if (policy != null) {
+ OMAttribute key = policy.getAttribute(new QName(Constants.NULL_NAMESPACE, "key"));
+ if (key != null) {
+ String keyValue = key.getAttributeValue();
+ if (keyValue != null && !"".equals(keyValue)) {
+ throttleMediator.setPolicyKey(keyValue);
+ } else {
+ handleException("key attribute should have a value ");
+ }
+ } else {
+ OMElement inLine = policy.getFirstElement();
+ if (inLine != null) {
+ throttleMediator.setInLinePolicy(inLine);
+ }
+ }
+ } else {
+ handleException("Throttle Mediator must have a policy");
+ }
+ // after successfully creating the mediator
+ // set its common attributes such as tracing etc
+ initMediator(throttleMediator,elem);
+ return throttleMediator;
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+ public QName getTagQName() {
+ return TAG_NAME;
+ }
+}
Propchange: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializer.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializer.java?view=diff&rev=519363&r1=519362&r2=519363
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializer.java (original)
+++ webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializer.java Sat Mar 17 09:18:32 2007
@@ -1,78 +1,78 @@
-/*
- * 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.apache.synapse.mediators.throttle;
-
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.OMNamespace;
-import org.apache.axiom.om.OMNode;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.synapse.Mediator;
-import org.apache.synapse.SynapseException;
-import org.apache.synapse.config.xml.AbstractMediatorSerializer;
-import org.apache.synapse.config.xml.Constants;
-
-/**
- * The Serializer for Throttle Mediator saving throttle instance
- */
-
-public class ThrottleMediatorSerializer extends AbstractMediatorSerializer {
-
- private static final Log log = LogFactory.getLog(ThrottleMediatorSerializer.class);
-
- private static final OMNamespace throttleNS
- = fac.createOMNamespace(Constants.SYNAPSE_NAMESPACE + "/throttle", "throttle");
-
- public OMElement serializeMediator(OMElement parent, Mediator m) {
- if (!(m instanceof ThrottleMediator)) {
- handleException("Invalid Mediator has passed to serializer");
- }
- ThrottleMediator throttleMediator = (ThrottleMediator) m;
- OMElement throttle = fac.createOMElement("throttle", throttleNS);
- OMElement policy = fac.createOMElement("policy", synNS);
- String key = throttleMediator.getPolicyKey();
- if (key != null) {
- policy.addAttribute(fac.createOMAttribute(
- "key", nullNS, key));
- throttle.addChild(policy);
- } else {
- OMNode inlinePolicy = throttleMediator.getInLinePolicy();
- if (inlinePolicy != null) {
- policy.addChild(inlinePolicy);
- throttle.addChild(policy);
- }
- }
- finalizeSerialization(throttle, throttleMediator);
- if (parent != null) {
- parent.addChild(throttle);
- }
- return throttle;
-
- }
-
- public String getMediatorClassName() {
- return ThrottleMediator.class.getName();
- }
-
- private void handleException(String msg) {
- log.error(msg);
- throw new SynapseException(msg);
- }
-
-}
+/*
+ * 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.apache.synapse.mediators.throttle;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMNode;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.config.xml.AbstractMediatorSerializer;
+import org.apache.synapse.config.xml.Constants;
+
+/**
+ * The Serializer for Throttle Mediator saving throttle instance
+ */
+
+public class ThrottleMediatorSerializer extends AbstractMediatorSerializer {
+
+ private static final Log log = LogFactory.getLog(ThrottleMediatorSerializer.class);
+
+ private static final OMNamespace throttleNS
+ = fac.createOMNamespace(Constants.SYNAPSE_NAMESPACE + "/throttle", "throttle");
+
+ public OMElement serializeMediator(OMElement parent, Mediator m) {
+ if (!(m instanceof ThrottleMediator)) {
+ handleException("Invalid Mediator has passed to serializer");
+ }
+ ThrottleMediator throttleMediator = (ThrottleMediator) m;
+ OMElement throttle = fac.createOMElement("throttle", throttleNS);
+ OMElement policy = fac.createOMElement("policy", synNS);
+ String key = throttleMediator.getPolicyKey();
+ if (key != null) {
+ policy.addAttribute(fac.createOMAttribute(
+ "key", nullNS, key));
+ throttle.addChild(policy);
+ } else {
+ OMNode inlinePolicy = throttleMediator.getInLinePolicy();
+ if (inlinePolicy != null) {
+ policy.addChild(inlinePolicy);
+ throttle.addChild(policy);
+ }
+ }
+ finalizeSerialization(throttle, throttleMediator);
+ if (parent != null) {
+ parent.addChild(throttle);
+ }
+ return throttle;
+
+ }
+
+ public String getMediatorClassName() {
+ return ThrottleMediator.class.getName();
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+}
Propchange: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializer.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java?view=diff&rev=519363&r1=519362&r2=519363
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java (original)
+++ webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java Sat Mar 17 09:18:32 2007
@@ -1,267 +1,267 @@
-/*
- * 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.apache.synapse.mediators.transform;
-
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.OMNode;
-import org.apache.axiom.om.impl.builder.StAXOMBuilder;
-import org.apache.axiom.om.xpath.AXIOMXPath;
-import org.apache.axiom.soap.SOAP11Constants;
-import org.apache.axiom.soap.SOAP12Constants;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.synapse.MessageContext;
-import org.apache.synapse.SynapseException;
-import org.apache.synapse.Constants;
-import org.apache.synapse.config.Util;
-import org.apache.synapse.config.Entry;
-import org.apache.synapse.mediators.AbstractMediator;
-import org.apache.synapse.mediators.MediatorProperty;
-import org.jaxen.JaxenException;
-
-import javax.xml.stream.XMLOutputFactory;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamWriter;
-import javax.xml.transform.*;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * The XSLT mediator performs an XSLT transformation requested, using
- * the current message. The source attribute (if available) spcifies the source element
- * on which the transformation would be applied. It will default to the first child of
- * the messages' SOAP body, if it is omitted. Additional properties passed into this
- * mediator would become parameters for XSLT
- */
-public class XSLTMediator extends AbstractMediator {
-
- private static final Log log = LogFactory.getLog(XSLTMediator.class);
- private static final Log trace = LogFactory.getLog(Constants.TRACE_LOGGER);
-
- /** The property key/name which refers to the XSLT to be used for the transformation */
- private String xsltKey = null;
-
- /** The (optional) XPath expression which yeilds the source element for a transformation */
- private AXIOMXPath source = null;
-
- /** Any parameters which should be passed into the XSLT transformation */
- private List properties = new ArrayList();
-
- /**
- * The Transformer instance used to perform XSLT transformations. This is not thread-safe
- * @see javax.xml.transform.Transformer
- */
- private Transformer transformer = null;
-
- /** Lock used to ensure thread-safe creation and use of the above Transformer */
- private final Object transformerLock = new Object();
-
- public static final String DEFAULT_XPATH = "//s11:Envelope/s11:Body/child::*[position()=1] | " +
- "//s12:Envelope/s12:Body/child::*[position()=1]";
-
- public XSLTMediator() {
- // create the default XPath
- try {
- this.source = new AXIOMXPath(DEFAULT_XPATH);
- this.source.addNamespace("s11", SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
- this.source.addNamespace("s12", SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
- } catch (JaxenException e) {
- handleException("Error creating source XPath expression", e);
- }
- }
-
- /**
- * Transforms this message (or its element specified as the source) using the
- * given XSLT transformation
- * @param synCtx the current message where the transformation will apply
- * @return true always
- */
- public boolean mediate(MessageContext synCtx) {
- log.debug("XSLT mediator mediate()");
- boolean shouldTrace = shouldTrace(synCtx.getTracingState());
- if (shouldTrace) {
- trace.trace("Start : XSLT mediator");
- }
- log.debug("Performing XSLT transformation against property with key : " + xsltKey);
- performXLST(synCtx,shouldTrace);
- if (shouldTrace) {
- trace.trace("Start : XSLT mediator");
- }
- return true;
- }
-
- private void performXLST(MessageContext msgCtx,boolean shouldTrace) {
-
- Source transformSrc = null;
- ByteArrayOutputStream baosForTarget = new ByteArrayOutputStream();
-
- // create a new Stream result over a new BAOS..
- StreamResult transformTgt = new StreamResult(baosForTarget);
-
- OMNode sourceNode = getTransformSource(msgCtx);
- if (shouldTrace) {
- trace.trace("Transformation source : " + sourceNode.toString());
- }
- if (log.isDebugEnabled()) {
- log.debug("Transformation source : " + sourceNode);
- }
-
- try {
- // create a byte array output stream and serialize the source node into it
- ByteArrayOutputStream baosForSource = new ByteArrayOutputStream();
- XMLStreamWriter xsWriterForSource = XMLOutputFactory.newInstance().
- createXMLStreamWriter(baosForSource);
-
- sourceNode.serialize(xsWriterForSource);
- transformSrc = new StreamSource(new ByteArrayInputStream(baosForSource.toByteArray()));
-
- } catch (XMLStreamException e) {
- handleException("Error gettting transform source " + e.getMessage(), e);
- }
-
- // build transformer - if necessary
- Entry dp = msgCtx.getConfiguration().getEntryDefinition(xsltKey);
-
- // if the xsltKey refers to a dynamic property
- if (dp != null && dp.isDynamic()) {
- if (!dp.isCached() || dp.isExpired()) {
- synchronized(transformerLock) {
- try {
- transformer = TransformerFactory.newInstance().
- newTransformer(Util.getStreamSource(
- msgCtx.getConfiguration().getEntry(xsltKey)
- ));
- } catch (TransformerConfigurationException e) {
- handleException("Error creating XSLT transformer using : " + xsltKey, e);
- }
- }
- }
-
- // if the property is not a DynamicProperty, we will create a transformer only once
- } else {
- if (transformer == null) {
- synchronized(transformerLock) {
- try {
- transformer = TransformerFactory.newInstance().
- newTransformer(
- Util.getStreamSource(
- msgCtx.getConfiguration().getEntry(xsltKey)));
- } catch (TransformerConfigurationException e) {
- handleException("Error creating XSLT transformer using : " + xsltKey, e);
- }
- }
- }
- }
-
- try {
- // perform transformation
- transformer.transform(transformSrc, transformTgt);
-
- StAXOMBuilder builder = new StAXOMBuilder(
- new ByteArrayInputStream(baosForTarget.toByteArray()));
- OMElement result = builder.getDocumentElement();
- if (shouldTrace) {
- trace.trace("Transformation result : " + result.toString());
- }
- if (log.isDebugEnabled()) {
- log.debug("Transformation result : " + result);
- }
-
- // replace the sourceNode with the result.
- sourceNode.insertSiblingAfter( result );
- sourceNode.detach();
-
- } catch (TransformerException e) {
- handleException("Error performing XSLT transformation " + xsltKey, e);
- } catch (XMLStreamException e) {
- handleException("Error building result from XSLT transformation", e);
- }
- }
-
-
- /**
- * Return the OMNode to be used for the transformation. If a source XPath is not specified,
- * this will default to the first child of the SOAP body i.e. - //*:Envelope/*:Body/child::*
- *
- * @param synCtx the message context
- * @return the OMNode against which the transformation should be performed
- */
- private OMNode getTransformSource(MessageContext synCtx) {
-
- try {
- Object o = source.evaluate(synCtx.getEnvelope());
- if (o instanceof OMNode) {
- return (OMNode) o;
- } else if (o instanceof List && !((List) o).isEmpty()) {
- return (OMNode) ((List) o).get(0); // Always fetches *only* the first
- } else {
- handleException("The evaluation of the XPath expression "
- + source + " must result in an OMNode");
- }
- } catch (JaxenException e) {
- handleException("Error evaluating XPath " + source + " on message");
- }
- return null;
- }
-
- private void handleException(String msg, Exception e) {
- log.error(msg, e);
- throw new SynapseException(msg, e);
- }
-
- private void handleException(String msg) {
- log.error(msg);
- throw new SynapseException(msg);
- }
-
- public AXIOMXPath getSource() {
- return source;
- }
-
- public void setSource(AXIOMXPath source) {
- this.source = source;
- }
-
- public String getXsltKey() {
- return xsltKey;
- }
-
- public void setXsltKey(String xsltKey) {
- this.xsltKey = xsltKey;
- }
-
- public void addProperty(MediatorProperty p) {
- properties.add(p);
- }
-
- public void addAllProperties(List list) {
- properties.addAll(list);
- }
-
- public List getProperties() {
- return properties;
- }
-}
-
-
+/*
+ * 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.apache.synapse.mediators.transform;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.axiom.soap.SOAP11Constants;
+import org.apache.axiom.soap.SOAP12Constants;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.Constants;
+import org.apache.synapse.config.Util;
+import org.apache.synapse.config.Entry;
+import org.apache.synapse.mediators.AbstractMediator;
+import org.apache.synapse.mediators.MediatorProperty;
+import org.jaxen.JaxenException;
+
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.transform.*;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * The XSLT mediator performs an XSLT transformation requested, using
+ * the current message. The source attribute (if available) spcifies the source element
+ * on which the transformation would be applied. It will default to the first child of
+ * the messages' SOAP body, if it is omitted. Additional properties passed into this
+ * mediator would become parameters for XSLT
+ */
+public class XSLTMediator extends AbstractMediator {
+
+ private static final Log log = LogFactory.getLog(XSLTMediator.class);
+ private static final Log trace = LogFactory.getLog(Constants.TRACE_LOGGER);
+
+ /** The property key/name which refers to the XSLT to be used for the transformation */
+ private String xsltKey = null;
+
+ /** The (optional) XPath expression which yeilds the source element for a transformation */
+ private AXIOMXPath source = null;
+
+ /** Any parameters which should be passed into the XSLT transformation */
+ private List properties = new ArrayList();
+
+ /**
+ * The Transformer instance used to perform XSLT transformations. This is not thread-safe
+ * @see javax.xml.transform.Transformer
+ */
+ private Transformer transformer = null;
+
+ /** Lock used to ensure thread-safe creation and use of the above Transformer */
+ private final Object transformerLock = new Object();
+
+ public static final String DEFAULT_XPATH = "//s11:Envelope/s11:Body/child::*[position()=1] | " +
+ "//s12:Envelope/s12:Body/child::*[position()=1]";
+
+ public XSLTMediator() {
+ // create the default XPath
+ try {
+ this.source = new AXIOMXPath(DEFAULT_XPATH);
+ this.source.addNamespace("s11", SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
+ this.source.addNamespace("s12", SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
+ } catch (JaxenException e) {
+ handleException("Error creating source XPath expression", e);
+ }
+ }
+
+ /**
+ * Transforms this message (or its element specified as the source) using the
+ * given XSLT transformation
+ * @param synCtx the current message where the transformation will apply
+ * @return true always
+ */
+ public boolean mediate(MessageContext synCtx) {
+ log.debug("XSLT mediator mediate()");
+ boolean shouldTrace = shouldTrace(synCtx.getTracingState());
+ if (shouldTrace) {
+ trace.trace("Start : XSLT mediator");
+ }
+ log.debug("Performing XSLT transformation against property with key : " + xsltKey);
+ performXLST(synCtx,shouldTrace);
+ if (shouldTrace) {
+ trace.trace("Start : XSLT mediator");
+ }
+ return true;
+ }
+
+ private void performXLST(MessageContext msgCtx,boolean shouldTrace) {
+
+ Source transformSrc = null;
+ ByteArrayOutputStream baosForTarget = new ByteArrayOutputStream();
+
+ // create a new Stream result over a new BAOS..
+ StreamResult transformTgt = new StreamResult(baosForTarget);
+
+ OMNode sourceNode = getTransformSource(msgCtx);
+ if (shouldTrace) {
+ trace.trace("Transformation source : " + sourceNode.toString());
+ }
+ if (log.isDebugEnabled()) {
+ log.debug("Transformation source : " + sourceNode);
+ }
+
+ try {
+ // create a byte array output stream and serialize the source node into it
+ ByteArrayOutputStream baosForSource = new ByteArrayOutputStream();
+ XMLStreamWriter xsWriterForSource = XMLOutputFactory.newInstance().
+ createXMLStreamWriter(baosForSource);
+
+ sourceNode.serialize(xsWriterForSource);
+ transformSrc = new StreamSource(new ByteArrayInputStream(baosForSource.toByteArray()));
+
+ } catch (XMLStreamException e) {
+ handleException("Error gettting transform source " + e.getMessage(), e);
+ }
+
+ // build transformer - if necessary
+ Entry dp = msgCtx.getConfiguration().getEntryDefinition(xsltKey);
+
+ // if the xsltKey refers to a dynamic property
+ if (dp != null && dp.isDynamic()) {
+ if (!dp.isCached() || dp.isExpired()) {
+ synchronized(transformerLock) {
+ try {
+ transformer = TransformerFactory.newInstance().
+ newTransformer(Util.getStreamSource(
+ msgCtx.getConfiguration().getEntry(xsltKey)
+ ));
+ } catch (TransformerConfigurationException e) {
+ handleException("Error creating XSLT transformer using : " + xsltKey, e);
+ }
+ }
+ }
+
+ // if the property is not a DynamicProperty, we will create a transformer only once
+ } else {
+ if (transformer == null) {
+ synchronized(transformerLock) {
+ try {
+ transformer = TransformerFactory.newInstance().
+ newTransformer(
+ Util.getStreamSource(
+ msgCtx.getConfiguration().getEntry(xsltKey)));
+ } catch (TransformerConfigurationException e) {
+ handleException("Error creating XSLT transformer using : " + xsltKey, e);
+ }
+ }
+ }
+ }
+
+ try {
+ // perform transformation
+ transformer.transform(transformSrc, transformTgt);
+
+ StAXOMBuilder builder = new StAXOMBuilder(
+ new ByteArrayInputStream(baosForTarget.toByteArray()));
+ OMElement result = builder.getDocumentElement();
+ if (shouldTrace) {
+ trace.trace("Transformation result : " + result.toString());
+ }
+ if (log.isDebugEnabled()) {
+ log.debug("Transformation result : " + result);
+ }
+
+ // replace the sourceNode with the result.
+ sourceNode.insertSiblingAfter( result );
+ sourceNode.detach();
+
+ } catch (TransformerException e) {
+ handleException("Error performing XSLT transformation " + xsltKey, e);
+ } catch (XMLStreamException e) {
+ handleException("Error building result from XSLT transformation", e);
+ }
+ }
+
+
+ /**
+ * Return the OMNode to be used for the transformation. If a source XPath is not specified,
+ * this will default to the first child of the SOAP body i.e. - //*:Envelope/*:Body/child::*
+ *
+ * @param synCtx the message context
+ * @return the OMNode against which the transformation should be performed
+ */
+ private OMNode getTransformSource(MessageContext synCtx) {
+
+ try {
+ Object o = source.evaluate(synCtx.getEnvelope());
+ if (o instanceof OMNode) {
+ return (OMNode) o;
+ } else if (o instanceof List && !((List) o).isEmpty()) {
+ return (OMNode) ((List) o).get(0); // Always fetches *only* the first
+ } else {
+ handleException("The evaluation of the XPath expression "
+ + source + " must result in an OMNode");
+ }
+ } catch (JaxenException e) {
+ handleException("Error evaluating XPath " + source + " on message");
+ }
+ return null;
+ }
+
+ private void handleException(String msg, Exception e) {
+ log.error(msg, e);
+ throw new SynapseException(msg, e);
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+ public AXIOMXPath getSource() {
+ return source;
+ }
+
+ public void setSource(AXIOMXPath source) {
+ this.source = source;
+ }
+
+ public String getXsltKey() {
+ return xsltKey;
+ }
+
+ public void setXsltKey(String xsltKey) {
+ this.xsltKey = xsltKey;
+ }
+
+ public void addProperty(MediatorProperty p) {
+ properties.add(p);
+ }
+
+ public void addAllProperties(List list) {
+ properties.addAll(list);
+ }
+
+ public List getProperties() {
+ return properties;
+ }
+}
+
+
Propchange: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/transform/XSLTMediatorFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/transform/XSLTMediatorSerializer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/validate/ValidateMediator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/validate/ValidateMediatorFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/validate/ValidateMediatorSerializer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/TestMediateHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/TestMediator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/TestMessageContext.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/AbstractTestCase.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/AbstractTestCase.java?view=diff&rev=519363&r1=519362&r2=519363
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/AbstractTestCase.java (original)
+++ webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/AbstractTestCase.java Sat Mar 17 09:18:32 2007
@@ -1,127 +1,127 @@
-/*
- * 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.apache.synapse.mediators;
-
-import org.apache.axiom.om.impl.builder.StAXOMBuilder;
-import org.apache.axiom.om.OMElement;
-import org.apache.synapse.config.xml.MediatorFactory;
-import org.apache.synapse.config.xml.MediatorSerializer;
-import org.apache.synapse.config.xml.MediatorFactoryFinder;
-import org.apache.synapse.Mediator;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.xml.sax.SAXException;
-import org.custommonkey.xmlunit.XMLTestCase;
-
-import javax.xml.stream.XMLStreamReader;
-import javax.xml.stream.XMLInputFactory;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.parsers.ParserConfigurationException;
-import java.io.StringReader;
-import java.io.IOException;
-
-/**
- *
- *
- */
-
-public abstract class AbstractTestCase extends XMLTestCase {
-
-
- private static final Log log = LogFactory.getLog(AbstractTestCase.class);
-
- public AbstractTestCase(String name) {
- super(name);
- }
-
- public AbstractTestCase() {
- }
-
- protected void setUp() throws Exception {
- super.setUp();
- }
-
- protected OMElement createOMElement(String xml) {
- try {
-
- XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xml));
- StAXOMBuilder builder = new StAXOMBuilder(reader);
- OMElement omElement = builder.getDocumentElement();
- return omElement;
-
- }
- catch (XMLStreamException e) {
- throw new RuntimeException(e);
- }
- }
-
- protected boolean serialization(String inputXml, MediatorFactory mediatorFactory, MediatorSerializer mediatorSerializer) {
-
- OMElement inputOM = createOMElement(inputXml);
- Mediator mediator = mediatorFactory.createMediator(inputOM);
- OMElement resultOM = mediatorSerializer.serializeMediator(null, mediator);
- try {
- assertXMLEqual(resultOM.toString(), inputXml);
- return true;
- } catch (SAXException e) {
- log.error(e);
- } catch (IOException e) {
- log.error(e);
- } catch (ParserConfigurationException e) {
- log.error(e);
- }
- return false;
- }
-
- protected boolean serialization(String inputXml, MediatorSerializer mediatorSerializer) {
- OMElement inputOM = createOMElement(inputXml);
- Mediator mediator = MediatorFactoryFinder.getInstance().getMediator(inputOM);
- OMElement resultOM = mediatorSerializer.serializeMediator(null, mediator);
- try {
- assertXMLEqual(resultOM.toString(), inputXml);
- return true;
- } catch (SAXException e) {
- log.error(e);
- } catch (IOException e) {
- log.error(e);
- } catch (ParserConfigurationException e) {
- log.error(e);
- }
- return false;
- }
-
- protected OMElement getParent() {
- String parentXML = "<synapse xmlns=\"http://ws.apache.org/ns/synapse\"><definitions></definitions></synapse>";
- return createOMElement(parentXML);
- }
-
- protected boolean compare(OMElement inputElement, OMElement serializedElement) {
- try {
- assertXMLEqual(inputElement.toString(), serializedElement.toString());
- return true;
- } catch (SAXException e) {
- log.error(e);
- } catch (IOException e) {
- log.error(e);
- } catch (ParserConfigurationException e) {
- log.error(e);
- }
- return false;
- }
+/*
+ * 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.apache.synapse.mediators;
+
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.om.OMElement;
+import org.apache.synapse.config.xml.MediatorFactory;
+import org.apache.synapse.config.xml.MediatorSerializer;
+import org.apache.synapse.config.xml.MediatorFactoryFinder;
+import org.apache.synapse.Mediator;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.xml.sax.SAXException;
+import org.custommonkey.xmlunit.XMLTestCase;
+
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.StringReader;
+import java.io.IOException;
+
+/**
+ *
+ *
+ */
+
+public abstract class AbstractTestCase extends XMLTestCase {
+
+
+ private static final Log log = LogFactory.getLog(AbstractTestCase.class);
+
+ public AbstractTestCase(String name) {
+ super(name);
+ }
+
+ public AbstractTestCase() {
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ protected OMElement createOMElement(String xml) {
+ try {
+
+ XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xml));
+ StAXOMBuilder builder = new StAXOMBuilder(reader);
+ OMElement omElement = builder.getDocumentElement();
+ return omElement;
+
+ }
+ catch (XMLStreamException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ protected boolean serialization(String inputXml, MediatorFactory mediatorFactory, MediatorSerializer mediatorSerializer) {
+
+ OMElement inputOM = createOMElement(inputXml);
+ Mediator mediator = mediatorFactory.createMediator(inputOM);
+ OMElement resultOM = mediatorSerializer.serializeMediator(null, mediator);
+ try {
+ assertXMLEqual(resultOM.toString(), inputXml);
+ return true;
+ } catch (SAXException e) {
+ log.error(e);
+ } catch (IOException e) {
+ log.error(e);
+ } catch (ParserConfigurationException e) {
+ log.error(e);
+ }
+ return false;
+ }
+
+ protected boolean serialization(String inputXml, MediatorSerializer mediatorSerializer) {
+ OMElement inputOM = createOMElement(inputXml);
+ Mediator mediator = MediatorFactoryFinder.getInstance().getMediator(inputOM);
+ OMElement resultOM = mediatorSerializer.serializeMediator(null, mediator);
+ try {
+ assertXMLEqual(resultOM.toString(), inputXml);
+ return true;
+ } catch (SAXException e) {
+ log.error(e);
+ } catch (IOException e) {
+ log.error(e);
+ } catch (ParserConfigurationException e) {
+ log.error(e);
+ }
+ return false;
+ }
+
+ protected OMElement getParent() {
+ String parentXML = "<synapse xmlns=\"http://ws.apache.org/ns/synapse\"><definitions></definitions></synapse>";
+ return createOMElement(parentXML);
+ }
+
+ protected boolean compare(OMElement inputElement, OMElement serializedElement) {
+ try {
+ assertXMLEqual(inputElement.toString(), serializedElement.toString());
+ return true;
+ } catch (SAXException e) {
+ log.error(e);
+ } catch (IOException e) {
+ log.error(e);
+ } catch (ParserConfigurationException e) {
+ log.error(e);
+ }
+ return false;
+ }
}
Propchange: webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/AbstractTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/TestUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: synapse-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: synapse-dev-help@ws.apache.org