Package org.restlet.data

Examples of org.restlet.data.Form


                                    + " method = " + method);
                    if (name != null && name.length() > 0 && action != null
                            && action.length() > 0 && method != null
                            && method.length() > 0
                            && "post".equalsIgnoreCase(method)) {
                        Form f = new Form();
                        NodeList nl = form.getChildNodes();
                        for (int i = 0; i < nl.getLength(); i++) {
                            Node n = nl.item(i);
                            if ("input".equalsIgnoreCase(n.getNodeName())) {
                                NamedNodeMap nnm3 = n.getAttributes();
                                String key = nnm3.getNamedItem("name")
                                        .getNodeValue();
                                String value = nnm3.getNamedItem("value")
                                        .getNodeValue();
                                if (key != null && key.length() > 0) {
                                    f.add(key, value);
                                }
                            }
                        }
                        // The form is ready to send...
                        Context.getCurrentLogger().info(
                                " Form size to send = " + f.size());
                        if (resource == null) {
                            resource = new ClientResource(action);
                        }
                        resource.setReference(action);
                        output = resource.post(f.getWebRepresentation());
                    }
                }

            }
        }
View Full Code Here


        return new EmptyRepresentation();
    }

    @Get("html")
    public Representation represent() {
        Form params = getQuery();
        log.info("OpenIDResource : " + params);

        String rc = params.getFirstValue("return");
        if (rc != null && rc.length() > 0) {
            Map<String, String> axRequired = new HashMap<String, String>();
            Map<String, String> axOptional = new HashMap<String, String>();
            Identifier i = verifyResponse(axRequired, axOptional);
            if (i == null) {
                log.info("Authentication Failed");
                getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
                return new StringRepresentation("Authentication Failed");
            }
            log.info("Identifier = " + i.getIdentifier());
            String id = i.getIdentifier();
            if (id != null) {
                // New Code, always return JSON and let filter handle any
                // callback.
                // TODO maybe move it to use Principal.
                JSONObject obj = new JSONObject();
                try {
                    obj.put("id", i.getIdentifier());
                    for (String s : axRequired.keySet()) {
                        obj.put(s, axRequired.get(s));
                    }
                    for (String s : axOptional.keySet()) {
                        obj.put(s, axOptional.get(s));
                    }
                } catch (JSONException e) {
                    log.log(Level.WARNING, "Failed to get the ID!", e);
                }

                getResponse().setEntity(new JsonRepresentation(obj));
            }
            // cleanup of cookie
            getResponse().getCookieSettings().remove(DESCRIPTOR_COOKIE);
            CookieSetting disc = new CookieSetting(DESCRIPTOR_COOKIE, "");
            disc.setMaxAge(0);
            getResponse().getCookieSettings().add(disc);
            // TODO save the identifier // send back to OAuth
            return getResponse().getEntity();
        }

        String target = params.getFirstValue("openid_identifier");
        if (target == null || target.length() == 0) {
            // No target - might be Yadis discovery
            String location = setXRDSHeader();
            StringBuilder html = new StringBuilder();
            html.append("<html><head><meta http-equiv=\"X-XRDS-Location\" content=\"");
            html.append(location);
            html.append("\"/></head></html>");
            return new StringRepresentation(html.toString(),
                    MediaType.TEXT_HTML);
        }

        try {
            StringBuilder returnToUrl = new StringBuilder();
            returnToUrl.append(getReference().getBaseRef());
            returnToUrl.append("?return=true");

            // --- Forward proxy setup (only if needed) ---
            // ProxyProperties proxyProps = new ProxyProperties();
            // proxyProps.setProxyName("proxy.example.com");
            // proxyProps.setProxyPort(8080);
            // HttpClientFactory.setProxyProperties(proxyProps);

            // perform discovery on the user-supplied identifier

            List<?> discoveries = null;
            discoveries = discovery.discover(target);
            for (Object o : discoveries) {
                if (o instanceof DiscoveryInformation) {
                    DiscoveryInformation di = (DiscoveryInformation) o;
                    log.info("Found - " + di.getOPEndpoint());
                    target = di.getOPEndpoint().toString();
                }
            }

            ConsumerManager manager = getManager(target);
            // try {
            // discoveries = manager.discover(target);
            // } catch (YadisException e) {
            // log.info("Could not connect in time!!!!!!!!!!!!!!!!!!!!!!");
            // return new
            // StringRepresentation("Could not connect to Identity Server in time.",MediaType.TEXT_HTML);
            // }

            // attempt to associate with the OpenID provider
            // and retrieve one service endpoint for authentication
            DiscoveryInformation discovered = manager.associate(discoveries);

            // store the discovery information in the user's session
            // getContext().getAttributes().put("openid-disc", discovered);
            String sessionId = String.valueOf(System
                    .identityHashCode(discovered));
            session.put(sessionId, discovered);

            getResponse().getCookieSettings().add(
                    new CookieSetting(DESCRIPTOR_COOKIE, sessionId));
            log.info("Setting DESCRIPTOR COOKIE");

            // obtain a AuthRequest message to be sent to the OpenID provider
            AuthRequest authReq = manager.authenticate(discovered,
                    returnToUrl.toString()); // TODO maybe add TIMESTAMP?
            // Domain wide realm add meta to main page
            // http://localhost:8080/oauth/xrds?returnTo=http://localhost:8080/oauth/openid_login\r\n
            // log.info("OpenID - REALM = " +
            // getReference().getHostIdentifier());
            // authReq.setRealm(getReference().getHostIdentifier().toString());
            log.info("OpenID - REALM = " + getReference().getBaseRef());
            authReq.setRealm(getReference().getBaseRef().toString());

            // Attribute Exchange - getting optional and required
            FetchRequest fetch = null;
            String[] optional = params.getValuesArray("ax_optional", true);
            for (String o : optional) {
                if (!ax.containsKey(o)) {
                    log.warning("Not supported AX extension : " + o);
                    continue;
                }
                if (fetch == null)
                    fetch = FetchRequest.createFetchRequest();
                fetch.addAttribute(o, ax.get(o), false);
            }

            String[] required = params.getValuesArray("ax_required", true);
            for (String r : required) {
                if (!ax.containsKey(r)) {
                    log.warning("Not supported AX extension : " + r);
                    continue;
                }
                if (fetch == null)
                    fetch = FetchRequest.createFetchRequest();
                fetch.addAttribute(r, ax.get(r), true);
            }

            if (fetch != null) {
                authReq.addExtension(fetch);
            }

            if (!discovered.isVersion2()) {
                log.info("REDIRECTING TEMPORARY");
                // Option 1: GET HTTP-redirect to the OpenID Provider endpoint
                // The only method supported in OpenID 1.x
                // redirect-URL usually limited ~2048 bytes
                redirectTemporary(authReq.getDestinationUrl(true));
                return null;
            } else {
                // Option 2: HTML FORM Redirection (Allows payloads >2048 bytes)

                Form msg = new Form();
                for (Object key : authReq.getParameterMap().keySet()) {
                    msg.add(key.toString(),
                            authReq.getParameterValue(key.toString()));
                    log.info("Adding to form - key " + key.toString()
                            + " : value"
                            + authReq.getParameterValue(key.toString()));
                }
View Full Code Here

        }
        String returnTo = getReference().getBaseRef().toString();
        String location = (returnTo != null) ? xrds.toString() + "?returnTo="
                + returnTo : xrds.toString();
        getLogger().info("XRDS endpoint = " + xrds);
        Form headers = (Form) getResponse().getAttributes().get(
                "org.restlet.http.headers");
        if (headers == null) {
            headers = new Form();
            headers.add("X-XRDS-Location", location);
            getResponse().getAttributes().put("org.restlet.http.headers",
                    headers);
        } else {
            headers.add("X-XRDS-Location", location);
        }
        return location;
    }
View Full Code Here

            l.info("OpenId - Http Redirect");
            response.redirectTemporary(authReq.getDestinationUrl(true));

        } else {
            l.info("OpenId - HTML Form Redirect");
            Form msg = new Form();
            for (Object key : authReq.getParameterMap().keySet()) {
                msg.add(key.toString(),
                        authReq.getParameterValue(key.toString()));
                l.info("Adding to form - key " + key.toString() + " : value"
                        + authReq.getParameterValue(key.toString()));
            }
            response.setEntity(generateForm(authReq));
View Full Code Here

     *         {@link Verifier#RESULT_VALID} if success,
     *         {@link Verifier#RESULT_MISSING} while waiting for a callback
     *         response.
     */
    public int verify(Request request, Response response) {
        Form params = request.getResourceRef().getQueryAsForm();
        if (this.isResponse(params)) {
            JSONObject obj = this.handleReturn(params, request, response);
            if (obj == null) {
                return Verifier.RESULT_INVALID;
            } else {
View Full Code Here

        HttpInboundRequest hir = new HttpInboundRequest(server.getContext(), c,
                "GET",
                "/control/accounts/netdev/subscriptions/emily/preferences",
                "HTTP/1.1");
        Form headers = new Form();
        headers.add("Accept", "application/json");
        headers.add("Content-Type", "application/json");
        headers.add("X-Requested-With", "XMLHttpRequest");
        headers.add("User-Agent", "");
        hir.setHeaders(headers);

        assertEquals("Target URI",
                "/control/accounts/netdev/subscriptions/emily/preferences", hir
                        .getResourceRef().getPath());
View Full Code Here

     * @param resourceRef
     *            The resource reference.
     * @return The canonicalized resource name.
     */
    private static String getCanonicalizedResourceName(Reference resourceRef) {
        Form form = resourceRef.getQueryAsForm();

        Parameter param = form.getFirst("comp", true);
        if (param != null) {
            StringBuilder sb = new StringBuilder(resourceRef.getPath());
            return sb.append("?").append("comp=").append(param.getValue())
                    .toString();
        }
View Full Code Here

     * @param subPath
     * @return
     * @throws IOException
     */
    private Representation check1(String subPath) throws IOException {
        Form form = new Form();
        form.add("a", "b");
        Representation webRepresentation = form.getWebRepresentation();
        Response response = post(subPath, webRepresentation);
        sysOutEntityIfError(response);
        assertEquals(Status.SUCCESS_OK, response.getStatus());
        assertEquals("a -> b\n", response.getEntity().getText());
        return webRepresentation;
View Full Code Here

     * @param subPath
     * @throws IOException
     */
    private void check2(String subPath) throws IOException {
        Response response;
        Form form = new Form();
        form.add("a", "b");
        form.add("c", "d");
        response = post(subPath, form.getWebRepresentation());
        sysOutEntityIfError(response);
        assertEquals(Status.SUCCESS_OK, response.getStatus());
        assertEquals("a -> b\nc -> d\n", response.getEntity().getText());
    }
View Full Code Here

     *                then only once.
     * @throws IOException
     */
    private void check3(String subPath, boolean cDouble) throws IOException {
        Response response;
        Form form = new Form();
        form.add("a", "b");
        form.add("c", "d");
        form.add("c", "d2");
        response = post(subPath, form.getWebRepresentation());
        sysOutEntityIfError(response);
        assertEquals(Status.SUCCESS_OK, response.getStatus());
        String expectedEntity = "a -> b\nc -> d\n";
        if (cDouble)
            expectedEntity += "c -> d2\n";
View Full Code Here

TOP

Related Classes of org.restlet.data.Form

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.