Best Cerberus-source code snippet using org.cerberus.service.soap.impl.SoapService.initializeProxyAuthenticator
Source:SoapService.java
...166 connection.close();167 return response;168 }169 }170 private void initializeProxyAuthenticator(final String proxyUser, final String proxyPassword) {171 if (proxyUser != null && proxyPassword != null) {172 Authenticator.setDefault(173 new Authenticator() {174 @Override175 public PasswordAuthentication getPasswordAuthentication() {176 return new PasswordAuthentication(177 proxyUser, proxyPassword.toCharArray()178 );179 }180 }181 );182 System.setProperty("http.proxyUser", proxyUser);183 System.setProperty("http.proxyPassword", proxyPassword);184 }185 }186 @Override187 public AnswerItem<AppService> callSOAP(String envelope, String servicePath, String soapOperation, String attachmentUrl, List<AppServiceHeader> header, String token, int timeOutMs, String system) {188 AnswerItem<AppService> result = new AnswerItem<>();189 String unescapedEnvelope = StringEscapeUtils.unescapeXml(envelope);190 boolean is12SoapVersion = SOAP_1_2_NAMESPACE_PATTERN.matcher(unescapedEnvelope).matches();191 AppService serviceSOAP = factoryAppService.create("", AppService.TYPE_SOAP, null, "", "", envelope, "", "", "", "", "", servicePath, "", soapOperation, "", null, "", null, null);192 serviceSOAP.setTimeoutms(timeOutMs);193 ByteArrayOutputStream out = null;194 MessageEvent message = null;195 if (StringUtil.isNullOrEmpty(servicePath)) {196 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP_SERVICEPATHMISSING);197 result.setResultMessage(message);198 return result;199 }200 if (StringUtil.isNullOrEmpty(envelope)) {201 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP_ENVELOPEMISSING);202 result.setResultMessage(message);203 return result;204 }205 // If header is null we create the list empty.206 if (header == null) {207 header = new ArrayList<>();208 }209 // We feed the header with token + Standard SOAP header.210 if (token != null) {211 header.add(factoryAppServiceHeader.create(null, "cerberus-token", token, "Y", 0, "", "", null, "", null));212 }213 if (StringUtil.isNullOrEmpty(soapOperation)) {214 header.add(factoryAppServiceHeader.create(null, "SOAPAction", "", "Y", 0, "", "", null, "", null));215 } else {216 header.add(factoryAppServiceHeader.create(null, "SOAPAction", "\"" + soapOperation + "\"", "Y", 0, "", "", null, "", null));217 }218 header.add(factoryAppServiceHeader.create(null, "Content-Type", is12SoapVersion ? SOAPConstants.SOAP_1_2_CONTENT_TYPE : SOAPConstants.SOAP_1_1_CONTENT_TYPE, "Y", 0, "", "", null, "", null));219 serviceSOAP.setHeaderList(header);220 SOAPConnectionFactory soapConnectionFactory;221 SOAPConnection soapConnection = null;222 try {223 //Initialize SOAP Connection224 soapConnectionFactory = SOAPConnectionFactory.newInstance();225 soapConnection = soapConnectionFactory.createConnection();226 LOG.debug("Connection opened");227 // Create SOAP Request228 LOG.debug("Create request");229 SOAPMessage input = createSoapRequest(envelope, soapOperation, header, token);230 //Add attachment File if specified231 if (!StringUtil.isNullOrEmpty(attachmentUrl)) {232 this.addAttachmentPart(input, attachmentUrl);233 // Store the SOAP Call234 out = new ByteArrayOutputStream();235 input.writeTo(out);236 LOG.debug("WS call with attachement : " + out.toString());237 serviceSOAP.setServiceRequest(out.toString());238 } else {239 // Store the SOAP Call240 out = new ByteArrayOutputStream();241 input.writeTo(out);242 LOG.debug("WS call : " + out.toString());243 }244 // We already set the item in order to keep the request message in case of failure of SOAP calls.245 serviceSOAP.setService(servicePath);246 result.setItem(serviceSOAP);247 // Call the WS248 LOG.debug("Calling WS.");249 // Reset previous Authentification.250 Authenticator.setDefault(null);251 serviceSOAP.setProxyWithCredential(false);252 serviceSOAP.setProxyUser(null);253 SOAPMessage soapResponse = null;254 if (proxyService.useProxy(servicePath, system)) {255 // Get Proxy host and port from parameters.256 String proxyHost = parameterService.getParameterStringByKey("cerberus_proxy_host", system, DEFAULT_PROXY_HOST);257 int proxyPort = parameterService.getParameterIntegerByKey("cerberus_proxy_port", system, DEFAULT_PROXY_PORT);258 serviceSOAP.setProxy(true);259 serviceSOAP.setProxyHost(proxyHost);260 serviceSOAP.setProxyPort(proxyPort);261 // Create the Proxy.262 SocketAddress sockaddr = new InetSocketAddress(proxyHost, proxyPort);263 try ( Socket socket = new Socket();) {264 socket.connect(sockaddr, 10000);265 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(socket.getInetAddress(), proxyPort));266 if (parameterService.getParameterBooleanByKey("cerberus_proxyauthentification_active", system, DEFAULT_PROXYAUTHENT_ACTIVATE)) {267 // Get the credentials from parameters.268 String proxyUser = parameterService.getParameterStringByKey("cerberus_proxyauthentification_user", system, DEFAULT_PROXYAUTHENT_USER);269 String proxyPass = parameterService.getParameterStringByKey("cerberus_proxyauthentification_password", system, DEFAULT_PROXYAUTHENT_PASSWORD);270 serviceSOAP.setProxyWithCredential(true);271 serviceSOAP.setProxyUser(proxyUser);272 // Define the credential to the proxy.273 initializeProxyAuthenticator(proxyUser, proxyPass);274 }275 // Call with Proxy.276 soapResponse = sendSOAPMessage(input, servicePath, proxy, timeOutMs);277 } catch (Exception e) {278 LOG.warn(e.toString(), e);279 }280 } else {281 serviceSOAP.setProxy(false);282 serviceSOAP.setProxyHost(null);283 serviceSOAP.setProxyPort(0);284 // Call without proxy.285 soapResponse = sendSOAPMessage(input, servicePath, null, timeOutMs);286// soapResponse = soapConnection.call(input, servicePath);287 }...
initializeProxyAuthenticator
Using AI Code Generation
1import org.apache.commons.httpclient.UsernamePasswordCredentials;2import org.apache.commons.httpclient.auth.AuthScope;3import org.apache.commons.httpclient.auth.AuthState;4import org.apache.commons.httpclient.auth.Authenticator;5import org.apache.commons.httpclient.auth.BasicScheme;6public class ProxyAuthenticator extends Authenticator {7 private final UsernamePasswordCredentials credentials;8 public ProxyAuthenticator(String username, String password) {9 this.credentials = new UsernamePasswordCredentials(username, password);10 }11 public void authenticate(AuthState authState, AuthScope authScope) {12 if (authScope.getHost().equals(credentials.getUserName()) && authScope.getPort() == credentials.getPassword().toCharArray().length) {13 authState.setAuthScheme(new BasicScheme());
initializeProxyAuthenticator
Using AI Code Generation
1 public void initializeProxyAuthenticator() {2 if (proxyLogin != null && proxyPassword != null) {3 Authenticator.setDefault(4 new Authenticator() {5 protected PasswordAuthentication getPasswordAuthentication() {6 return new PasswordAuthentication(proxyLogin, proxyPassword.toCharArray());7 }8 });9 }10 }
initializeProxyAuthenticator
Using AI Code Generation
1public void initializeProxyAuthenticator(SoapLibrary soapLibrary) {2 if (soapLibrary.getProxyHost() != null && !soapLibrary.getProxyHost().isEmpty()) {3 if (soapLibrary.getProxyUsername() != null && !soapLibrary.getProxyUsername().isEmpty()) {4 Authenticator authenticator = new Authenticator() {5 public PasswordAuthentication getPasswordAuthentication() {6 return new PasswordAuthentication(soapLibrary.getProxyUsername(), soapLibrary.getProxyPassword().toCharArray());7 }8 };9 Authenticator.setDefault(authenticator);10 }11 }12 }13public void initializeProxyAuthenticator(SoapLibrary soapLibrary) {14 if (soapLibrary.getProxyHost() != null && !soapLibrary.getProxyHost().isEmpty()) {15 if (soapLibrary.getProxyUsername() != null && !soapLibrary.getProxyUsername().isEmpty()) {16 Authenticator authenticator = new Authenticator() {17 public PasswordAuthentication getPasswordAuthentication() {18 return new PasswordAuthentication(soapLibrary.getProxyUsername(), soapLibrary.getProxyPassword().toCharArray());19 }20 };21 Authenticator.setDefault(authenticator);22 }23 }24 }25public void initializeProxyAuthenticator(SoapLibrary soapLibrary) {26 if (soapLibrary.getProxyHost() != null && !soapLibrary.getProxyHost().isEmpty()) {27 if (soapLibrary.getProxyUsername() != null && !soapLibrary.getProxyUsername().isEmpty()) {28 Authenticator authenticator = new Authenticator() {29 public PasswordAuthentication getPasswordAuthentication() {30 return new PasswordAuthentication(soapLibrary.getProxyUsername(), soapLibrary.getProxyPassword().toCharArray());31 }32 };33 Authenticator.setDefault(authenticator);34 }35 }36 }
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!!