NTLM-Authentication using HTTPClient, JCIFSEngine and NTLMSchemeFactory

Apache's HttpClient 4.0 does not provide support for the NTLM authentication scheme out of the box and probably never will. The reasons for that are legal rather than technical. (Source: http://hc.apache.org/httpcomponents-client/ntlm.html)

But Apache proposes an implementation blue print using the classes JCIFSEngine and NTLMSchemeFactory.

The last step is to glue it together. This example authenticates against IIS6 and Windows2003 domain.

 httpclient = new DefaultHttpClient();
 httpclient.getParams().setParameter("http.useragent", "Mozilla/5.0 (X11; U; Linux i686; en; rv:1.7.3)");
 httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
 httpclient.getCredentialsProvider().setCredentials(new AuthScope(HOSTNAME, -1),
                new NTCredentials(USERNAME, PASSWORD, LOCAL, DOMAIN));

TomcatMBeanViewer

Preliminary information

Tomcat 4 uses already the JMX-API. But there is no easy way to get a list of all available MBeans. JMX stands for Java Management extensions.

Tested Environment

  • Java 1.4
  • Tomcat 4.1

MBeanViewer web application

Insert this code in your servlet or JSP.

    MBeanServer mBeanServer = null;
    ArrayList arrayList = MBeanServerFactory.findMBeanServer(null);
    if (arrayList.size() > 0) {
        mBeanServer = (MBeanServer)arrayList.get(0);
    } else {
        mBeanServer = MBeanServerFactory.createMBeanServer();
    }

    Set allMBeans = mBeanServer.queryNames(null,null);
    for(Iterator i = allMBeans.iterator(); i.hasNext(); ) {
       ObjectName objectName = (ObjectName)i.next();

       log.debug(objectName.getDomain()); 
       log.debug(objectName.getCanonicalName());
    }

Results

Domain Canonical Name
Catalina Catalina:name=channelSocket,type=JkHandler
Catalina Catalina:host=localhost,name=users,path=/manager,resourcetype=Context,service=Tomcat-Standalone,type=ResourceLink
Catalina Catalina:resourcetype=Global,type=NamingResources
... ...

Log4jSessionFilter

Preliminary information

Log4J allows to attach information to the current thread. Log4J calls this NDC. NDC stands for "Nested Diagnostic Context".

Tested Environment

  • Java 1.4 /1.5
  • Log4j 1.2.x
  • Servlet API 2.3/2.4

Instructions

1. To include in every log message the current session-ID add a ServletFilter which attachs the session-ID to the NDC

public class Log4JFilter implements Filter {

    public void doFilter(
            ServletRequest request,
            ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {

        String contextInformation = "";

        if (request instanceof HttpServletRequest) {
            HttpServletRequest httpRequest = (HttpServletRequest)request;
            contextInformation += httpRequest.getSession().getId();
        }

        // push information
        NDC.push(contextInformation);

        try {
            chain.doFilter(request, response);
        } finally {
            // clean up NDC
            NDC.remove();
        }
    }
}

2. Enable the filter in web.xml

  <filter>
    <filter-name>Log4JFilter</filter-name>
    <display-name>Log4JFilter</display-name>
    <description>Intercepting Filter that provides Log4J context information</description>
    <filter-class>Log4JFilter </filter-class>
  </filter>

  <filter-mapping>
    <filter-name>Log4JContextFilter</filter-name>
    <url-pattern>*.do</url-pattern>
  </filter-mapping>

3. Add the context information to the log4j-Pattern. Relevant to NDC is "%x".

   <param name="ConversionPattern" value="%d [%x] %-5p %c - %m"/>

Resulting logfile

2006-05-26 17:02:45,171 [R0rhSjqecO1gSGBDjj9omt] INFO com.Foo - bar() has been called 

Posted on by Peter Eichenauer
Tags: java log4j ndc

Firebird on Windows

Preliminary information  

Firebird is a database system with a very small memory foot print. It's quite easy to set up the database system, but the JDBC-URL format is a bit unusual.  

So maybe for somebody setting up Firebird-JDBC, this example might be useful. 

Firebird JDBC page 


Example of connection properties 

connection.url=jdbc:firebirdsql://localhost:3050/C:\\install\\Firebird\\MYDATABASE.FDB 
connection.driver_class=org.firebirdsql.jdbc.FBDriver 
connection.username=sysdba 
connection.password=****** 

Tested environment 

* Java 1.4 
* firebirdsql-full.jar (Version: 1.5.0RC4JDK_1.4)