2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://www.gnu.org/licenses/.
18 package cz.xelfi.quoridor.freemarkerdor;
20 import com.sun.jersey.api.client.WebResource;
21 import com.sun.jersey.api.client.WebResource.Builder;
23 import java.util.HashMap;
25 import java.util.UUID;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.Path;
28 import javax.ws.rs.PathParam;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.core.Cookie;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
34 /** Keeps "requests". A request is UUID that maps to a URL which can
37 * @author Jaroslav Tulach <jtulach@netbeans.org>
39 public final class Requests extends Object {
40 private final Map<UUID,Builder> requests = new HashMap<UUID, Builder>();
41 private final WebResource root;
43 Requests(WebResource path) {
49 @Produces(MediaType.TEXT_HTML)
50 public Response redirect(@PathParam("id") String uuid) {
51 UUID r = UUID.fromString(uuid);
52 Builder web = requests.get(r);
54 return Response.status(Response.Status.NOT_FOUND).build();
56 return Response.ok(web.get(String.class)).build();
59 boolean isVerified(String uuid) {
60 return uuid != null && requests.containsKey(UUID.fromString(uuid));
63 URI register(String login, WebResource request) {
64 UUID uuid = UUID.randomUUID();
65 WebResource callback = root.path(uuid.toString());
66 Builder builder = request.queryParam("verified", uuid.toString()).
67 cookie(Cookie.valueOf("login=" + login));
68 requests.put(uuid, builder);
69 return callback.getURI();