Tuesday, May 31, 2016

Eclipse connectivity issue with Corporate Proxy

Overview 

Some applications we use need to access the web through the Corporate proxy and pull in content. In many cases what happens is the request fails as the application is unable to negotiate the connection using the NTLM protocol. Examples of this are the Marketplace in Eclipse Luna or installing packages with npm.
Cntlm is a tool that will work around this problem by proxying these requests locally and talking to the Corporate proxy to establish a connection. From the project website: "Cntlm is an NTLM / NTLMv2 authenticating HTTP/1.1 proxy. It caches auth'd connections for reuse, offers TCP/IP tunneling (port forwarding) thru parent proxy and much much more."

Setup Instructions

  1. Download and install cntlm from http://sourceforge.net/projects/cntlm/
  2. Edit C:\Program Files (x86)\Cntlm\cntlm.ini and make the following changes (make sure you are running your text editor as administrator​):​
    a. Add your Corporate username
    Username <corporate proxy server login>

    b. Comment out domain (we don’t need it)
    # Domain    corp-uk

    c. Change Proxy and Add additional NoProxy
    Proxy           <HTTPPROXYNAME>:<PORT>
    NoProxy         localhost, 127.0.0.*

    d. Get password hash from the command line
    > cd "C:\Program Files (x86)\Cntlm\"
    > cntlm -H -c cntlm.ini

    e. Paste the resulting 3 lines, which contain hashes of your password, back into your config.ini:
    PassLM          <HASH>
    PassNT          <HASH>
    PassNTLMv2      <HASH>    # Only for user <USERNAME>, domain '<HTTP PROXY NAME>'

    f. Comment out clear text password parameter
    # Password <fake password>

    g. Start CNTLM
    See instructions in README.txt (in the cntlm installation directory)

    h. Test CNTLM from the comand line
    > cntlm -M http://www.google.com

    After entering your Corporate password when prompted, you should see a response similar to the following (the HTTP 200 response indicating a success):

    Config profile  1/4... OK (HTTP code: 200)
    ----------------------------[ Profile  0 ]------
    Auth            NTLMv2
    PassNTLMv2      <your password hash>
    ------------------------------------------------
  3. Reboot your computer.  CNTLM will be installed and will run as a windows service and you won't have sto start it again manually unless of course you stop it manually.
After configuring and starting cntlm, applications should be able to access the web with no further action needed.

Changing CNTLM Password After CORPORATE Password Reset

  1. Get password hash from the command line
    > cd "C:\Program Files (x86)\Cntlm\"
    > cntlm -H -c cntlm.ini
  2. Copy the resulting 3 lines, which contain hashes of your passwordPassLM <HASH>
    PassNT <HASH>
    PassNTLMv2 <HASH> # Only for user <USERNAME>, domain '<HTTPPROXYHOST>'
  3. Open the cntlm.ini (configuration setting file) and replace (paste) the information from step two into your cntlm.ini file.
  4. Save, stop then start cntlm.  Information on how to do that can be found in the "Starting/Stopping CNTLM Manually" section below.

Starting/Stopping CNTLM Manually

  1. To start stop CNTLM manually open a command prompt as an Administrator
  2. To stop CNTLM type
    1. net stop cntlm
  3. To start CNTLM type:
    1. net start cntlm

Further info

Cntlm technical manual

How to Configure Eclipse Proxy Settings

Note: If installing from an external site hangs at “calculating requirements and dependencies” after following the instructions here, try unchecking the "Contact all update sites during install" box.
Beginning with Eclipse version 4.4 (Luna), special steps are needed in order for Eclipse to contact external sites (e.g. to install updates or plugins).
If when attempting to install plugins or open the Eclipse Marketplace, you receive an error stating "Proxy Authentication Required", this article is for you.

Background

The cause of the proxy error is a new set of HTTP client libraries that Eclipse uses beginning with v4.4. Removing those libraries, along with configuring Eclipse to talk to the proxy, will cause Eclipse to fall back on the JRE HTTP client libs. This should resolve the issue.

Part A: Configure Eclipse's Proxy Settings

First, you must configure Eclipse to play nice with the proxy by doing the following:
  1. In Eclipse go to Window>Preferences>General>Network Connections
  2. Choose Manual for the Active Provider
  3. For both HTTP and HTTPS, enter the following settings:
    • Host: <PROXY HOST>
    • Port: <PROXY PORT>
    • User: <PROXY USERID>
    • Password <PROXY USER PASSWORD>
    Note you will need to update this each time you change your password.
  4. Click OK to save your settings.

Part B: Remove Problematic Libraries

With Eclipse not running, go to Eclipse's plugins directory (e.g. C:\Program Files\Eclipse\plugins) and delete or rename the following three .jar files:

org.eclipse.ecf.provider.filetransfer.httpclient4.ssl_1.0.0.v20140528-1625.jar
org.eclipse.ecf.provider.filetransfer.httpclient4_1.0.500.v20140528-1625.jar
org.eclipse.ecf.provider.filetransfer.ssl_1.0.0.v20140528-1625.jar

After performing parts A and B above, Eclipse should be able to contact external update sites without any issues.    Notes
This information was compiled from this StackOverflow question and this bug report comment on the Eclipse bug tracker.

In addition to above 2 parts, see if https://wiki.eclipse.org/Disabling_Apache_Httpclient helps or
configure the following in eclipse.ini file.
-Djava.net.preferIPv4Stack=true
-Declipse.p2.unsignedPolicy=allow
-Dcom.sun.management.jmxremote
-Dorg.eclipse.ecf.provider.filetransfer.excludeContributors=org.eclipse.ecf.provider.filetransfer.httpclient4


Thursday, May 12, 2016



Best way to create a Singleton Pattern.


The Singleton Design Pattern is one of the Creational (creation of instance in best possible way) patterns.

The usage of enum, a new feature from Java5, allows safe way to implement Singleton design pattern as Java ensures that any enum value is instantiated only once in memory (JVM).

This singleton instance is accessible from anywhere since Java Enum values are globally accessible.

The drawback is that the enum type is somewhat inflexible as it does not allow lazy
initialization.

package org.comp.core;
public enum MyEnumSingleton {
INSTANCE;

       private MyEnumSingleton(){}


  public static void method1(){

//Do something 
}

  public static List buildStateCodeList(){
              final List<String>  stateCodeList = new ArrayList<String>();
             stateCodeList.add("AR");
             stateCodeList.add("AZ");
             ....
             ....
             return stateCodeList ;
}
}

From Caller:
List<String> stateCode_USA_List = MyEnumSingleton.INSTANCE.buildStateCodeList();


The following is more appropriate in  Distributed environment.   

package org.comp.core;
import java.io.*;
public class MySerializedSingleton implements Serializable{
private static final long serialVersionUID = -12345678923456789123L;
// Private/default constructor
private MySerializedSingleton(){}
//Inner static class to create instance (A safer way)
private static class MySingletonHelper{
private static final MySerializedSingleton instance = new MySerializedSingleton();
}

//Return singleton instance
public static MySerializedSingleton getInstance(){
return MySingletonHelper.instance;
}

/**
* Implement the readResolve() method to avoid creating a new instance in 'Deserialization'            scenario.  
*/
protected Object readResolve() {
return getInstance();
}

}