Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.paypal.base;
  
 import java.net.Proxy;
 import java.net.URL;
 
 
Wrapper class used for HttpsURLConnection
 
 public class DefaultHttpConnection extends HttpConnection {

Secure Socket Layer context
 
 	private SSLContext sslContext;
 
 	public DefaultHttpConnection() {
 		try {
 			 = SSLUtil.getSSLContext(null);
 		} catch (SSLConfigurationException e) {
 			throw new RuntimeException(e);
 		}
 	}
 
 	public void setupClientSSL(String certPathString certKey)
 		try {
 			this. = SSLUtil.setupClientSSL(certPathcertKey);
 		} catch (Exception e) {
 			throw new SSLConfigurationException(e.getMessage(), e);
 		}
 	}
 
 			HttpConfiguration clientConfigurationthrows IOException {
 		this. = clientConfiguration;
 		URL url = new URL(this..getEndPointUrl());
 		Proxy proxy = null;
 		String proxyHost = this..getProxyHost();
 		int proxyPort = this..getProxyPort();
 		if ((proxyHost != null) && (proxyPort > 0)) {
 			SocketAddress addr = new InetSocketAddress(proxyHostproxyPort);
 			proxy = new Proxy(..addr);
 		}
 		if (proxy != null) {
 			this. = (HttpURLConnectionurl.openConnection(proxy);
 		} else {
 			this. = (HttpURLConnectionurl
 		}
 		if (this. instanceof HttpsURLConnection) {
 		}
 
 		if (this..getProxyUserName() != null
 				&& this..getProxyPassword() != null) {
 			final String username = this..getProxyUserName();
 			final String password = this..getProxyPassword();
 			Authenticator authenticator = new DefaultPasswordAuthenticator(
 					usernamepassword);
 			Authenticator.setDefault(authenticator);
 		}
 
 		System.setProperty("http.maxConnections",
 		System.setProperty("sun.net.http.errorstream.enableBuffering""true");
 		this..setDoInput(true);
 		this..setDoOutput(true);
 		
 	}

 
Workaround for a bug in HttpURLConnection.setRequestMethod(String) The implementation of Sun/Oracle is throwing a ProtocolException when the method is other than the HTTP/1.1 default methods. So to use PATCH and others, we must apply this workaround. See issue http://java.net/jira/browse/JERSEY-639
    private static void setRequestMethodViaJreBugWorkaround(final HttpURLConnection httpURLConnectionfinal String method) {
        try {
            httpURLConnection.setRequestMethod(method); // Check whether we are running on a buggy JRE
        } catch (final ProtocolException pe) {
            try {
                final Class<?> httpURLConnectionClass = httpURLConnection.getClass();
				AccessController
							public Object run() throws NoSuchFieldException,
								try {
									httpURLConnection.setRequestMethod(method);
									// Check whether we are running on a buggy
									// JRE
catch (final ProtocolException pe) {
									Class<?> connectionClass = httpURLConnection
									Field delegateField = null;
									try {
										delegateField = connectionClass
												.getDeclaredField("delegate");
										delegateField.setAccessible(true);
										HttpURLConnection delegateConnection = (HttpURLConnectiondelegateField
												.get(httpURLConnection);
												delegateConnectionmethod);
catch (NoSuchFieldException e) {
										// Ignore for now, keep going
catch (IllegalArgumentException e) {
										throw new RuntimeException(e);
catch (IllegalAccessException e) {
										throw new RuntimeException(e);
									}
									try {
										Field methodField;
										while (connectionClass != null) {
											try {
												methodField = connectionClass
														.getDeclaredField("method");
catch (NoSuchFieldException e) {
												connectionClass = connectionClass
												continue;
											}
											methodField.setAccessible(true);
											methodField.set(httpURLConnection,
													method);
											break;
										}
catch (final Exception e) {
										throw new RuntimeException(e);
									}
								}
								return null;
							}
						});
            } catch (final PrivilegedActionException e) {
                final Throwable cause = e.getCause();
                if (cause instanceof RuntimeException) {
                    throw (RuntimeExceptioncause;
                } else {
                    throw new RuntimeException(cause);
                }
            }
        }
    }

Private class for password based authentication

Author(s):
kjayakumar
	private static class DefaultPasswordAuthenticator extends Authenticator {

Username
		private String userName;

Password
		private String password;
		public DefaultPasswordAuthenticator(String userNameString password) {
			this. = userName;
			this. = password;
		}
		}
	}
New to GrepCode? Check out our FAQ X