Allowing users to specify their preferred language
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 08 Nov 2009 09:54:32 +0100
changeset 1460b889d9e4ee1
parent 145 ac9bd9be5263
child 147 37e67c923018
Allowing users to specify their preferred language
freemarkerdor/pom.xml
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UserInfo.java
freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle.properties
freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle_cs.properties
freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java
     1.1 --- a/freemarkerdor/pom.xml	Sat Nov 07 23:26:03 2009 +0100
     1.2 +++ b/freemarkerdor/pom.xml	Sun Nov 08 09:54:32 2009 +0100
     1.3 @@ -10,13 +10,13 @@
     1.4    <groupId>org.apidesign</groupId>
     1.5    <artifactId>freemarkerdor</artifactId>
     1.6    <name>freemarkerdor</name>
     1.7 -  <version>1.36</version>
     1.8 +  <version>1.40</version>
     1.9    <url>http://maven.apache.org</url>
    1.10    <dependencies>
    1.11      <dependency>
    1.12        <groupId>${project.groupId}</groupId>
    1.13        <artifactId>webidor</artifactId>
    1.14 -      <version>1.6</version>
    1.15 +      <version>[1.8,2.0)</version>
    1.16        <scope>test</scope>
    1.17      </dependency>
    1.18      <dependency>
    1.19 @@ -122,3 +122,4 @@
    1.20  
    1.21  
    1.22  
    1.23 +
     2.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sat Nov 07 23:26:03 2009 +0100
     2.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Nov 08 09:54:32 2009 +0100
     2.3 @@ -29,6 +29,7 @@
     2.4  import com.sun.jersey.api.client.Client;
     2.5  import com.sun.jersey.api.client.UniformInterfaceException;
     2.6  import com.sun.jersey.api.client.WebResource;
     2.7 +import com.sun.jersey.api.client.config.DefaultClientConfig;
     2.8  import com.sun.jersey.api.container.httpserver.HttpServerFactory;
     2.9  import com.sun.jersey.api.core.PackagesResourceConfig;
    2.10  import com.sun.jersey.api.core.ResourceConfig;
    2.11 @@ -88,7 +89,7 @@
    2.12  
    2.13      @Context
    2.14      private HttpHeaders headers;
    2.15 -    private String user;
    2.16 +    private UserInfo user;
    2.17      private String uuid;
    2.18  
    2.19      public UI() {
    2.20 @@ -98,15 +99,15 @@
    2.21          Cookie cookie = headers.getCookies().get("login");
    2.22          if (cookie != null) {
    2.23              String id = cookie.getValue();
    2.24 -            String us;
    2.25 +            UserInfo us;
    2.26              try {
    2.27 -                us = base.path("login").queryParam("id", id).
    2.28 -                    accept(MediaType.TEXT_PLAIN).get(String.class);
    2.29 -            } catch (UniformInterfaceException ex) {
    2.30 +                us = base.path("users").queryParam("loginID", id).
    2.31 +                    accept(MediaType.TEXT_XML).get(UserInfo.class);
    2.32 +            } catch (Exception ex) {
    2.33                  ex.printStackTrace();
    2.34 -                us = "";
    2.35 +                us = null;
    2.36              }
    2.37 -            if (us.length() > 0) {
    2.38 +            if (us != null && us.getId().length() > 0) {
    2.39                  user = us;
    2.40                  uuid = id;
    2.41                  return null;
    2.42 @@ -124,7 +125,7 @@
    2.43          uuid = base.path("login").queryParam("name", name).queryParam("password", password).
    2.44              accept(MediaType.TEXT_PLAIN).put(String.class);
    2.45          if (uuid != null) {
    2.46 -            user = name;
    2.47 +            user = new UserInfo(name);
    2.48              NewCookie nc = new NewCookie("login", uuid, null, null, null, 3600 * 24 * 7, false);
    2.49              return Response.ok().cookie(nc).entity(viewable("login.fmt", null)).build();
    2.50          } else {
    2.51 @@ -241,7 +242,7 @@
    2.52          }
    2.53          WebResource wr = base.path("games").path(id).
    2.54              queryParam("loginID", uuid).
    2.55 -            queryParam("player", user);
    2.56 +            queryParam("player", user.getId());
    2.57          try {
    2.58              if (type.equals("resign")) {
    2.59                  wr.queryParam("move", "RESIGN").put();
    2.60 @@ -274,7 +275,7 @@
    2.61          }
    2.62          WebResource wr = base.path("games").path(id).
    2.63              queryParam("loginID", uuid).
    2.64 -            queryParam("player", user).
    2.65 +            queryParam("player", user.getId()).
    2.66              queryParam("comment", comment);
    2.67          wr.put();
    2.68          
    2.69 @@ -293,7 +294,7 @@
    2.70              return Response.status(Response.Status.FORBIDDEN).entity(v).build();
    2.71          }
    2.72  
    2.73 -        if (user.equals(white) || user.equals(black)) {
    2.74 +        if (user.getId().equals(white) || user.getId().equals(black)) {
    2.75              Object obj =
    2.76                  base.path("games").
    2.77                  queryParam("loginID", uuid).
    2.78 @@ -302,7 +303,7 @@
    2.79              return Response.ok(welcomeImpl()).build();
    2.80          } else {
    2.81              return Response.status(Response.Status.NOT_FOUND).
    2.82 -                entity(welcomeImpl("message", "You (" + user + ") must be white or black!")).build();
    2.83 +                entity(welcomeImpl("message", "You (" + user.getId() + ") must be white or black!")).build();
    2.84          }
    2.85      }
    2.86  
    2.87 @@ -311,6 +312,36 @@
    2.88          return viewable("index.fmt", got, args);
    2.89      }
    2.90  
    2.91 +
    2.92 +    @GET
    2.93 +    @Path("options")
    2.94 +    public Response changeOptions(
    2.95 +        @QueryParam("email") String email,
    2.96 +        @QueryParam("language") String language
    2.97 +    ) {
    2.98 +        Viewable v = checkLogin();
    2.99 +        if (v != null) {
   2.100 +            return Response.status(Response.Status.FORBIDDEN).entity(v).build();
   2.101 +        }
   2.102 +
   2.103 +        if (email != null) {
   2.104 +            UserInfo ui = base.path("users/" + user.getId()).
   2.105 +                queryParam("loginID", uuid).
   2.106 +                queryParam("name", "email").
   2.107 +                queryParam("value", email).accept(MediaType.TEXT_XML).post(UserInfo.class);
   2.108 +        }
   2.109 +
   2.110 +        if (language != null) {
   2.111 +            UserInfo ui = base.path("users/" + user.getId()).
   2.112 +                queryParam("loginID", uuid).
   2.113 +                queryParam("name", "language").
   2.114 +                queryParam("value", language).
   2.115 +                accept(MediaType.TEXT_XML).post(UserInfo.class);
   2.116 +        }
   2.117 +
   2.118 +        return welcome(10);
   2.119 +    }
   2.120 +    
   2.121      //
   2.122      // start the server
   2.123      //
   2.124 @@ -322,6 +353,7 @@
   2.125          }
   2.126          String remoteAPI = args.length >= 2 ? args[1] : null;
   2.127  
   2.128 +        Locale.setDefault(Locale.ROOT);
   2.129  
   2.130          Callable<Void> r = startServers(port, remoteAPI);
   2.131  
   2.132 @@ -370,11 +402,22 @@
   2.133  
   2.134      private Viewable viewable(String page, Document doc, Object... more) {
   2.135          ResourceBundle rb = null;
   2.136 -        for (Locale l : headers.getAcceptableLanguages()) {
   2.137 -            try {
   2.138 -                rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
   2.139 -            } catch (MissingResourceException e) {
   2.140 -                // OK
   2.141 +        String lng = user == null ? null : user.getProperty("language"); // NOI18N
   2.142 +        if (lng != null) {
   2.143 +                try {
   2.144 +                    Locale l = new Locale(lng);
   2.145 +                    rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
   2.146 +                } catch (MissingResourceException e) {
   2.147 +                    // OK
   2.148 +                }
   2.149 +        }
   2.150 +        if (rb == null) {
   2.151 +            for (Locale l : headers.getAcceptableLanguages()) {
   2.152 +                try {
   2.153 +                    rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
   2.154 +                } catch (MissingResourceException e) {
   2.155 +                    // OK
   2.156 +                }
   2.157              }
   2.158          }
   2.159          if (rb == null) {
   2.160 @@ -383,7 +426,10 @@
   2.161  
   2.162          Map<String,Object> map = new HashMap<String,Object>();
   2.163          map.put("doc", doc);
   2.164 -        map.put("user", user);
   2.165 +        if (user != null) {
   2.166 +            map.put("user", user.getId());
   2.167 +            map.put("email", user.getProperty("email"));
   2.168 +        }
   2.169          map.put("bundle", rb);
   2.170          map.put("now", System.currentTimeMillis());
   2.171          map.put("version", version);
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UserInfo.java	Sun Nov 08 09:54:32 2009 +0100
     3.3 @@ -0,0 +1,86 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * The contents of this file are subject to the terms of either the GNU
     3.8 + * General Public License Version 2 only ("GPL") or the Common
     3.9 + * Development and Distribution License("CDDL") (collectively, the
    3.10 + * "License"). You may not use this file except in compliance with the
    3.11 + * License. You can obtain a copy of the License at
    3.12 + * http://www.netbeans.org/cddl-gplv2.html
    3.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.14 + * specific language governing permissions and limitations under the
    3.15 + * License.  When distributing the software, include this License Header
    3.16 + * Notice in each file and include the License file at
    3.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    3.18 + * particular file as subject to the "Classpath" exception as provided
    3.19 + * by Sun in the GPL Version 2 section of the License file that
    3.20 + * accompanied this code. If applicable, add the following below the
    3.21 + * License Header, with the fields enclosed by brackets [] replaced by
    3.22 + * your own identifying information:
    3.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.24 + *
    3.25 + * Contributor(s):
    3.26 + *
    3.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    3.28 + */
    3.29 +
    3.30 +package cz.xelfi.quoridor.freemarkerdor;
    3.31 +
    3.32 +import java.util.List;
    3.33 +import javax.xml.bind.annotation.XmlAccessType;
    3.34 +import javax.xml.bind.annotation.XmlAccessorType;
    3.35 +import javax.xml.bind.annotation.XmlAttribute;
    3.36 +import javax.xml.bind.annotation.XmlElement;
    3.37 +import javax.xml.bind.annotation.XmlRootElement;
    3.38 +import javax.xml.bind.annotation.XmlValue;
    3.39 +
    3.40 +/**
    3.41 + *
    3.42 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.43 + */
    3.44 +@XmlRootElement(name="user")
    3.45 +@XmlAccessorType(XmlAccessType.NONE)
    3.46 +public final class UserInfo extends Object {
    3.47 +    @XmlAttribute
    3.48 +    private String id;
    3.49 +    @XmlElement(name="property")
    3.50 +    private List<Property> properties;
    3.51 +
    3.52 +    UserInfo() {
    3.53 +    }
    3.54 +
    3.55 +    public UserInfo(String id) {
    3.56 +        this.id = id;
    3.57 +    }
    3.58 +
    3.59 +    public String getProperty(String name) {
    3.60 +        if (properties == null) {
    3.61 +            return null;
    3.62 +        }
    3.63 +        for (Property p : properties) {
    3.64 +            if (p.name.equals(name)) {
    3.65 +                return p.value;
    3.66 +            }
    3.67 +        }
    3.68 +        return null;
    3.69 +    }
    3.70 +
    3.71 +    public String getId() {
    3.72 +        return id;
    3.73 +    }
    3.74 +
    3.75 +    public static final class Property {
    3.76 +        @XmlAttribute
    3.77 +        private String name;
    3.78 +        @XmlValue
    3.79 +        private String value;
    3.80 +
    3.81 +        private Property() {
    3.82 +        }
    3.83 +
    3.84 +        Property(String name, String value) {
    3.85 +            this.name = name;
    3.86 +            this.value = value;
    3.87 +        }
    3.88 +    }
    3.89 +}
     4.1 --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle.properties	Sat Nov 07 23:26:03 2009 +0100
     4.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle.properties	Sun Nov 08 09:54:32 2009 +0100
     4.3 @@ -66,3 +66,13 @@
     4.4  LOGIN=Login!
     4.5  logged=You are logged in as {0}.
     4.6  home=Start!
     4.7 +
     4.8 +
     4.9 +OPTIONS=Options
    4.10 +EMAIL=Email:
    4.11 +CHANGE_EMAIL=Update!
    4.12 +LANGUAGE=Language:
    4.13 +ENGLISH=English
    4.14 +CZECH=\u010Cesky
    4.15 +CHANGE_LANGUAGE=Change!
    4.16 +
     5.1 --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle_cs.properties	Sat Nov 07 23:26:03 2009 +0100
     5.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle_cs.properties	Sun Nov 08 09:54:32 2009 +0100
     5.3 @@ -81,3 +81,10 @@
     5.4  LOGIN=P\u0159ihl\u00E1sit se
     5.5  logged=Jste p\u0159ihl\u00E1\u0161en jako {0}
     5.6  home=Za\u010D\u00EDt hr\u00E1t
     5.7 +
     5.8 +OPTIONS=Nastaven\u00ED
     5.9 +EMAIL=E-Kontakt:
    5.10 +CHANGE_EMAIL=Zm\u011Bnit!
    5.11 +LANGUAGE=Jazyk:
    5.12 +CHANGE_LANGUAGE=Zm\u011Bnit!
    5.13 +
     6.1 --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt	Sat Nov 07 23:26:03 2009 +0100
     6.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt	Sun Nov 08 09:54:32 2009 +0100
     6.3 @@ -164,6 +164,21 @@
     6.4          </p>
     6.5        </#if>
     6.6  
     6.7 +      <h5>${bundle.OPTIONS}</h5>
     6.8 +      
     6.9 +      <form action="/options">
    6.10 +            ${bundle.EMAIL} <input type="text" name="email" value='${email!""}' />
    6.11 +            <input type="submit" value="${bundle.CHANGE_EMAIL}" />
    6.12 +      </form>
    6.13 +      <form action="/options">
    6.14 +            ${bundle.LANGUAGE}
    6.15 +              <select name="language" id="column">
    6.16 +                  <option value="en">${bundle.ENGLISH}</option>
    6.17 +                  <option value="cs">${bundle.CZECH}</option>
    6.18 +              </select>
    6.19 +            <input type="submit" value="${bundle.CHANGE_LANGUAGE}" />
    6.20 +      </form>
    6.21 +
    6.22        <hr/>
    6.23        ${bundle("copyright", version)}
    6.24    </body>
     7.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java	Sat Nov 07 23:26:03 2009 +0100
     7.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java	Sun Nov 08 09:54:32 2009 +0100
     7.3 @@ -32,12 +32,8 @@
     7.4  import java.io.FileNotFoundException;
     7.5  import java.io.FileOutputStream;
     7.6  import java.io.IOException;
     7.7 -import java.util.ArrayList;
     7.8 -import java.util.Collections;
     7.9 -import java.util.List;
    7.10  import java.util.Properties;
    7.11  import java.util.logging.Logger;
    7.12 -import javax.ws.rs.DefaultValue;
    7.13  import javax.ws.rs.GET;
    7.14  import javax.ws.rs.POST;
    7.15  import javax.ws.rs.Path;