# HG changeset patch
# User Jaroslav Tulach
# Date 1257670472 -3600
# Node ID 0b889d9e4ee17fb8881c8651288a8616d70ef689
# Parent ac9bd9be52630d4c47287c3282c82ef6cbf2a3bf
Allowing users to specify their preferred language
diff -r ac9bd9be5263 -r 0b889d9e4ee1 freemarkerdor/pom.xml
--- a/freemarkerdor/pom.xml Sat Nov 07 23:26:03 2009 +0100
+++ b/freemarkerdor/pom.xml Sun Nov 08 09:54:32 2009 +0100
@@ -10,13 +10,13 @@
org.apidesign
freemarkerdor
freemarkerdor
- 1.36
+ 1.40
http://maven.apache.org
${project.groupId}
webidor
- 1.6
+ [1.8,2.0)
test
@@ -122,3 +122,4 @@
+
diff -r ac9bd9be5263 -r 0b889d9e4ee1 freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
--- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Sat Nov 07 23:26:03 2009 +0100
+++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Sun Nov 08 09:54:32 2009 +0100
@@ -29,6 +29,7 @@
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
@@ -88,7 +89,7 @@
@Context
private HttpHeaders headers;
- private String user;
+ private UserInfo user;
private String uuid;
public UI() {
@@ -98,15 +99,15 @@
Cookie cookie = headers.getCookies().get("login");
if (cookie != null) {
String id = cookie.getValue();
- String us;
+ UserInfo us;
try {
- us = base.path("login").queryParam("id", id).
- accept(MediaType.TEXT_PLAIN).get(String.class);
- } catch (UniformInterfaceException ex) {
+ us = base.path("users").queryParam("loginID", id).
+ accept(MediaType.TEXT_XML).get(UserInfo.class);
+ } catch (Exception ex) {
ex.printStackTrace();
- us = "";
+ us = null;
}
- if (us.length() > 0) {
+ if (us != null && us.getId().length() > 0) {
user = us;
uuid = id;
return null;
@@ -124,7 +125,7 @@
uuid = base.path("login").queryParam("name", name).queryParam("password", password).
accept(MediaType.TEXT_PLAIN).put(String.class);
if (uuid != null) {
- user = name;
+ user = new UserInfo(name);
NewCookie nc = new NewCookie("login", uuid, null, null, null, 3600 * 24 * 7, false);
return Response.ok().cookie(nc).entity(viewable("login.fmt", null)).build();
} else {
@@ -241,7 +242,7 @@
}
WebResource wr = base.path("games").path(id).
queryParam("loginID", uuid).
- queryParam("player", user);
+ queryParam("player", user.getId());
try {
if (type.equals("resign")) {
wr.queryParam("move", "RESIGN").put();
@@ -274,7 +275,7 @@
}
WebResource wr = base.path("games").path(id).
queryParam("loginID", uuid).
- queryParam("player", user).
+ queryParam("player", user.getId()).
queryParam("comment", comment);
wr.put();
@@ -293,7 +294,7 @@
return Response.status(Response.Status.FORBIDDEN).entity(v).build();
}
- if (user.equals(white) || user.equals(black)) {
+ if (user.getId().equals(white) || user.getId().equals(black)) {
Object obj =
base.path("games").
queryParam("loginID", uuid).
@@ -302,7 +303,7 @@
return Response.ok(welcomeImpl()).build();
} else {
return Response.status(Response.Status.NOT_FOUND).
- entity(welcomeImpl("message", "You (" + user + ") must be white or black!")).build();
+ entity(welcomeImpl("message", "You (" + user.getId() + ") must be white or black!")).build();
}
}
@@ -311,6 +312,36 @@
return viewable("index.fmt", got, args);
}
+
+ @GET
+ @Path("options")
+ public Response changeOptions(
+ @QueryParam("email") String email,
+ @QueryParam("language") String language
+ ) {
+ Viewable v = checkLogin();
+ if (v != null) {
+ return Response.status(Response.Status.FORBIDDEN).entity(v).build();
+ }
+
+ if (email != null) {
+ UserInfo ui = base.path("users/" + user.getId()).
+ queryParam("loginID", uuid).
+ queryParam("name", "email").
+ queryParam("value", email).accept(MediaType.TEXT_XML).post(UserInfo.class);
+ }
+
+ if (language != null) {
+ UserInfo ui = base.path("users/" + user.getId()).
+ queryParam("loginID", uuid).
+ queryParam("name", "language").
+ queryParam("value", language).
+ accept(MediaType.TEXT_XML).post(UserInfo.class);
+ }
+
+ return welcome(10);
+ }
+
//
// start the server
//
@@ -322,6 +353,7 @@
}
String remoteAPI = args.length >= 2 ? args[1] : null;
+ Locale.setDefault(Locale.ROOT);
Callable r = startServers(port, remoteAPI);
@@ -370,11 +402,22 @@
private Viewable viewable(String page, Document doc, Object... more) {
ResourceBundle rb = null;
- for (Locale l : headers.getAcceptableLanguages()) {
- try {
- rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
- } catch (MissingResourceException e) {
- // OK
+ String lng = user == null ? null : user.getProperty("language"); // NOI18N
+ if (lng != null) {
+ try {
+ Locale l = new Locale(lng);
+ rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
+ } catch (MissingResourceException e) {
+ // OK
+ }
+ }
+ if (rb == null) {
+ for (Locale l : headers.getAcceptableLanguages()) {
+ try {
+ rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
+ } catch (MissingResourceException e) {
+ // OK
+ }
}
}
if (rb == null) {
@@ -383,7 +426,10 @@
Map map = new HashMap();
map.put("doc", doc);
- map.put("user", user);
+ if (user != null) {
+ map.put("user", user.getId());
+ map.put("email", user.getProperty("email"));
+ }
map.put("bundle", rb);
map.put("now", System.currentTimeMillis());
map.put("version", version);
diff -r ac9bd9be5263 -r 0b889d9e4ee1 freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UserInfo.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UserInfo.java Sun Nov 08 09:54:32 2009 +0100
@@ -0,0 +1,86 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common
+ * Development and Distribution License("CDDL") (collectively, the
+ * "License"). You may not use this file except in compliance with the
+ * License. You can obtain a copy of the License at
+ * http://www.netbeans.org/cddl-gplv2.html
+ * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
+ * specific language governing permissions and limitations under the
+ * License. When distributing the software, include this License Header
+ * Notice in each file and include the License file at
+ * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the GPL Version 2 section of the License file that
+ * accompanied this code. If applicable, add the following below the
+ * License Header, with the fields enclosed by brackets [] replaced by
+ * your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * Contributor(s):
+ *
+ * Portions Copyrighted 2009 Jaroslav Tulach
+ */
+
+package cz.xelfi.quoridor.freemarkerdor;
+
+import java.util.List;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlValue;
+
+/**
+ *
+ * @author Jaroslav Tulach
+ */
+@XmlRootElement(name="user")
+@XmlAccessorType(XmlAccessType.NONE)
+public final class UserInfo extends Object {
+ @XmlAttribute
+ private String id;
+ @XmlElement(name="property")
+ private List properties;
+
+ UserInfo() {
+ }
+
+ public UserInfo(String id) {
+ this.id = id;
+ }
+
+ public String getProperty(String name) {
+ if (properties == null) {
+ return null;
+ }
+ for (Property p : properties) {
+ if (p.name.equals(name)) {
+ return p.value;
+ }
+ }
+ return null;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public static final class Property {
+ @XmlAttribute
+ private String name;
+ @XmlValue
+ private String value;
+
+ private Property() {
+ }
+
+ Property(String name, String value) {
+ this.name = name;
+ this.value = value;
+ }
+ }
+}
diff -r ac9bd9be5263 -r 0b889d9e4ee1 freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle.properties
--- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle.properties Sat Nov 07 23:26:03 2009 +0100
+++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle.properties Sun Nov 08 09:54:32 2009 +0100
@@ -66,3 +66,13 @@
LOGIN=Login!
logged=You are logged in as {0}.
home=Start!
+
+
+OPTIONS=Options
+EMAIL=Email:
+CHANGE_EMAIL=Update!
+LANGUAGE=Language:
+ENGLISH=English
+CZECH=\u010Cesky
+CHANGE_LANGUAGE=Change!
+
diff -r ac9bd9be5263 -r 0b889d9e4ee1 freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle_cs.properties
--- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle_cs.properties Sat Nov 07 23:26:03 2009 +0100
+++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle_cs.properties Sun Nov 08 09:54:32 2009 +0100
@@ -81,3 +81,10 @@
LOGIN=P\u0159ihl\u00E1sit se
logged=Jste p\u0159ihl\u00E1\u0161en jako {0}
home=Za\u010D\u00EDt hr\u00E1t
+
+OPTIONS=Nastaven\u00ED
+EMAIL=E-Kontakt:
+CHANGE_EMAIL=Zm\u011Bnit!
+LANGUAGE=Jazyk:
+CHANGE_LANGUAGE=Zm\u011Bnit!
+
diff -r ac9bd9be5263 -r 0b889d9e4ee1 freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt
--- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt Sat Nov 07 23:26:03 2009 +0100
+++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt Sun Nov 08 09:54:32 2009 +0100
@@ -164,6 +164,21 @@
#if>
+ ${bundle.OPTIONS}
+
+
+
+
${bundle("copyright", version)}