freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/Requests.java
changeset 233 ecddc9f373bb
child 264 d60370059c3c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/Requests.java	Thu Mar 04 19:37:58 2010 +0100
     1.3 @@ -0,0 +1,81 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * The contents of this file are subject to the terms of either the GNU
     1.8 + * General Public License Version 2 only ("GPL") or the Common
     1.9 + * Development and Distribution License("CDDL") (collectively, the
    1.10 + * "License"). You may not use this file except in compliance with the
    1.11 + * License. You can obtain a copy of the License at
    1.12 + * http://www.netbeans.org/cddl-gplv2.html
    1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.14 + * specific language governing permissions and limitations under the
    1.15 + * License.  When distributing the software, include this License Header
    1.16 + * Notice in each file and include the License file at
    1.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.18 + * particular file as subject to the "Classpath" exception as provided
    1.19 + * by Sun in the GPL Version 2 section of the License file that
    1.20 + * accompanied this code. If applicable, add the following below the
    1.21 + * License Header, with the fields enclosed by brackets [] replaced by
    1.22 + * your own identifying information:
    1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.24 + *
    1.25 + * Contributor(s):
    1.26 + *
    1.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    1.28 + */
    1.29 +
    1.30 +package cz.xelfi.quoridor.freemarkerdor;
    1.31 +
    1.32 +import com.sun.jersey.api.client.WebResource;
    1.33 +import com.sun.jersey.api.client.WebResource.Builder;
    1.34 +import java.net.URI;
    1.35 +import java.util.HashMap;
    1.36 +import java.util.Map;
    1.37 +import java.util.UUID;
    1.38 +import javax.ws.rs.GET;
    1.39 +import javax.ws.rs.Path;
    1.40 +import javax.ws.rs.PathParam;
    1.41 +import javax.ws.rs.Produces;
    1.42 +import javax.ws.rs.core.Cookie;
    1.43 +import javax.ws.rs.core.MediaType;
    1.44 +import javax.ws.rs.core.Response;
    1.45 +
    1.46 +/** Keeps "requests". A request is UUID that maps to a URL which can
    1.47 + * be executed later.
    1.48 + *
    1.49 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.50 + */
    1.51 +public final class Requests extends Object {
    1.52 +    private final Map<UUID,Builder> requests = new HashMap<UUID, Builder>();
    1.53 +    private final WebResource root;
    1.54 +
    1.55 +    Requests(WebResource path) {
    1.56 +        root = path;
    1.57 +    }
    1.58 +
    1.59 +    @GET
    1.60 +    @Path("{id}")
    1.61 +    @Produces(MediaType.TEXT_HTML)
    1.62 +    public Response redirect(@PathParam("id") String uuid) {
    1.63 +        UUID r = UUID.fromString(uuid);
    1.64 +        Builder web = requests.get(r);
    1.65 +        if (web == null) {
    1.66 +            return Response.status(Response.Status.NOT_FOUND).build();
    1.67 +        }
    1.68 +        return Response.ok(web.get(String.class)).build();
    1.69 +    }
    1.70 +
    1.71 +    boolean isVerified(String uuid) {
    1.72 +        return uuid != null && requests.containsKey(UUID.fromString(uuid));
    1.73 +    }
    1.74 +
    1.75 +    URI register(String login, WebResource request) {
    1.76 +        UUID uuid = UUID.randomUUID();
    1.77 +        WebResource callback = root.path(uuid.toString());
    1.78 +        Builder builder = request.queryParam("verified", uuid.toString()).
    1.79 +            cookie(Cookie.valueOf("login=" + login));
    1.80 +        requests.put(uuid, builder);
    1.81 +        return callback.getURI();
    1.82 +    }
    1.83 +
    1.84 +}