jtulach@41
|
1 |
/*
|
jtulach@41
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jtulach@41
|
3 |
*
|
jtulach@41
|
4 |
* The contents of this file are subject to the terms of either the GNU
|
jtulach@41
|
5 |
* General Public License Version 2 only ("GPL") or the Common
|
jtulach@41
|
6 |
* Development and Distribution License("CDDL") (collectively, the
|
jtulach@41
|
7 |
* "License"). You may not use this file except in compliance with the
|
jtulach@41
|
8 |
* License. You can obtain a copy of the License at
|
jtulach@41
|
9 |
* http://www.netbeans.org/cddl-gplv2.html
|
jtulach@41
|
10 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jtulach@41
|
11 |
* specific language governing permissions and limitations under the
|
jtulach@41
|
12 |
* License. When distributing the software, include this License Header
|
jtulach@41
|
13 |
* Notice in each file and include the License file at
|
jtulach@41
|
14 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
|
jtulach@41
|
15 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@41
|
16 |
* by Sun in the GPL Version 2 section of the License file that
|
jtulach@41
|
17 |
* accompanied this code. If applicable, add the following below the
|
jtulach@41
|
18 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jtulach@41
|
19 |
* your own identifying information:
|
jtulach@41
|
20 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jtulach@41
|
21 |
*
|
jtulach@41
|
22 |
* Contributor(s):
|
jtulach@41
|
23 |
*
|
jtulach@41
|
24 |
* Portions Copyrighted 2009 Jaroslav Tulach
|
jtulach@41
|
25 |
*/
|
jtulach@41
|
26 |
|
jtulach@41
|
27 |
package cz.xelfi.quoridor.freemarkerdor;
|
jtulach@41
|
28 |
|
jtulach@41
|
29 |
import com.sun.jersey.api.client.Client;
|
jtulach@41
|
30 |
import com.sun.jersey.api.client.WebResource;
|
jtulach@41
|
31 |
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
|
jtulach@41
|
32 |
import com.sun.jersey.api.core.PackagesResourceConfig;
|
jtulach@41
|
33 |
import com.sun.jersey.api.core.ResourceConfig;
|
jtulach@41
|
34 |
import com.sun.jersey.api.view.Viewable;
|
jtulach@41
|
35 |
import com.sun.net.httpserver.HttpServer;
|
jtulach@41
|
36 |
import cz.xelfi.quoridor.webidor.resources.Quoridor;
|
jaroslav@46
|
37 |
import java.io.File;
|
jaroslav@46
|
38 |
import java.io.FileInputStream;
|
jtulach@41
|
39 |
import java.io.IOException;
|
jtulach@41
|
40 |
import java.net.URI;
|
jaroslav@46
|
41 |
import java.util.Properties;
|
jaroslav@50
|
42 |
import java.util.concurrent.Callable;
|
jtulach@43
|
43 |
import javax.ws.rs.DefaultValue;
|
jaroslav@47
|
44 |
import javax.ws.rs.FormParam;
|
jtulach@41
|
45 |
import javax.ws.rs.GET;
|
jaroslav@46
|
46 |
import javax.ws.rs.POST;
|
jtulach@41
|
47 |
import javax.ws.rs.Path;
|
jtulach@41
|
48 |
import javax.ws.rs.PathParam;
|
jaroslav@46
|
49 |
import javax.ws.rs.Produces;
|
jtulach@42
|
50 |
import javax.ws.rs.QueryParam;
|
jtulach@42
|
51 |
import javax.ws.rs.core.Context;
|
jtulach@42
|
52 |
import javax.ws.rs.core.HttpHeaders;
|
jtulach@41
|
53 |
import javax.ws.rs.core.MediaType;
|
jaroslav@46
|
54 |
import javax.ws.rs.core.NewCookie;
|
jaroslav@46
|
55 |
import javax.ws.rs.core.Response;
|
jaroslav@48
|
56 |
import org.w3c.dom.Document;
|
jtulach@41
|
57 |
|
jtulach@41
|
58 |
/**
|
jtulach@41
|
59 |
*
|
jtulach@41
|
60 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@41
|
61 |
*/
|
jtulach@41
|
62 |
@Path("/")
|
jtulach@41
|
63 |
public final class UI {
|
jtulach@41
|
64 |
private static WebResource base;
|
jtulach@42
|
65 |
|
jtulach@42
|
66 |
@Context
|
jtulach@42
|
67 |
private HttpHeaders headers;
|
jaroslav@46
|
68 |
private String user;
|
jtulach@42
|
69 |
|
jtulach@41
|
70 |
public UI() {
|
jtulach@41
|
71 |
}
|
jtulach@41
|
72 |
|
jaroslav@46
|
73 |
private Viewable checkLogin() {
|
jaroslav@46
|
74 |
if (headers.getCookies().containsKey("login")) {
|
jaroslav@46
|
75 |
user = headers.getCookies().get("login").getValue();
|
jaroslav@46
|
76 |
return null;
|
jaroslav@46
|
77 |
}
|
jaroslav@46
|
78 |
return new Viewable("login.fmt", null);
|
jaroslav@46
|
79 |
}
|
jaroslav@46
|
80 |
|
jaroslav@46
|
81 |
@POST
|
jaroslav@46
|
82 |
@Path("login")
|
jaroslav@46
|
83 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@46
|
84 |
public Response login(
|
jaroslav@47
|
85 |
@FormParam("name") String name, @FormParam("password") String password
|
jaroslav@46
|
86 |
) throws Exception {
|
jaroslav@46
|
87 |
File f = new File(new File(new File(System.getProperty("user.home")), ".quoridor"), "passwd");
|
jaroslav@46
|
88 |
Properties p = new Properties();
|
jtulach@54
|
89 |
try {
|
jtulach@54
|
90 |
p.load(new FileInputStream(f));
|
jtulach@54
|
91 |
} catch (IOException ex) {
|
jtulach@54
|
92 |
ex.printStackTrace();
|
jtulach@54
|
93 |
}
|
jaroslav@46
|
94 |
if (name != null && password.equals(p.getProperty(name))) {
|
jaroslav@46
|
95 |
return Response.seeOther(new URI("/")).cookie(new NewCookie("login", name)).entity(welcomeImpl()).build();
|
jaroslav@46
|
96 |
} else {
|
jaroslav@46
|
97 |
Viewable v = new Viewable("login.fmt", "Invalid name or password: " + name);
|
jaroslav@46
|
98 |
return Response.status(1).entity(v).build();
|
jaroslav@46
|
99 |
}
|
jaroslav@46
|
100 |
}
|
jaroslav@46
|
101 |
|
jtulach@41
|
102 |
@GET
|
jaroslav@46
|
103 |
@Produces(MediaType.TEXT_HTML)
|
jtulach@54
|
104 |
public Viewable welcome() {
|
jaroslav@46
|
105 |
Viewable v = checkLogin();
|
jaroslav@46
|
106 |
if (v != null) {
|
jaroslav@46
|
107 |
return v;
|
jtulach@42
|
108 |
}
|
jaroslav@46
|
109 |
return welcomeImpl();
|
jtulach@41
|
110 |
}
|
jtulach@41
|
111 |
|
jtulach@41
|
112 |
@GET
|
jtulach@43
|
113 |
@Path("games/{id}/")
|
jaroslav@46
|
114 |
@Produces(MediaType.TEXT_HTML)
|
jtulach@54
|
115 |
public Viewable board(@PathParam("id") String id) {
|
jaroslav@46
|
116 |
Viewable v = checkLogin();
|
jaroslav@46
|
117 |
if (v != null) {
|
jaroslav@46
|
118 |
return v;
|
jaroslav@46
|
119 |
}
|
jtulach@54
|
120 |
Document doc = base.path("games").path(id).accept(MediaType.TEXT_XML).get(Document.class);
|
jtulach@54
|
121 |
return new Viewable("game.fmt", doc);
|
jtulach@42
|
122 |
}
|
jtulach@41
|
123 |
|
jtulach@42
|
124 |
@GET
|
jtulach@43
|
125 |
@Path("games/{id}/move")
|
jaroslav@46
|
126 |
@Produces(MediaType.TEXT_HTML)
|
jtulach@43
|
127 |
public Viewable move(
|
jtulach@43
|
128 |
@PathParam("id") String id,
|
jtulach@43
|
129 |
@QueryParam("type") String type,
|
jtulach@43
|
130 |
@QueryParam("direction") String direction,
|
jtulach@43
|
131 |
@QueryParam("direction-next") @DefaultValue("") String directionNext,
|
jtulach@43
|
132 |
@QueryParam("column") @DefaultValue("") String column,
|
jtulach@43
|
133 |
@QueryParam("row") @DefaultValue("") String row
|
jtulach@54
|
134 |
) {
|
jaroslav@46
|
135 |
Viewable v = checkLogin();
|
jaroslav@46
|
136 |
if (v != null) {
|
jaroslav@46
|
137 |
return v;
|
jaroslav@46
|
138 |
}
|
jaroslav@46
|
139 |
WebResource wr = base.path("games").path(id).queryParam("player", user);
|
jtulach@43
|
140 |
if (type.equals("fence")) {
|
jaroslav@46
|
141 |
wr.queryParam("move", direction.charAt(0) + column + row).put();
|
jtulach@43
|
142 |
return board(id);
|
jtulach@43
|
143 |
}
|
jtulach@43
|
144 |
if (type.equals("move")) {
|
jaroslav@46
|
145 |
wr.queryParam("move", direction + directionNext).put();
|
jtulach@43
|
146 |
return board(id);
|
jtulach@43
|
147 |
}
|
jtulach@43
|
148 |
return board(id);
|
jtulach@43
|
149 |
}
|
jtulach@43
|
150 |
|
jtulach@43
|
151 |
@GET
|
jtulach@42
|
152 |
@Path("games/create")
|
jaroslav@46
|
153 |
@Produces(MediaType.TEXT_HTML)
|
jtulach@42
|
154 |
public Viewable create(
|
jtulach@42
|
155 |
@QueryParam("white") String white,
|
jtulach@42
|
156 |
@QueryParam("black") String black
|
jtulach@54
|
157 |
) {
|
jaroslav@46
|
158 |
Viewable v = checkLogin();
|
jaroslav@46
|
159 |
if (v != null) {
|
jaroslav@46
|
160 |
return v;
|
jaroslav@46
|
161 |
}
|
jtulach@54
|
162 |
Object obj =
|
jtulach@42
|
163 |
base.path("games").queryParam("white", white).
|
jtulach@54
|
164 |
queryParam("black", black).post(Document.class);
|
jtulach@54
|
165 |
return welcome();
|
jtulach@41
|
166 |
}
|
jtulach@41
|
167 |
|
jtulach@54
|
168 |
private Viewable welcomeImpl() {
|
jtulach@54
|
169 |
final Object got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class);
|
jtulach@54
|
170 |
return new Viewable("index.fmt", got);
|
jtulach@41
|
171 |
}
|
jtulach@41
|
172 |
|
jtulach@41
|
173 |
//
|
jtulach@41
|
174 |
// start the server
|
jtulach@41
|
175 |
//
|
jtulach@41
|
176 |
|
jtulach@41
|
177 |
public static void main(String[] args) throws Exception {
|
jaroslav@50
|
178 |
Callable<Void> r = startServers();
|
jaroslav@50
|
179 |
|
jaroslav@50
|
180 |
System.in.read();
|
jaroslav@50
|
181 |
r.call();
|
jaroslav@50
|
182 |
System.exit(0);
|
jaroslav@50
|
183 |
}
|
jaroslav@50
|
184 |
|
jaroslav@50
|
185 |
static Callable<Void> startServers() throws Exception {
|
jaroslav@50
|
186 |
final HttpServer api = Quoridor.start(9998);
|
jtulach@41
|
187 |
Client client = new Client();
|
jtulach@41
|
188 |
base = client.resource(new URI("http://localhost:9998/api/"));
|
jtulach@41
|
189 |
|
jaroslav@50
|
190 |
final HttpServer s = start(9997);
|
jtulach@41
|
191 |
System.out.println(
|
jtulach@41
|
192 |
"Quoridor started at port 9997\n" + "Hit enter to stop it..."
|
jtulach@41
|
193 |
);
|
jaroslav@50
|
194 |
|
jaroslav@50
|
195 |
return new Callable<Void>() {
|
jaroslav@50
|
196 |
public Void call() throws Exception {
|
jaroslav@50
|
197 |
s.stop(0);
|
jaroslav@50
|
198 |
api.stop(0);
|
jaroslav@50
|
199 |
return null;
|
jaroslav@50
|
200 |
}
|
jaroslav@50
|
201 |
};
|
jtulach@41
|
202 |
}
|
jtulach@41
|
203 |
|
jtulach@41
|
204 |
static HttpServer start(int port) throws IOException {
|
jtulach@41
|
205 |
final String baseUri = "http://localhost:" + port + "/";
|
jtulach@41
|
206 |
ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.freemarkerdor");
|
jtulach@41
|
207 |
HttpServer server = HttpServerFactory.create(baseUri, rc);
|
jtulach@41
|
208 |
server.start();
|
jtulach@41
|
209 |
return server;
|
jtulach@41
|
210 |
}
|
jtulach@41
|
211 |
|
jtulach@41
|
212 |
}
|