Coder Social home page Coder Social logo

Comments (4)

nvguerreiro avatar nvguerreiro commented on September 26, 2024 1

Hi, I managed to use my corporate proxy with NTLM integrated authentication (no username and password specified) on my Windows 8 workstation, without changing this library's source code.

Here's my code:

import com.windowsazure.messaging.*;

import java.util.concurrent.Future;

import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.HttpHost;
import org.apache.http.impl.auth.BasicSchemeFactory;
import org.apache.http.impl.auth.DigestSchemeFactory;
import org.apache.http.impl.auth.win.WindowsCredentialsProvider;
import org.apache.http.impl.auth.win.WindowsNTLMSchemeFactory;
import org.apache.http.impl.auth.win.WindowsNegotiateSchemeFactory;
import org.apache.http.impl.client.SystemDefaultCredentialsProvider;
import org.apache.http.impl.client.WinHttpClients;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;

public class Test9 {
    private static HttpAsyncClientBuilder createAsyncBuilderWithProxy(String proxyHost, int proxyPort) {
        if (WinHttpClients.isWinAuthAvailable()) {
            final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
                    .register(AuthSchemes.BASIC, new BasicSchemeFactory())
                    .register(AuthSchemes.DIGEST, new DigestSchemeFactory())
                    .register(AuthSchemes.NTLM, new WindowsNTLMSchemeFactory(null))
                    .register(AuthSchemes.SPNEGO, new WindowsNegotiateSchemeFactory(null))
                    .build();
            final CredentialsProvider credsProvider = new WindowsCredentialsProvider(new SystemDefaultCredentialsProvider());
            return HttpAsyncClientBuilder.create()
                    .setDefaultCredentialsProvider(credsProvider)
                    .setDefaultAuthSchemeRegistry(authSchemeRegistry)
					.setProxy(new HttpHost(proxyHost, proxyPort));
        } else {
            return HttpAsyncClientBuilder.create().setProxy(new HttpHost(proxyHost, proxyPort));
        }
    }

	public static void main(String[] args) throws Exception {
		if(args.length < 4) {
			System.err.println("syntax: java Test9 <hub connection string> <hub name> <push notification address> <push message>");
		} else {
			String hubConnectionString = args[0];
			String hubName = args[1];
			String pushNotificationAddress = args[2];
			String pushMessage = args[3];

			CloseableHttpAsyncClient httpClient = createAsyncBuilderWithProxy("proxy.corporate.com", 8080).build();
			httpClient.start();
			HttpClientManager.setHttpAsyncClient(httpClient);
			
			NotificationHub hub = new NotificationHub(hubConnectionString, hubName);

			Notification notification = Notification.createGcmNotifiation(pushMessage);
            
			hub.sendDirectNotification(notification, pushNotificationAddress);
			
			System.out.println("Notification sent!");
			
			httpClient.close();
		}
	}
}

from azure-notificationhubs-java-backend.

jmgrozas avatar jmgrozas commented on September 26, 2024

I have the same problem ... My work around is using environment var to select the strategy of httpclient creation ....

To select strategy:

System.setProperty(HttpClientManager.SYSTEM_HTTP_CLIENT_CREATE,HttpClientManager.HttpClientType.SYSTEM)

/** Name of system property to activate httpAsyncClient create with system properties */
public static final String SYSTEM_HTTP_CLIENT_CREATE = "http_client_type";

private static CloseableHttpAsyncClient httpAsyncClient;

public static CloseableHttpAsyncClient getHttpAsyncClient() {
	if(httpAsyncClient == null) {
		synchronized(HttpClientManager.class) {
			if(httpAsyncClient == null) {
				/* Create http client by type */
				HttpClientType clientType = HttpClientType.valueOf(System.getProperty(SYSTEM_HTTP_CLIENT_CREATE));
				CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
				if (HttpClientType.MINIMAL.equals(clientType)){
					client = HttpAsyncClients.createMinimal();
				} else if (HttpClientType.SYSTEM.equals(clientType)){
					client = HttpAsyncClients.createSystem();
				}else {
					client = HttpAsyncClients.createDefault();
				}
				client.start();
				httpAsyncClient = client;	    	   
			}
		}
	}
	  
	return httpAsyncClient;
}

public static void setHttpAsyncClient(CloseableHttpAsyncClient httpAsyncClient) {
	synchronized (HttpClientManager.class) {
		if (HttpClientManager.httpAsyncClient == null) {
			HttpClientManager.httpAsyncClient = httpAsyncClient;
		} else {
			throw new RuntimeException("HttpAsyncClient was already set before or default one is being used.");
		}
	}
}

/**
 * Enum represent httpClients
 * 
 * @author manuel
 *
 */
public static enum HttpClientType {
	DEFAULT, MINIMAL, SYSTEM;
}

``

I hope that Azure will change strategy of implementation....

from azure-notificationhubs-java-backend.

paulrho avatar paulrho commented on September 26, 2024

This would still be worth building into the library, an option to use system proxy defaults or some other (standard) way to specify a proxy. Currently trying to "add" a proxy to our existing app (without changing code) - and realise now it is not possible.

from azure-notificationhubs-java-backend.

mpodwysocki avatar mpodwysocki commented on September 26, 2024

This is by design to allow you to set your own proxy via setting the HttpClientManager.setHttpAsyncClient(httpClient);

from azure-notificationhubs-java-backend.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.