Search in sources :

Example 21 with HttpRequestHandlerChain

use of org.apache.commons.httpclient.server.HttpRequestHandlerChain in project ecf by eclipse.

the class TestBasicAuth method testPreemptiveAuthorizationTrueWithoutCreds.

public void testPreemptiveAuthorizationTrueWithoutCreds() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
    HttpState state = new HttpState();
    this.client.setState(state);
    this.client.getParams().setAuthenticationPreemptive(true);
    this.server.setRequestHandler(handlerchain);
    GetMethod httpget = new GetMethod("/test/");
    try {
        this.client.executeMethod(httpget);
    } finally {
        httpget.releaseConnection();
    }
    assertNotNull(httpget.getStatusLine());
    assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget.getStatusLine().getStatusCode());
    Header auth = httpget.getRequestHeader("Authorization");
    assertNull(auth);
    AuthState authstate = httpget.getHostAuthState();
    assertNotNull(authstate.getAuthScheme());
    assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
    assertNotNull(authstate.getRealm());
    assertTrue(authstate.isPreemptive());
}
Also used : Header(org.apache.commons.httpclient.Header) HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) FeedbackService(org.apache.commons.httpclient.FeedbackService) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) HttpState(org.apache.commons.httpclient.HttpState) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 22 with HttpRequestHandlerChain

use of org.apache.commons.httpclient.server.HttpRequestHandlerChain in project ecf by eclipse.

the class TestBasicAuth method testCustomAuthorizationHeader.

public void testCustomAuthorizationHeader() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
    this.server.setRequestHandler(handlerchain);
    GetMethod httpget = new GetMethod("/test/");
    String authResponse = "Basic " + EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
    httpget.addRequestHeader(new Header("Authorization", authResponse));
    try {
        this.client.executeMethod(httpget);
    } finally {
        httpget.releaseConnection();
    }
    assertNotNull(httpget.getStatusLine());
    assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
}
Also used : Header(org.apache.commons.httpclient.Header) HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) FeedbackService(org.apache.commons.httpclient.FeedbackService) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 23 with HttpRequestHandlerChain

use of org.apache.commons.httpclient.server.HttpRequestHandlerChain in project ecf by eclipse.

the class TestBasicAuth method testPreemptiveAuthorizationFailure.

public void testPreemptiveAuthorizationFailure() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    UsernamePasswordCredentials wrongcreds = new UsernamePasswordCredentials("testuser", "garbage");
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
    HttpState state = new HttpState();
    state.setCredentials(AuthScope.ANY, wrongcreds);
    this.client.setState(state);
    this.client.getParams().setAuthenticationPreemptive(true);
    this.server.setRequestHandler(handlerchain);
    GetMethod httpget = new GetMethod("/test/");
    try {
        this.client.executeMethod(httpget);
    } finally {
        httpget.releaseConnection();
    }
    assertNotNull(httpget.getStatusLine());
    assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget.getStatusLine().getStatusCode());
    AuthState authstate = httpget.getHostAuthState();
    assertNotNull(authstate.getAuthScheme());
    assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
    assertEquals("test", authstate.getRealm());
    assertTrue(authstate.isPreemptive());
}
Also used : HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) FeedbackService(org.apache.commons.httpclient.FeedbackService) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) HttpState(org.apache.commons.httpclient.HttpState) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 24 with HttpRequestHandlerChain

use of org.apache.commons.httpclient.server.HttpRequestHandlerChain in project ecf by eclipse.

the class TestEntityEnclosingMethod method testEnclosedEntityRepeatable.

public void testEnclosedEntityRepeatable() throws Exception {
    String inputstr = "This is a test message";
    byte[] input = inputstr.getBytes("US-ASCII");
    InputStream instream = new ByteArrayInputStream(input);
    RequestEntity requestentity = new InputStreamRequestEntity(instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
    PostMethod method = new PostMethod("/");
    method.setRequestEntity(requestentity);
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new EchoService()));
    this.server.setRequestHandler(handlerchain);
    this.client.getState().setCredentials(AuthScope.ANY, creds);
    try {
        this.client.executeMethod(method);
        assertEquals(200, method.getStatusCode());
        String body = method.getResponseBodyAsString();
        assertEquals(inputstr, body);
        assertNull(method.getRequestHeader("Transfer-Encoding"));
        assertNotNull(method.getRequestHeader("Content-Length"));
        assertEquals(input.length, Integer.parseInt(method.getRequestHeader("Content-Length").getValue()));
    } finally {
        method.releaseConnection();
    }
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler) InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity)

Example 25 with HttpRequestHandlerChain

use of org.apache.commons.httpclient.server.HttpRequestHandlerChain in project ecf by eclipse.

the class TestEntityEnclosingMethod method testEnclosedEntityNonRepeatable.

public void testEnclosedEntityNonRepeatable() throws Exception {
    String inputstr = "This is a test message";
    byte[] input = inputstr.getBytes("US-ASCII");
    InputStream instream = new ByteArrayInputStream(input);
    RequestEntity requestentity = new InputStreamRequestEntity(instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
    PostMethod method = new PostMethod("/");
    method.setRequestEntity(requestentity);
    method.setContentChunked(true);
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new EchoService()));
    this.server.setRequestHandler(handlerchain);
    this.client.getState().setCredentials(AuthScope.ANY, creds);
    try {
        this.client.executeMethod(method);
        fail("ProtocolException should have been thrown");
    } catch (ProtocolException ex) {
    // expected
    } finally {
        method.releaseConnection();
    }
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler) InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity)

Aggregations

AuthRequestHandler (org.apache.commons.httpclient.server.AuthRequestHandler)37 HttpRequestHandlerChain (org.apache.commons.httpclient.server.HttpRequestHandlerChain)37 HttpServiceHandler (org.apache.commons.httpclient.server.HttpServiceHandler)37 GetMethod (org.apache.commons.httpclient.methods.GetMethod)22 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)14 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)14 PostMethod (org.apache.commons.httpclient.methods.PostMethod)13 FeedbackService (org.apache.commons.httpclient.FeedbackService)12 HttpState (org.apache.commons.httpclient.HttpState)11 Header (org.apache.commons.httpclient.Header)10 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 EchoService (org.apache.commons.httpclient.EchoService)2 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)2 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)2 HeadMethod (org.apache.commons.httpclient.methods.HeadMethod)1 PutMethod (org.apache.commons.httpclient.methods.PutMethod)1