home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (NTLMScheme.java)

This example source code file (NTLMScheme.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java tags/keywords

authenticationexception, challenge_received, chararraybuffer, classcastexception, credentials, invalidcredentialsexception, malformedchallengeexception, ntcredentials, ntlm, ntlmengine, ntlmscheme, state, string, uninitiated

The NTLMScheme.java example source code

 * ====================================================================
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.

package org.apache.http.impl.auth;

import org.apache.http.annotation.NotThreadSafe;

import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.auth.AUTH;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.InvalidCredentialsException;
import org.apache.http.auth.MalformedChallengeException;
import org.apache.http.auth.NTCredentials;
import org.apache.http.impl.auth.AuthSchemeBase;
import org.apache.http.message.BufferedHeader;
import org.apache.http.util.CharArrayBuffer;

 * NTLM is a proprietary authentication scheme developed by Microsoft 
 * and optimized for Windows platforms.
 * <p>
 * Please note that the NTLM scheme requires an external 
 * {@link NTLMEngine} implementation to function!
 * For details please refer to 
 * <a href="http://hc.apache.org/httpcomponents-client/ntlm.html">
 * this document</a>.
 * @since 4.0
public class NTLMScheme extends AuthSchemeBase {

    enum State {
    private final NTLMEngine engine;
    private State state;
    private String challenge;
    public NTLMScheme(final NTLMEngine engine) {
        if (engine == null) {
            throw new IllegalArgumentException("NTLM engine may not be null");
        this.engine = engine;
        this.state = State.UNINITIATED;
        this.challenge = null;
    public String getSchemeName() {
        return "ntlm";

    public String getParameter(String name) {
        // String parameters not supported
        return null;

    public String getRealm() {
        // NTLM does not support the concept of an authentication realm
        return null;

    public boolean isConnectionBased() {
        return true;

    protected void parseChallenge(
            final CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException {
        String challenge = buffer.substringTrimmed(pos, len);
        if (challenge.length() == 0) {
            if (this.state == State.UNINITIATED) {
                this.state = State.CHALLENGE_RECEIVED;
            } else {
                this.state = State.FAILED;
            this.challenge = null;
        } else {
            this.state = State.MSG_TYPE2_RECEVIED;
            this.challenge = challenge;

    public Header authenticate(
            final Credentials credentials, 
            final HttpRequest request) throws AuthenticationException {
        NTCredentials ntcredentials = null;
        try {
            ntcredentials = (NTCredentials) credentials;
        } catch (ClassCastException e) {
            throw new InvalidCredentialsException(
             "Credentials cannot be used for NTLM authentication: " 
              + credentials.getClass().getName());
        String response = null;
        if (this.state == State.CHALLENGE_RECEIVED || this.state == State.FAILED) {
            response = this.engine.generateType1Msg(
            this.state = State.MSG_TYPE1_GENERATED;
        } else if (this.state == State.MSG_TYPE2_RECEVIED) {
            response = this.engine.generateType3Msg(
            this.state = State.MSG_TYPE3_GENERATED;
        } else {
            throw new AuthenticationException("Unexpected state: " + this.state);
        CharArrayBuffer buffer = new CharArrayBuffer(32);
        if (isProxy()) {
        } else {
        buffer.append(": NTLM ");
        return new BufferedHeader(buffer);

    public boolean isComplete() {
        return this.state == State.MSG_TYPE3_GENERATED || this.state == State.FAILED;


Other Java examples (source code examples)

Here is a short list of links related to this Java NTLMScheme.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.