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 com.sun.jersey.spi.resource.Singleton;
|
jtulach@35
|
33 |
import com.sun.net.httpserver.HttpServer;
|
jtulach@37
|
34 |
import java.io.File;
|
jtulach@82
|
35 |
import java.io.FileInputStream;
|
jtulach@35
|
36 |
import java.io.IOException;
|
jaroslav@52
|
37 |
import java.net.ServerSocket;
|
jtulach@82
|
38 |
import java.util.HashMap;
|
jtulach@82
|
39 |
import java.util.Map;
|
jtulach@82
|
40 |
import java.util.Properties;
|
jtulach@82
|
41 |
import java.util.UUID;
|
jtulach@82
|
42 |
import javax.ws.rs.GET;
|
jtulach@82
|
43 |
import javax.ws.rs.PUT;
|
jtulach@35
|
44 |
import javax.ws.rs.Path;
|
jtulach@82
|
45 |
import javax.ws.rs.Produces;
|
jtulach@82
|
46 |
import javax.ws.rs.QueryParam;
|
jtulach@82
|
47 |
import javax.ws.rs.core.MediaType;
|
jtulach@35
|
48 |
|
jtulach@35
|
49 |
/**
|
jtulach@35
|
50 |
*
|
jtulach@35
|
51 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@35
|
52 |
*/
|
jtulach@41
|
53 |
@Path("/api")
|
jtulach@35
|
54 |
@Singleton
|
jtulach@35
|
55 |
public final class Quoridor {
|
jtulach@37
|
56 |
private final File path;
|
jtulach@37
|
57 |
private Games games;
|
jtulach@82
|
58 |
private final Map<UUID,String> loggedIn;
|
jtulach@37
|
59 |
|
jtulach@37
|
60 |
public Quoridor() {
|
jtulach@37
|
61 |
final String prop = System.getProperty("quoridor.dir"); // NOI18N
|
jtulach@37
|
62 |
if (prop == null) {
|
jtulach@37
|
63 |
throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
|
jtulach@37
|
64 |
}
|
jtulach@37
|
65 |
path = new File(prop);
|
jtulach@37
|
66 |
path.mkdirs();
|
jtulach@82
|
67 |
loggedIn = new HashMap<UUID, String>();
|
jtulach@37
|
68 |
}
|
jtulach@35
|
69 |
|
jtulach@35
|
70 |
@Path("games")
|
jtulach@36
|
71 |
public Games getGames() {
|
jtulach@37
|
72 |
if (games == null) {
|
jtulach@82
|
73 |
games = new Games(new File(path, "games"), this); // NOI18N
|
jtulach@37
|
74 |
}
|
jtulach@35
|
75 |
return games;
|
jtulach@35
|
76 |
}
|
jtulach@35
|
77 |
|
jtulach@82
|
78 |
@Path("login")
|
jtulach@82
|
79 |
@PUT
|
jtulach@82
|
80 |
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
|
jtulach@82
|
81 |
public String login(
|
jtulach@82
|
82 |
@QueryParam("name") String name,
|
jtulach@82
|
83 |
@QueryParam("password") String password
|
jtulach@82
|
84 |
) {
|
jtulach@82
|
85 |
File f = new File(path, "passwd"); // NOI18Nt
|
jtulach@82
|
86 |
Properties p = new Properties();
|
jtulach@82
|
87 |
try {
|
jtulach@82
|
88 |
p.load(new FileInputStream(f));
|
jtulach@82
|
89 |
} catch (IOException ex) {
|
jtulach@82
|
90 |
ex.printStackTrace();
|
jtulach@82
|
91 |
}
|
jtulach@82
|
92 |
if (name != null && password.equals(p.getProperty(name))) {
|
jtulach@82
|
93 |
UUID uuid = UUID.randomUUID();
|
jtulach@82
|
94 |
loggedIn.put(uuid, name);
|
jtulach@82
|
95 |
return uuid.toString();
|
jtulach@82
|
96 |
} else {
|
jtulach@82
|
97 |
return null;
|
jtulach@82
|
98 |
}
|
jtulach@82
|
99 |
|
jtulach@82
|
100 |
}
|
jtulach@82
|
101 |
|
jtulach@82
|
102 |
@Path("login")
|
jtulach@82
|
103 |
@GET
|
jtulach@82
|
104 |
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
|
jtulach@82
|
105 |
public String isLoggedIn(
|
jtulach@82
|
106 |
@QueryParam("id") String id
|
jtulach@82
|
107 |
) {
|
jaroslav@85
|
108 |
String ret = null;
|
jtulach@83
|
109 |
try {
|
jaroslav@85
|
110 |
if (id != null) {
|
jaroslav@85
|
111 |
ret = loggedIn.get(UUID.fromString(id));
|
jaroslav@85
|
112 |
}
|
jtulach@83
|
113 |
} catch (IllegalArgumentException ex) {
|
jaroslav@85
|
114 |
// OK, happens for invalid ids
|
jtulach@83
|
115 |
}
|
jaroslav@85
|
116 |
return ret == null ? "" : ret;
|
jtulach@82
|
117 |
}
|
jtulach@82
|
118 |
|
jtulach@35
|
119 |
//
|
jtulach@35
|
120 |
// start the server
|
jtulach@35
|
121 |
//
|
jtulach@35
|
122 |
|
jtulach@35
|
123 |
public static void main(String[] args) throws IOException {
|
jtulach@41
|
124 |
HttpServer s = start(9998);
|
jtulach@41
|
125 |
System.out.println(
|
jtulach@41
|
126 |
"Quoridor started at port 9998\n" + "Hit enter to stop it..."
|
jtulach@41
|
127 |
);
|
jtulach@41
|
128 |
System.in.read();
|
jtulach@41
|
129 |
s.stop(0);
|
jtulach@41
|
130 |
System.exit(0);
|
jtulach@41
|
131 |
}
|
jtulach@35
|
132 |
|
jtulach@41
|
133 |
public static HttpServer start(int port) throws IOException {
|
jaroslav@52
|
134 |
if (port == -1) {
|
jaroslav@52
|
135 |
ServerSocket ss = new ServerSocket(0);
|
jaroslav@52
|
136 |
port =ss.getLocalPort();
|
jaroslav@52
|
137 |
ss.close();
|
jaroslav@52
|
138 |
}
|
jtulach@41
|
139 |
final String baseUri = "http://localhost:" + port + "/";
|
jtulach@35
|
140 |
|
jtulach@38
|
141 |
File home = new File(System.getProperty("user.home"));
|
jtulach@38
|
142 |
File quoridor = new File(home, ".quoridor");
|
jtulach@38
|
143 |
|
jtulach@38
|
144 |
System.setProperty("quoridor.dir", quoridor.getPath());
|
jtulach@38
|
145 |
|
jtulach@35
|
146 |
ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
|
jtulach@41
|
147 |
HttpServer server = HttpServerFactory.create(baseUri, rc);
|
jtulach@41
|
148 |
server.start();
|
jtulach@41
|
149 |
return server;
|
jtulach@35
|
150 |
}
|
jtulach@35
|
151 |
|
jtulach@35
|
152 |
}
|