Search in sources :

Example 1 with Attributes

use of org.eclipse.jetty.util.Attributes in project druid by druid-io.

the class KerberosJettyHttpClientProvider method get.

@Override
public HttpClient get() {
    final HttpClient httpClient = delegateProvider.get();
    httpClient.getAuthenticationStore().addAuthentication(new Authentication() {

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

        @Override
        public Result authenticate(final Request request, ContentResponse response, Authentication.HeaderInfo headerInfo, Attributes context) {
            return new Result() {

                @Override
                public URI getURI() {
                    return request.getURI();
                }

                @Override
                public void apply(Request request) {
                    try {
                        // No need to set cookies as they are handled by Jetty Http Client itself.
                        URI uri = request.getURI();
                        if (DruidKerberosUtil.needToSendCredentials(httpClient.getCookieStore(), uri)) {
                            log.debug("No Auth Cookie found for URI[%s]. Existing Cookies[%s] Authenticating... ", uri, httpClient.getCookieStore().getCookies());
                            final String host = request.getHost();
                            DruidKerberosUtil.authenticateIfRequired(config);
                            UserGroupInformation currentUser = UserGroupInformation.getCurrentUser();
                            String challenge = currentUser.doAs(new PrivilegedExceptionAction<String>() {

                                @Override
                                public String run() throws Exception {
                                    return DruidKerberosUtil.kerberosChallenge(host);
                                }
                            });
                            request.getHeaders().add(HttpHeaders.Names.AUTHORIZATION, "Negotiate " + challenge);
                        } else {
                            log.debug("Found Auth Cookie found for URI[%s].", uri);
                        }
                    } catch (Throwable e) {
                        Throwables.propagate(e);
                    }
                }
            };
        }
    });
    return httpClient;
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) Attributes(org.eclipse.jetty.util.Attributes) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) URI(java.net.URI) Authentication(org.eclipse.jetty.client.api.Authentication) HttpClient(org.eclipse.jetty.client.HttpClient) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 2 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") Object 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 3 with Attributes

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

the class Dispatcher method include.

@Override
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException {
    Request baseRequest = Request.getBaseRequest(request);
    if (!(request instanceof HttpServletRequest))
        request = new ServletRequestHttpWrapper(request);
    if (!(response instanceof HttpServletResponse))
        response = new ServletResponseHttpWrapper(response);
    final DispatcherType old_type = baseRequest.getDispatcherType();
    final Attributes old_attr = baseRequest.getAttributes();
    final MultiMap<String> old_query_params = baseRequest.getQueryParameters();
    try {
        baseRequest.setDispatcherType(DispatcherType.INCLUDE);
        baseRequest.getResponse().include();
        if (_named != null) {
            _contextHandler.handle(_named, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
        } else {
            IncludeAttributes attr = new IncludeAttributes(old_attr);
            attr._requestURI = _uri.getPath();
            attr._contextPath = _contextHandler.getContextPath();
            // set by ServletHandler
            attr._servletPath = null;
            attr._pathInfo = _pathInContext;
            attr._query = _uri.getQuery();
            if (attr._query != null)
                baseRequest.mergeQueryParameters(baseRequest.getQueryString(), attr._query, false);
            baseRequest.setAttributes(attr);
            _contextHandler.handle(_pathInContext, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
        }
    } finally {
        baseRequest.setAttributes(old_attr);
        baseRequest.getResponse().included();
        baseRequest.setQueryParameters(old_query_params);
        baseRequest.resetParameters();
        baseRequest.setDispatcherType(old_type);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Attributes(org.eclipse.jetty.util.Attributes) HttpServletResponse(javax.servlet.http.HttpServletResponse) DispatcherType(javax.servlet.DispatcherType)

Example 4 with Attributes

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

the class Dispatcher method forward.

protected void forward(ServletRequest request, ServletResponse response, DispatcherType dispatch) throws ServletException, IOException {
    Request baseRequest = Request.getBaseRequest(request);
    Response base_response = baseRequest.getResponse();
    base_response.resetForForward();
    if (!(request instanceof HttpServletRequest))
        request = new ServletRequestHttpWrapper(request);
    if (!(response instanceof HttpServletResponse))
        response = new ServletResponseHttpWrapper(response);
    final HttpURI old_uri = baseRequest.getHttpURI();
    final String old_context_path = baseRequest.getContextPath();
    final String old_servlet_path = baseRequest.getServletPath();
    final String old_path_info = baseRequest.getPathInfo();
    final MultiMap<String> old_query_params = baseRequest.getQueryParameters();
    final Attributes old_attr = baseRequest.getAttributes();
    final DispatcherType old_type = baseRequest.getDispatcherType();
    try {
        baseRequest.setDispatcherType(dispatch);
        if (_named != null) {
            _contextHandler.handle(_named, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
        } else {
            ForwardAttributes attr = new ForwardAttributes(old_attr);
            //for queryString is allowed to be null, but cannot be null for the other values.
            if (old_attr.getAttribute(FORWARD_REQUEST_URI) != null) {
                attr._pathInfo = (String) old_attr.getAttribute(FORWARD_PATH_INFO);
                attr._query = (String) old_attr.getAttribute(FORWARD_QUERY_STRING);
                attr._requestURI = (String) old_attr.getAttribute(FORWARD_REQUEST_URI);
                attr._contextPath = (String) old_attr.getAttribute(FORWARD_CONTEXT_PATH);
                attr._servletPath = (String) old_attr.getAttribute(FORWARD_SERVLET_PATH);
            } else {
                attr._pathInfo = old_path_info;
                attr._query = old_uri.getQuery();
                attr._requestURI = old_uri.getPath();
                attr._contextPath = old_context_path;
                attr._servletPath = old_servlet_path;
            }
            HttpURI uri = new HttpURI(old_uri.getScheme(), old_uri.getHost(), old_uri.getPort(), _uri.getPath(), _uri.getParam(), _uri.getQuery(), _uri.getFragment());
            baseRequest.setHttpURI(uri);
            baseRequest.setContextPath(_contextHandler.getContextPath());
            baseRequest.setServletPath(null);
            baseRequest.setPathInfo(_pathInContext);
            if (_uri.getQuery() != null || old_uri.getQuery() != null)
                baseRequest.mergeQueryParameters(old_uri.getQuery(), _uri.getQuery(), true);
            baseRequest.setAttributes(attr);
            _contextHandler.handle(_pathInContext, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
            if (!baseRequest.getHttpChannelState().isAsync())
                commitResponse(response, baseRequest);
        }
    } finally {
        baseRequest.setHttpURI(old_uri);
        baseRequest.setContextPath(old_context_path);
        baseRequest.setServletPath(old_servlet_path);
        baseRequest.setPathInfo(old_path_info);
        baseRequest.setQueryParameters(old_query_params);
        baseRequest.resetParameters();
        baseRequest.setAttributes(old_attr);
        baseRequest.setDispatcherType(old_type);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Attributes(org.eclipse.jetty.util.Attributes) HttpServletResponse(javax.servlet.http.HttpServletResponse) DispatcherType(javax.servlet.DispatcherType) HttpURI(org.eclipse.jetty.http.HttpURI)

Example 5 with Attributes

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

the class ContextHandlerMBean method removeContextAttribute.

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

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