jtulach@35
|
1 |
/*
|
jtulach@35
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jtulach@35
|
3 |
*
|
jtulach@35
|
4 |
* The contents of this file are subject to the terms of either the GNU
|
jtulach@35
|
5 |
* General Public License Version 2 only ("GPL") or the Common
|
jtulach@35
|
6 |
* Development and Distribution License("CDDL") (collectively, the
|
jtulach@35
|
7 |
* "License"). You may not use this file except in compliance with the
|
jtulach@35
|
8 |
* License. You can obtain a copy of the License at
|
jtulach@35
|
9 |
* http://www.netbeans.org/cddl-gplv2.html
|
jtulach@35
|
10 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jtulach@35
|
11 |
* specific language governing permissions and limitations under the
|
jtulach@35
|
12 |
* License. When distributing the software, include this License Header
|
jtulach@35
|
13 |
* Notice in each file and include the License file at
|
jtulach@35
|
14 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
|
jtulach@35
|
15 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@35
|
16 |
* by Sun in the GPL Version 2 section of the License file that
|
jtulach@35
|
17 |
* accompanied this code. If applicable, add the following below the
|
jtulach@35
|
18 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jtulach@35
|
19 |
* your own identifying information:
|
jtulach@35
|
20 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jtulach@35
|
21 |
*
|
jtulach@35
|
22 |
* Contributor(s):
|
jtulach@35
|
23 |
*
|
jtulach@35
|
24 |
* Portions Copyrighted 2009 Jaroslav Tulach
|
jtulach@35
|
25 |
*/
|
jtulach@35
|
26 |
|
jtulach@35
|
27 |
package cz.xelfi.quoridor.webidor.resources;
|
jtulach@35
|
28 |
|
jtulach@35
|
29 |
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
|
jtulach@35
|
30 |
import com.sun.jersey.api.core.PackagesResourceConfig;
|
jtulach@35
|
31 |
import com.sun.jersey.api.core.ResourceConfig;
|
jtulach@35
|
32 |
import cz.xelfi.quoridor.IllegalPositionException;
|
jtulach@35
|
33 |
import cz.xelfi.quoridor.webidor.*;
|
jtulach@35
|
34 |
import com.sun.jersey.spi.resource.Singleton;
|
jtulach@35
|
35 |
import com.sun.net.httpserver.HttpServer;
|
jtulach@35
|
36 |
import cz.xelfi.quoridor.Move;
|
jtulach@35
|
37 |
import java.io.IOException;
|
jtulach@35
|
38 |
import java.util.ArrayList;
|
jtulach@35
|
39 |
import java.util.HashMap;
|
jtulach@35
|
40 |
import java.util.List;
|
jtulach@35
|
41 |
import java.util.Map;
|
jtulach@35
|
42 |
import javax.ws.rs.GET;
|
jtulach@35
|
43 |
import javax.ws.rs.POST;
|
jtulach@35
|
44 |
import javax.ws.rs.PUT;
|
jtulach@35
|
45 |
import javax.ws.rs.Path;
|
jtulach@35
|
46 |
import javax.ws.rs.PathParam;
|
jtulach@35
|
47 |
import javax.ws.rs.Produces;
|
jtulach@35
|
48 |
import javax.ws.rs.QueryParam;
|
jtulach@35
|
49 |
import javax.ws.rs.core.MediaType;
|
jtulach@35
|
50 |
|
jtulach@35
|
51 |
/**
|
jtulach@35
|
52 |
*
|
jtulach@35
|
53 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@35
|
54 |
*/
|
jtulach@35
|
55 |
@Path("/")
|
jtulach@35
|
56 |
@Singleton
|
jtulach@35
|
57 |
public final class Quoridor {
|
jtulach@35
|
58 |
private List<Game> games = new ArrayList<Game>();
|
jtulach@35
|
59 |
|
jtulach@35
|
60 |
@POST
|
jtulach@35
|
61 |
@Path("games")
|
jtulach@35
|
62 |
public Game createGame(@QueryParam("white") String user1, @QueryParam("black") String user2) {
|
jtulach@35
|
63 |
Game g = new Game(user1, user2);
|
jtulach@35
|
64 |
games.add(g);
|
jtulach@35
|
65 |
return g;
|
jtulach@35
|
66 |
}
|
jtulach@35
|
67 |
|
jtulach@35
|
68 |
@PUT
|
jtulach@35
|
69 |
@Path("games/{id}")
|
jtulach@35
|
70 |
public Game applyMove(@PathParam("id") String id, @QueryParam("player") String player, @QueryParam("move") String move)
|
jtulach@35
|
71 |
throws IllegalPositionException {
|
jtulach@35
|
72 |
Game g = findGame(id);
|
jtulach@35
|
73 |
if (g == null) {
|
jtulach@35
|
74 |
throw new IllegalArgumentException("Unknown game " + id);
|
jtulach@35
|
75 |
}
|
jtulach@35
|
76 |
Move m = Move.valueOf(move);
|
jtulach@35
|
77 |
g.apply(player, m);
|
jtulach@35
|
78 |
return g;
|
jtulach@35
|
79 |
}
|
jtulach@35
|
80 |
|
jtulach@35
|
81 |
@GET
|
jtulach@35
|
82 |
@Produces(MediaType.APPLICATION_JSON)
|
jtulach@35
|
83 |
@Path("games")
|
jtulach@35
|
84 |
public List<Game> getGames() {
|
jtulach@35
|
85 |
return games;
|
jtulach@35
|
86 |
}
|
jtulach@35
|
87 |
|
jtulach@35
|
88 |
private Game findGame(String id) {
|
jtulach@35
|
89 |
for (Game g : games) {
|
jtulach@35
|
90 |
if (g.getId().equals(id)) {
|
jtulach@35
|
91 |
return g;
|
jtulach@35
|
92 |
}
|
jtulach@35
|
93 |
}
|
jtulach@35
|
94 |
return null;
|
jtulach@35
|
95 |
}
|
jtulach@35
|
96 |
|
jtulach@35
|
97 |
|
jtulach@35
|
98 |
//
|
jtulach@35
|
99 |
// start the server
|
jtulach@35
|
100 |
//
|
jtulach@35
|
101 |
|
jtulach@35
|
102 |
public static void main(String[] args) throws IOException {
|
jtulach@35
|
103 |
|
jtulach@35
|
104 |
final String baseUri = "http://localhost:9998/";
|
jtulach@35
|
105 |
final Map<String, String> initParams = new HashMap<String, String>();
|
jtulach@35
|
106 |
|
jtulach@35
|
107 |
initParams.put("com.sun.jersey.config.property.packages",
|
jtulach@35
|
108 |
"cz.xelfi.quoridor.webidor.resources");
|
jtulach@35
|
109 |
|
jtulach@35
|
110 |
System.out.println("Starting HttpServer...");
|
jtulach@35
|
111 |
ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
|
jtulach@35
|
112 |
HttpServer threadSelector = HttpServerFactory.create(baseUri, rc);
|
jtulach@35
|
113 |
threadSelector.start();
|
jtulach@35
|
114 |
System.out.println(String.format(
|
jtulach@35
|
115 |
"Jersey app started with WADL available at %sapplication.wadl\n" +
|
jtulach@35
|
116 |
"Try out %shelloworld\nHit enter to stop it...", baseUri, baseUri));
|
jtulach@35
|
117 |
System.in.read();
|
jtulach@35
|
118 |
threadSelector.stop(0);
|
jtulach@35
|
119 |
System.exit(0);
|
jtulach@35
|
120 |
}
|
jtulach@35
|
121 |
|
jtulach@35
|
122 |
}
|