Skip to content

Commit

Permalink
[JBWS-3912]:Add throttling feature support test case using user defin…
Browse files Browse the repository at this point in the history
…ed ThrottlingManager
  • Loading branch information
jimma committed May 11, 2024
1 parent 09566ef commit c74cc29
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jboss.wsf.stack.cxf;
package org.jboss.wsf.stack.cxf.features.throttling;

import java.security.AccessController;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jboss.wsf.stack.cxf;
package org.jboss.wsf.stack.cxf.features.throttling;

import org.apache.cxf.Bus;
import org.apache.cxf.feature.AbstractPortableFeature;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.jboss.test.ws.jaxws.cxf.throttling;

import jakarta.jws.WebService;

@WebService(targetNamespace = "http://org.jboss.ws/jaxws/cxf/throttling/hello")
public interface Hello
{
String sayHello(String input);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jboss.test.ws.jaxws.cxf.throttling;
import jakarta.jws.WebService;
import jakarta.xml.ws.WebServiceException;

@WebService
(
serviceName = "HelloService",
endpointInterface = "org.jboss.test.ws.jaxws.cxf.throttling.Hello",
targetNamespace = "http://org.jboss.ws/jaxws/cxf/throttling/hello"
)
public class HelloImpl implements Hello
{
public String sayHello(String input) {
if (input.equals("error")) {
throw new WebServiceException("Error input");
}
return input;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
@WebService(targetNamespace = "http://org.jboss.ws/jaxws/cxf/throttling")
public interface HelloWorld
{
String echo(String input) throws InterruptedException;
String echo(String input);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jboss.test.ws.jaxws.cxf.throttling;

import java.util.Collections;
import java.util.List;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.throttling.ThrottleResponse;
import org.apache.cxf.throttling.ThrottlingManager;
import org.jboss.wsf.spi.deployment.Endpoint;

public class TestThrottlingManager extends ThrottleResponse implements ThrottlingManager {
@Override
public List<String> getDecisionPhases() {
return Collections.singletonList(Phase.PRE_STREAM);
}

@Override
public ThrottleResponse getThrottleResponse(String phase, Message m) {
Endpoint endpoint = m.getExchange().get(Endpoint.class);
if (endpoint != null) {
if (endpoint.getEndpointMetrics().getFaultCount() >= 3) {
this.setResponseCode(429);
this.setDelay(200);
return this;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.FileAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.wsf.stack.cxf.client.Constants;
Expand All @@ -51,23 +52,29 @@ public class ThrottlingTestCase extends JBossWSTest {
@Deployment(testable = false)
public static WebArchive createDeployment() {
WebArchive archive = ShrinkWrap.create(WebArchive.class, "jaxws-cxf-throttling.war")
.setManifest(new StringAsset("Manifest-Version: 1.0\n"
+ "Dependencies: org.apache.cxf.impl\n"))
.addClass(HelloWorld.class)
.addClass(HelloWorldImpl.class)
.addClass(Hello.class)
.addClass(HelloImpl.class)
.addClass(TestThrottlingManager.class)
.setWebXML(new File(JBossWSTestHelper.getTestResourcesDir() + "/jaxws/cxf/throttling/WEB-INF/web.xml"))
.add(new FileAsset(new File(JBossWSTestHelper.getTestResourcesDir() + "/jaxws/cxf/throttling/WEB-INF/jaxws-endpoint-config.xml")), "jaxws-endpoint-config.xml");
return archive;
}



@Test
@RunAsClient
public void testThrottling() throws Exception {
public void testThrottlingWithDefaultManager() throws Exception {
//Throttling feature only allows 5 invocations and getPort access wsdl already
//called for once.
HelloWorld port = getPort();
HelloWorld port = getHelloWorldPort();
for (int i = 0; i < 4; i++) {
String response = port.echo("hello");
Assertions.assertEquals("hello", response, "hello is expected and i:" + i);
Assertions.assertEquals("hello", response, "hello is expected");
}

try {
Expand All @@ -76,17 +83,53 @@ public void testThrottling() throws Exception {
} catch (jakarta.xml.ws.WebServiceException e) {
Assertions.assertEquals(((BindingProvider)port).getResponseContext().get("jakarta.xml.ws.http.response.code"), 429);
}
}

@Test
@RunAsClient
public void testThrottlingWithTestManager() throws Exception {
Hello port = getHelloPort();
String response = port.sayHello("hello");
Assertions.assertEquals("hello", response, "hello is expected");


for (int i=0; i < 3; i++) {
try {
String res = port.sayHello("error");
fail("Exception not thrown");
} catch (jakarta.xml.ws.WebServiceException e) {
//expected
}
}

try {
String res = port.sayHello("hello");
fail("Exception not thrown");
} catch (jakarta.xml.ws.WebServiceException e) {
Assertions.assertEquals(((BindingProvider)port).getResponseContext().get("jakarta.xml.ws.http.response.code"), 429);
}

}

private HelloWorld getPort() throws MalformedURLException

private HelloWorld getHelloWorldPort() throws MalformedURLException
{
URL wsdlURL = new URL(baseURL.toString() + "/jaxws-cxf-throttling?wsdl");
URL wsdlURL = new URL(baseURL.toString() + "/helloworld?wsdl");
QName serviceName = new QName("http://org.jboss.ws/jaxws/cxf/throttling", "HelloWorldService");
Service service = Service.create(wsdlURL, serviceName);
QName portQName = new QName("http://org.jboss.ws/jaxws/cxf/throttling", "HelloWorldImplPort");
HelloWorld port = (HelloWorld) service.getPort(portQName, HelloWorld.class);
return port;
}

private Hello getHelloPort() throws MalformedURLException
{
URL wsdlURL = new URL(baseURL.toString() + "/hello?wsdl");
QName serviceName = new QName("http://org.jboss.ws/jaxws/cxf/throttling/hello", "HelloService");
Service service = Service.create(wsdlURL, serviceName);
QName portQName = new QName("http://org.jboss.ws/jaxws/cxf/throttling/hello", "HelloImplPort");
Hello port = (Hello) service.getPort(portQName, Hello.class);
return port;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.test.ws.jaxws.samples.wsse.policy.jaas;

import java.io.File;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;

import org.apache.cxf.ws.security.SecurityConstants;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.wsf.test.JBossWSTest;
import org.jboss.wsf.test.JBossWSTestHelper;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* WS-Security Policy username test case leveraging JAAS container integration.
* WS-SecurityPolicy 1.2 used for policies in the included wsdl contract .
*
* @author alessio.soldano@jboss.com
* @since 26-May-2011
*/
@RunWith(Arquillian.class)
public final class UsernameAuthorizationTestCase extends JBossWSTest
{
@ArquillianResource
private URL baseURL;

@Deployment(testable = false)
public static WebArchive createDeployment() {
WebArchive archive = ShrinkWrap.create(WebArchive.class, "jaxws-samples-wsse-policy-username-jaas.war");
archive
.setManifest(new StringAsset("Manifest-Version: 1.0\n"
+ "Dependencies: org.jboss.ws.cxf.jbossws-cxf-client\n"))
.addClass(org.jboss.test.ws.jaxws.samples.wsse.policy.jaas.POJOEndpointAuthorizationInterceptor.class)
.addClass(org.jboss.test.ws.jaxws.samples.wsse.policy.jaas.ServiceIface.class)
.addClass(org.jboss.test.ws.jaxws.samples.wsse.policy.jaas.ServiceImpl.class)
.addClass(org.jboss.test.ws.jaxws.samples.wsse.policy.jaxws.GreetMe.class)
.addClass(org.jboss.test.ws.jaxws.samples.wsse.policy.jaxws.GreetMeResponse.class)
.addClass(org.jboss.test.ws.jaxws.samples.wsse.policy.jaxws.SayHello.class)
.addClass(org.jboss.test.ws.jaxws.samples.wsse.policy.jaxws.SayHelloResponse.class)
.addAsWebInfResource(new File(JBossWSTestHelper.getTestResourcesDir() + "/jaxws/samples/wsse/policy/jaas/auth/WEB-INF/jaxws-endpoint-config.xml"), "jaxws-endpoint-config.xml")
.addAsWebInfResource(new File(JBossWSTestHelper.getTestResourcesDir() + "/jaxws/samples/wsse/policy/jaas/auth/WEB-INF/jboss-web.xml"), "jboss-web.xml")
.addAsWebInfResource(new File(JBossWSTestHelper.getTestResourcesDir() + "/jaxws/samples/wsse/policy/jaas/auth/WEB-INF/wsdl/SecurityService.wsdl"), "wsdl/SecurityService.wsdl")
.addAsWebInfResource(new File(JBossWSTestHelper.getTestResourcesDir() + "/jaxws/samples/wsse/policy/jaas/auth/WEB-INF/wsdl/SecurityService_schema1.xsd"), "wsdl/SecurityService_schema1.xsd")
.setWebXML(new File(JBossWSTestHelper.getTestResourcesDir() + "/jaxws/samples/wsse/policy/jaas/auth/WEB-INF/web.xml"));
archive.as(ZipExporter.class).exportTo(new java.io.File("/home/jimma/tmp/jaxws-samples-wsse-policy-username-jaas.war"), true);
return archive;
}

@Test
@RunAsClient
public void test() throws Exception
{
QName serviceName = new QName("http://www.jboss.org/jbossws/ws-extensions/wssecuritypolicy", "SecurityService");
URL wsdlURL = new URL(baseURL + "/jaxws-samples-wsse-policy-username-jaas?wsdl");
Service service = Service.create(wsdlURL, serviceName);
ServiceIface proxy = (ServiceIface)service.getPort(ServiceIface.class);
setupWsse(proxy, "kermit");
assertEquals("Secure Hello World!", proxy.sayHello());
}

@Test
@RunAsClient
public void testUnauthenticated() throws Exception
{
QName serviceName = new QName("http://www.jboss.org/jbossws/ws-extensions/wssecuritypolicy", "SecurityService");
URL wsdlURL = new URL(baseURL + "/jaxws-samples-wsse-policy-username-jaas?wsdl");
Service service = Service.create(wsdlURL, serviceName);
ServiceIface proxy = (ServiceIface)service.getPort(ServiceIface.class);
setupWsse(proxy, "snoopy");
try
{
proxy.sayHello();
fail("User snoopy shouldn't be authenticated.");
}
catch (Exception e)
{
//OK
}
}

@Test
@RunAsClient
public void testUnauthorized() throws Exception
{
QName serviceName = new QName("http://www.jboss.org/jbossws/ws-extensions/wssecuritypolicy", "SecurityService");
URL wsdlURL = new URL(baseURL + "/jaxws-samples-wsse-policy-username-jaas?wsdl");
Service service = Service.create(wsdlURL, serviceName);
ServiceIface proxy = (ServiceIface)service.getPort(ServiceIface.class);
setupWsse(proxy, "kermit");
try
{
proxy.greetMe();
fail("User kermit shouldn't be authorized to call greetMe().");
}
catch (Exception e)
{
assertEquals("Unauthorized", e.getMessage());
}
}

private void setupWsse(ServiceIface proxy, String username)
{
((BindingProvider)proxy).getRequestContext().put(SecurityConstants.USERNAME, username);
((BindingProvider)proxy).getRequestContext().put(SecurityConstants.CALLBACK_HANDLER, "org.jboss.test.ws.jaxws.samples.wsse.policy.jaas.UsernamePasswordCallback");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,38 @@
</property>
<property>
<property-name>##throttlingFeature</property-name>
<property-value>org.jboss.wsf.stack.cxf.JBossWSThrottlingFeature</property-value>
<property-value>org.jboss.wsf.stack.cxf.features.throttling.JBossWSThrottlingFeature</property-value>
</property>
<property>
<property-name>##throttlingFeature.throttlingManager</property-name>
<property-value>##throttlingManager</property-value>
</property>
<property>
<property-name>##throttlingManager</property-name>
<property-value>org.jboss.wsf.stack.cxf.EndpointMetricsThrottlingManager</property-value>
<property-value>org.jboss.wsf.stack.cxf.features.throttling.EndpointMetricsThrottlingManager</property-value>
</property>
<property>
<property-name>##throttlingManager.requestCountThreshold</property-name>
<property-value>5</property-value>
</property>
</endpoint-config>

<endpoint-config>
<config-name>org.jboss.test.ws.jaxws.cxf.throttling.HelloImpl</config-name>
<property>
<property-name>cxf.features</property-name>
<property-value>##throttlingFeature</property-value>
</property>
<property>
<property-name>##throttlingFeature</property-name>
<property-value>org.jboss.wsf.stack.cxf.features.throttling.JBossWSThrottlingFeature</property-value>
</property>
<property>
<property-name>##throttlingFeature.throttlingManager</property-name>
<property-value>##throttlingManager</property-value>
</property>
<property>
<property-name>##throttlingManager</property-name>
<property-value>org.jboss.test.ws.jaxws.cxf.throttling.TestThrottlingManager</property-value>
</property>
</endpoint-config>
</jaxws-config>
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<servlet>
<servlet-name>HelloService</servlet-name>
<servlet-name>HelloWorldService</servlet-name>
<servlet-class>org.jboss.test.ws.jaxws.cxf.throttling.HelloWorldImpl</servlet-class>
</servlet>
<servlet>
<servlet-name>HelloService</servlet-name>
<servlet-class>org.jboss.test.ws.jaxws.cxf.throttling.HelloImpl</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorldService</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HelloService</servlet-name>
<url-pattern>/*</url-pattern>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>

0 comments on commit c74cc29

Please sign in to comment.