/** Ask for security credentials. */ private CredentialsConfigurator supplyCredentials( Application application, ProxyClient proxyClient) { String displayName = application.getStorage().getCustomProperty(DataSourceDescriptor.PROPERTY_NAME); if (displayName == null) displayName = proxyClient.getUrl().toString(); CredentialsConfigurator jsc = CredentialsConfigurator.supplyCredentials(displayName); if (jsc != null) proxyClient.setCredentials(jsc.getUsername(), jsc.getPassword()); return jsc; }
private void connect( Application application, ProxyClient proxyClient, ApplicationRemovedListener listener, ApplicationAvailabilityListener aListener) { while (true) { try { proxyClient.connect(); application.notifyWhenRemoved(listener); application.addPropertyChangeListener(Stateful.PROPERTY_STATE, aListener); break; } catch (SecurityException e) { LOGGER.log(Level.INFO, "connect", e); // NOI18N if (proxyClient.hasSSLStubCheck()) { Storage storage = application.getStorage(); String noSSLProp = JmxApplicationProvider.PROPERTY_RETRY_WITHOUT_SSL; String noSSL = storage.getCustomProperty(noSSLProp); if (noSSL != null && Boolean.parseBoolean(noSSL)) { // NOI18N proxyClient.setInsecure(); continue; } else { String conn = storage.getCustomProperty(DataSourceDescriptor.PROPERTY_NAME); if (conn == null) conn = storage.getCustomProperty(ApplicationType.PROPERTY_SUGGESTED_NAME); if (conn == null) conn = proxyClient.getUrl().toString(); String msg = NbBundle.getMessage(JmxModelImpl.class, "MSG_Insecure_SSL", conn); // NOI18N String title = NbBundle.getMessage(JmxModelImpl.class, "Title_Insecure_SSL"); // NOI18N String retry = NbBundle.getMessage(JmxModelImpl.class, "Retry_Insecure_SSL"); // NOI18N JLabel l = new JLabel(msg); JCheckBox c = new JCheckBox(); Mnemonics.setLocalizedText(c, retry); c.setSelected(noSSL == null); JPanel p = new JPanel(new BorderLayout(0, 20)); p.add(l, BorderLayout.CENTER); p.add(c, BorderLayout.SOUTH); NotifyDescriptor dd = new NotifyDescriptor.Confirmation(p, title, NotifyDescriptor.YES_NO_OPTION); if (DialogDisplayer.getDefault().notify(dd) == NotifyDescriptor.YES_OPTION) { storage.setCustomProperty(noSSLProp, Boolean.toString(c.isSelected())); proxyClient.setInsecure(); continue; } else { break; } } } if (supplyCredentials(application, proxyClient) == null) { break; } } } }
/** 初始化httpclient */ static { int connectionTimeout = 30000; int soTimeout = 30000; try { connectionTimeout = Integer.parseInt(System.getProperty("sun.net.client.defaultConnectTimeout", "30000")); } catch (Exception e) { } try { soTimeout = Integer.parseInt(System.getProperty("sun.net.client.defaultReadTimeout", "30000")); } catch (Exception e) { } MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.getParams().setDefaultMaxConnectionsPerHost(10); connectionManager.getParams().setMaxTotalConnections(300); connectionManager.getParams().setConnectionTimeout(connectionTimeout); connectionManager.getParams().setSoTimeout(soTimeout); client.setHttpConnectionManager(connectionManager); // 忽略cookie 避免 Cookie rejected 警告 HttpClientParams clientParams = new HttpClientParams(); clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES); client.setParams(clientParams); // 支持https Protocol myhttps = new Protocol("https", new SSLSocketFactory(), 443); Protocol.registerProtocol("https", myhttps); // 设置代理 if (ProxyClient.getProxy() != null) { client.getHostConfiguration().setProxy(ProxyClient.getHost(), ProxyClient.getPort()); client.getParams().setAuthenticationPreemptive(true); if (ProxyClient.getUsername() != null && !ProxyClient.getUsername().trim().equals("")) { client .getState() .setProxyCredentials( AuthScope.ANY, new UsernamePasswordCredentials( ProxyClient.getUsername().trim(), ProxyClient.getPassword().trim())); } } }
public void handleRequest(final HttpServerExchange exchange) throws Exception { final ProxyClient.ProxyTarget target = proxyClient.findTarget(exchange); if (target == null) { log.debugf("No proxy target for request to %s", exchange.getRequestURL()); next.handleRequest(exchange); return; } final long timeout = maxRequestTime > 0 ? System.currentTimeMillis() + maxRequestTime : 0; final ProxyClientHandler clientHandler = new ProxyClientHandler(exchange, target, timeout, maxConnectionRetries); if (timeout > 0) { final XnioExecutor.Key key = exchange .getIoThread() .executeAfter( new Runnable() { @Override public void run() { clientHandler.cancel(exchange); } }, maxRequestTime, TimeUnit.MILLISECONDS); exchange.putAttachment(TIMEOUT_KEY, key); exchange.addExchangeCompleteListener( new ExchangeCompletionListener() { @Override public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) { key.remove(); nextListener.proceed(); } }); } exchange.dispatch( exchange.isInIoThread() ? SameThreadExecutor.INSTANCE : exchange.getIoThread(), clientHandler); }
/** * Returns the {@link JMXServiceURL} associated to this (@code JmxModel}. * * @return the {@link JMXServiceURL} associated to this (@code JmxModel}. */ public JMXServiceURL getJMXServiceURL() { if (client != null) { return client.getUrl(); } return null; }
/** * Returns the {@link MBeanServerConnection} for the connection to an application. The returned * {@code MBeanServerConnection} object becomes invalid when the connection state is changed to * the {@link ConnectionState#DISCONNECTED DISCONNECTED} state. * * @return the {@code MBeanServerConnection} for the connection to an application. It returns * {@code null} if the JMX connection couldn't be established. */ public MBeanServerConnection getMBeanServerConnection() { if (client != null) { return client.getMBeanServerConnection(); } return null; }
/** * Returns the current connection state. * * @return the current connection state. */ public ConnectionState getConnectionState() { if (client != null) { return client.getConnectionState(); } return ConnectionState.DISCONNECTED; }