Comments (4)
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.
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.
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.
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)
- When trying making a new NotificationHub programmatically, it causes 401 Unauthorized Error. HOT 1
- [BUG] 400 Bad Request when exporting registrations HOT 1
- iOS not receiving pending push after turn on Internet from Azure Notification Hub
- extra dependency needed: org.apache.httpcomponents.client5 httpclient5 HOT 2
- [BUG] : java.lang.RuntimeException: org.apache.hc.core5.http.ParseException: Invalid protocol version; error at offset 0: <[0x2e][0x2f][0x30][0x31][0x32][0x33][0x34][0x35][0x36][0x37][0x38][0x39][0x3a][0x3b][0x3c][0x3d][0x3e]> HOT 4
- [BUG] sendDirectNotification for batch of devices is returning 500 status for FCM notifications HOT 1
- [BUG] Notification Hubs incompatible with Spring? java.lang.RuntimeException: org.apache.hc.core5.http.ParseException: Invalid protocol version; error at offset 0: <[0x2e][0x2f][0x30][0x31][0x32][0x33][0x34][0x35][0x36][0x37][0x38][0x39][0x3a][0x3b][0x3c][0x3d][0x3e]> HOT 1
- [BUG] Caused by: java.lang.ClassNotFoundException: org.apache.hc.client5.http.entity.mime.ContentBody with 1.0.4 of azure notificationhub sdk HOT 1
- Unable to successfully send android notification HOT 1
- [BUG] Connection closed while dispatching a push notification
- [BUG] Get a notification hub by the hub path not working HOT 2
- [QUERY] Missing notifications on ios 13+ with apns HOT 1
- [BUG] Can not send message to Baidu PNS server.
- [FEATURE REQ] Quarkus 3 compatability
- Can't send UWP notification when toast tag with parameters HOT 5
- Getting 401 - Invalid token HOT 1
- Error: 400 Bad Request - notificationhub job payload is invalid HOT 2
- NotificationHub.createOrUpdateInstallation got Internal server error. HOT 3
- Bintray and JCenter Decommissioning HOT 3
- createWindowsNotification doesn't add X-WNS-Type header when the message body has xml prolog HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from azure-notificationhubs-java-backend.