/** * Certain aspects of the container, such as logging, need the authentication information to behave properly. * This method updates the request with the necessary objects to recognize the authenticated user. */ private void setJettyAuthentication(Subject subject) { // In unit test environments there may not be a current connection. If any nulls are encountered // then, by definition, there is no container to update. HttpConnection connection = HttpConnection.getCurrentConnection(); if (connection == null) { return; } Request jettyRequest = connection.getHttpChannel().getRequest(); if (jettyRequest == null) { return; } // This cast down is safe; subject is always created with this type of principal PrincipalWithRoles principal = (PrincipalWithRoles) subject.getPrincipal(); UserIdentity identity = principal.toUserIdentity(); jettyRequest.setAuthentication(new UserAuthentication(SecurityContext.BASIC_AUTH, identity)); }
private void honourMaxKeepAliveRequests() { if (jDiscContext.serverConfig.maxKeepAliveRequests() > 0) { HttpConnection connection = getConnection(servletRequest); if (connection.getMessagesIn() >= jDiscContext.serverConfig.maxKeepAliveRequests()) { connection.getGenerator().setPersistent(false); } } }
@Override public Object getAttribute(String name) { switch (name) { case "org.eclipse.jetty.server.HttpConnection": HttpConnection connection = mock(HttpConnection.class); when(connection.getCreatedTimeStamp()).thenReturn(System.currentTimeMillis()); return connection; default: return null; } }
private ServletRequest newServletRequest() throws Exception { MockHttpServletRequest parent = new MockHttpServletRequest("GET", uri.toString()); parent.setProtocol(Version.HTTP_1_1.toString()); parent.setRemoteHost(host); parent.setRemotePort(port); parent.setParameter(paramName, paramValue); parent.setParameter(listParamName, listParamValue); parent.addHeader(headerName, headerValue); parent.setAttribute(attributeName, attributeValue); HttpConnection connection = Mockito.mock(HttpConnection.class); when(connection.getCreatedTimeStamp()).thenReturn(System.currentTimeMillis()); parent.setAttribute("org.eclipse.jetty.server.HttpConnection", connection); return new ServletRequest(parent, uri); }
@Override public void handle( String target, Request baseRequest, HttpServletRequest req, HttpServletResponse res) throws IOException { HttpConnection conn = HttpConnection.getCurrentConnection(); baseRequest.setHandled(true); try { log(req); } finally { reply(conn, res); } }
private void reply(HttpConnection conn, HttpServletResponse res) throws IOException { byte[] msg = message(conn); res.setHeader(HttpHeader.CONTENT_TYPE.asString(), "text/plain; charset=ISO-8859-1"); res.setContentLength(msg.length); try { CacheHeaders.setNotCacheable(res); } finally { try (ServletOutputStream out = res.getOutputStream()) { out.write(msg); } } }
private static byte[] message(HttpConnection conn) { String msg; if (conn == null) { msg = ""; } else { msg = conn.getHttpChannel().getResponse().getReason(); if (msg == null) { msg = HttpStatus.getMessage(conn.getHttpChannel().getResponse().getStatus()); } } return msg.getBytes(ISO_8859_1); }
@Override protected Request getRequest(final Object[] args) { if (args == null || args.length < 1) { return null; } if (args[0] instanceof HttpConnection) { try { HttpConnection connection = (HttpConnection) args[0]; return connection.getRequest(); } catch (Throwable ignored) { } } return null; }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { Request httpRequest = (request instanceof Request) ? (Request) request : HttpConnection.getCurrentConnection().getHttpChannel().getRequest(); // If there already is an API key present then perform no further action String apiKeyHeader = httpRequest.getHeader(ApiKeyRequest.AUTHENTICATION_HEADER); String apiKeyParam = httpRequest.getParameter(ApiKeyRequest.AUTHENTICATION_PARAM); if (!Strings.isNullOrEmpty(apiKeyHeader) || !Strings.isNullOrEmpty(apiKeyParam)) { chain.doFilter(request, response); return; } // If there is no authentication header then perform no further action String authenticationHeader = httpRequest.getHeader(HttpHeader.AUTHORIZATION.asString()); if (Strings.isNullOrEmpty(authenticationHeader)) { chain.doFilter(request, response); return; } // Parse the authentication header to determine if it matches the replication user's credentials int space = authenticationHeader.indexOf(' '); if (space != -1 && "basic".equalsIgnoreCase(authenticationHeader.substring(0, space))) { try { String credentials = new String( BaseEncoding.base64().decode(authenticationHeader.substring(space+1)), Charsets.UTF_8); for (Map.Entry<String, String> entry : _basicAuthToApiKeyMap.entrySet()) { if (entry.getKey().equals(credentials)) { // The user name and password matches the replication credentials. Insert the header. HttpFields fields = httpRequest.getHttpFields(); fields.put(ApiKeyRequest.AUTHENTICATION_HEADER, entry.getValue()); } } } catch (Exception e) { // Ok, the header wasn't formatted properly. Do nothing. } } chain.doFilter(request, response); }
public static HttpConnection getConnection(HttpServletRequest request) { return (HttpConnection)request.getAttribute("org.eclipse.jetty.server.HttpConnection"); }