Search in sources :

Example 6 with Attributes

use of org.eclipse.jetty.util.Attributes in project jetty.project by eclipse.

the class ContextHandlerMBean method setContextAttribute.

@ManagedOperation(value = "Set context attribute", impact = "ACTION")
public void setContextAttribute(@Name(value = "name", description = "attribute name") String name, @Name(value = "value", description = "attribute value") String value) {
    Attributes attrs = ((ContextHandler) _managed).getAttributes();
    attrs.setAttribute(name, value);
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Attributes(org.eclipse.jetty.util.Attributes) ManagedOperation(org.eclipse.jetty.util.annotation.ManagedOperation)

Example 7 with Attributes

use of org.eclipse.jetty.util.Attributes in project jetty.project by eclipse.

the class ContextHandlerMBean method getContextAttributes.

@ManagedAttribute("Map of context attributes")
public Map<String, Object> getContextAttributes() {
    Map<String, Object> map = new HashMap<String, Object>();
    Attributes attrs = ((ContextHandler) _managed).getAttributes();
    Enumeration<String> en = attrs.getAttributeNames();
    while (en.hasMoreElements()) {
        String name = (String) en.nextElement();
        Object value = attrs.getAttribute(name);
        map.put(name, value);
    }
    return map;
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HashMap(java.util.HashMap) Attributes(org.eclipse.jetty.util.Attributes) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) ManagedAttribute(org.eclipse.jetty.util.annotation.ManagedAttribute)

Example 8 with Attributes

use of org.eclipse.jetty.util.Attributes in project jetty.project by eclipse.

the class HttpClientAuthenticationTest method test_Authentication_ThrowsException.

@Test
public void test_Authentication_ThrowsException() throws Exception {
    startBasic(new EmptyServerHandler());
    // Request without Authentication would cause a 401,
    // but the client will throw an exception trying to
    // send the credentials to the server.
    final String cause = "thrown_explicitly_by_test";
    client.getAuthenticationStore().addAuthentication(new Authentication() {

        @Override
        public boolean matches(String type, URI uri, String realm) {
            return true;
        }

        @Override
        public Result authenticate(Request request, ContentResponse response, HeaderInfo headerInfo, Attributes context) {
            throw new RuntimeException(cause);
        }
    });
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure").timeout(5, TimeUnit.SECONDS).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            Assert.assertEquals(cause, result.getFailure().getMessage());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Attributes(org.eclipse.jetty.util.Attributes) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Result(org.eclipse.jetty.client.api.Result) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Authentication(org.eclipse.jetty.client.api.Authentication) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) Test(org.junit.Test)

Example 9 with Attributes

use of org.eclipse.jetty.util.Attributes in project knox by apache.

the class FrontendFunctionProcessorTest method testFrontendFunctionsWithFrontendUriConfigOnJsonRequestBody.

@Test
public void testFrontendFunctionsWithFrontendUriConfigOnJsonRequestBody() throws Exception {
    // This hooks up the filter in rewrite.xml in this class' test resource directory.
    Map<String, String> initParams = new HashMap<>();
    initParams.put("response.body", "test-filter");
    // This simulates having gateway.frontend.uri in gateway-site.xml
    Attributes attributes = new AttributesMap();
    attributes.setAttribute(FrontendFunctionDescriptor.FRONTEND_URI_ATTRIBUTE, new URI("mock-frontend-scheme://mock-frontend-host:777/mock-frontend-path"));
    setUp("test-user", initParams, attributes);
    String input = TestUtils.getResourceString(FrontendFunctionProcessorTest.class, "test-input-body.json", "UTF-8");
    interaction.expect().method("GET").requestUrl("http://test-host:42/test-path");
    interaction.respond().status(200).contentType("application/json").characterEncoding("UTF-8").content(input, Charset.forName("UTF-8"));
    interactions.add(interaction);
    request.setMethod("GET");
    request.setURI("/test-path");
    // request.setVersion( "HTTP/1.1" );
    request.setHeader("Host", "test-host:42");
    response = TestUtils.execute(server, request);
    assertThat(response.getStatus(), Is.is(200));
    String json = response.getContent();
    // Note: The Jetty ServletTester/HttpTester doesn't return very good values.
    JsonAssert.with(json).assertThat("$.url", is("mock-frontend-scheme://mock-frontend-host:777/mock-frontend-path"));
    JsonAssert.with(json).assertThat("$.scheme", is("mock-frontend-scheme"));
    JsonAssert.with(json).assertThat("$.host", is("mock-frontend-host"));
    JsonAssert.with(json).assertThat("$.port", is("777"));
    JsonAssert.with(json).assertThat("$.addr", is("mock-frontend-host:777"));
    JsonAssert.with(json).assertThat("$.address", is("mock-frontend-host:777"));
    JsonAssert.with(json).assertThat("$.path", is("/mock-frontend-path"));
}
Also used : HashMap(java.util.HashMap) Attributes(org.eclipse.jetty.util.Attributes) AttributesMap(org.eclipse.jetty.util.AttributesMap) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) URI(java.net.URI) Test(org.junit.Test)

Aggregations

Attributes (org.eclipse.jetty.util.Attributes)9 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)4 URI (java.net.URI)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 ManagedOperation (org.eclipse.jetty.util.annotation.ManagedOperation)3 HashMap (java.util.HashMap)2 DispatcherType (javax.servlet.DispatcherType)2 ServletRequest (javax.servlet.ServletRequest)2 Authentication (org.eclipse.jetty.client.api.Authentication)2 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)2 Request (org.eclipse.jetty.client.api.Request)2 Test (org.junit.Test)2 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ServletResponse (javax.servlet.ServletResponse)1 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)1 HttpClient (org.eclipse.jetty.client.HttpClient)1 Response (org.eclipse.jetty.client.api.Response)1 Result (org.eclipse.jetty.client.api.Result)1