365947 Set headers for Auth failure and retry in http-spi
This commit is contained in:
parent
e96f828dcf
commit
f8cd2e9a0c
|
@ -15,6 +15,8 @@ package org.eclipse.jetty.http.spi;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
@ -25,6 +27,7 @@ import org.eclipse.jetty.server.handler.ContextHandler;
|
||||||
|
|
||||||
import com.sun.net.httpserver.Authenticator;
|
import com.sun.net.httpserver.Authenticator;
|
||||||
import com.sun.net.httpserver.Authenticator.Result;
|
import com.sun.net.httpserver.Authenticator.Result;
|
||||||
|
import com.sun.net.httpserver.BasicAuthenticator;
|
||||||
import com.sun.net.httpserver.HttpContext;
|
import com.sun.net.httpserver.HttpContext;
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
import com.sun.net.httpserver.HttpHandler;
|
import com.sun.net.httpserver.HttpHandler;
|
||||||
|
@ -108,12 +111,23 @@ public class HttpSpiContextHandler extends ContextHandler
|
||||||
if (result instanceof Authenticator.Failure)
|
if (result instanceof Authenticator.Failure)
|
||||||
{
|
{
|
||||||
int rc = ((Authenticator.Failure)result).getResponseCode();
|
int rc = ((Authenticator.Failure)result).getResponseCode();
|
||||||
|
for (Map.Entry<String,List<String>> header : httpExchange.getResponseHeaders().entrySet())
|
||||||
|
{
|
||||||
|
for (String value : header.getValue())
|
||||||
|
resp.addHeader(header.getKey(),value);
|
||||||
|
}
|
||||||
resp.sendError(rc);
|
resp.sendError(rc);
|
||||||
}
|
}
|
||||||
else if (result instanceof Authenticator.Retry)
|
else if (result instanceof Authenticator.Retry)
|
||||||
{
|
{
|
||||||
int rc = ((Authenticator.Retry)result).getResponseCode();
|
int rc = ((Authenticator.Retry)result).getResponseCode();
|
||||||
resp.sendError(rc);
|
for (Map.Entry<String,List<String>> header : httpExchange.getResponseHeaders().entrySet())
|
||||||
|
{
|
||||||
|
for (String value : header.getValue())
|
||||||
|
resp.addHeader(header.getKey(),value);
|
||||||
|
}
|
||||||
|
resp.setStatus(rc);
|
||||||
|
resp.flushBuffer();
|
||||||
}
|
}
|
||||||
else if (result instanceof Authenticator.Success)
|
else if (result instanceof Authenticator.Success)
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
package org.eclipse.jetty.http.spi;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.sun.net.httpserver.BasicAuthenticator;
|
||||||
|
import com.sun.net.httpserver.Headers;
|
||||||
|
import com.sun.net.httpserver.HttpContext;
|
||||||
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
|
import com.sun.net.httpserver.HttpHandler;
|
||||||
|
import com.sun.net.httpserver.HttpServer;
|
||||||
|
|
||||||
|
|
||||||
|
public class TestSPIServer
|
||||||
|
{
|
||||||
|
public static void main(String[] args) throws Exception
|
||||||
|
{
|
||||||
|
String host="localhost";
|
||||||
|
int port = 8080;
|
||||||
|
|
||||||
|
HttpServer server = new JettyHttpServerProvider().createHttpServer(new
|
||||||
|
InetSocketAddress(host, port), 10);
|
||||||
|
server.start();
|
||||||
|
|
||||||
|
final HttpContext httpContext = server.createContext("/",
|
||||||
|
new HttpHandler()
|
||||||
|
{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(HttpExchange exchange) throws IOException
|
||||||
|
{
|
||||||
|
Headers responseHeaders = exchange.getResponseHeaders();
|
||||||
|
responseHeaders.set("Content-Type","text/plain");
|
||||||
|
exchange.sendResponseHeaders(200,0);
|
||||||
|
|
||||||
|
OutputStream responseBody = exchange.getResponseBody();
|
||||||
|
Headers requestHeaders = exchange.getRequestHeaders();
|
||||||
|
Set<String> keySet = requestHeaders.keySet();
|
||||||
|
Iterator<String> iter = keySet.iterator();
|
||||||
|
while (iter.hasNext())
|
||||||
|
{
|
||||||
|
String key = iter.next();
|
||||||
|
List values = requestHeaders.get(key);
|
||||||
|
String s = key + " = " + values.toString() + "\n";
|
||||||
|
responseBody.write(s.getBytes());
|
||||||
|
}
|
||||||
|
responseBody.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
httpContext.setAuthenticator(new BasicAuthenticator("Test")
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean checkCredentials(String username, String password)
|
||||||
|
{
|
||||||
|
if ("username".equals(username) && password.equals("password"))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Thread.sleep(10000000);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue