Guest User

Untitled

a guest
Mar 12th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1.     static class CookiesHandler implements HttpHandler {
  2.         public void handle(HttpExchange exchange) throws IOException {
  3.             String response = "Hello World!";
  4.  
  5.             exchange.getResponseHeaders().set("Set-cookie", "ciaseczko2=testoweCiasteczko3; domain=localhost; path=/echo/");
  6.            
  7.             exchange.getResponseHeaders().set("Content-Type", "text/plain; charset=UTF-8");
  8.            
  9.             String jsonString = JsonWriter.objectToJson(exchange.getResponseHeaders());
  10.             response = JsonWriter.formatJson(jsonString);
  11.            
  12.             exchange.sendResponseHeaders(200, response.length());
  13.             OutputStream os = exchange.getResponseBody();
  14.             os.write(response.getBytes());
  15.             os.close();
  16.         }
  17.     }
  18.  
  19. static class AuthHandler implements HttpHandler {
  20.         public void handle(HttpExchange exchange) throws IOException {
  21.             String response = "Hello World!";
  22.            
  23.             String user = "u";
  24.             String pass = "p";
  25.             String userAndPass = user + ":" + pass;
  26.                        
  27.             String auth = exchange.getRequestHeaders().getFirst("Authorization");
  28.            
  29.             if(auth==null){
  30.                 notLogin(exchange,response);
  31.             } else {
  32.                 if(auth.startsWith("Basic ")){
  33.                     String creds = auth.substring(6);
  34.                     byte[] decoded = javax.xml.bind.DatatypeConverter.parseBase64Binary(creds);
  35.                     String responseUserAndPassword = new String(decoded, "UTF-8");
  36.                     if(responseUserAndPassword.equals(userAndPass)){
  37.                         response = "zalogowany";
  38.                         exchange.getResponseHeaders().set("Content-Type", "text/plain; charset=utf-8");
  39.                         exchange.sendResponseHeaders(200, response.length());
  40.                         OutputStream os = exchange.getResponseBody();
  41.                         os.write(response.getBytes());
  42.                         os.close();
  43.                     }else{
  44.                         notLogin(exchange,response);
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.        
  50.         public void notLogin(HttpExchange exchange, String response) throws IOException{
  51.             exchange.getResponseHeaders().set("WWW-Authenticate", "Basic");
  52.             exchange.sendResponseHeaders(401, 0);
  53.             OutputStream os = exchange.getResponseBody();
  54.             os.write(response.getBytes());
  55.             os.close();
  56.         }
  57.     }
  58.  
  59. static class Auth2Handler implements HttpHandler {
  60.         public void handle(HttpExchange exchange) throws IOException {
  61.             String response = "Hello World!";
  62.  
  63.             response = "Hello " + exchange.getPrincipal().getUsername();
  64.            
  65.             exchange.getResponseHeaders().set("Content-Type", "text/plain; charset=UTF-8");
  66.             exchange.sendResponseHeaders(200, response.length());
  67.             OutputStream os = exchange.getResponseBody();
  68.             os.write(response.getBytes());
  69.             os.close();
  70.         }
  71.     }
  72.  
  73. main(){
  74.     ...
  75.     HttpContext auth2 = server.createContext("/auth2/", new Auth2Handler());
  76.         auth2.setAuthenticator(new BasicAuthenticator("user:a, password:p"){
  77.             @Override
  78.             public boolean checkCredentials(String user, String pwd) {
  79.                 return user.equals("a") && pwd.equals("p");
  80.             }
  81.         });
  82. }
Add Comment
Please, Sign In to add comment