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