package au.com.codeka.warworlds.server.account;

import com.google.common.base.Strings;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import au.com.codeka.warworlds.common.Log;
import au.com.codeka.warworlds.common.proto.Account;
import au.com.codeka.warworlds.common.proto.Empire;
import au.com.codeka.warworlds.common.proto.LoginRequest;
import au.com.codeka.warworlds.common.proto.LoginResponse;
import au.com.codeka.warworlds.server.ProtobufHttpServlet;
import au.com.codeka.warworlds.server.net.ServerSocketManager;
import au.com.codeka.warworlds.server.store.DataStore;
import au.com.codeka.warworlds.server.world.EmpireManager;
import au.com.codeka.warworlds.server.world.WatchableObject;

/**
 * This servlet is posted to in order to "log in". You'll get a pointer to the socket to connect
 * to for the actual main game connection.
 */
public class LoginServlet extends ProtobufHttpServlet {
  private final Log log = new Log("LoginServlet");

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    LoginRequest req = LoginRequest.ADAPTER.decode(request.getInputStream());
    if (Strings.isNullOrEmpty(req.cookie)) {
      log.warning("No cookie in request, not connected.");
      response.setStatus(403);
      return;
    }

    log.info("Login request received, cookie=%s", req.cookie);
    Account account = DataStore.i.accounts().get(req.cookie);
    if (account == null) {
      log.warning("No account for cookie, not connecting: %s", req.cookie);
      response.setStatus(401);
      return;
    }
    WatchableObject<Empire> empire = EmpireManager.i.getEmpire(account.empire_id);

    // Tell the server socket to expect a connection from this client.
    ServerSocketManager.i.addPendingConnection(account, empire, null /* encryptionKey */);

    writeProtobuf(response,
        new LoginResponse.Builder()
            .port(8081)
            .empire(empire.get())
            .build());
  }
}

@@ -1794,16 +1794,155 @@
 ire_id);
+%0A    if (empire == null) %7B%0A      log.warning(%22No empire with ID %25d%22, account.empire_id);%0A      response.setStatus(404);%0A      return;%0A    %7D
 %0A%0A    //

Handle null empire (though shouldn't happen in real life)