/*
 * The MIT License
 *
 * Copyright 2015 avm.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package jpt.app01;

import java.io.IOException;
import java.util.Optional;
import java.util.logging.Logger;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import jpt.app01.session.Session;
import jpt.app01.session.SessionFilter;
import jpt.app01.session.SessionRegistry;

/**
 *
 * @author avm
 */
class LoginHandler implements HttpHandler {
  private static final Logger LOG = Logger.getLogger(HttpHandler.class.getName());
  
  private final String redirectUrl;
  private final SessionRegistry sessionRegistry;

  public LoginHandler(SessionRegistry sessionRegistry, String redirectUrl) {
    this.redirectUrl = redirectUrl;
    this.sessionRegistry = sessionRegistry;
  }

  @Override
  public void handle(HttpExchange exchange) throws IOException {
    final Session session = sessionRegistry.create("JohnDoe");

    SessionFilter.setCookie(exchange.getResponseHeaders(), session);
    RedirectResponder.respond(exchange, redirectUrl, "home", Optional.of("Session " + session.getId() + " created"));
  }
  
}

@@ -1184,56 +1184,183 @@
 ava.
-util.Optional;%0Aimport java.util.logging.Logger;%0A
+io.InputStream;%0Aimport java.io.PrintStream;%0Aimport java.util.Optional;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0Aimport com.sun.net.httpserver.Headers;
 %0Aimp
@@ -2050,68 +2050,1732 @@
     
-final Session session = sessionRegistry.create(%22JohnDoe%22);%0A%0A
+if (!%22POST%22.equals(exchange.getRequestMethod())) %7B%0A      Headers responseHeaders = exchange.getResponseHeaders();%0A      responseHeaders.set(%22Content-Type%22, %22text/html%22);%0A      exchange.sendResponseHeaders(200, 0);%0A      if (%22HEAD%22.equals(exchange.getRequestMethod())) return;%0A      try (PrintStream out = new PrintStream(exchange.getResponseBody())) %7B%0A        HeaderResponse.send(out, Optional.empty(), %22Login form%22);%0A        out.println(%22%3CDIV%3E%22);%0A        out.println(%22Please log in:%3CBR%3E%22);%0A        out.println(%22%3Cform action='login' method='post'%3E%22);%0A        out.println(%22  User name: %3Cinput type='text' name='username'%3E%3Cbr%3E%22);%0A        out.println(%22  Password: %3Cinput type='password' name='password'%3E%3Cbr%3E%22);%0A        out.println(%22  %3Cinput type='submit' value='Submit'%3E%22);%0A        out.println(%22%3C/form%3E%22);%0A        out.println(%22%3C/DIV%3E%22);%0A      %7D catch (Exception e) %7B%0A        LOG.log(Level.SEVERE, %22Failed login form: %22, e);%0A        throw(e);%0A      %7D%0A    %7D else %7B%0A      final String body;%0A      try (final InputStream in = exchange.getRequestBody()) %7B%0A        StringBuilder bodyBuilder = new StringBuilder();%0A        int count;%0A        byte%5B%5D buf = new byte%5B2%3C%3C12%5D;%0A        while (0 %3C= (count = in.read(buf))) %7B%0A          bodyBuilder.append(new String(buf, 0, count));%0A        %7D%0A        body = bodyBuilder.toString();%0A      %7D catch (Exception e) %7B%0A        LOG.log(Level.SEVERE, %22Failed reading login query from body%22, e);%0A        throw(e);%0A      %7D%0A      final Optional%3CString%3E username = QueryParser.getFirstParameterValue(body, %22username%22);%0A      // Ignore password for now%0A      LOG.info(() -%3E String.format(%22User '%25s' authenticated%22, username.get()));%0A      final Session session = sessionRegistry.create(username.get());%0A%0A  
     
@@ -3839,16 +3839,18 @@
 ssion);%0A
+  
     Redi
@@ -3959,16 +3959,22 @@
 ted%22));%0A
+    %7D%0A
   %7D%0A  %0A%7D

add login form