freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
changeset 146 0b889d9e4ee1
parent 132 23055e2d6539
child 152 07e3bcb65c1d
     1.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Oct 25 14:42:33 2009 +0100
     1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Nov 08 09:54:32 2009 +0100
     1.3 @@ -29,6 +29,7 @@
     1.4  import com.sun.jersey.api.client.Client;
     1.5  import com.sun.jersey.api.client.UniformInterfaceException;
     1.6  import com.sun.jersey.api.client.WebResource;
     1.7 +import com.sun.jersey.api.client.config.DefaultClientConfig;
     1.8  import com.sun.jersey.api.container.httpserver.HttpServerFactory;
     1.9  import com.sun.jersey.api.core.PackagesResourceConfig;
    1.10  import com.sun.jersey.api.core.ResourceConfig;
    1.11 @@ -88,7 +89,7 @@
    1.12  
    1.13      @Context
    1.14      private HttpHeaders headers;
    1.15 -    private String user;
    1.16 +    private UserInfo user;
    1.17      private String uuid;
    1.18  
    1.19      public UI() {
    1.20 @@ -98,15 +99,15 @@
    1.21          Cookie cookie = headers.getCookies().get("login");
    1.22          if (cookie != null) {
    1.23              String id = cookie.getValue();
    1.24 -            String us;
    1.25 +            UserInfo us;
    1.26              try {
    1.27 -                us = base.path("login").queryParam("id", id).
    1.28 -                    accept(MediaType.TEXT_PLAIN).get(String.class);
    1.29 -            } catch (UniformInterfaceException ex) {
    1.30 +                us = base.path("users").queryParam("loginID", id).
    1.31 +                    accept(MediaType.TEXT_XML).get(UserInfo.class);
    1.32 +            } catch (Exception ex) {
    1.33                  ex.printStackTrace();
    1.34 -                us = "";
    1.35 +                us = null;
    1.36              }
    1.37 -            if (us.length() > 0) {
    1.38 +            if (us != null && us.getId().length() > 0) {
    1.39                  user = us;
    1.40                  uuid = id;
    1.41                  return null;
    1.42 @@ -124,7 +125,7 @@
    1.43          uuid = base.path("login").queryParam("name", name).queryParam("password", password).
    1.44              accept(MediaType.TEXT_PLAIN).put(String.class);
    1.45          if (uuid != null) {
    1.46 -            user = name;
    1.47 +            user = new UserInfo(name);
    1.48              NewCookie nc = new NewCookie("login", uuid, null, null, null, 3600 * 24 * 7, false);
    1.49              return Response.ok().cookie(nc).entity(viewable("login.fmt", null)).build();
    1.50          } else {
    1.51 @@ -241,7 +242,7 @@
    1.52          }
    1.53          WebResource wr = base.path("games").path(id).
    1.54              queryParam("loginID", uuid).
    1.55 -            queryParam("player", user);
    1.56 +            queryParam("player", user.getId());
    1.57          try {
    1.58              if (type.equals("resign")) {
    1.59                  wr.queryParam("move", "RESIGN").put();
    1.60 @@ -274,7 +275,7 @@
    1.61          }
    1.62          WebResource wr = base.path("games").path(id).
    1.63              queryParam("loginID", uuid).
    1.64 -            queryParam("player", user).
    1.65 +            queryParam("player", user.getId()).
    1.66              queryParam("comment", comment);
    1.67          wr.put();
    1.68          
    1.69 @@ -293,7 +294,7 @@
    1.70              return Response.status(Response.Status.FORBIDDEN).entity(v).build();
    1.71          }
    1.72  
    1.73 -        if (user.equals(white) || user.equals(black)) {
    1.74 +        if (user.getId().equals(white) || user.getId().equals(black)) {
    1.75              Object obj =
    1.76                  base.path("games").
    1.77                  queryParam("loginID", uuid).
    1.78 @@ -302,7 +303,7 @@
    1.79              return Response.ok(welcomeImpl()).build();
    1.80          } else {
    1.81              return Response.status(Response.Status.NOT_FOUND).
    1.82 -                entity(welcomeImpl("message", "You (" + user + ") must be white or black!")).build();
    1.83 +                entity(welcomeImpl("message", "You (" + user.getId() + ") must be white or black!")).build();
    1.84          }
    1.85      }
    1.86  
    1.87 @@ -311,6 +312,36 @@
    1.88          return viewable("index.fmt", got, args);
    1.89      }
    1.90  
    1.91 +
    1.92 +    @GET
    1.93 +    @Path("options")
    1.94 +    public Response changeOptions(
    1.95 +        @QueryParam("email") String email,
    1.96 +        @QueryParam("language") String language
    1.97 +    ) {
    1.98 +        Viewable v = checkLogin();
    1.99 +        if (v != null) {
   1.100 +            return Response.status(Response.Status.FORBIDDEN).entity(v).build();
   1.101 +        }
   1.102 +
   1.103 +        if (email != null) {
   1.104 +            UserInfo ui = base.path("users/" + user.getId()).
   1.105 +                queryParam("loginID", uuid).
   1.106 +                queryParam("name", "email").
   1.107 +                queryParam("value", email).accept(MediaType.TEXT_XML).post(UserInfo.class);
   1.108 +        }
   1.109 +
   1.110 +        if (language != null) {
   1.111 +            UserInfo ui = base.path("users/" + user.getId()).
   1.112 +                queryParam("loginID", uuid).
   1.113 +                queryParam("name", "language").
   1.114 +                queryParam("value", language).
   1.115 +                accept(MediaType.TEXT_XML).post(UserInfo.class);
   1.116 +        }
   1.117 +
   1.118 +        return welcome(10);
   1.119 +    }
   1.120 +    
   1.121      //
   1.122      // start the server
   1.123      //
   1.124 @@ -322,6 +353,7 @@
   1.125          }
   1.126          String remoteAPI = args.length >= 2 ? args[1] : null;
   1.127  
   1.128 +        Locale.setDefault(Locale.ROOT);
   1.129  
   1.130          Callable<Void> r = startServers(port, remoteAPI);
   1.131  
   1.132 @@ -370,11 +402,22 @@
   1.133  
   1.134      private Viewable viewable(String page, Document doc, Object... more) {
   1.135          ResourceBundle rb = null;
   1.136 -        for (Locale l : headers.getAcceptableLanguages()) {
   1.137 -            try {
   1.138 -                rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
   1.139 -            } catch (MissingResourceException e) {
   1.140 -                // OK
   1.141 +        String lng = user == null ? null : user.getProperty("language"); // NOI18N
   1.142 +        if (lng != null) {
   1.143 +                try {
   1.144 +                    Locale l = new Locale(lng);
   1.145 +                    rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
   1.146 +                } catch (MissingResourceException e) {
   1.147 +                    // OK
   1.148 +                }
   1.149 +        }
   1.150 +        if (rb == null) {
   1.151 +            for (Locale l : headers.getAcceptableLanguages()) {
   1.152 +                try {
   1.153 +                    rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
   1.154 +                } catch (MissingResourceException e) {
   1.155 +                    // OK
   1.156 +                }
   1.157              }
   1.158          }
   1.159          if (rb == null) {
   1.160 @@ -383,7 +426,10 @@
   1.161  
   1.162          Map<String,Object> map = new HashMap<String,Object>();
   1.163          map.put("doc", doc);
   1.164 -        map.put("user", user);
   1.165 +        if (user != null) {
   1.166 +            map.put("user", user.getId());
   1.167 +            map.put("email", user.getProperty("email"));
   1.168 +        }
   1.169          map.put("bundle", rb);
   1.170          map.put("now", System.currentTimeMillis());
   1.171          map.put("version", version);