Skip to content

Instantly share code, notes, and snippets.

@cschneider
Created January 18, 2012 14:49
Show Gist options
  • Save cschneider/1633319 to your computer and use it in GitHub Desktop.
Save cschneider/1633319 to your computer and use it in GitHub Desktop.
Index: src/main/java/org/apache/cxf/transport/http/policy/PolicyDataEngine.java
===================================================================
--- src/main/java/org/apache/cxf/transport/http/policy/PolicyDataEngine.java (revision 0)
+++ src/main/java/org/apache/cxf/transport/http/policy/PolicyDataEngine.java (revision 0)
@@ -0,0 +1,41 @@
+/**
+ * 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.cxf.transport.http.policy;
+
+import javax.xml.namespace.QName;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.service.model.EndpointInfo;
+import org.apache.cxf.transport.Conduit;
+import org.apache.cxf.transport.Destination;
+
+public interface PolicyDataEngine {
+ <T> T getPolicy(QName name, Message message, T confPolicy, Class<T> dataClass,
+ PolicyCalculator<T> intersector);
+
+ <T> T getClientEndpointPolicy(QName name, Bus bus, EndpointInfo ei, Conduit c, Class<T> dataClass,
+ PolicyCalculator<T> policyCalculator);
+
+ <T> T getServerEndpointPolicy(QName name, Bus bus, EndpointInfo ei, Destination d,
+ Class<T> dataClass, PolicyCalculator<T> policyCalculator);
+
+ <T> void assertMessage(QName name, Message message, T confPol, PolicyCalculator<T> policyCalculator,
+ Class<T> dataClass);
+}
Index: src/main/java/org/apache/cxf/transport/http/policy/HTTPClientAssertionBuilder.java
===================================================================
--- src/main/java/org/apache/cxf/transport/http/policy/HTTPClientAssertionBuilder.java (revision 1232340)
+++ src/main/java/org/apache/cxf/transport/http/policy/HTTPClientAssertionBuilder.java (working copy)
@@ -65,7 +65,7 @@
return false;
}
JaxbAssertion<HTTPClientPolicy> other = JaxbAssertion.cast((Assertion)policyComponent);
- return PolicyUtils.equals(this.getData(), other.getData());
+ return new ClientPolicyCalculator().equals(this.getData(), other.getData());
}
@Override
Index: src/main/java/org/apache/cxf/transport/http/policy/PolicyCalculator.java
===================================================================
--- src/main/java/org/apache/cxf/transport/http/policy/PolicyCalculator.java (revision 0)
+++ src/main/java/org/apache/cxf/transport/http/policy/PolicyCalculator.java (revision 0)
@@ -0,0 +1,27 @@
+/**
+ * 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.cxf.transport.http.policy;
+
+import org.apache.cxf.message.Message;
+
+public interface PolicyCalculator<T> {
+ T intersect(T policy1, T policy2);
+
+ boolean isAsserted(Message message, T policy, T refPolicy);
+}
Index: src/main/java/org/apache/cxf/transport/http/policy/PolicyUtils.java
===================================================================
--- src/main/java/org/apache/cxf/transport/http/policy/PolicyUtils.java (revision 1232340)
+++ src/main/java/org/apache/cxf/transport/http/policy/PolicyUtils.java (working copy)
@@ -16,697 +16,125 @@
* specific language governing permissions and limitations
* under the License.
*/
-
package org.apache.cxf.transport.http.policy;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
import javax.xml.namespace.QName;
import org.apache.cxf.Bus;
-import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.message.Message;
-import org.apache.cxf.message.MessageUtils;
import org.apache.cxf.service.model.EndpointInfo;
import org.apache.cxf.transport.Conduit;
import org.apache.cxf.transport.Destination;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.apache.cxf.transports.http.configuration.HTTPServerPolicy;
-import org.apache.cxf.ws.policy.AssertionInfo;
-import org.apache.cxf.ws.policy.AssertionInfoMap;
-import org.apache.cxf.ws.policy.PolicyEngine;
-import org.apache.cxf.ws.policy.PolicyException;
-import org.apache.cxf.ws.policy.builder.jaxb.JaxbAssertion;
-import org.apache.neethi.Assertion;
+import org.apache.cxf.transports.http.configuration.ObjectFactory;
-/**
- *
- */
public final class PolicyUtils {
+ public static final QName HTTPCLIENTPOLICY_ASSERTION_QNAME = new ObjectFactory().createClient(null)
+ .getName();
+ public static final QName HTTPSERVERPOLICY_ASSERTION_QNAME = new ObjectFactory().createServer(null)
+ .getName();
- public static final String HTTPCONF_NAMESPACE =
- "http://cxf.apache.org/transports/http/configuration";
- public static final QName HTTPCLIENTPOLICY_ASSERTION_QNAME =
- new QName(HTTPCONF_NAMESPACE, "client");
- public static final QName HTTPSERVERPOLICY_ASSERTION_QNAME =
- new QName(HTTPCONF_NAMESPACE, "server");
-
- private static final Logger LOG = LogUtils.getL7dLogger(PolicyUtils.class);
-
- /**
- * Prevents instantiation.
- *
- */
- private PolicyUtils() {
+ private PolicyUtils() {
}
-
/**
- * Returns a HTTPClientPolicy that is compatible with the assertions included in the
- * service, endpoint, operation and message policy subjects AND the HTTPClientPolicy
- * passed as a second argument.
+ * Returns a HTTPClientPolicy that is compatible with the assertions
+ * included in the service, endpoint, operation and message policy subjects
+ * AND the HTTPClientPolicy passed as a second argument.
+ *
* @param message the message
* @param confPolicy the additional policy to be compatible with
* @return the HTTPClientPolicy for the message
- * @throws PolicyException if no compatible HTTPClientPolicy can be determined
+ * @throws PolicyException if no compatible HTTPClientPolicy can be
+ * determined
*/
public static HTTPClientPolicy getClient(Message message, HTTPClientPolicy confPolicy) {
- HTTPClientPolicy pol = message.get(HTTPClientPolicy.class);
- if (pol != null) {
- return intersect(pol, confPolicy);
- }
- AssertionInfoMap amap = message.get(AssertionInfoMap.class);
- if (null == amap || amap.isEmpty()) {
- return confPolicy;
- }
- Collection<AssertionInfo> ais = amap.get(HTTPCLIENTPOLICY_ASSERTION_QNAME);
- if (null == ais) {
- return confPolicy;
- }
- Collection<Assertion> alternative = new ArrayList<Assertion>();
- for (AssertionInfo ai : ais) {
- alternative.add(ai.getAssertion());
- }
- HTTPClientPolicy compatible = getClient(alternative);
- if (null != compatible && null != confPolicy) {
- if (PolicyUtils.compatible(compatible, confPolicy)) {
- compatible = intersect(compatible, confPolicy);
- } else {
- LogUtils.log(LOG, Level.SEVERE, "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS");
- throw new PolicyException(new org.apache.cxf.common.i18n.Message(
- "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS", LOG));
- }
- }
- return compatible;
- }
-
- /**
- * Returns a HTTPServerPolicy that is compatible with the assertions included in the
- * service, endpoint, operation and message policy subjects AND the HTTPServerPolicy
- * passed as a second argument.
- * @param message the message
- * @param confPolicy the additional policy to be compatible with
- * @return the HTTPServerPolicy for the message
- * @throws PolicyException if no compatible HTTPServerPolicy can be determined
- */
- public static HTTPServerPolicy getServer(Message message, HTTPServerPolicy confPolicy) {
- AssertionInfoMap amap = message.get(AssertionInfoMap.class);
- if (null == amap) {
- return confPolicy;
- }
- Collection<AssertionInfo> ais = amap.get(HTTPSERVERPOLICY_ASSERTION_QNAME);
- if (null == ais) {
- return confPolicy;
- }
- Collection<Assertion> alternative = new ArrayList<Assertion>();
- for (AssertionInfo ai : ais) {
- alternative.add(ai.getAssertion());
- }
- HTTPServerPolicy compatible = getServer(alternative);
- if (null != compatible && null != confPolicy) {
- if (PolicyUtils.compatible(compatible, confPolicy)) {
- compatible = intersect(compatible, confPolicy);
- } else {
- LogUtils.log(LOG, Level.SEVERE, "INCOMPATIBLE_HTTPSERVERPOLICY_ASSERTIONS");
- throw new PolicyException(new org.apache.cxf.common.i18n.Message(
- "INCOMPATIBLE_HTTPSERVERPOLICY_ASSERTIONS", LOG));
- }
- }
- return compatible;
+ PolicyDataEngine policyDataEngine = new PolicyDataEngineImpl();
+ return policyDataEngine.getPolicy(HTTPCLIENTPOLICY_ASSERTION_QNAME, message, confPolicy,
+ HTTPClientPolicy.class, new ClientPolicyCalculator());
}
/**
- * Returns a HTTPClientPolicy that is compatible with the assertions included in the
- * service and endpoint policy subjects, or null if there are no such assertions.
+ * Returns a HTTPClientPolicy that is compatible with the assertions
+ * included in the service and endpoint policy subjects, or null if there
+ * are no such assertions.
+ *
* @param bus the bus
* @param ei the endpoint info
* @param c the conduit
* @return the compatible policy
- * @throws PolicyException if no compatible HTTPClientPolicy can be determined
+ * @throws PolicyException if no compatible HTTPClientPolicy can be
+ * determined
*/
public static HTTPClientPolicy getClient(Bus bus, EndpointInfo ei, Conduit c) {
- PolicyEngine pe = bus.getExtension(PolicyEngine.class);
- Collection<Assertion> alternative = pe.getClientEndpointPolicy(ei, c).getChosenAlternative();
- HTTPClientPolicy compatible = null;
- for (Assertion a : alternative) {
- if (HTTPCLIENTPOLICY_ASSERTION_QNAME.equals(a.getName())) {
- HTTPClientPolicy p = JaxbAssertion.cast(a, HTTPClientPolicy.class).getData();
- if (null == compatible) {
- compatible = p;
- } else {
- compatible = intersect(compatible, p);
- if (null == compatible) {
- LogUtils.log(LOG, Level.SEVERE, "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS");
- throw new PolicyException(new org.apache.cxf.common.i18n.Message(
- "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS", LOG));
- }
- }
- }
- }
- return compatible;
+ PolicyDataEngine policyDataEngine = new PolicyDataEngineImpl();
+ return policyDataEngine.getClientEndpointPolicy(HTTPCLIENTPOLICY_ASSERTION_QNAME, bus, ei, c,
+ HTTPClientPolicy.class, new ClientPolicyCalculator());
}
/**
- * Returns a HTTPServerPolicy that is compatible with the assertions included in the
- * service and endpoint policy subjects, or null if there are no such assertions.
- * @param bus the bus
- * @param ei the endpoint info
- * @param d the destination
- * @return the compatible policy
- * @throws PolicyException if no compatible HTTPServerPolicy can be determined
- */
- public static HTTPServerPolicy getServer(Bus b, EndpointInfo ei, Destination d) {
- PolicyEngine pe = b.getExtension(PolicyEngine.class);
- Collection<Assertion> alternative = pe.getServerEndpointPolicy(ei, d).getChosenAlternative();
- HTTPServerPolicy compatible = null;
- for (Assertion a : alternative) {
- if (HTTPSERVERPOLICY_ASSERTION_QNAME.equals(a.getName())) {
- HTTPServerPolicy p = JaxbAssertion.cast(a, HTTPServerPolicy.class).getData();
- if (null == compatible) {
- compatible = p;
- } else {
- compatible = intersect(compatible, p);
- if (null == compatible) {
- LogUtils.log(LOG, Level.SEVERE, "INCOMPATIBLE_HTTPSERVERPOLICY_ASSERTIONS");
- throw new PolicyException(new org.apache.cxf.common.i18n.Message(
- "INCOMPATIBLE_HTTPSERVERPOLICY_ASSERTIONS", LOG));
- }
- }
- }
- }
- return compatible;
- }
-
- /**
- * Asserts all HTTPClientPolicy assertions that are compatible with the specified
- * client policy.
+ * Asserts all HTTPClientPolicy assertions that are compatible with the
+ * specified client policy.
+ *
* @param message the current message
* @param client the client policy
*/
- public static void assertClientPolicy(Message message, HTTPClientPolicy client) {
- HTTPClientPolicy pol = message.get(HTTPClientPolicy.class);
- if (pol != null) {
- client = intersect(pol, client);
- }
-
- AssertionInfoMap aim = message.get(AssertionInfoMap.class);
- if (null == aim) {
- return;
- }
- Collection<AssertionInfo> ais = aim.get(HTTPCLIENTPOLICY_ASSERTION_QNAME);
- if (null == ais || ais.size() == 0) {
- return;
- }
-
- // assert all assertion(s) that are compatible with the value configured for the conduit
-
- if (MessageUtils.isOutbound(message)) {
- for (AssertionInfo ai : ais) {
- HTTPClientPolicy p = (JaxbAssertion.cast(ai.getAssertion(),
- HTTPClientPolicy.class)).getData();
- if (compatible(p, client)) {
- ai.setAsserted(true);
- }
- }
- } else {
- for (AssertionInfo ai : ais) {
- ai.setAsserted(true);
- }
- }
+ public static void assertClientPolicy(Message message, HTTPClientPolicy confPolicy) {
+ PolicyDataEngine policyDataEngine = new PolicyDataEngineImpl();
+ policyDataEngine.assertMessage(HTTPCLIENTPOLICY_ASSERTION_QNAME, message, confPolicy,
+ new ClientPolicyCalculator(), HTTPClientPolicy.class);
}
-
- /**
- * Asserts all HTTPServerPolicy assertions that are equal to the specified
- * server policy.
- * @param message the current message
- * @param server the server policy
- */
- public static void assertServerPolicy(Message message, HTTPServerPolicy server) {
-
- AssertionInfoMap aim = message.get(AssertionInfoMap.class);
- if (null == aim) {
- return;
- }
- Collection<AssertionInfo> ais = aim.get(HTTPSERVERPOLICY_ASSERTION_QNAME);
- if (null == ais || ais.size() == 0) {
- return;
- }
-
- // assert all assertion(s) that are equal to the value configured for the conduit
-
- if (MessageUtils.isOutbound(message)) {
- for (AssertionInfo ai : ais) {
- ai.setAsserted(true);
- }
- } else {
- for (AssertionInfo ai : ais) {
- HTTPServerPolicy p = (JaxbAssertion.cast(ai.getAssertion(),
- HTTPServerPolicy.class)).getData();
- if (equals(p, server)) {
- ai.setAsserted(true);
- }
- }
- }
- }
-
+
/**
- * Checks if two HTTPClientPolicy objects are compatible.
- * @param p1 one client policy
- * @param p2 another client policy
- * @return true iff policies are compatible
+ * Returns a HTTPServerPolicy that is compatible with the assertions
+ * included in the service, endpoint, operation and message policy subjects
+ * AND the HTTPServerPolicy passed as a second argument.
+ *
+ * @param message the message
+ * @param confPolicy the additional policy to be compatible with
+ * @return the HTTPServerPolicy for the message
+ * @throws PolicyException if no compatible HTTPServerPolicy can be
+ * determined
*/
- public static boolean compatible(HTTPClientPolicy p1, HTTPClientPolicy p2) {
-
- if (p1 == p2 || p1.equals(p2)) {
- return true;
- }
-
- boolean compatible = true;
-
- if (compatible) {
- compatible &= compatible(p1.getAccept(), p2.getAccept());
- }
-
- if (compatible) {
- compatible &= compatible(p1.getAcceptEncoding(), p2.getAcceptEncoding());
- }
-
- if (compatible) {
- compatible &= compatible(p1.getAcceptLanguage(), p2.getAcceptLanguage());
- }
-
- if (compatible) {
- compatible &= compatible(p1.getBrowserType(), p2.getBrowserType());
- }
-
- if (compatible) {
- compatible &= !p1.isSetCacheControl() || !p2.isSetCacheControl()
- || p1.getCacheControl().equals(p2.getCacheControl());
- }
-
- if (compatible) {
- compatible = !p1.isSetConnection() || !p2.isSetConnection()
- || p1.getConnection().value().equals(p2.getConnection().value());
- }
-
- if (compatible) {
- compatible = !p1.isSetContentType() || !p2.isSetContentType()
- || p1.getContentType().equals(p2.getContentType());
- }
-
- if (compatible) {
- compatible &= compatible(p1.getCookie(), p2.getCookie());
- }
-
- // REVISIT: Should compatibility require strict equality?
-
- if (compatible) {
- compatible &= compatible(p1.getDecoupledEndpoint(), p2.getDecoupledEndpoint());
- }
-
- if (compatible) {
- compatible &= compatible(p1.getHost(), p2.getHost());
- }
-
- if (compatible) {
- compatible &= compatible(p1.getProxyServer(), p2.getProxyServer());
- }
-
- if (compatible) {
- compatible &= !p1.isSetProxyServerPort() || !p2.isSetProxyServerPort()
- || p1.getProxyServerPort() == p2.getProxyServerPort();
- }
-
- if (compatible) {
- compatible &= !p1.isSetProxyServerType() || !p2.isSetProxyServerType()
- || p1.getProxyServerType().equals(p2.getProxyServerType());
- }
-
- if (compatible) {
- compatible &= compatible(p1.getReferer(), p2.getReferer());
- }
-
- if (compatible) {
- compatible &= p1.isAllowChunking() == p2.isAllowChunking();
- }
-
- if (compatible) {
- compatible &= p1.isAutoRedirect() == p2.isAutoRedirect();
- }
-
- return compatible;
+ public static HTTPServerPolicy getServer(Message message, HTTPServerPolicy confPolicy) {
+ PolicyDataEngine policyDataEngine = new PolicyDataEngineImpl();
+ return policyDataEngine.getPolicy(HTTPSERVERPOLICY_ASSERTION_QNAME,
+ message,
+ confPolicy,
+ HTTPServerPolicy.class,
+ new ServerPolicyCalculator());
}
-
-
+
/**
- * Returns a new HTTPClientPolicy that is compatible with the two specified policies or
- * null if no compatible policy can be determined.
- * @param p1 one policy
- * @param p2 another policy
+ * Returns a HTTPServerPolicy that is compatible with the assertions
+ * included in the service and endpoint policy subjects, or null if there
+ * are no such assertions.
+ *
+ * @param bus the bus
+ * @param ei the endpoint info
+ * @param d the destination
* @return the compatible policy
+ * @throws PolicyException if no compatible HTTPServerPolicy can be
+ * determined
*/
- public static HTTPClientPolicy intersect(HTTPClientPolicy p1, HTTPClientPolicy p2) {
-
- // incompatibilities
-
- if (!compatible(p1, p2)) {
- return null;
- }
-
- // ok - compute compatible policy
-
- HTTPClientPolicy p = new HTTPClientPolicy();
- p.setAccept(combine(p1.getAccept(), p2.getAccept()));
- p.setAcceptEncoding(combine(p1.getAcceptEncoding(), p2.getAcceptEncoding()));
- p.setAcceptLanguage(combine(p1.getAcceptLanguage(), p2.getAcceptLanguage()));
- if (p1.isSetAllowChunking()) {
- p.setAllowChunking(p1.isAllowChunking());
- } else if (p2.isSetAllowChunking()) {
- p.setAllowChunking(p2.isAllowChunking());
- }
- if (p1.isSetAutoRedirect()) {
- p.setAutoRedirect(p1.isAutoRedirect());
- } else if (p2.isSetAutoRedirect()) {
- p.setAutoRedirect(p2.isAutoRedirect());
- }
- p.setBrowserType(combine(p1.getBrowserType(), p2.getBrowserType()));
- if (p1.isSetCacheControl()) {
- p.setCacheControl(p1.getCacheControl());
- } else if (p2.isSetCacheControl()) {
- p.setCacheControl(p2.getCacheControl());
- }
- if (p1.isSetConnection()) {
- p.setConnection(p1.getConnection());
- } else if (p2.isSetConnection()) {
- p.setConnection(p2.getConnection());
- }
- if (p1.isSetContentType()) {
- p.setContentType(p1.getContentType());
- } else if (p2.isSetContentType()) {
- p.setContentType(p2.getContentType());
- }
- p.setCookie(combine(p1.getCookie(), p2.getCookie()));
- p.setDecoupledEndpoint(combine(p1.getDecoupledEndpoint(), p2.getDecoupledEndpoint()));
- p.setHost(combine(p1.getHost(), p2.getHost()));
- p.setProxyServer(combine(p1.getProxyServer(), p2.getProxyServer()));
- if (p1.isSetProxyServerPort()) {
- p.setProxyServerPort(p1.getProxyServerPort());
- } else if (p2.isSetProxyServerPort()) {
- p.setProxyServerPort(p2.getProxyServerPort());
- }
- if (p1.isSetProxyServerType()) {
- p.setProxyServerType(p1.getProxyServerType());
- } else if (p2.isSetProxyServerType()) {
- p.setProxyServerType(p2.getProxyServerType());
- }
- p.setReferer(combine(p1.getReferer(), p2.getReferer()));
- if (p1.isSetConnectionTimeout() || p2.isSetConnectionTimeout()) {
- p.setConnectionTimeout(Math.min(p1.getConnectionTimeout(), p2.getConnectionTimeout()));
- }
- if (p1.isSetReceiveTimeout() || p2.isSetReceiveTimeout()) {
- p.setReceiveTimeout(Math.min(p1.getReceiveTimeout(), p2.getReceiveTimeout()));
- }
-
- return p;
+ public static HTTPServerPolicy getServer(Bus bus, EndpointInfo ei, Destination d) {
+ return new PolicyDataEngineImpl().getServerEndpointPolicy(HTTPSERVERPOLICY_ASSERTION_QNAME,
+ bus, ei, d, HTTPServerPolicy.class,
+ new ServerPolicyCalculator());
}
-
- /**
- * Determines if two HTTPClientPolicy objects are equal.
- * REVISIT: Check if this can be replaced by a generated equals method.
- * @param p1 one client policy
- * @param p2 another client policy
- * @return true iff the two policies are equal
- */
- public static boolean equals(HTTPClientPolicy p1, HTTPClientPolicy p2) {
- if (p1 == p2) {
- return true;
- }
- boolean result = true;
- result &= (p1.isAllowChunking() == p2.isAllowChunking())
- && (p1.isAutoRedirect() == p2.isAutoRedirect())
- && equals(p1.getAccept(), p2.getAccept())
- && equals(p1.getAcceptEncoding(), p2.getAcceptEncoding())
- && equals(p1.getAcceptLanguage(), p2.getAcceptLanguage())
- && equals(p1.getBrowserType(), p2.getBrowserType());
- if (!result) {
- return false;
- }
-
- result &= (p1.getCacheControl() == null
- ? p2.getCacheControl() == null
- : p1.getCacheControl().equals(p2.getCacheControl())
- && p1.getConnection().value().equals(p2.getConnection().value()))
- && (p1.getConnectionTimeout() == p2.getConnectionTimeout())
- && equals(p1.getContentType(), p2.getContentType())
- && equals(p1.getCookie(), p2.getCookie())
- && equals(p1.getDecoupledEndpoint(), p2.getDecoupledEndpoint())
- && equals(p1.getHost(), p2.getHost());
- if (!result) {
- return false;
- }
- result &= equals(p1.getProxyServer(), p2.getProxyServer())
- && (p1.isSetProxyServerPort()
- ? p1.getProxyServerPort() == p2.getProxyServerPort()
- : !p2.isSetProxyServerPort())
- && p1.getProxyServerType().value().equals(p2.getProxyServerType().value())
- && (p1.getReceiveTimeout() == p2.getReceiveTimeout())
- && equals(p1.getReferer(), p2.getReferer());
-
- return result;
- }
-
/**
- * Checks if two HTTPServerPolicy objects are compatible.
- * @param p1 one server policy
- * @param p2 another server policy
- * @return true iff policies are compatible
- */
- public static boolean compatible(HTTPServerPolicy p1, HTTPServerPolicy p2) {
-
- if (p1 == p2 || p1.equals(p2)) {
- return true;
- }
-
- boolean compatible = true;
-
- if (compatible) {
- compatible &= !p1.isSetCacheControl() || !p2.isSetCacheControl()
- || p1.getCacheControl().equals(p2.getCacheControl());
- }
-
- if (compatible) {
- compatible &= compatible(p1.getContentEncoding(), p2.getContentEncoding());
- }
-
- if (compatible) {
- compatible &= compatible(p1.getContentLocation(), p2.getContentLocation());
- }
-
- if (compatible) {
- compatible &= compatible(p1.getContentType(), p2.getContentType());
- }
-
- if (compatible) {
- compatible &= compatible(p1.getRedirectURL(), p2.getRedirectURL());
- }
-
- if (compatible) {
- compatible &= compatible(p1.getServerType(), p2.getServerType());
- }
-
- if (compatible) {
- compatible &= p1.isHonorKeepAlive() == p2.isHonorKeepAlive();
- }
-
- if (compatible) {
- compatible &= p1.isSuppressClientReceiveErrors() == p2.isSuppressClientReceiveErrors();
- }
-
- if (compatible) {
- compatible &= p1.isSuppressClientSendErrors() == p2.isSuppressClientSendErrors();
- }
- if (compatible) {
- compatible &= compatible(p1.getKeepAliveParameters(), p2.getKeepAliveParameters());
- }
-
- return compatible;
- }
-
- /**
- * Returns a new HTTPServerPolicy that is compatible with the two specified policies or
- * null if no compatible policy can be determined.
- * @param p1 one policy
- * @param p2 another policy
- * @return the compatible policy
+ *
+ * Asserts all HTTPServerPolicy assertions that are equal to the specified
+ * server policy.
+ *
+ * @param message the current message
+ * @param server the server policy
*/
- public static HTTPServerPolicy intersect(HTTPServerPolicy p1, HTTPServerPolicy p2) {
-
- if (!compatible(p1, p2)) {
- return null;
- }
-
- HTTPServerPolicy p = new HTTPServerPolicy();
- if (p1.isSetCacheControl()) {
- p.setCacheControl(p1.getCacheControl());
- } else if (p2.isSetCacheControl()) {
- p.setCacheControl(p2.getCacheControl());
- }
- p.setContentEncoding(combine(p1.getContentEncoding(), p2.getContentEncoding()));
- p.setContentLocation(combine(p1.getContentLocation(), p2.getContentLocation()));
- if (p1.isSetContentType()) {
- p.setContentType(p1.getContentType());
- } else if (p2.isSetContentType()) {
- p.setContentType(p2.getContentType());
- }
- if (p1.isSetHonorKeepAlive()) {
- p.setHonorKeepAlive(p1.isHonorKeepAlive());
- } else if (p2.isSetHonorKeepAlive()) {
- p.setHonorKeepAlive(p2.isHonorKeepAlive());
- }
- if (p1.isSetKeepAliveParameters()) {
- p.setKeepAliveParameters(p1.getKeepAliveParameters());
- } else if (p2.isSetKeepAliveParameters()) {
- p.setKeepAliveParameters(p2.getKeepAliveParameters());
- }
-
- if (p1.isSetReceiveTimeout() || p2.isSetReceiveTimeout()) {
- p.setReceiveTimeout(Math.min(p1.getReceiveTimeout(), p2.getReceiveTimeout()));
- }
- p.setRedirectURL(combine(p1.getRedirectURL(), p2.getRedirectURL()));
- p.setServerType(combine(p1.getServerType(), p2.getServerType()));
- if (p1.isSetSuppressClientReceiveErrors()) {
- p.setSuppressClientReceiveErrors(p1.isSuppressClientReceiveErrors());
- } else if (p2.isSetSuppressClientReceiveErrors()) {
- p.setSuppressClientReceiveErrors(p2.isSuppressClientReceiveErrors());
- }
- if (p1.isSetSuppressClientSendErrors()) {
- p.setSuppressClientSendErrors(p1.isSuppressClientSendErrors());
- } else if (p2.isSetSuppressClientSendErrors()) {
- p.setSuppressClientSendErrors(p2.isSuppressClientSendErrors());
- }
-
- return p;
+ public static void assertServerPolicy(Message message, HTTPServerPolicy server) {
+ new PolicyDataEngineImpl().assertMessage(HTTPSERVERPOLICY_ASSERTION_QNAME, message, server,
+ new ServerPolicyCalculator(), HTTPServerPolicy.class);
}
-
- /**
- * Determines if two HTTPServerPolicy objects are equal.
- * REVISIT: Check if this can be replaced by a generated equals method.
- * @param p1 one server policy
- * @param p2 another server policy
- * @return true iff the two policies are equal
- */
- public static boolean equals(HTTPServerPolicy p1, HTTPServerPolicy p2) {
- if (p1 == p2) {
- return true;
- }
- boolean result = true;
- result &= (p1.isHonorKeepAlive() == p2.isHonorKeepAlive())
- && (p1.getCacheControl() == null
- ? p2.getCacheControl() == null
- : p1.getCacheControl().equals(p2.getCacheControl()))
- && equals(p1.getContentEncoding(), p2.getContentEncoding())
- && equals(p1.getContentLocation(), p2.getContentLocation())
- && equals(p1.getContentType(), p2.getContentType())
- && equals(p1.getKeepAliveParameters(), p2.getKeepAliveParameters());
- if (!result) {
- return false;
- }
- result &= (p1.getReceiveTimeout() == p2.getReceiveTimeout())
- && equals(p1.getRedirectURL(), p2.getRedirectURL())
- && equals(p1.getServerType(), p2.getServerType())
- && (p1.isSuppressClientReceiveErrors() == p2.isSuppressClientReceiveErrors())
- && (p1.isSuppressClientSendErrors() == p2.isSuppressClientSendErrors());
-
- return result;
- }
-
- private static String combine(String s1, String s2) {
- return s1 == null ? s2 : s1;
- }
-
- private static boolean equals(String s1, String s2) {
- return s1 == null ? s2 == null : s1.equals(s2);
- }
-
- private static boolean compatible(String s1, String s2) {
- return s1 == null || s2 == null || s1.equals(s2);
- }
-
-
-
-
-
- private static HTTPClientPolicy getClient(Collection<Assertion> alternative) {
- HTTPClientPolicy compatible = null;
- for (Assertion a : alternative) {
- if (HTTPCLIENTPOLICY_ASSERTION_QNAME.equals(a.getName())) {
- HTTPClientPolicy p = JaxbAssertion.cast(a, HTTPClientPolicy.class).getData();
- if (null == compatible) {
- compatible = p;
- } else {
- compatible = intersect(compatible, p);
- if (null == compatible) {
- LogUtils.log(LOG, Level.SEVERE, "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS");
- org.apache.cxf.common.i18n.Message m =
- new org.apache.cxf.common.i18n.Message(
- "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS", LOG);
- throw new PolicyException(m);
- }
- }
- }
- }
- return compatible;
- }
-
- private static HTTPServerPolicy getServer(Collection<Assertion> alternative) {
- HTTPServerPolicy compatible = null;
- for (Assertion a : alternative) {
- if (HTTPSERVERPOLICY_ASSERTION_QNAME.equals(a.getName())) {
- HTTPServerPolicy p = JaxbAssertion.cast(a, HTTPServerPolicy.class).getData();
- if (null == compatible) {
- compatible = p;
- } else {
- compatible = intersect(compatible, p);
- if (null == compatible) {
- LogUtils.log(LOG, Level.SEVERE, "INCOMPATIBLE_HTTPSERVERPOLICY_ASSERTIONS");
- org.apache.cxf.common.i18n.Message m =
- new org.apache.cxf.common.i18n.Message(
- "INCOMPATIBLE_HTTPSERVERPOLICY_ASSERTIONS", LOG);
- throw new PolicyException(m);
- }
- }
- }
- }
- return compatible;
- }
-
- public static String toString(HTTPClientPolicy p) {
- StringBuilder buf = new StringBuilder();
- buf.append(p);
- buf.append("[DecoupledEndpoint=\"");
- buf.append(p.getDecoupledEndpoint());
- buf.append("\", ReceiveTimeout=");
- buf.append(p.getReceiveTimeout());
- buf.append("])");
- return buf.toString();
- }
-
- public static String toString(HTTPServerPolicy p) {
- StringBuilder buf = new StringBuilder();
- buf.append(p);
- buf.append("[ContentType=\"");
- buf.append(p.getContentType());
- buf.append("\", ReceiveTimeout=");
- buf.append(p.getReceiveTimeout());
- buf.append("])");
- return buf.toString();
-
- }
-
-
}
Index: src/main/java/org/apache/cxf/transport/http/policy/StringUtils.java
===================================================================
--- src/main/java/org/apache/cxf/transport/http/policy/StringUtils.java (revision 0)
+++ src/main/java/org/apache/cxf/transport/http/policy/StringUtils.java (revision 0)
@@ -0,0 +1,38 @@
+/**
+ * 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.cxf.transport.http.policy;
+
+public final class StringUtils {
+
+ private StringUtils() {
+ }
+
+ static String combine(String s1, String s2) {
+ return s1 == null ? s2 : s1;
+ }
+
+ static boolean equals(String s1, String s2) {
+ return s1 == null ? s2 == null : s1.equals(s2);
+ }
+
+ static boolean compatible(String s1, String s2) {
+ return s1 == null || s2 == null || s1.equals(s2);
+ }
+
+}
Index: src/main/java/org/apache/cxf/transport/http/policy/ServerPolicyCalculator.java
===================================================================
--- src/main/java/org/apache/cxf/transport/http/policy/ServerPolicyCalculator.java (revision 0)
+++ src/main/java/org/apache/cxf/transport/http/policy/ServerPolicyCalculator.java (revision 0)
@@ -0,0 +1,176 @@
+/**
+ * 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.cxf.transport.http.policy;
+
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageUtils;
+import org.apache.cxf.transports.http.configuration.HTTPServerPolicy;
+
+public class ServerPolicyCalculator implements PolicyCalculator<HTTPServerPolicy> {
+ /**
+ * Returns a new HTTPServerPolicy that is compatible with the two specified
+ * policies or null if no compatible policy can be determined.
+ *
+ * @param p1 one policy
+ * @param p2 another policy
+ * @return the compatible policy
+ */
+ public HTTPServerPolicy intersect(HTTPServerPolicy p1, HTTPServerPolicy p2) {
+
+ if (!compatible(p1, p2)) {
+ return null;
+ }
+
+ HTTPServerPolicy p = new HTTPServerPolicy();
+ if (p1.isSetCacheControl()) {
+ p.setCacheControl(p1.getCacheControl());
+ } else if (p2.isSetCacheControl()) {
+ p.setCacheControl(p2.getCacheControl());
+ }
+ p.setContentEncoding(StringUtils.combine(p1.getContentEncoding(), p2.getContentEncoding()));
+ p.setContentLocation(StringUtils.combine(p1.getContentLocation(), p2.getContentLocation()));
+ if (p1.isSetContentType()) {
+ p.setContentType(p1.getContentType());
+ } else if (p2.isSetContentType()) {
+ p.setContentType(p2.getContentType());
+ }
+ if (p1.isSetHonorKeepAlive()) {
+ p.setHonorKeepAlive(p1.isHonorKeepAlive());
+ } else if (p2.isSetHonorKeepAlive()) {
+ p.setHonorKeepAlive(p2.isHonorKeepAlive());
+ }
+ if (p1.isSetKeepAliveParameters()) {
+ p.setKeepAliveParameters(p1.getKeepAliveParameters());
+ } else if (p2.isSetKeepAliveParameters()) {
+ p.setKeepAliveParameters(p2.getKeepAliveParameters());
+ }
+
+ if (p1.isSetReceiveTimeout() || p2.isSetReceiveTimeout()) {
+ p.setReceiveTimeout(Math.min(p1.getReceiveTimeout(), p2.getReceiveTimeout()));
+ }
+ p.setRedirectURL(StringUtils.combine(p1.getRedirectURL(), p2.getRedirectURL()));
+ p.setServerType(StringUtils.combine(p1.getServerType(), p2.getServerType()));
+ if (p1.isSetSuppressClientReceiveErrors()) {
+ p.setSuppressClientReceiveErrors(p1.isSuppressClientReceiveErrors());
+ } else if (p2.isSetSuppressClientReceiveErrors()) {
+ p.setSuppressClientReceiveErrors(p2.isSuppressClientReceiveErrors());
+ }
+ if (p1.isSetSuppressClientSendErrors()) {
+ p.setSuppressClientSendErrors(p1.isSuppressClientSendErrors());
+ } else if (p2.isSetSuppressClientSendErrors()) {
+ p.setSuppressClientSendErrors(p2.isSuppressClientSendErrors());
+ }
+
+ return p;
+ }
+
+ /**
+ * Checks if two HTTPServerPolicy objects are compatible.
+ *
+ * @param p1 one server policy
+ * @param p2 another server policy
+ * @return true iff policies are compatible
+ */
+ public boolean compatible(HTTPServerPolicy p1, HTTPServerPolicy p2) {
+
+ if (p1 == p2 || p1.equals(p2)) {
+ }
+
+ boolean compatible = true;
+
+ if (compatible) {
+ compatible &= !p1.isSetCacheControl() || !p2.isSetCacheControl()
+ || p1.getCacheControl().equals(p2.getCacheControl());
+ }
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getContentEncoding(), p2.getContentEncoding());
+ }
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getContentLocation(), p2.getContentLocation());
+ }
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getContentType(), p2.getContentType());
+ }
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getRedirectURL(), p2.getRedirectURL());
+ }
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getServerType(), p2.getServerType());
+ }
+
+ if (compatible) {
+ compatible &= p1.isHonorKeepAlive() == p2.isHonorKeepAlive();
+ }
+
+ if (compatible) {
+ compatible &= p1.isSuppressClientReceiveErrors() == p2.isSuppressClientReceiveErrors();
+ }
+
+ if (compatible) {
+ compatible &= p1.isSuppressClientSendErrors() == p2.isSuppressClientSendErrors();
+ }
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getKeepAliveParameters(), p2.getKeepAliveParameters());
+ }
+
+ return compatible;
+ }
+
+ /**
+ * Determines if two HTTPServerPolicy objects are equal. REVISIT: Check if
+ * this can be replaced by a generated equals method.
+ *
+ * @param p1 one server policy
+ * @param p2 another server policy
+ * @return true iff the two policies are equal
+ */
+ public boolean equals(HTTPServerPolicy p1, HTTPServerPolicy p2) {
+ if (p1 == p2) {
+ return true;
+ }
+ boolean result = true;
+
+ result &= (p1.isHonorKeepAlive() == p2.isHonorKeepAlive())
+ && (p1.getCacheControl() == null ? p2.getCacheControl() == null : p1.getCacheControl()
+ .equals(p2.getCacheControl()))
+ && StringUtils.equals(p1.getContentEncoding(), p2.getContentEncoding())
+ && StringUtils.equals(p1.getContentLocation(), p2.getContentLocation())
+ && StringUtils.equals(p1.getContentType(), p2.getContentType())
+ && StringUtils.equals(p1.getKeepAliveParameters(), p2.getKeepAliveParameters());
+ if (!result) {
+ return false;
+ }
+ result &= (p1.getReceiveTimeout() == p2.getReceiveTimeout())
+ && StringUtils.equals(p1.getRedirectURL(), p2.getRedirectURL())
+ && StringUtils.equals(p1.getServerType(), p2.getServerType())
+ && (p1.isSuppressClientReceiveErrors() == p2.isSuppressClientReceiveErrors())
+ && (p1.isSuppressClientSendErrors() == p2.isSuppressClientSendErrors());
+
+ return result;
+ }
+
+ public boolean isAsserted(Message message, HTTPServerPolicy policy, HTTPServerPolicy refPolicy) {
+ return MessageUtils.isOutbound(message) || equals(policy, refPolicy);
+ }
+}
Index: src/main/java/org/apache/cxf/transport/http/policy/HTTPServerAssertionBuilder.java
===================================================================
--- src/main/java/org/apache/cxf/transport/http/policy/HTTPServerAssertionBuilder.java (revision 1232340)
+++ src/main/java/org/apache/cxf/transport/http/policy/HTTPServerAssertionBuilder.java (working copy)
@@ -65,7 +65,7 @@
return false;
}
JaxbAssertion<HTTPServerPolicy> other = JaxbAssertion.cast((Assertion)policyComponent);
- return PolicyUtils.equals(this.getData(), other.getData());
+ return new ServerPolicyCalculator().equals(this.getData(), other.getData());
}
@Override
Index: src/main/java/org/apache/cxf/transport/http/policy/PolicyDataEngineImpl.java
===================================================================
--- src/main/java/org/apache/cxf/transport/http/policy/PolicyDataEngineImpl.java (revision 0)
+++ src/main/java/org/apache/cxf/transport/http/policy/PolicyDataEngineImpl.java (revision 0)
@@ -0,0 +1,138 @@
+/**
+ * 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.cxf.transport.http.policy;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.xml.namespace.QName;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.service.model.EndpointInfo;
+import org.apache.cxf.transport.Conduit;
+import org.apache.cxf.transport.Destination;
+import org.apache.cxf.ws.policy.AssertionInfo;
+import org.apache.cxf.ws.policy.AssertionInfoMap;
+import org.apache.cxf.ws.policy.PolicyEngine;
+import org.apache.cxf.ws.policy.PolicyException;
+import org.apache.cxf.ws.policy.builder.jaxb.JaxbAssertion;
+import org.apache.neethi.Assertion;
+
+public class PolicyDataEngineImpl implements PolicyDataEngine {
+ private static final Logger LOG = LogUtils.getL7dLogger(PolicyDataEngineImpl.class);
+
+ public <T> T getClientEndpointPolicy(QName name, Bus bus, EndpointInfo ei, Conduit c, Class<T> dataClass,
+ PolicyCalculator<T> intersector) {
+ PolicyEngine pe = bus.getExtension(PolicyEngine.class);
+ Collection<Assertion> alternative = pe.getClientEndpointPolicy(ei, c).getChosenAlternative();
+ List<T> filteredPolicies = new ArrayList<T>();
+ for (Assertion a : alternative) {
+ if (name.equals(a.getName())) {
+ T p = JaxbAssertion.cast(a, dataClass).getData();
+ filteredPolicies.add(p);
+ }
+ }
+ return getPolicy(filteredPolicies, intersector);
+ }
+
+ public <T> T getPolicy(QName name, Message message, T confPolicy, Class<T> dataClass,
+ PolicyCalculator<T> intersector) {
+ T pol = message.get(dataClass);
+ if (pol != null) {
+ return intersector.intersect(pol, confPolicy);
+ }
+ List<T> policies = getPoliciesFromMessage(name, message, dataClass);
+ policies.add(confPolicy);
+ return getPolicy(policies, intersector);
+ }
+
+ public <T> T getServerEndpointPolicy(QName name, Bus bus, EndpointInfo ei, Destination d,
+ Class<T> dataClass, PolicyCalculator<T> policyCalculator) {
+ PolicyEngine pe = bus.getExtension(PolicyEngine.class);
+ Collection<Assertion> alternative = pe.getServerEndpointPolicy(ei, d).getChosenAlternative();
+ List<T> filteredPolicies = new ArrayList<T>();
+ for (Assertion a : alternative) {
+ if (name.equals(a.getName())) {
+ T p = JaxbAssertion.cast(a, dataClass).getData();
+ filteredPolicies.add(p);
+ }
+ }
+ return getPolicy(filteredPolicies, policyCalculator);
+ }
+
+ private <T> List<T> getPoliciesFromMessage(QName name, Message message, Class<T> dataClass) {
+ List<T> policies = new ArrayList<T>();
+ AssertionInfoMap amap = message.get(AssertionInfoMap.class);
+ if (null == amap || amap.isEmpty()) {
+ return policies;
+ }
+ Collection<AssertionInfo> ais = amap.get(name);
+ if (null == ais) {
+ return policies;
+ }
+ for (AssertionInfo ai : ais) {
+ T policy = JaxbAssertion.cast(ai.getAssertion(), dataClass).getData();
+ policies.add(policy);
+ }
+ return policies;
+ }
+
+ public <T> void assertMessage(QName name, Message message, T confPol,
+ PolicyCalculator<T> policyCalculator, Class<T> dataClass) {
+ T messagePol = message.get(dataClass);
+ final T refPolicy = (messagePol != null) ? policyCalculator.intersect(messagePol, confPol) : confPol;
+
+ AssertionInfoMap amap = message.get(AssertionInfoMap.class);
+ if (null == amap || amap.isEmpty()) {
+ return;
+ }
+ Collection<AssertionInfo> ais = amap.get(name);
+ for (AssertionInfo ai : ais) {
+ T policy = JaxbAssertion.cast(ai.getAssertion(), dataClass).getData();
+ ai.setAsserted(policyCalculator.isAsserted(message, policy, refPolicy));
+ }
+ }
+
+ private static <T> T getPolicy(List<T> policies, PolicyCalculator<T> intersector) {
+ T compatible = null;
+ for (T p : policies) {
+ if (null == compatible) {
+ compatible = p;
+ } else {
+ compatible = intersector.intersect(compatible, p);
+ if (null == compatible) {
+ logAndThrowPolicyException();
+ }
+ }
+ }
+ return compatible;
+ }
+
+ private static void logAndThrowPolicyException() {
+ LogUtils.log(LOG, Level.SEVERE, "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS");
+ throw new PolicyException(new org.apache.cxf.common.i18n.Message(
+ "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS", LOG));
+ }
+
+}
Index: src/main/java/org/apache/cxf/transport/http/policy/ClientPolicyCalculator.java
===================================================================
--- src/main/java/org/apache/cxf/transport/http/policy/ClientPolicyCalculator.java (revision 0)
+++ src/main/java/org/apache/cxf/transport/http/policy/ClientPolicyCalculator.java (revision 0)
@@ -0,0 +1,238 @@
+/**
+ * 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.cxf.transport.http.policy;
+
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageUtils;
+import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
+
+public class ClientPolicyCalculator implements PolicyCalculator<HTTPClientPolicy> {
+
+ public boolean isAsserted(Message message, HTTPClientPolicy policy, HTTPClientPolicy refPolicy) {
+ boolean outbound = MessageUtils.isOutbound(message);
+ boolean compatible = compatible(policy, refPolicy);
+ return !outbound || compatible;
+ }
+
+ /**
+ * Determines if two HTTPClientPolicy objects are equal. REVISIT: Check if
+ * this can be replaced by a generated equals method.
+ *
+ * @param p1 one client policy
+ * @param p2 another client policy
+ * @return true iff the two policies are equal
+ */
+ public boolean equals(HTTPClientPolicy p1, HTTPClientPolicy p2) {
+ if (p1 == p2) {
+ return true;
+ }
+ boolean result = true;
+ result &= (p1.isAllowChunking() == p2.isAllowChunking())
+ && (p1.isAutoRedirect() == p2.isAutoRedirect())
+ && StringUtils.equals(p1.getAccept(), p2.getAccept())
+ && StringUtils.equals(p1.getAcceptEncoding(), p2.getAcceptEncoding())
+ && StringUtils.equals(p1.getAcceptLanguage(), p2.getAcceptLanguage())
+ && StringUtils.equals(p1.getBrowserType(), p2.getBrowserType());
+ if (!result) {
+ return false;
+ }
+
+ result &= (p1.getCacheControl() == null ? p2.getCacheControl() == null : p1.getCacheControl()
+ .equals(p2.getCacheControl()) && p1.getConnection().value().equals(p2.getConnection().value()))
+ && (p1.getConnectionTimeout() == p2.getConnectionTimeout())
+ && StringUtils.equals(p1.getContentType(), p2.getContentType())
+ && StringUtils.equals(p1.getCookie(), p2.getCookie())
+ && StringUtils.equals(p1.getDecoupledEndpoint(), p2.getDecoupledEndpoint())
+ && StringUtils.equals(p1.getHost(), p2.getHost());
+ if (!result) {
+ return false;
+ }
+
+ result &= StringUtils.equals(p1.getProxyServer(), p2.getProxyServer())
+ && (p1.isSetProxyServerPort() ? p1.getProxyServerPort() == p2.getProxyServerPort() : !p2
+ .isSetProxyServerPort())
+ && p1.getProxyServerType().value().equals(p2.getProxyServerType().value())
+ && (p1.getReceiveTimeout() == p2.getReceiveTimeout())
+ && StringUtils.equals(p1.getReferer(), p2.getReferer());
+
+ return result;
+ }
+
+ /**
+ * Returns a new HTTPClientPolicy that is compatible with the two specified
+ * policies or null if no compatible policy can be determined.
+ *
+ * @param p1 one policy
+ * @param p2 another policy
+ * @return the compatible policy
+ */
+ public HTTPClientPolicy intersect(HTTPClientPolicy p1, HTTPClientPolicy p2) {
+
+ // incompatibilities
+
+ if (!compatible(p1, p2)) {
+ return null;
+ }
+
+ // ok - compute compatible policy
+
+ HTTPClientPolicy p = new HTTPClientPolicy();
+ p.setAccept(StringUtils.combine(p1.getAccept(), p2.getAccept()));
+ p.setAcceptEncoding(StringUtils.combine(p1.getAcceptEncoding(), p2.getAcceptEncoding()));
+ p.setAcceptLanguage(StringUtils.combine(p1.getAcceptLanguage(), p2.getAcceptLanguage()));
+ if (p1.isSetAllowChunking()) {
+ p.setAllowChunking(p1.isAllowChunking());
+ } else if (p2.isSetAllowChunking()) {
+ p.setAllowChunking(p2.isAllowChunking());
+ }
+ if (p1.isSetAutoRedirect()) {
+ p.setAutoRedirect(p1.isAutoRedirect());
+ } else if (p2.isSetAutoRedirect()) {
+ p.setAutoRedirect(p2.isAutoRedirect());
+ }
+ p.setBrowserType(StringUtils.combine(p1.getBrowserType(), p2.getBrowserType()));
+ if (p1.isSetCacheControl()) {
+ p.setCacheControl(p1.getCacheControl());
+ } else if (p2.isSetCacheControl()) {
+ p.setCacheControl(p2.getCacheControl());
+ }
+ if (p1.isSetConnection()) {
+ p.setConnection(p1.getConnection());
+ } else if (p2.isSetConnection()) {
+ p.setConnection(p2.getConnection());
+ }
+ if (p1.isSetContentType()) {
+ p.setContentType(p1.getContentType());
+ } else if (p2.isSetContentType()) {
+ p.setContentType(p2.getContentType());
+ }
+ p.setCookie(StringUtils.combine(p1.getCookie(), p2.getCookie()));
+ p.setDecoupledEndpoint(StringUtils.combine(p1.getDecoupledEndpoint(), p2.getDecoupledEndpoint()));
+ p.setHost(StringUtils.combine(p1.getHost(), p2.getHost()));
+ p.setProxyServer(StringUtils.combine(p1.getProxyServer(), p2.getProxyServer()));
+ if (p1.isSetProxyServerPort()) {
+ p.setProxyServerPort(p1.getProxyServerPort());
+ } else if (p2.isSetProxyServerPort()) {
+ p.setProxyServerPort(p2.getProxyServerPort());
+ }
+ if (p1.isSetProxyServerType()) {
+ p.setProxyServerType(p1.getProxyServerType());
+ } else if (p2.isSetProxyServerType()) {
+ p.setProxyServerType(p2.getProxyServerType());
+ }
+ p.setReferer(StringUtils.combine(p1.getReferer(), p2.getReferer()));
+ if (p1.isSetConnectionTimeout() || p2.isSetConnectionTimeout()) {
+ p.setConnectionTimeout(Math.min(p1.getConnectionTimeout(), p2.getConnectionTimeout()));
+ }
+ if (p1.isSetReceiveTimeout() || p2.isSetReceiveTimeout()) {
+ p.setReceiveTimeout(Math.min(p1.getReceiveTimeout(), p2.getReceiveTimeout()));
+ }
+
+ return p;
+ }
+
+ /**
+ * Checks if two HTTPClientPolicy objects are compatible.
+ *
+ * @param p1 one client policy
+ * @param p2 another client policy
+ * @return true iff policies are compatible
+ */
+ public boolean compatible(HTTPClientPolicy p1, HTTPClientPolicy p2) {
+
+ if (p1 == p2 || p1.equals(p2)) {
+ return true;
+ }
+
+ boolean compatible = true;
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getAccept(), p2.getAccept());
+ }
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getAcceptEncoding(), p2.getAcceptEncoding());
+ }
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getAcceptLanguage(), p2.getAcceptLanguage());
+ }
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getBrowserType(), p2.getBrowserType());
+ }
+
+ if (compatible) {
+ compatible &= !p1.isSetCacheControl() || !p2.isSetCacheControl()
+ || p1.getCacheControl().equals(p2.getCacheControl());
+ }
+
+ if (compatible) {
+ compatible = !p1.isSetConnection() || !p2.isSetConnection()
+ || p1.getConnection().value().equals(p2.getConnection().value());
+ }
+
+ if (compatible) {
+ compatible = !p1.isSetContentType() || !p2.isSetContentType()
+ || p1.getContentType().equals(p2.getContentType());
+ }
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getCookie(), p2.getCookie());
+ }
+
+ // REVISIT: Should compatibility require strict equality?
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getDecoupledEndpoint(), p2.getDecoupledEndpoint());
+ }
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getHost(), p2.getHost());
+ }
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getProxyServer(), p2.getProxyServer());
+ }
+
+ if (compatible) {
+ compatible &= !p1.isSetProxyServerPort() || !p2.isSetProxyServerPort()
+ || p1.getProxyServerPort() == p2.getProxyServerPort();
+ }
+
+ if (compatible) {
+ compatible &= !p1.isSetProxyServerType() || !p2.isSetProxyServerType()
+ || p1.getProxyServerType().equals(p2.getProxyServerType());
+ }
+
+ if (compatible) {
+ compatible &= StringUtils.compatible(p1.getReferer(), p2.getReferer());
+ }
+
+ if (compatible) {
+ compatible &= p1.isAllowChunking() == p2.isAllowChunking();
+ }
+
+ if (compatible) {
+ compatible &= p1.isAutoRedirect() == p2.isAutoRedirect();
+ }
+
+ return compatible;
+ }
+}
Index: src/test/java/org/apache/cxf/transport/http/policy/PolicyUtilsTest.java
===================================================================
--- src/test/java/org/apache/cxf/transport/http/policy/PolicyUtilsTest.java (revision 1232340)
+++ src/test/java/org/apache/cxf/transport/http/policy/PolicyUtilsTest.java (working copy)
@@ -50,108 +50,6 @@
control = EasyMock.createNiceControl();
}
- @Test
- public void testCompatibleClientPolicies() {
- HTTPClientPolicy p1 = new HTTPClientPolicy();
- assertTrue("Policy is not compatible with itself.", PolicyUtils.compatible(p1, p1));
- HTTPClientPolicy p2 = new HTTPClientPolicy();
- assertTrue("Policies are not compatible.", PolicyUtils.compatible(p1, p2));
- p1.setBrowserType("browser");
- assertTrue("Policies are not compatible.", PolicyUtils.compatible(p1, p2));
- p1.setBrowserType(null);
- p1.setConnectionTimeout(10000);
- assertTrue("Policies are not compatible.", PolicyUtils.compatible(p1, p2));
- p1.setAllowChunking(false);
- assertTrue("Policies are compatible.", !PolicyUtils.compatible(p1, p2));
- p2.setAllowChunking(false);
- assertTrue("Policies are compatible.", PolicyUtils.compatible(p1, p2));
- }
-
- @Test
- public void testIntersectClientPolicies() {
- HTTPClientPolicy p1 = new HTTPClientPolicy();
- HTTPClientPolicy p2 = new HTTPClientPolicy();
- HTTPClientPolicy p = null;
-
- p1.setBrowserType("browser");
- p = PolicyUtils.intersect(p1, p2);
- assertEquals("browser", p.getBrowserType());
- p1.setBrowserType(null);
- p1.setConnectionTimeout(10000L);
- p = PolicyUtils.intersect(p1, p2);
- assertEquals(10000L, p.getConnectionTimeout());
- p1.setAllowChunking(false);
- p2.setAllowChunking(false);
- p = PolicyUtils.intersect(p1, p2);
- assertTrue(!p.isAllowChunking());
- }
-
- @Test
- public void testEqualClientPolicies() {
- HTTPClientPolicy p1 = new HTTPClientPolicy();
- assertTrue(PolicyUtils.equals(p1, p1));
- HTTPClientPolicy p2 = new HTTPClientPolicy();
- assertTrue(PolicyUtils.equals(p1, p2));
- p1.setDecoupledEndpoint("http://localhost:8080/decoupled");
- assertTrue(!PolicyUtils.equals(p1, p2));
- p2.setDecoupledEndpoint("http://localhost:8080/decoupled");
- assertTrue(PolicyUtils.equals(p1, p2));
- p1.setReceiveTimeout(10000L);
- assertTrue(!PolicyUtils.equals(p1, p2));
- }
-
- @Test
- public void testCompatibleServerPolicies() {
- HTTPServerPolicy p1 = new HTTPServerPolicy();
- assertTrue("Policy is not compatible with itself.", PolicyUtils.compatible(p1, p1));
- HTTPServerPolicy p2 = new HTTPServerPolicy();
- assertTrue("Policies are not compatible.", PolicyUtils.compatible(p1, p2));
- p1.setServerType("server");
- assertTrue("Policies are not compatible.", PolicyUtils.compatible(p1, p2));
- p1.setServerType(null);
- p1.setReceiveTimeout(10000);
- assertTrue("Policies are not compatible.", PolicyUtils.compatible(p1, p2));
- p1.setSuppressClientSendErrors(false);
- assertTrue("Policies are compatible.", PolicyUtils.compatible(p1, p2));
- p1.setSuppressClientSendErrors(true);
- assertTrue("Policies are compatible.", !PolicyUtils.compatible(p1, p2));
- p2.setSuppressClientSendErrors(true);
- assertTrue("Policies are compatible.", PolicyUtils.compatible(p1, p2));
- }
-
- @Test
- public void testIntersectServerPolicies() {
- HTTPServerPolicy p1 = new HTTPServerPolicy();
- HTTPServerPolicy p2 = new HTTPServerPolicy();
- HTTPServerPolicy p = null;
-
- p1.setServerType("server");
- p = PolicyUtils.intersect(p1, p2);
- assertEquals("server", p.getServerType());
- p1.setServerType(null);
- p1.setReceiveTimeout(10000L);
- p = PolicyUtils.intersect(p1, p2);
- assertEquals(10000L, p.getReceiveTimeout());
- p1.setSuppressClientSendErrors(true);
- p2.setSuppressClientSendErrors(true);
- p = PolicyUtils.intersect(p1, p2);
- assertTrue(p.isSuppressClientSendErrors());
- }
-
-
- @Test
- public void testEqualServerPolicies() {
- HTTPServerPolicy p1 = new HTTPServerPolicy();
- assertTrue(PolicyUtils.equals(p1, p1));
- HTTPServerPolicy p2 = new HTTPServerPolicy();
- assertTrue(PolicyUtils.equals(p1, p2));
- p1.setContentEncoding("encoding");
- assertTrue(!PolicyUtils.equals(p1, p2));
- p2.setContentEncoding("encoding");
- assertTrue(PolicyUtils.equals(p1, p2));
- p1.setSuppressClientSendErrors(true);
- assertTrue(!PolicyUtils.equals(p1, p2));
- }
@Test
public void testAssertClientPolicyNoop() {
@@ -183,6 +81,7 @@
control.verify();
}
+
@Test
public void testAssertClientPolicyOutbound() {
testAssertClientPolicy(true);
@@ -192,6 +91,13 @@
public void testAssertClientPolicyInbound() {
testAssertClientPolicy(false);
}
+
+ public AssertionInfo getClientPolicyAssertionInfo(HTTPClientPolicy policy) {
+ JaxbAssertion<HTTPClientPolicy> assertion =
+ new JaxbAssertion<HTTPClientPolicy>(PolicyUtils.HTTPCLIENTPOLICY_ASSERTION_QNAME, false);
+ assertion.setData(policy);
+ return new AssertionInfo(assertion);
+ }
void testAssertClientPolicy(boolean outbound) {
Message message = control.createMock(Message.class);
@@ -202,19 +108,9 @@
HTTPClientPolicy icmp = new HTTPClientPolicy();
icmp.setAllowChunking(false);
- JaxbAssertion<HTTPClientPolicy> ea =
- new JaxbAssertion<HTTPClientPolicy>(PolicyUtils.HTTPCLIENTPOLICY_ASSERTION_QNAME, false);
- ea.setData(ep);
- JaxbAssertion<HTTPClientPolicy> cma =
- new JaxbAssertion<HTTPClientPolicy>(PolicyUtils.HTTPCLIENTPOLICY_ASSERTION_QNAME, false);
- cma.setData(cmp);
- JaxbAssertion<HTTPClientPolicy> icma =
- new JaxbAssertion<HTTPClientPolicy>(PolicyUtils.HTTPCLIENTPOLICY_ASSERTION_QNAME, false);
- icma.setData(icmp);
-
- AssertionInfo eai = new AssertionInfo(ea);
- AssertionInfo cmai = new AssertionInfo(cma);
- AssertionInfo icmai = new AssertionInfo(icma);
+ AssertionInfo eai = getClientPolicyAssertionInfo(ep);
+ AssertionInfo cmai = getClientPolicyAssertionInfo(cmp);
+ AssertionInfo icmai = getClientPolicyAssertionInfo(icmp);
AssertionInfoMap aim = new AssertionInfoMap(CastUtils.cast(Collections.EMPTY_LIST,
PolicyAssertion.class));
@@ -225,10 +121,10 @@
aim.put(PolicyUtils.HTTPCLIENTPOLICY_ASSERTION_QNAME, ais);
EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(aim);
Exchange ex = control.createMock(Exchange.class);
- EasyMock.expect(message.getExchange()).andReturn(ex);
- EasyMock.expect(ex.getOutMessage()).andReturn(outbound ? message : null);
+ EasyMock.expect(message.getExchange()).andReturn(ex).atLeastOnce();
+ EasyMock.expect(ex.getOutMessage()).andReturn(outbound ? message : null).atLeastOnce();
if (!outbound) {
- EasyMock.expect(ex.getOutFaultMessage()).andReturn(null);
+ EasyMock.expect(ex.getOutFaultMessage()).andReturn(null).atLeastOnce();
}
control.replay();
@@ -248,6 +144,13 @@
public void testAssertServerPolicyInbound() {
testAssertServerPolicy(false);
}
+
+ public AssertionInfo getServerPolicyAssertionInfo(HTTPServerPolicy policy) {
+ JaxbAssertion<HTTPServerPolicy> assertion =
+ new JaxbAssertion<HTTPServerPolicy>(PolicyUtils.HTTPSERVERPOLICY_ASSERTION_QNAME, false);
+ assertion.setData(policy);
+ return new AssertionInfo(assertion);
+ }
void testAssertServerPolicy(boolean outbound) {
Message message = control.createMock(Message.class);
@@ -258,38 +161,25 @@
HTTPServerPolicy icmp = new HTTPServerPolicy();
icmp.setSuppressClientSendErrors(true);
- JaxbAssertion<HTTPServerPolicy> ea =
- new JaxbAssertion<HTTPServerPolicy>(PolicyUtils.HTTPSERVERPOLICY_ASSERTION_QNAME, false);
- ea.setData(ep);
- JaxbAssertion<HTTPServerPolicy> ma =
- new JaxbAssertion<HTTPServerPolicy>(PolicyUtils.HTTPSERVERPOLICY_ASSERTION_QNAME, false);
- ma.setData(mp);
- JaxbAssertion<HTTPServerPolicy> cma =
- new JaxbAssertion<HTTPServerPolicy>(PolicyUtils.HTTPSERVERPOLICY_ASSERTION_QNAME, false);
- cma.setData(cmp);
- JaxbAssertion<HTTPServerPolicy> icma =
- new JaxbAssertion<HTTPServerPolicy>(PolicyUtils.HTTPSERVERPOLICY_ASSERTION_QNAME, false);
- icma.setData(icmp);
-
- AssertionInfo eai = new AssertionInfo(ea);
- AssertionInfo mai = new AssertionInfo(ma);
- AssertionInfo cmai = new AssertionInfo(cma);
- AssertionInfo icmai = new AssertionInfo(icma);
+ AssertionInfo eai = getServerPolicyAssertionInfo(ep);
+ AssertionInfo mai = getServerPolicyAssertionInfo(mp);
+ AssertionInfo cmai = getServerPolicyAssertionInfo(cmp);
+ AssertionInfo icmai = getServerPolicyAssertionInfo(icmp);
- AssertionInfoMap aim = new AssertionInfoMap(CastUtils.cast(Collections.EMPTY_LIST,
- PolicyAssertion.class));
Collection<AssertionInfo> ais = new ArrayList<AssertionInfo>();
ais.add(eai);
ais.add(mai);
ais.add(cmai);
ais.add(icmai);
+ AssertionInfoMap aim = new AssertionInfoMap(CastUtils.cast(Collections.EMPTY_LIST,
+ PolicyAssertion.class));
aim.put(PolicyUtils.HTTPSERVERPOLICY_ASSERTION_QNAME, ais);
- EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(aim);
+ EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(aim).atLeastOnce();
Exchange ex = control.createMock(Exchange.class);
- EasyMock.expect(message.getExchange()).andReturn(ex);
- EasyMock.expect(ex.getOutMessage()).andReturn(outbound ? message : null);
+ EasyMock.expect(message.getExchange()).andReturn(ex).atLeastOnce();
+ EasyMock.expect(ex.getOutMessage()).andReturn(outbound ? message : null).atLeastOnce();
if (!outbound) {
- EasyMock.expect(ex.getOutFaultMessage()).andReturn(null);
+ EasyMock.expect(ex.getOutFaultMessage()).andReturn(null).atLeastOnce();
}
control.replay();
Index: src/test/java/org/apache/cxf/transport/http/policy/ClientPolicyCalculatorTest.java
===================================================================
--- src/test/java/org/apache/cxf/transport/http/policy/ClientPolicyCalculatorTest.java (revision 0)
+++ src/test/java/org/apache/cxf/transport/http/policy/ClientPolicyCalculatorTest.java (revision 0)
@@ -0,0 +1,79 @@
+/**
+ * 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.cxf.transport.http.policy;
+
+import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ClientPolicyCalculatorTest extends Assert {
+ @Test
+ public void testCompatibleClientPolicies() {
+ ClientPolicyCalculator calc = new ClientPolicyCalculator();
+ HTTPClientPolicy p1 = new HTTPClientPolicy();
+ assertTrue("Policy is not compatible with itself.", calc.compatible(p1, p1));
+ HTTPClientPolicy p2 = new HTTPClientPolicy();
+ assertTrue("Policies are not compatible.", calc.compatible(p1, p2));
+ p1.setBrowserType("browser");
+ assertTrue("Policies are not compatible.", calc.compatible(p1, p2));
+ p1.setBrowserType(null);
+ p1.setConnectionTimeout(10000);
+ assertTrue("Policies are not compatible.", calc.compatible(p1, p2));
+ p1.setAllowChunking(false);
+ assertTrue("Policies are compatible.", !calc.compatible(p1, p2));
+ p2.setAllowChunking(false);
+ assertTrue("Policies are compatible.", calc.compatible(p1, p2));
+ }
+
+ @Test
+ public void testIntersectClientPolicies() {
+ ClientPolicyCalculator calc = new ClientPolicyCalculator();
+ HTTPClientPolicy p1 = new HTTPClientPolicy();
+ HTTPClientPolicy p2 = new HTTPClientPolicy();
+ HTTPClientPolicy p = null;
+
+ p1.setBrowserType("browser");
+ p = calc.intersect(p1, p2);
+ assertEquals("browser", p.getBrowserType());
+ p1.setBrowserType(null);
+ p1.setConnectionTimeout(10000L);
+ p = calc.intersect(p1, p2);
+ assertEquals(10000L, p.getConnectionTimeout());
+ p1.setAllowChunking(false);
+ p2.setAllowChunking(false);
+ p = calc.intersect(p1, p2);
+ assertTrue(!p.isAllowChunking());
+ }
+
+ @Test
+ public void testEqualClientPolicies() {
+ ClientPolicyCalculator calc = new ClientPolicyCalculator();
+ HTTPClientPolicy p1 = new HTTPClientPolicy();
+ assertTrue(calc.equals(p1, p1));
+ HTTPClientPolicy p2 = new HTTPClientPolicy();
+ assertTrue(calc.equals(p1, p2));
+ p1.setDecoupledEndpoint("http://localhost:8080/decoupled");
+ assertTrue(!calc.equals(p1, p2));
+ p2.setDecoupledEndpoint("http://localhost:8080/decoupled");
+ assertTrue(calc.equals(p1, p2));
+ p1.setReceiveTimeout(10000L);
+ assertTrue(!calc.equals(p1, p2));
+ }
+
+}
Index: src/test/java/org/apache/cxf/transport/http/policy/ServerPolicyCalculatorTest.java
===================================================================
--- src/test/java/org/apache/cxf/transport/http/policy/ServerPolicyCalculatorTest.java (revision 0)
+++ src/test/java/org/apache/cxf/transport/http/policy/ServerPolicyCalculatorTest.java (revision 0)
@@ -0,0 +1,81 @@
+/**
+ * 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.cxf.transport.http.policy;
+
+import org.apache.cxf.transports.http.configuration.HTTPServerPolicy;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ServerPolicyCalculatorTest extends Assert {
+ @Test
+ public void testCompatibleServerPolicies() {
+ ServerPolicyCalculator spc = new ServerPolicyCalculator();
+ HTTPServerPolicy p1 = new HTTPServerPolicy();
+ assertTrue("Policy is not compatible with itself.", spc.compatible(p1, p1));
+ HTTPServerPolicy p2 = new HTTPServerPolicy();
+ assertTrue("Policies are not compatible.", spc.compatible(p1, p2));
+ p1.setServerType("server");
+ assertTrue("Policies are not compatible.", spc.compatible(p1, p2));
+ p1.setServerType(null);
+ p1.setReceiveTimeout(10000);
+ assertTrue("Policies are not compatible.", spc.compatible(p1, p2));
+ p1.setSuppressClientSendErrors(false);
+ assertTrue("Policies are compatible.", spc.compatible(p1, p2));
+ p1.setSuppressClientSendErrors(true);
+ assertTrue("Policies are compatible.", !spc.compatible(p1, p2));
+ p2.setSuppressClientSendErrors(true);
+ assertTrue("Policies are compatible.", spc.compatible(p1, p2));
+ }
+
+ @Test
+ public void testIntersectServerPolicies() {
+ ServerPolicyCalculator spc = new ServerPolicyCalculator();
+ HTTPServerPolicy p1 = new HTTPServerPolicy();
+ HTTPServerPolicy p2 = new HTTPServerPolicy();
+ HTTPServerPolicy p = null;
+
+ p1.setServerType("server");
+ p = spc.intersect(p1, p2);
+ assertEquals("server", p.getServerType());
+ p1.setServerType(null);
+ p1.setReceiveTimeout(10000L);
+ p = spc.intersect(p1, p2);
+ assertEquals(10000L, p.getReceiveTimeout());
+ p1.setSuppressClientSendErrors(true);
+ p2.setSuppressClientSendErrors(true);
+ p = spc.intersect(p1, p2);
+ assertTrue(p.isSuppressClientSendErrors());
+ }
+
+
+ @Test
+ public void testEqualServerPolicies() {
+ ServerPolicyCalculator spc = new ServerPolicyCalculator();
+ HTTPServerPolicy p1 = new HTTPServerPolicy();
+ assertTrue(spc.equals(p1, p1));
+ HTTPServerPolicy p2 = new HTTPServerPolicy();
+ assertTrue(spc.equals(p1, p2));
+ p1.setContentEncoding("encoding");
+ assertTrue(!spc.equals(p1, p2));
+ p2.setContentEncoding("encoding");
+ assertTrue(spc.equals(p1, p2));
+ p1.setSuppressClientSendErrors(true);
+ assertTrue(!spc.equals(p1, p2));
+ }
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment