org.apache.commons.httpclient.UsernamePasswordCredentials Java Examples
The following examples show how to use
org.apache.commons.httpclient.UsernamePasswordCredentials.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpTemplateDownloader.java From cloudstack with Apache License 2.0 | 7 votes |
private void checkCredentials(String user, String password) { try { Pair<String, Integer> hostAndPort = UriUtils.validateUrl(downloadUrl); if ((user != null) && (password != null)) { client.getParams().setAuthenticationPreemptive(true); Credentials defaultcreds = new UsernamePasswordCredentials(user, password); client.getState().setCredentials(new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM), defaultcreds); s_logger.info("Added username=" + user + ", password=" + password + "for host " + hostAndPort.first() + ":" + hostAndPort.second()); } else { s_logger.info("No credentials configured for host=" + hostAndPort.first() + ":" + hostAndPort.second()); } } catch (IllegalArgumentException iae) { errorString = iae.getMessage(); status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; inited = false; } }
Example #2
Source File: CredentialsUtils.java From httpclientAuthHelper with Apache License 2.0 | 7 votes |
public static void setNTLMCredentials(HttpClient httpClient, UsernamePasswordCredentials credentials, String domain) { initNTLMv2(); String localHostName; try { localHostName = Inet4Address.getLocalHost().getHostName(); } catch (Exception e) { localHostName = ""; } AuthScope authscope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT); httpClient.getState().setCredentials( authscope, new NTCredentials( credentials.getUserName(), credentials.getPassword(), localHostName, domain)); }
Example #3
Source File: ErrorReportSender.java From otroslogviewer with Apache License 2.0 | 6 votes |
public String sendReport(Map<String, String> values) throws IOException { HttpClient httpClient = new HttpClient(); HostConfiguration hostConfiguration = new HostConfiguration(); if (!StringUtils.isBlank(proxy)) { hostConfiguration.setProxy(proxy, proxyPort); if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password)) { httpClient.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password)); } } httpClient.setHostConfiguration(hostConfiguration); PostMethod method = new PostMethod(getSendUrl()); addHttpPostParams(values, method); int executeMethod = httpClient.executeMethod(method); LOGGER.info("HTTP result of report send POST: " + executeMethod); return IOUtils.toString(method.getResponseBodyAsStream()); }
Example #4
Source File: HttpUtil.java From openhab1-addons with Eclipse Public License 2.0 | 6 votes |
/** * Extracts username and password from the given <code>url</code>. A valid * url to extract {@link Credentials} from looks like: * * <pre> * http://username:[email protected] * </pre> * * @param url the URL to extract {@link Credentials} from * * @return the exracted Credentials or <code>null</code> if the given * <code>url</code> does not contain credentials */ protected static Credentials extractCredentials(String url) { Matcher matcher = URL_CREDENTIALS_PATTERN.matcher(url); if (matcher.matches()) { matcher.reset(); String username = ""; String password = ""; while (matcher.find()) { username = matcher.group(1); password = matcher.group(2); } Credentials credentials = new UsernamePasswordCredentials(username, password); return credentials; } return null; }
Example #5
Source File: UriUtils.java From cloudstack with Apache License 2.0 | 6 votes |
public static InputStream getInputStreamFromUrl(String url, String user, String password) { try { Pair<String, Integer> hostAndPort = validateUrl(url); HttpClient httpclient = new HttpClient(new MultiThreadedHttpConnectionManager()); if ((user != null) && (password != null)) { httpclient.getParams().setAuthenticationPreemptive(true); Credentials defaultcreds = new UsernamePasswordCredentials(user, password); httpclient.getState().setCredentials(new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM), defaultcreds); s_logger.info("Added username=" + user + ", password=" + password + "for host " + hostAndPort.first() + ":" + hostAndPort.second()); } // Execute the method. GetMethod method = new GetMethod(url); int statusCode = httpclient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { s_logger.error("Failed to read from URL: " + url); return null; } return method.getResponseBodyAsStream(); } catch (Exception ex) { s_logger.error("Failed to read from URL: " + url); return null; } }
Example #6
Source File: TlsTunnelBuilder.java From jframe with Apache License 2.0 | 6 votes |
private Socket AuthenticateProxy(ConnectMethod method, ProxyClient client, String proxyHost, int proxyPort, String proxyUsername, String proxyPassword) throws IOException { if(method.getProxyAuthState().getAuthScheme().getSchemeName().equalsIgnoreCase("ntlm")) { // If Auth scheme is NTLM, set NT credentials with blank host and domain name client.getState().setProxyCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUsername, proxyPassword,"","")); } else { // If Auth scheme is Basic/Digest, set regular Credentials client.getState().setProxyCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUsername, proxyPassword)); } ProxyClient.ConnectResponse response = client.connect(); Socket socket = response.getSocket(); if (socket == null) { method = response.getConnectMethod(); throw new ProtocolException("Proxy Authentication failed. Socket not created: " + method.getStatusLine()); } return socket; }
Example #7
Source File: HttpClientFactory.java From olat with Apache License 2.0 | 6 votes |
/** * A HttpClient with basic authentication and no host or port setting. Can only be used to retrieve absolute URLs * * @param user * can be NULL * @param password * can be NULL * @return HttpClient */ public static HttpClient getHttpClientInstance(String user, String password) { HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); HttpConnectionParams params = connectionManager.getParams(); // wait max 10 seconds to establish connection params.setConnectionTimeout(10000); // a read() call on the InputStream associated with this Socket // will block for only this amount params.setSoTimeout(10000); HttpClient c = new HttpClient(connectionManager); // use basic authentication if available if (user != null && user.length() > 0) { AuthScope authScope = new AuthScope(null, -1, null); Credentials credentials = new UsernamePasswordCredentials(user, password); c.getState().setCredentials(authScope, credentials); } return c; }
Example #8
Source File: HttpClientFactory.java From olat with Apache License 2.0 | 6 votes |
/** * A HttpClient with basic authentication and no host or port setting. Can only be used to retrieve absolute URLs * * @param user * can be NULL * @param password * can be NULL * @return HttpClient */ public static HttpClient getHttpClientInstance(String user, String password) { HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); HttpConnectionParams params = connectionManager.getParams(); // wait max 10 seconds to establish connection params.setConnectionTimeout(10000); // a read() call on the InputStream associated with this Socket // will block for only this amount params.setSoTimeout(10000); HttpClient c = new HttpClient(connectionManager); // use basic authentication if available if (user != null && user.length() > 0) { AuthScope authScope = new AuthScope(null, -1, null); Credentials credentials = new UsernamePasswordCredentials(user, password); c.getState().setCredentials(authScope, credentials); } return c; }
Example #9
Source File: BasicScheme.java From knopflerfish.org with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Returns a basic <tt>Authorization</tt> header value for the given * {@link UsernamePasswordCredentials} and charset. * * @param credentials The credentials to encode. * @param charset The charset to use for encoding the credentials * * @return a basic authorization string * * @since 3.0 */ public static String authenticate(UsernamePasswordCredentials credentials, String charset) { LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials, String)"); if (credentials == null) { throw new IllegalArgumentException("Credentials may not be null"); } if (charset == null || charset.length() == 0) { throw new IllegalArgumentException("charset may not be null or empty"); } StringBuffer buffer = new StringBuffer(); buffer.append(credentials.getUserName()); buffer.append(":"); buffer.append(credentials.getPassword()); return "Basic " + EncodingUtil.getAsciiString( Base64.encodeBase64(EncodingUtil.getBytes(buffer.toString(), charset))); }
Example #10
Source File: BasicAuthLoader.java From development with Apache License 2.0 | 6 votes |
/** * Retrieves the content under the given URL with username and passwort * authentication. * * @param url * the URL to read * @param username * @param password * @return the read content. * @throws IOException * if an I/O exception occurs. */ private static byte[] getUrlContent(URL url, String username, String password) throws IOException { final HttpClient client = new HttpClient(); // Set credentials: client.getParams().setAuthenticationPreemptive(true); final Credentials credentials = new UsernamePasswordCredentials( username, password); client.getState() .setCredentials( new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM), credentials); // Retrieve content: final GetMethod method = new GetMethod(url.toString()); final int status = client.executeMethod(method); if (status != HttpStatus.SC_OK) { throw new IOException("Error " + status + " while retrieving " + url); } return method.getResponseBody(); }
Example #11
Source File: RORClientTest.java From development with Apache License 2.0 | 6 votes |
@Test public void createClient() throws Exception { // given System.setProperty(HTTPS_PROXY_HOST, HTTPS_PROXY_HOST_VALUE); System.setProperty(HTTPS_PROXY_PORT, HTTPS_PROXY_PORT_VALUE); System.setProperty(HTTPS_PROXY_USER, HTTPS_PROXY_USER_VALUE); System.setProperty(HTTPS_PROXY_PASSWORD, HTTPS_PROXY_PASSWORD_VALUE); Credentials proxyCredentials = new UsernamePasswordCredentials( HTTPS_PROXY_USER_VALUE, HTTPS_PROXY_PASSWORD_VALUE); AuthScope authScope = new AuthScope(HTTPS_PROXY_HOST_VALUE, Integer.parseInt(HTTPS_PROXY_PORT_VALUE)); // when HttpClient client = vdcClient.createHttpClient(); // then assertEquals(HTTPS_PROXY_HOST_VALUE, client.getHostConfiguration() .getProxyHost()); assertEquals(HTTPS_PROXY_PORT_VALUE, String.valueOf(client.getHostConfiguration().getProxyPort())); assertEquals(proxyCredentials, client.getState().getProxyCredentials(authScope)); }
Example #12
Source File: PortFactory.java From development with Apache License 2.0 | 6 votes |
/** * Retrieves the content under the given URL with username and passwort * authentication. * * @param url * @param username * @param password * @return * @throws IOException */ private static byte[] getUrlContent(URL url, String username, String password) throws IOException { final HttpClient client = new HttpClient(); // Set credentials: client.getParams().setAuthenticationPreemptive(true); final Credentials credentials = new UsernamePasswordCredentials( username, password); client.getState() .setCredentials( new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM), credentials); // Retrieve content: final GetMethod method = new GetMethod(url.toString()); final int status = client.executeMethod(method); if (status != HttpStatus.SC_OK) { throw new IOException("Error " + status + " while retrieving " + url); } return method.getResponseBody(); }
Example #13
Source File: UriUtils.java From cosmic with Apache License 2.0 | 6 votes |
public static InputStream getInputStreamFromUrl(final String url, final String user, final String password) { try { final Pair<String, Integer> hostAndPort = validateUrl(url); final HttpClient httpclient = new HttpClient(new MultiThreadedHttpConnectionManager()); if ((user != null) && (password != null)) { httpclient.getParams().setAuthenticationPreemptive(true); final Credentials defaultcreds = new UsernamePasswordCredentials(user, password); httpclient.getState().setCredentials(new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM), defaultcreds); s_logger.info("Added username=" + user + ", password=" + password + "for host " + hostAndPort.first() + ":" + hostAndPort.second()); } // Execute the method. final GetMethod method = new GetMethod(url); final int statusCode = httpclient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { s_logger.error("Failed to read from URL: " + url); return null; } return method.getResponseBodyAsStream(); } catch (final Exception ex) { s_logger.error("Failed to read from URL: " + url); return null; } }
Example #14
Source File: HttpWorker.java From office-365-connector-plugin with Apache License 2.0 | 6 votes |
private HttpClient getHttpClient() { HttpClient client = new HttpClient(); Jenkins jenkins = Jenkins.getInstance(); if (jenkins != null) { ProxyConfiguration proxy = jenkins.proxy; if (proxy != null) { client.getHostConfiguration().setProxy(proxy.name, proxy.port); String username = proxy.getUserName(); String password = proxy.getPassword(); // Consider it to be passed if username specified. Sufficient? if (StringUtils.isNotBlank(username)) { client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); } } } client.getParams().setConnectionManagerTimeout(timeout); client.getHttpConnectionManager().getParams().setSoTimeout(timeout); client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout); return client; }
Example #15
Source File: ThankYouWriterTest.java From jrpip with Apache License 2.0 | 6 votes |
public void testAddRequest() throws Exception { AuthenticatedUrl url = new AuthenticatedUrl(this.getJrpipUrl(), new UsernamePasswordCredentials("username")); HttpMessageTransport transport = new HttpMessageTransport(); Cookie cookie1 = new Cookie("domain", "cookie1", "val1", "/", 1000, false); Cookie cookie2 = new Cookie("domain", "cookie2", "val2", "/", 1000, false); Cookie cookie3 = new Cookie("domain", "cookie3", "val3", "/", 1000, false); ThankYouWriter thankYouWriter = ThankYouWriter.getINSTANCE(); thankYouWriter.stopThankYouThread(); thankYouWriter.addRequest(transport, new HttpMessageTransportData(url, true, 0, new Cookie[]{cookie1, cookie2, cookie3}), new RequestId(1)); thankYouWriter.addRequest(transport, new HttpMessageTransportData(url, true, 0, new Cookie[]{cookie1, cookie2, cookie3}), new RequestId(2)); // same combination thankYouWriter.addRequest(transport, new HttpMessageTransportData(url, true, 0, new Cookie[]{cookie3, cookie2, cookie1}), new RequestId(3)); // cookie order changed thankYouWriter.addRequest(transport, new HttpMessageTransportData(url, true, 0, new Cookie[]{cookie3}), new RequestId(4)); // mismatch cookies thankYouWriter.addRequest(transport, new HttpMessageTransportData(url, true, 0, new Cookie[]{}), new RequestId(5)); // no cookies thankYouWriter.addRequest(transport, new HttpMessageTransportData(url, true, 0, null) , new RequestId(6)); // null cookies assertEquals(3, thankYouWriter.getPendingRequests()); }
Example #16
Source File: ProfileFilter.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
private void authenticate(UsernamePasswordCredentials credentials) throws Throwable { logger.debug("IN: userId = " + credentials.getUserName()); try { ISecurityServiceSupplier supplier = SecurityServiceSupplierFactory.createISecurityServiceSupplier(); SpagoBIUserProfile profile = supplier.checkAuthentication(credentials.getUserName(), credentials.getPassword()); if (profile == null) { logger.error("Authentication failed for user " + credentials.getUserName()); throw new SecurityException("Authentication failed"); } } catch (Throwable t) { logger.error("Error while authenticating userId = " + credentials.getUserName(), t); throw t; } finally { logger.debug("OUT"); } }
Example #17
Source File: ProfileFilter.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
private String getUserIdInWebModeWithoutSSO(HttpServletRequest httpRequest) { UsernamePasswordCredentials credentials = this.findUserCredentials(httpRequest); if (credentials != null) { logger.debug("User credentials found."); if (!httpRequest.getMethod().equalsIgnoreCase("POST")) { logger.error("Request method is not POST!!!"); throw new InvalidMethodException(); } logger.debug("Authenticating user ..."); try { this.authenticate(credentials); logger.debug("User authenticated"); httpRequest.getSession().setAttribute(SsoServiceInterface.SILENT_LOGIN, Boolean.TRUE); } catch (Throwable t) { logger.error("Authentication failed", t); throw new SilentAuthenticationFailedException(); } } else { logger.debug("User credentials not found."); } String userId = credentials != null ? credentials.getUserName() : null; return userId; }
Example #18
Source File: OAuth2Client.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
public HttpClient getHttpClient() { String proxyUrl = System.getProperty("http.proxyHost"); String proxyPort = System.getProperty("http.proxyPort"); String proxyUser = System.getProperty("http.proxyUsername"); String proxyPassword = System.getProperty("http.proxyPassword"); HttpClient client = new HttpClient(); if (proxyUrl != null && proxyPort != null) { logger.debug("Setting proxy configuration ..."); client.getHostConfiguration().setProxy(proxyUrl, Integer.parseInt(proxyPort)); if (proxyUser != null) { logger.debug("Setting proxy authentication configuration ..."); HttpState state = new HttpState(); state.setProxyCredentials(null, null, new UsernamePasswordCredentials(proxyUser, proxyPassword)); client.setState(state); } } else { logger.debug("No proxy configuration found"); } return client; }
Example #19
Source File: AuthUtilsTest.java From httpclientAuthHelper with Apache License 2.0 | 5 votes |
@Ignore("Not yet implemented") public void testUseNTLMforMixedAuth() throws IOException { String url = "yourCLAIMSandNTLMserver"; SSLUtils.trustAllSSLCertificates(); CredentialsUtils.setNTLMCredentials(client, new UsernamePasswordCredentials("xxx", "xxx"), "domain"); int respose = executeRequestReturnStatus(url); assertEquals("Should return a 200 response", 200, respose); }
Example #20
Source File: TestRailClient.java From testrail-jenkins-plugin with Apache License 2.0 | 5 votes |
private HttpClient setUpHttpClient(HttpMethod method) { HttpClient httpclient = new HttpClient(); httpclient.getParams().setAuthenticationPreemptive(true); httpclient.getState().setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(this.user, this.password) ); method.setDoAuthentication(true); method.addRequestHeader("Content-Type", "application/json"); return httpclient; }
Example #21
Source File: Proxy.java From egdownloader with GNU General Public License v2.0 | 5 votes |
public static HttpClient getHttpClient(){ HttpClient httpClient = new HttpClient(); if(useProxy){ if(useIEProxy){ String[] ieproxys = getIEProxy(); if(ieproxys != null){ //取第一个代理设置 String[] arr = ieproxys[0].split("="); if(arr.length == 2){ //String type = arr[0]; String[] hostport = arr[1].split(":"); if(hostport.length == 2){ httpClient.getHostConfiguration().setProxy(hostport[0], Integer.parseInt(hostport[1])); } } } }else{ //设置代理服务器的ip地址和端口 httpClient.getHostConfiguration().setProxy(ip, Integer.parseInt(port)); //使用抢先认证 httpClient.getParams().setAuthenticationPreemptive(true); //如果代理需要密码验证,这里设置用户名密码 if(username != null && pwd != null){ httpClient.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, pwd)); } } } return httpClient; }
Example #22
Source File: HttpClientBuilder.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Builds an HTTP client with the given settings. Settings are NOT reset to their default values after a client has * been created. * * @return the created client. */ public HttpClient buildClient() { if (httpsProtocolSocketFactory != null) { Protocol.registerProtocol("https", new Protocol("https", httpsProtocolSocketFactory, 443)); } HttpClientParams clientParams = new HttpClientParams(); clientParams.setAuthenticationPreemptive(isPreemptiveAuthentication()); clientParams.setContentCharset(getContentCharSet()); clientParams.setParameter(HttpClientParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler( connectionRetryAttempts, false)); HttpConnectionManagerParams connMgrParams = new HttpConnectionManagerParams(); connMgrParams.setConnectionTimeout(getConnectionTimeout()); connMgrParams.setDefaultMaxConnectionsPerHost(getMaxConnectionsPerHost()); connMgrParams.setMaxTotalConnections(getMaxTotalConnections()); connMgrParams.setReceiveBufferSize(getReceiveBufferSize()); connMgrParams.setSendBufferSize(getSendBufferSize()); connMgrParams.setTcpNoDelay(isTcpNoDelay()); MultiThreadedHttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager(); connMgr.setParams(connMgrParams); HttpClient httpClient = new HttpClient(clientParams, connMgr); if (proxyHost != null) { HostConfiguration hostConfig = new HostConfiguration(); hostConfig.setProxy(proxyHost, proxyPort); httpClient.setHostConfiguration(hostConfig); if (proxyUsername != null) { AuthScope proxyAuthScope = new AuthScope(proxyHost, proxyPort); UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(proxyUsername, proxyPassword); httpClient.getState().setProxyCredentials(proxyAuthScope, proxyCredentials); } } return httpClient; }
Example #23
Source File: CredentialsUtils.java From httpclientAuthHelper with Apache License 2.0 | 5 votes |
public static void setProxyHost(HttpClient httpClient, UsernamePasswordCredentials proxyCredentials, String proxyHost, int proxyPort) { if (proxyHost != null && !proxyHost.isEmpty()) { httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort); if (proxyCredentials != null) { HttpState state = new HttpState(); state.setProxyCredentials(new AuthScope(proxyHost, proxyPort), proxyCredentials); httpClient.setState(state); } } }
Example #24
Source File: CustomNegotiateScheme.java From httpclientAuthHelper with Apache License 2.0 | 5 votes |
private CustomConfiguration getCustomConfiguration(UsernamePasswordCredentials credentials) { AppConfigurationEntry[] defaultConfiguration = new AppConfigurationEntry[1]; Map options = new HashMap(); options.put("principal", credentials.getUserName()); options.put("client", "true"); options.put("debug", "false"); defaultConfiguration[0] = new AppConfigurationEntry("com.sun.security.auth.module.Krb5LoginModule", AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); return new CustomConfiguration(defaultConfiguration); }
Example #25
Source File: AuthUtilsTest.java From httpclientAuthHelper with Apache License 2.0 | 5 votes |
@Test public void testBasicAuth() throws IOException { String url = "http://browserspy.dk/password-ok.php"; int respose1 = executeRequestReturnStatus(url); assertEquals("Should return a 401 response", 401, respose1); CredentialsUtils.setBasicAuthCredentials(client, new UsernamePasswordCredentials("test", "test")); int respose2 = executeRequestReturnStatus(url); assertEquals("Should return a 200 response", 200, respose2); }
Example #26
Source File: AuthUtilsTest.java From httpclientAuthHelper with Apache License 2.0 | 5 votes |
public void testKERBEROS() throws IOException { AuthUtils.securityLogging(SecurityLogType.KERBEROS, true); String url = "your url"; SSLUtils.trustAllSSLCertificates(); CredentialsUtils .setKerberosCredentials(client, new UsernamePasswordCredentials("user", "password"), "domain", "KDC"); String respose = executeRequestReturnResponseAsString(url); System.out.print(respose); // assertEquals("Should return a 200 response", 200, respose); }
Example #27
Source File: AuthUtilsTest.java From httpclientAuthHelper with Apache License 2.0 | 5 votes |
@Ignore("Not yet implemented") public void testNTLM() throws IOException { String url = "yourNTLMserver"; SSLUtils.trustAllSSLCertificates(); CredentialsUtils.setNTLMCredentials(client, new UsernamePasswordCredentials("xxx", "xxx"), "domain"); int respose = executeRequestReturnStatus(url); assertEquals("Should return a 200 response", 200, respose); }
Example #28
Source File: InstancesManagerServiceImpl.java From geofence with GNU General Public License v2.0 | 5 votes |
private void setAuth(HttpClient client, String url, String username, String pw) throws MalformedURLException { URL u = new URL(url); if(username != null && pw != null) { Credentials defaultcreds = new UsernamePasswordCredentials(username, pw); client.getState().setCredentials(new AuthScope(u.getHost(), u.getPort()), defaultcreds); client.getParams().setAuthenticationPreemptive(true); // GS2 by default always requires authentication } else { if(logger.isDebugEnabled()) { logger.debug("Not setting credentials to access to " + url); } } }
Example #29
Source File: PhoneValidationUtils.java From fenixedu-academic with GNU Lesser General Public License v3.0 | 5 votes |
private void initCIISTSMSGateway() { final String CIIST_SMS_USERNAME = FenixEduAcademicConfiguration.getConfiguration().getCIISTSMSUsername(); final String CIIST_SMS_PASSWORD = FenixEduAcademicConfiguration.getConfiguration().getCIISTSMSPassword(); CIIST_SMS_GATEWAY_URL = FenixEduAcademicConfiguration.getConfiguration().getCIISTSMSGatewayUrl(); if (!StringUtils.isEmpty(CIIST_SMS_USERNAME) && !StringUtils.isEmpty(CIIST_SMS_PASSWORD)) { CIIST_CLIENT = new HttpClient(); Credentials credentials = new UsernamePasswordCredentials(CIIST_SMS_USERNAME, CIIST_SMS_PASSWORD); CIIST_CLIENT.getState().setCredentials(AuthScope.ANY, credentials); } }
Example #30
Source File: BaseWebScriptTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected void setUp() throws Exception { super.setUp(); if (remoteServer != null) { httpClient = new HttpClient(); httpClient.getParams().setBooleanParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true); if (remoteServer.username != null) { httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(remoteServer.username, remoteServer.password)); } } }