Categories
Coding

Generics Problem #1 Solved

I have found a solution for the generics related error I mentioned here. The solution was to parameterize Comparable, e.g. instead of:

Foo implements Comparable
use:
Foo implements Comparable<ActionState>

This solves the issue for Eclipse. It’s slightly strange that Eclipse flags this as an error, and not IDEA.

Categories
Coding

NTLM Proxy Authentication with Neon

Neon is really a great library, and has recently been released as version 0.25.0, which has built-in support for NTLM authentication. Here is a sample program showing how this can be used in practise.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ne_socket.h"
#include "ne_session.h"
#include "ne_request.h"
#include "ne_auth.h"

typedef struct {
static const int challenge_issued = 0;
} user_data;

/*
* Response reader callback function
*
*/
void my_response_reader(void *userdata, const char *buf, size_t len) {
printf("Received: %s\n", buf);
}

/*
* Authentication callback
*/

static int my_auth(void *userdata, const char *realm, int attempts, char *username, char *password)
{
strncpy(username, "username", NE_ABUFSIZ);
strncpy(password, "password", NE_ABUFSIZ);
user_data* data = (user_data*)userdata;
return attempts;
}

int main(int argc, char* argv[]) {
user_data data;
int success = ne_sock_init();
char dropbuf[4096];

if (success != 0) {
printf("Cannot initialize Neon library");
exit(1);
}

ne_session* session = ne_session_create("http", "www.google.com", 80);
ne_session_proxy(session, "proxy1.researchkitchen.co.uk", 8080);
ne_set_proxy_auth(session, my_auth, (void*)&data);
ne_request *req = ne_request_create(session, "GET", "/");
ne_add_request_header(req, "Connection", "Keep-Alive");

ne_debug_init(stdout, NE_DBG_HTTPAUTH);

if (ne_request_dispatch(req))
{
printf("An error occurred\n");
const char* error = ne_get_error(session);
printf("%s", error);
exit(1);
}
else
{
printf("Response status code was %d\n", ne_get_status(req)->code);
ne_request_destroy(req);
}

ne_close_connection(session);
ne_session_destroy(session);
return 0;
}

Categories
Coding

JDK-Based NTLM Authentication

When Sun released the 1.4.2 version of the JDK, they slipped in support for native NTLM authentication on Windows. This is done by retrieving the logged-on user’s username and password from the OS whenever an NTLM challenge is received. The only information that needs to be supplied is the NT domain. Here is an example that authenticates through an MS proxy, using my currently active credentials.


package uk.co.researchkitchen.ntlm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class TestJDKNTLM {

    private void execute() throws IOException {
        System.setProperty("proxySet""true");
        System.setProperty("http.proxyHost""proxy1.researchkitchen.co.uk");
        System.setProperty("http.proxyPort""8080");
        System.setProperty("http.auth.ntlm.domain""MYDOMAIN");

        URL url = new URL("http://www.yahoo.com");
        URLConnection conn = url.openConnection();

        conn.setAllowUserInteraction(true);
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        int read = 0;
        String body = null;

        do {
          body = reader.readLine();
          System.out.println(body);
        while (body != null);

    }

    public static void main(String[] argsthrows IOException {
        new TestJDKNTLM().execute();
    }

}