List of usage examples for org.apache.http.auth AuthScope getPort
public int getPort()
From source file:org.eclipse.ecf.internal.provider.filetransfer.httpclient4.HttpClientProxyCredentialProvider.java
private boolean matchAuthScopeAndProxy(AuthScope authscope, Proxy proxy) { ProxyAddress proxyAddress = proxy.getAddress(); return (authscope.getHost().equals(proxyAddress.getHostName()) && (authscope.getPort() == proxyAddress.getPort())); }
From source file:com.mooregreatsoftware.gitprocess.lib.CredentialHelperCredentialsProvider.java
@Nullable @Override/*from w ww. j a v a2 s .c om*/ @SuppressWarnings("override.return.invalid") public Credentials getCredentials(AuthScope authscope) { if (authscope == null) return null; @SuppressWarnings("argument.type.incompatible") URI uri = e(() -> new URI(authscope.getScheme(), null, authscope.getHost(), authscope.getPort(), null, null, null)); final String credentialHelper = remoteConfig.credentialHelper(uri); if (credentialHelper == null) return null; final String progName; if (credentialHelper.startsWith("/")) { progName = credentialHelper; } else { progName = "git-credential-" + credentialHelper; } return null; }
From source file:org.apache.taverna.activities.rest.RESTActivityCredentialsProvider.java
@Override public Credentials getCredentials(AuthScope authscope) { logger.info("Looking for credentials for: Host - " + authscope.getHost() + ";" + "Port - " + authscope.getPort() + ";" + "Realm - " + authscope.getRealm() + ";" + "Authentication scheme - " + authscope.getScheme());//from w w w. j a v a2s . co m // Ask the superclass first Credentials creds = super.getCredentials(authscope); if (creds != null) { /* * We have used setCredentials() on this class (for proxy host, * port, username,password) just before we invoked the http request, * which will then pick the proxy credentials up from here. */ return creds; } // Otherwise, ask Credential Manager if is can provide the credential String AUTHENTICATION_REQUEST_MSG = "This REST service requires authentication in " + authscope.getRealm(); try { UsernamePassword credentials = null; /* * if port is 80 - use HTTP, don't append port if port is 443 - use * HTTPS, don't append port any other port - append port + do 2 * tests: * * --- test HTTPS first has...() * --- if not there, do get...() for HTTP (which will save the thing) * * (save both these entries for HTTP + HTTPS if not there) */ // build the service URI back to front StringBuilder serviceURI = new StringBuilder(); serviceURI.insert(0, "/#" + URLEncoder.encode(authscope.getRealm(), "UTF-16")); if (authscope.getPort() != DEFAULT_HTTP_PORT && authscope.getPort() != DEFAULT_HTTPS_PORT) { // non-default port - add port name to the URI serviceURI.insert(0, ":" + authscope.getPort()); } serviceURI.insert(0, authscope.getHost()); serviceURI.insert(0, "://"); // now the URI is complete, apart from the protocol name if (authscope.getPort() == DEFAULT_HTTP_PORT || authscope.getPort() == DEFAULT_HTTPS_PORT) { // definitely HTTP or HTTPS serviceURI.insert(0, (authscope.getPort() == DEFAULT_HTTP_PORT ? HTTP_PROTOCOL : HTTPS_PROTOCOL)); // request credentials from CrendentialManager credentials = credentialManager.getUsernameAndPasswordForService(URI.create(serviceURI.toString()), true, AUTHENTICATION_REQUEST_MSG); } else { /* * non-default port - will need to try both HTTP and HTTPS; just * check (no pop-up will be shown) if credentials are there - * one protocol that matched will be used; if */ if (credentialManager .hasUsernamePasswordForService(URI.create(HTTPS_PROTOCOL + serviceURI.toString()))) { credentials = credentialManager.getUsernameAndPasswordForService( URI.create(HTTPS_PROTOCOL + serviceURI.toString()), true, AUTHENTICATION_REQUEST_MSG); } else if (credentialManager .hasUsernamePasswordForService(URI.create(HTTP_PROTOCOL + serviceURI.toString()))) { credentials = credentialManager.getUsernameAndPasswordForService( URI.create(HTTP_PROTOCOL + serviceURI.toString()), true, AUTHENTICATION_REQUEST_MSG); } else { /* * Neither of the two options succeeded, request details with a * popup for HTTP... */ credentials = credentialManager.getUsernameAndPasswordForService( URI.create(HTTP_PROTOCOL + serviceURI.toString()), true, AUTHENTICATION_REQUEST_MSG); /* * ...then save a second entry with HTTPS protocol (if the * user has chosen to save the credentials) */ if (credentials != null && credentials.isShouldSave()) { credentialManager.addUsernameAndPasswordForService(credentials, URI.create(HTTPS_PROTOCOL + serviceURI.toString())); } } } if (credentials != null) { logger.info("Credentials obtained successfully"); return new RESTActivityCredentials(credentials.getUsername(), credentials.getPasswordAsString()); } } catch (Exception e) { logger.error("Unexpected error while trying to obtain user's credential from CredentialManager", e); } // error or nothing was found logger.info("Credentials not found - the user must have refused to enter them."); return null; }
From source file:ucar.unidata.util.AccountManager.java
/** * _more_/*from w w w.j a va2 s.com*/ * * @param scope _more_ * * @return _more_ */ public Credentials getCredentialsnew(AuthScope scope) { //String host, int port, boolean proxy String host = scope.getHost(); int port = scope.getPort(); String realm = scope.getRealm(); // public Credentials getCredentials(AuthScheme scheme, String host, // int port, boolean proxy) // throws InvalidCredentialsException { if (scope == null) { throw new IllegalArgumentException("Null scope"); } /* //TODO: check if the scheme in the auth is rfc26217 if ( !(scheme instanceof RFC2617Scheme)) { throw new InvalidCredentialsException( "Unsupported authentication scheme: " + scheme.getSchemeName()); } */ String key = host + realm; // System.err.println ("got auth call " + key); UserInfo userInfo = getUserNamePassword(key, "The server " + host + ":" + port + " requires a username/password"); if (userInfo == null) { return null; } return new UsernamePasswordCredentials(userInfo.getUserId(), userInfo.getPassword()); }
From source file:org.openiot.gsn.http.rest.PushRemoteWrapper.java
public DataField[] registerAndGetStructure() throws IOException, ClassNotFoundException { // Create the POST request HttpPost httpPost = new HttpPost(initParams.getRemoteContactPointEncoded(lastReceivedTimestamp)); // Add the POST parameters httpPost.setEntity(new UrlEncodedFormEntity(postParameters, HTTP.UTF_8)); ////w w w . j a v a2 s . c o m httpPost.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE); // Create local execution context HttpContext localContext = new BasicHttpContext(); // NotificationRegistry.getInstance().addNotification(uid, this); int tries = 0; AuthState authState = null; // while (tries < 2) { tries++; HttpResponse response = null; try { // Execute the POST request response = httpclient.execute(httpPost, localContext); // int sc = response.getStatusLine().getStatusCode(); // if (sc == HttpStatus.SC_OK) { logger.debug(new StringBuilder().append("Wants to consume the structure packet from ") .append(initParams.getRemoteContactPoint())); structure = (DataField[]) XSTREAM.fromXML(response.getEntity().getContent()); logger.debug("Connection established for: " + initParams.getRemoteContactPoint()); break; } else { if (sc == HttpStatus.SC_UNAUTHORIZED) authState = (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE); // Target host authentication required else if (sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) authState = (AuthState) localContext.getAttribute(ClientContext.PROXY_AUTH_STATE); // Proxy authentication required else { logger.error(new StringBuilder().append("Unexpected POST status code returned: ").append(sc) .append("\nreason: ").append(response.getStatusLine().getReasonPhrase())); } if (authState != null) { if (initParams.getUsername() == null || (tries > 1 && initParams.getUsername() != null)) { logger.error("A valid username/password required to connect to the remote host: " + initParams.getRemoteContactPoint()); } else { AuthScope authScope = authState.getAuthScope(); logger.warn(new StringBuilder().append("Setting Credentials for host: ") .append(authScope.getHost()).append(":").append(authScope.getPort())); Credentials creds = new UsernamePasswordCredentials(initParams.getUsername(), initParams.getPassword()); httpclient.getCredentialsProvider().setCredentials(authScope, creds); } } } } catch (RuntimeException ex) { // In case of an unexpected exception you may want to abort // the HTTP request in order to shut down the underlying // connection and release it back to the connection manager. logger.warn("Aborting the HTTP POST request."); httpPost.abort(); throw ex; } finally { if (response != null && response.getEntity() != null) { response.getEntity().consumeContent(); } } } if (structure == null) throw new RuntimeException("Cannot connect to the remote host."); return structure; }
From source file:org.openiot.gsn.http.rest.RestRemoteWrapper.java
public DataField[] connectToRemote() throws IOException, ClassNotFoundException { // Create the GET request HttpGet httpget = new HttpGet(initParams.getRemoteContactPointEncoded(lastReceivedTimestamp)); // Create local execution context HttpContext localContext = new BasicHttpContext(); ///*from w ww . j av a 2s. c om*/ structure = null; int tries = 0; AuthState authState = null; // if (inputStream != null) { try { if (response != null && response.getEntity() != null) { response.getEntity().consumeContent(); } inputStream.close(); inputStream = null; } catch (Exception e) { logger.debug(e.getMessage(), e); } } // while (tries < 2) { tries++; try { // Execute the GET request response = httpclient.execute(httpget, localContext); // int sc = response.getStatusLine().getStatusCode(); // if (sc == HttpStatus.SC_OK) { logger.debug(new StringBuilder().append("Wants to consume the structure packet from ") .append(initParams.getRemoteContactPoint())); inputStream = XSTREAM.createObjectInputStream(response.getEntity().getContent()); structure = (DataField[]) inputStream.readObject(); logger.warn("Connection established for: " + initParams.getRemoteContactPoint()); break; } else { if (sc == HttpStatus.SC_UNAUTHORIZED) authState = (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE); // Target host authentication required else if (sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) authState = (AuthState) localContext.getAttribute(ClientContext.PROXY_AUTH_STATE); // Proxy authentication required else { logger.error(new StringBuilder().append("Unexpected GET status code returned: ").append(sc) .append("\nreason: ").append(response.getStatusLine().getReasonPhrase())); } if (authState != null) { if (initParams.getUsername() == null || (tries > 1 && initParams.getUsername() != null)) { logger.error("A valid username/password required to connect to the remote host: " + initParams.getRemoteContactPoint()); } else { AuthScope authScope = authState.getAuthScope(); logger.warn(new StringBuilder().append("Setting Credentials for host: ") .append(authScope.getHost()).append(":").append(authScope.getPort())); Credentials creds = new UsernamePasswordCredentials(initParams.getUsername(), initParams.getPassword()); httpclient.getCredentialsProvider().setCredentials(authScope, creds); } } } } catch (RuntimeException ex) { // In case of an unexpected exception you may want to abort // the HTTP request in order to shut down the underlying // connection and release it back to the connection manager. logger.warn("Aborting the HTTP GET request."); httpget.abort(); throw ex; } finally { if (structure == null) { if (response != null && response.getEntity() != null) { response.getEntity().consumeContent(); } } } } if (structure == null) throw new RuntimeException("Cannot connect to the remote host: " + initParams.getRemoteContactPoint()); return structure; }
From source file:gsn.http.rest.RestRemoteWrapper.java
public DataField[] connectToRemote() throws IOException, ClassNotFoundException { // Create the GET request HttpGet httpget = new HttpGet(initParams.getRemoteContactPointEncoded(lastReceivedTimestamp)); // Create local execution context HttpContext localContext = new BasicHttpContext(); ////from w w w .j a va2 s .c om structure = null; int tries = 0; AuthState authState = null; // if (inputStream != null) { try { if (response != null && response.getEntity() != null) { response.getEntity().consumeContent(); } inputStream.close(); inputStream = null; } catch (Exception e) { logger.debug(e.getMessage(), e); } } // while (tries < 2) { tries++; try { // Execute the GET request response = httpclient.execute(httpget, localContext); // int sc = response.getStatusLine().getStatusCode(); // if (sc == HttpStatus.SC_OK) { logger.debug(new StringBuilder().append("Wants to consume the structure packet from ") .append(initParams.getRemoteContactPoint()).toString()); inputStream = XSTREAM.createObjectInputStream(response.getEntity().getContent()); structure = (DataField[]) inputStream.readObject(); logger.warn("Connection established for: " + initParams.getRemoteContactPoint()); break; } else { if (sc == HttpStatus.SC_UNAUTHORIZED) authState = (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE); // Target host authentication required else if (sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) authState = (AuthState) localContext.getAttribute(ClientContext.PROXY_AUTH_STATE); // Proxy authentication required else { logger.error(new StringBuilder().append("Unexpected GET status code returned: ").append(sc) .append("\nreason: ").append(response.getStatusLine().getReasonPhrase()) .toString()); } if (authState != null) { if (initParams.getUsername() == null || (tries > 1 && initParams.getUsername() != null)) { logger.error("A valid username/password required to connect to the remote host: " + initParams.getRemoteContactPoint()); } else { AuthScope authScope = authState.getAuthScope(); logger.warn(new StringBuilder().append("Setting Credentials for host: ") .append(authScope.getHost()).append(":").append(authScope.getPort()) .toString()); Credentials creds = new UsernamePasswordCredentials(initParams.getUsername(), initParams.getPassword()); httpclient.getCredentialsProvider().setCredentials(authScope, creds); } } } } catch (RuntimeException ex) { // In case of an unexpected exception you may want to abort // the HTTP request in order to shut down the underlying // connection and release it back to the connection manager. logger.warn("Aborting the HTTP GET request."); httpget.abort(); throw ex; } finally { if (structure == null) { if (response != null && response.getEntity() != null) { response.getEntity().consumeContent(); } } } } if (structure == null) throw new RuntimeException("Cannot connect to the remote host: " + initParams.getRemoteContactPoint()); return structure; }
From source file:ucar.unidata.util.AccountManager.java
/** * Do the authentication/*www. j a v a2 s . c o m*/ * @param scope authscope * * @return Null if the user presses cancel. Else return the credentials * */ public Credentials getCredentials(AuthScope scope) { //TODO: What should this do? if (scope == null) { throw new IllegalArgumentException("Authentication scope may not be null"); } if (currentCredentials == null) { String host = scope.getHost(); int port = scope.getPort(); String key = host + ":" + port + ":" + scope.getRealm(); // System.err.println ("got auth call " + key); UserInfo userInfo = getUserNamePassword(key, "The server " + host + ":" + port + " requires a username/password"); if (userInfo == null) { return null; } currentCredentials = new UsernamePasswordCredentials(userInfo.getUserId(), userInfo.getPassword()); } return currentCredentials; }