List of usage examples for java.net Authenticator setDefault
public static synchronized void setDefault(Authenticator a)
From source file:org.openmrs.module.yank.api.impl.RestClient.java
public static String getResource(String serverName, String username, String password, String resourceName, String uuid) {//from w w w .j a v a 2 s . co m Authenticator.setDefault(new BasicAuth(username, password)); return getRootResource(serverName).path(resourceName).path(uuid).queryParam("v", "full") .accept(MediaType.APPLICATION_JSON_TYPE).get(String.class); }
From source file:org.owasp.dependencycheck.utils.URLConnectionFactory.java
/** * Utility method to create an HttpURLConnection. If the application is configured to use a proxy this method will retrieve * the proxy settings and use them when setting up the connection. * * @param url the url to connect to//from w ww. j a v a2 s. c o m * @return an HttpURLConnection * @throws URLConnectionFailureException thrown if there is an exception */ @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE", justification = "Just being extra safe") public static HttpURLConnection createHttpURLConnection(URL url) throws URLConnectionFailureException { HttpURLConnection conn = null; final String proxyUrl = Settings.getString(Settings.KEYS.PROXY_SERVER); try { if (proxyUrl != null && !matchNonProxy(url)) { final int proxyPort = Settings.getInt(Settings.KEYS.PROXY_PORT); final SocketAddress address = new InetSocketAddress(proxyUrl, proxyPort); final String username = Settings.getString(Settings.KEYS.PROXY_USERNAME); final String password = Settings.getString(Settings.KEYS.PROXY_PASSWORD); if (username != null && password != null) { final Authenticator auth = new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { if (getRequestorType().equals(Authenticator.RequestorType.PROXY)) { return new PasswordAuthentication(username, password.toCharArray()); } return super.getPasswordAuthentication(); } }; Authenticator.setDefault(auth); } final Proxy proxy = new Proxy(Proxy.Type.HTTP, address); conn = (HttpURLConnection) url.openConnection(proxy); } else { conn = (HttpURLConnection) url.openConnection(); } final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 10000); conn.setConnectTimeout(timeout); conn.setInstanceFollowRedirects(true); } catch (IOException ex) { if (conn != null) { try { conn.disconnect(); } finally { conn = null; } } throw new URLConnectionFailureException("Error getting connection.", ex); } return conn; }
From source file:com.pras.conn._HttpConHandler.java
private Proxy getProxy() { /**// w w w . j a v a2s. co m * Connect through a Proxy */ Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(PROXY_USER, PROXY_PASS.toCharArray()); } }); return new Proxy(Type.HTTP, new InetSocketAddress(PROXY_URL, PROXY_PORT)); // return Proxy.NO_PROXY; }
From source file:org.talend.commons.utils.network.NetworkUtil.java
public static void loadAuthenticator() { // get parameter from System.properties. if (Boolean.getBoolean("http.proxySet")) {//$NON-NLS-1$ // authentification for the url by using username and password Authenticator.setDefault(new Authenticator() { @Override// w ww.j a v a2 s .c om protected PasswordAuthentication getPasswordAuthentication() { String httpProxyUser = System.getProperty("http.proxyUser"); //$NON-NLS-1$ String httpProxyPassword = System.getProperty("http.proxyPassword"); //$NON-NLS-1$ String httpsProxyUser = System.getProperty("https.proxyUser"); //$NON-NLS-1$ String httpsProxyPassword = System.getProperty("https.proxyPassword"); //$NON-NLS-1$ String proxyUser = null; char[] proxyPassword = new char[0]; if (StringUtils.isNotEmpty(httpProxyUser)) { proxyUser = httpProxyUser; if (StringUtils.isNotEmpty(httpProxyPassword)) { proxyPassword = httpProxyPassword.toCharArray(); } } else if (StringUtils.isNotEmpty(httpsProxyUser)) { proxyUser = httpsProxyUser; if (StringUtils.isNotEmpty(httpsProxyPassword)) { proxyPassword = httpsProxyPassword.toCharArray(); } } return new PasswordAuthentication(proxyUser, proxyPassword); } }); } else { Authenticator.setDefault(null); } }
From source file:org.openecomp.sdnc.dmaapclient.SdncOdlConnection.java
private SdncOdlConnection(String url, String user, String password) { this.url = url; this.user = user; this.password = password; try {/*from w ww . ja v a 2 s. c o m*/ URL sdncUrl = new URL(url); Authenticator.setDefault(new SdncAuthenticator(user, password)); this.httpConn = (HttpURLConnection) sdncUrl.openConnection(); } catch (Exception e) { LOG.error("Unable to create http connection", e); } }
From source file:org.rhq.modules.plugins.jbossas7.ASUploadConnection.java
public ASUploadConnection(String dcHost, int port, String user, String password) { this.host = dcHost; this.port = port; if (user != null) { passwordAuthenticator = new AS7Authenticator(user, password); Authenticator.setDefault(passwordAuthenticator); }//from w w w . j a va 2 s . co m }
From source file:net.reichholf.dreamdroid.helpers.SimpleHttpClient.java
private void clearCredentials() { Authenticator.setDefault(null); }
From source file:com.cloudera.recordservice.tests.ClusterConfiguration.java
/** * This method sends a get request to CM using the given URL. The return value * of that request is returned in the method in string form. *//* w ww . ja va 2 s . co m*/ public String clusterGetRequest(URL url) throws IOException { Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username_, password_.toCharArray()); } }); StringBuilder ret = new StringBuilder(); BufferedReader br = null; try { URLConnection cc = url.openConnection(); br = new BufferedReader(new InputStreamReader(cc.getInputStream())); String line; while ((line = br.readLine()) != null) { ret.append(line); } } finally { if (br != null) { br.close(); } } return ret.toString(); }
From source file:de.rallye.test.GroupsTest.java
@Test public void testLogin() throws IOException { Authenticator.setDefault(new Authenticator() { @Override//w w w . j ava 2 s.com protected PasswordAuthentication getPasswordAuthentication() { String realm = getRequestingPrompt(); assertEquals("Should be right Realm", "RallyeNewUser", realm); return new PasswordAuthentication(String.valueOf(1), "test".toCharArray()); } }); URL url = new URL("http://127.0.0.1:10111/groups/1"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("PUT"); conn.setDoOutput(true); conn.addRequestProperty("Content-Type", "application/json"); // conn.setFixedLengthStreamingMode(post.length); conn.getOutputStream().write(MockDataAdapter.validLogin.getBytes()); int code = conn.getResponseCode(); Authenticator.setDefault(null); try { assertEquals("Code should be 200", 200, code); } catch (AssertionError e) { System.err.println("This is the content:"); List<String> contents = IOUtils.readLines((InputStream) conn.getContent()); for (String line : contents) System.err.println(line); throw e; } }
From source file:web.kz.rhq.modules.plugins.jbossas7.ASUploadConnection.java
public ASUploadConnection(String dcHost, int port, String user, String password) { if (dcHost == null) { throw new IllegalArgumentException("Management host cannot be null."); }/* ww w.ja va 2s . c o m*/ if (port <= 0 || port > 65535) { throw new IllegalArgumentException("Invalid port: " + port); } this.host = dcHost; this.port = port; if (user != null) { passwordAuthenticator = new AS7Authenticator(user, password); Authenticator.setDefault(passwordAuthenticator); } timeout = DEFAULT_TIMEOUT; }