Best Citrus code snippet using com.consol.citrus.ws.security.SecurityHandlerFactory
Source:SOAP_API_EndToEnd_IT.java
...47import com.consol.citrus.annotations.CitrusTest;48import com.consol.citrus.dsl.endpoint.CitrusEndpoints;49import com.consol.citrus.dsl.runner.TestRunner;50import com.consol.citrus.http.security.BasicAuthConstraint;51import com.consol.citrus.ws.security.SecurityHandlerFactory;52import com.consol.citrus.ws.security.User;53import com.consol.citrus.ws.server.WebServiceServer;54import com.jayway.jsonpath.Configuration;55import com.jayway.jsonpath.JsonPath;56import com.jayway.jsonpath.TypeRef;57import com.jayway.jsonpath.spi.json.JacksonJsonProvider;58import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;59/**60 * End to end test, in the sense that SOAP API generator is used to create the61 * API client connector instead of having a predefined connector. Tests all62 * combinations of path/operation, defined/referenced parameters.63 */64@Testcontainers65public class SOAP_API_EndToEnd_IT extends SyndesisIntegrationTestSupport {66 @Container67 public static final SyndesisIntegrationRuntimeContainer INTEGRATION_CONTAINER;68 private static final WebServiceServer SOAP_SERVER;69 static {70 final SecurityHandlerFactory securityHandlerFactory = new SecurityHandlerFactory();71 final User testUser = new User();72 testUser.setName("test");73 testUser.setPassword("secret");74 final String[] authenticated = new String[] {"authenticated"};75 testUser.setRoles(authenticated);76 securityHandlerFactory.setUsers(Collections.singletonList(testUser));77 securityHandlerFactory.setConstraints(Collections.singletonMap("/endpoint/*", new BasicAuthConstraint(authenticated)));78 try {79 securityHandlerFactory.afterPropertiesSet();80 SOAP_SERVER = startup(CitrusEndpoints.soap()81 .server()82 .port(SocketUtils.findAvailableTcpPort())83 .autoStart(true)84 .securityHandler(securityHandlerFactory.getObject())...
Source:SoapConnectorBasicAuth_IT.java
...25import com.consol.citrus.dsl.endpoint.CitrusEndpoints;26import com.consol.citrus.dsl.runner.TestRunner;27import com.consol.citrus.exceptions.CitrusRuntimeException;28import com.consol.citrus.http.security.BasicAuthConstraint;29import com.consol.citrus.http.security.SecurityHandlerFactory;30import com.consol.citrus.http.security.User;31import com.consol.citrus.ws.server.WebServiceServer;32import io.syndesis.test.SyndesisTestEnvironment;33import io.syndesis.test.container.integration.SyndesisIntegrationRuntimeContainer;34import io.syndesis.test.itest.SyndesisIntegrationTestSupport;35import org.eclipse.jetty.security.AbstractLoginService;36import org.eclipse.jetty.security.HashLoginService;37import org.eclipse.jetty.security.IdentityService;38import org.eclipse.jetty.security.PropertyUserStore;39import org.eclipse.jetty.security.SecurityHandler;40import org.eclipse.jetty.util.security.Credential;41import org.junit.jupiter.api.Test;42import org.springframework.util.SocketUtils;43import org.testcontainers.containers.GenericContainer;44import org.testcontainers.junit.jupiter.Container;45import org.testcontainers.junit.jupiter.Testcontainers;46import static org.hamcrest.CoreMatchers.is;47@Testcontainers48public class SoapConnectorBasicAuth_IT extends SyndesisIntegrationTestSupport {49 private static final int SOAP_SERVER_PORT = SocketUtils.findAvailableTcpPort();50 private static final String USERNAME = "registered";51 private static final String PASSWORD = "secret";52 private static final List<User> USERS = new ArrayList<User>();53 private static final String[] ROLES = new String[]{USERNAME};54 private static final User USER = new User();55 static {56 org.testcontainers.Testcontainers.exposeHostPorts(SOAP_SERVER_PORT);57 USER.setName(USERNAME);58 USER.setRoles(ROLES);59 USER.setPassword(PASSWORD);60 USERS.add(USER);61 }62 private static final WebServiceServer SOAP_SERVER = startup(soapServer());63 private static final String REQUEST_PAYLOAD =64 "<ns1:sayHi xmlns:ns1=\"http://camel.apache.org/cxf/wsrm\">" +65 "<arg0 xmlns=\"http://camel.apache.org/cxf/wsrm\">BasicAuth</arg0>" +66 "</ns1:sayHi>";67 private static final String RESPONSE_PAYLOAD =68 "<ns1:sayHiResponse xmlns:ns1=\"http://camel.apache.org/cxf/wsrm\">" +69 " <ns1:return xmlns=\"http://camel.apache.org/cxf/wsrm\">Hello BasicAuth!</ns1:return>" +70 "</ns1:sayHiResponse>";71 /**72 * Integration uses api connector to send SOAP client requests to a REST endpoint. The client API connector was generated73 * from SOAP WSDL1.1 specification.74 * <p>75 * The integration invokes following sequence of client requests on the test server76 * Invoke operation sayHi.77 */78 @Container79 public static final SyndesisIntegrationRuntimeContainer INTEGRATION_CONTAINER = new SyndesisIntegrationRuntimeContainer.Builder()80 .name("soap-basic-auth")81 .fromExport(SoapConnectorBasicAuth_IT.class.getResource("SOAPBasicAuthentication-export"))82 .customize("$..configuredProperties.period", "5000")83 .customize("$..configuredProperties.address",84 String.format("http://%s:%s/HelloWorld", GenericContainer.INTERNAL_HOST_HOSTNAME, SOAP_SERVER_PORT))85 .build()86 .withNetwork(getSyndesisDb().getNetwork())87 .withExposedPorts(SyndesisTestEnvironment.getServerPort(),88 SyndesisTestEnvironment.getManagementPort());89 @Test90 @CitrusTest91 public void testSayHi(@CitrusResource TestRunner runner) {92 runner.sql(builder -> builder.dataSource(sampleDb())93 .statement("delete from contact"));94 runner.echo("SayHi operation");95 runner.soap(builder -> builder.server(SOAP_SERVER)96 .receive()97 .payload(REQUEST_PAYLOAD));98 runner.soap(builder -> builder.server(SOAP_SERVER)99 .send()100 .payload(RESPONSE_PAYLOAD));101 runner.repeatOnError()102 .index("retries")103 .autoSleep(1000L)104 .until(is(6))105 .actions(runner.query(builder -> builder.dataSource(sampleDb())106 .statement("select count(*) as found_records from contact where first_name like 'Hello BasicAuth!'")107 .validateScript("assert rows.get(0).get(\"found_records\") > 0", "groovy")));108 }109 public static WebServiceServer soapServer() {110 return CitrusEndpoints.soap()111 .server()112 .port(SOAP_SERVER_PORT)113 .securityHandler(basicAuthSecurityHandler())114 .autoStart(true)115 .timeout(600000L)116 .build();117 }118 public static SecurityHandler basicAuthSecurityHandler() {119 try {120 return basicAuthSecurityHandlerFactoryBean().getObject();121 } catch (Exception e) {122 throw new CitrusRuntimeException("Failed to create basic auth security handler", e);123 }124 }125 public static SecurityHandlerFactory basicAuthSecurityHandlerFactoryBean() {126 SecurityHandlerFactory securityHandlerFactory = new SecurityHandlerFactory();127 securityHandlerFactory.setUsers(USERS);128 securityHandlerFactory.setLoginService(basicAuthLoginService(basicAuthUserStore()));129 securityHandlerFactory.setConstraints(130 Collections.singletonMap("/*", new BasicAuthConstraint(ROLES)));131 return securityHandlerFactory;132 }133 public static HashLoginService basicAuthLoginService(PropertyUserStore basicAuthUserStore) {134 return new HashLoginService() {135 @Override136 protected void doStart() throws Exception {137 setUserStore(basicAuthUserStore);138 basicAuthUserStore.start();139 super.doStart();140 }...
Source:SecurityHandlerFactory.java
...42 *43 * @author Christoph Deppisch44 * @since 1.345 */46public class SecurityHandlerFactory implements FactoryBean<SecurityHandler>, InitializingPhase {47 /** User credentials known to login service */48 private List<User> users = new ArrayList<>();49 /** Realm name for this security handler */50 private String realm = "realm";51 /** List of constraints with mapping path as key */52 private Map<String, Constraint> constraints = new HashMap<String, Constraint>();53 /** User login service consolidated for user authentication */54 private LoginService loginService;55 /** Authenticator implementation - basic auth by default */56 private Authenticator authenticator = new BasicAuthenticator();57 /**58 * Construct new security handler for basic authentication.59 */60 public SecurityHandler getObject() throws Exception {...
SecurityHandlerFactory
Using AI Code Generation
1import com.consol.citrus.ws.security.SecurityHandlerFactory;2import org.apache.cxf.configuration.security.AuthorizationPolicy;3import org.apache.cxf.endpoint.Client;4import org.apache.cxf.frontend.ClientProxy;5import org.apache.cxf.interceptor.Interceptor;6import org.apache.cxf.message.Message;7import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;8import java.util.HashMap;9import java.util.Map;10public class 3 {11 public static void main(String[] args) {12 Map<String, Object> securityHandlerProperties = new HashMap<String, Object>();13 securityHandlerProperties.put("passwordType", "PasswordText");14 securityHandlerProperties.put("passwordCallbackClass", "com.consol.citrus.ws.security.WsPasswordCallback");15 securityHandlerProperties.put("encryptionUser", "client");16 securityHandlerProperties.put("encryptionKeyIdentifier", "IssuerSerial");17 securityHandlerProperties.put("signatureUser", "client");18 securityHandlerProperties.put("signatureKeyIdentifier", "IssuerSerial");19 SecurityHandlerFactory securityHandlerFactory = new SecurityHandlerFactory(securityHandlerProperties);20 Client client = ClientProxy.getClient(soap);21 client.getOutInterceptors().add(securityHandlerFactory.getSecurityHandler());22 UsernameTokenInterceptor usernameTokenInterceptor = new UsernameTokenInterceptor();23 Map<String, Object> usernameTokenInterceptorProperties = new HashMap<String, Object>();24 usernameTokenInterceptorProperties.put("passwordType", "PasswordText");25 usernameTokenInterceptorProperties.put("passwordCallbackClass", "com.consol.citrus.ws.security.WsPasswordCallback");
SecurityHandlerFactory
Using AI Code Generation
1import com.consol.citrus.ws.security.SecurityHandlerFactory;2import com.consol.citrus.ws.security.Wss4jSecurityHandler;3import org.springframework.core.io.ClassPathResource;4import org.springframework.ws.soap.security.wss4j2.Wss4jSecurityInterceptor;5import org.springframework.ws.soap.security.wss4j2.callback.SimplePasswordValidationCallbackHandler;6import org.springframework.ws.soap.security.wss4j2.support.CryptoFactoryBean;7import org.springframework.ws.soap.security.xwss.XwsSecurityInterceptor;8import org.springframework.ws.soap.security.xwss.callback.SimplePasswordValidationCallbackHandler;9import org.springframework.ws.soap.security.xwss.support.CryptoFactoryBean;10import org.springframework.ws.soap.security.xwss.XwsSecurityInterceptor;11import org.springframework.ws.soap.security.xwss.callback.SimplePasswordValidationCallbackHandler;12import org.springframework.ws.soap.security.xwss.support.CryptoFactoryBean;13import org.springframework.ws.soap.security.xwss.XwsSecurityInterceptor;14import org.springframework.ws.soap.security.xwss.callback.SimplePasswordValidationCallbackHandler;15import org.springframework.ws.soap.security.xwss.support.CryptoFactoryBean;16import org.springframework.ws.soap.security.xwss.XwsSecurityInterceptor;17import org.springframework.ws.soap.security.xwss.callback.SimplePasswordValidationCallbackHandler;18import org.springframework.ws.soap.security.xwss.support.CryptoFactoryBean;19import java.util.Properties;20public class WSSecurityHandlerFactory implements SecurityHandlerFactory {21 public Wss4jSecurityHandler createSecurityHandler(String type) {22 Wss4jSecurityHandler securityHandler = new Wss4jSecurityHandler();23 Wss4jSecurityInterceptor securityInterceptor = new Wss4jSecurityInterceptor();24 securityHandler.setSecurityInterceptor(securityInterceptor);25 if ("basic".equals(type)) {26 securityInterceptor.setValidationActions("UsernameToken");27 securityInterceptor.setValidationCallbackHandler(new SimplePasswordValidationCallbackHandler());28 } else if ("digest".equals(type)) {29 securityInterceptor.setValidationActions("UsernameToken");30 securityInterceptor.setValidationCallbackHandler(new SimplePasswordValidationCallbackHandler());31 securityInterceptor.setValidationPasswordType("PasswordDigest");32 } else if ("signature".equals(type)) {33 securityInterceptor.setValidationActions("Timestamp Signature");34 securityInterceptor.setSecurementActions("Timestamp Signature");35 securityInterceptor.setSecurementPasswordType("PasswordText");
SecurityHandlerFactory
Using AI Code Generation
1SecurityHandlerFactory securityHandlerFactory = new SecurityHandlerFactory();2securityHandlerFactory.setKeyStore("classpath:com/consol/citrus/ws/security/keystore.jks");3securityHandlerFactory.setKeyStorePassword("changeit");4securityHandlerFactory.setKeyStoreType("JKS");5securityHandlerFactory.setPrivateKeyAlias("server");6securityHandlerFactory.setPrivateKeyPassword("changeit");7securityHandlerFactory.setTrustStore("classpath:com/consol/citrus/ws/security/truststore.jks");8securityHandlerFactory.setTrustStorePassword("changeit");9securityHandlerFactory.setTrustStoreType("JKS");10securityHandlerFactory.setTrustKeyAlias("client");11securityHandlerFactory.setValidateCertificate(false);12Server server = new Server();13server.setPort(8080);14server.getEndpoints().add(new SimpleEndpointAdapter() {15 public boolean isSupportingEndpointUri(String endpointUri) {16 }17});18server.getEndpointConfiguration().setSecurityHandler(securityHandlerFactory.getObject());19server.start();20SecurityHandlerFactory securityHandlerFactory = new SecurityHandlerFactory();21securityHandlerFactory.setKeyStore("classpath:com/consol/citrus/ws/security/keystore.jks");22securityHandlerFactory.setKeyStorePassword("changeit");23securityHandlerFactory.setKeyStoreType("JKS");24securityHandlerFactory.setPrivateKeyAlias("server");25securityHandlerFactory.setPrivateKeyPassword("changeit");26securityHandlerFactory.setTrustStore("classpath:com/consol/citrus/ws/security/truststore.jks");27securityHandlerFactory.setTrustStorePassword("changeit");28securityHandlerFactory.setTrustStoreType("JKS");29securityHandlerFactory.setTrustKeyAlias("client");30securityHandlerFactory.setValidateCertificate(false);31Server server = new Server();32server.setPort(8080);33server.getEndpoints().add(new SimpleEndpointAdapter() {34 public boolean isSupportingEndpointUri(String endpointUri) {35 }36});37server.getEndpointConfiguration().setSecurityHandler(securityHandlerFactory.getObject());38server.start();
SecurityHandlerFactory
Using AI Code Generation
1import java.io.File;2import org.springframework.context.support.ClassPathXmlApplicationContext;3import com.consol.citrus.ws.client.WebServiceClient;4public class 3 {5public static void main(String[] args) throws Exception {6ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:3.xml");7WebServiceClient webServiceClient = context.getBean("webServiceClient", WebServiceClient.class);8webServiceClient.send("request");9context.close();10}11}12port="${serverPort}"13import java.io.File;14import org.springframework.context.support
SecurityHandlerFactory
Using AI Code Generation
1package com.consol.citrus.ws;2import com.consol.citrus.ws.client.WebServiceClient;3import com.consol.citrus.ws.security.SecurityHandlerFactory;4import org.springframework.context.annotation.Bean;5import org.springframework.context.annotation.Configuration;6public class WebServiceClientConfig {7 public WebServiceClient webServiceClient() {8 WebServiceClient client = new WebServiceClient();9 client.setSecurityHandler(SecurityHandlerFactory.createUsernameTokenSecurityHandler("admin", "password"));10 return client;11 }12}13package com.consol.citrus.ws;14import com.consol.citrus.ws.client.WebServiceClient;15import com.consol.citrus.ws.message.SoapMessage;16import com.consol.citrus.ws.message.SoapMessageHeaders;17import org.springframework.beans.factory.annotation.Autowired;18import org.springframework.context.annotation.Bean;19import org.springframework.context.annotation.Configuration;20public class WebServiceClientConfig {21 private WebServiceClient webServiceClient;22 public SoapMessage greetingRequest() {23 SoapMessage greetingRequest = new SoapMessage();24 "</ns0:GreetingRequest>");25 greetingRequest.setHeader(SoapMessageHeaders.SOAP_ACTION, "sayHello");26 return greetingRequest;27 }28 public SoapMessage greetingResponse() {29 SoapMessage greetingResponse = new SoapMessage();30 "</ns0:GreetingResponse>");31 return greetingResponse;32 }33}
SecurityHandlerFactory
Using AI Code Generation
1public class SecurityHandlerFactoryTest {2 public void testSecurityHandlerFactory() {3 SecurityHandlerFactory securityHandlerFactory = new SecurityHandlerFactory();4 securityHandlerFactory.setSecurityHandlers(Arrays.asList(new SecurityHandler[]{new Wss4jSecurityHandler()}));5 securityHandlerFactory.afterPropertiesSet();6 Assert.assertNotNull(securityHandlerFactory.getObject());7 }8}9public class SecurityHandlerFactoryBeanTest {10 public void testSecurityHandlerFactoryBean() {11 SecurityHandlerFactoryBean securityHandlerFactoryBean = new SecurityHandlerFactoryBean();12 securityHandlerFactoryBean.setSecurityHandlers(Arrays.asList(new SecurityHandler[]{new Wss4jSecurityHandler()}));13 Assert.assertNotNull(securityHandlerFactoryBean.getObject());14 }15}16public class SoapAttachmentSecurityHandlerTest {17 public void testSoapAttachmentSecurityHandler() {18 SoapAttachmentSecurityHandler soapAttachmentSecurityHandler = new SoapAttachmentSecurityHandler();19 soapAttachmentSecurityHandler.setSecurityHandler(new Wss4jSecurityHandler());20 soapAttachmentSecurityHandler.setSecurityHeaderName("Security");21 soapAttachmentSecurityHandler.afterPropertiesSet();22 Assert.assertNotNull(soapAttachmentSecurityHandler);23 }24}25public class SoapSecurityHandlerTest {26 public void testSoapSecurityHandler() {27 SoapSecurityHandler soapSecurityHandler = new SoapSecurityHandler();28 soapSecurityHandler.setSecurityHandler(new Wss4jSecurityHandler());29 soapSecurityHandler.setSecurityHeaderName("Security");30 soapSecurityHandler.afterPropertiesSet();31 Assert.assertNotNull(soapSecurityHandler);32 }33}34public class Wss4jSecurityHandlerTest {35 public void testWss4jSecurityHandler() {36 Wss4jSecurityHandler wss4jSecurityHandler = new Wss4jSecurityHandler();37 wss4jSecurityHandler.setSecurityActions("UsernameToken");38 wss4jSecurityHandler.setSecurityUsername("citrus
SecurityHandlerFactory
Using AI Code Generation
1public static SecurityHandler getSecurityHandler() throws Exception{2 SecurityHandlerFactory factory = new SecurityHandlerFactory();3 factory.setSecurityHandler("securityHandler");4 factory.setSecurityHandlerClass("com.consol.citrus.ws.security.WSSecurityHandler");5 factory.setProperties(getSecurityHandlerProperties());6 return factory.getObject();7}8private static Properties getSecurityHandlerProperties() {9 Properties properties = new Properties();10 properties.setProperty("user", "user");11 properties.setProperty("password", "password");12 return properties;13}14public static SecurityHandler getSecurityHandler() throws Exception{15 WSSecurityHandler securityHandler = new WSSecurityHandler();16 securityHandler.setUser("user");17 securityHandler.setPassword("password");18 return securityHandler;19}20public static SecurityHandler getSecurityHandler() throws Exception{21 SecurityHandlerFactory factory = new SecurityHandlerFactory();22 factory.setSecurityHandler("securityHandler");23 factory.setSecurityHandlerClass("com.consol.citrus.ws.security.WSSecurityHandler");24 factory.setProperties(getSecurityHandlerProperties());25 return factory.getObject();26}27private static Properties getSecurityHandlerProperties() {28 Properties properties = new Properties();29 properties.setProperty("user", "user");30 properties.setProperty("password", "password");31 return properties;32}33public static SecurityHandler getSecurityHandler() throws Exception{34 WSSecurityHandler securityHandler = new WSSecurityHandler();35 securityHandler.setUser("user");36 securityHandler.setPassword("password");37 return securityHandler;38}39public static SecurityHandler getSecurityHandler() throws Exception{40 SecurityHandlerFactory factory = new SecurityHandlerFactory();41 factory.setSecurityHandler("securityHandler");42 factory.setSecurityHandlerClass("com.consol.citrus.ws.security.WSSecurityHandler");43 factory.setProperties(getSecurityHandlerProperties());44 return factory.getObject();45}46private static Properties getSecurityHandlerProperties()
SecurityHandlerFactory
Using AI Code Generation
1import com.consol.citrus.ws.security.*;2import com.consol.citrus.ws.security.handler.*;3import java.util.*;4import java.io.*;5import java.net.*;6import javax.xml.namespace.QName;7import javax.xml.ws.BindingProvider;8import javax.xml.ws.handler.Handler;9import javax.xml.ws.handler.MessageContext;10import javax.xml.ws.soap.SOAPBinding;11{12 public static void main(String[] args) throws Exception13 {14 Service service = Service.create(url, qname);15 HelloWorld hello = service.getPort(HelloWorld.class);16 SecurityHandlerFactory shf = new SecurityHandlerFactory();17 SecurityHandler sh = shf.createSecurityHandler("com.consol.citrus.ws.security.handler.UsernameTokenHandler");18 List<Handler> handlers = new ArrayList<Handler>();19 handlers.add(sh);20 BindingProvider bp = (BindingProvider) hello;21 SOAPBinding binding = (SOAPBinding) bp.getBinding();22 binding.setHandlerChain(handlers);23 Map<String, Object> requestContext = bp.getRequestContext();24 requestContext.put(BindingProvider.USERNAME_PROPERTY, "user");25 requestContext.put(BindingProvider.PASSWORD_PROPERTY, "pass");26 System.out.println(hello.sayHello("user"));27 }28}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!