Package org.restlet.data

Examples of org.restlet.data.Cookie


                .getText());
    }

    public void testCookies() throws IOException {
        final Request request = createGetRequest("cookies/cookieName");
        request.getCookies().add(new Cookie("cookieName", "cookie-value"));
        final Response response = accessServer(request);
        assertEquals(Status.SUCCESS_OK, response.getStatus());
        assertEquals("cookieName=cookie-value", response.getEntity().getText());
    }
View Full Code Here


        return result;
    }

    @Override
    public Cookie readValue() throws IOException {
        Cookie result = null;
        Parameter pair = readPair(false);

        if (this.globalVersion == -1) {
            // Cookies version not yet detected
            if (pair.getName().equalsIgnoreCase(NAME_VERSION)) {
                if (pair.getValue() != null) {
                    this.globalVersion = Integer.parseInt(pair.getValue());
                } else {
                    throw new IOException(
                            "Empty cookies version attribute detected. Please check your cookie header");
                }
            } else {
                // Set the default version for old Netscape cookies
                this.globalVersion = 0;
            }
        }

        while ((pair != null) && (pair.getName().charAt(0) == '$')) {
            // Unexpected special attribute
            // Silently ignore it as it may have been introduced by new
            // specifications
            pair = readPair(false);
        }

        if (pair != null) {
            // Set the cookie name and value
            result = new Cookie(this.globalVersion, pair.getName(), pair
                    .getValue());
            pair = readPair(true);
        }

        while ((pair != null) && (pair.getName().charAt(0) == '$')) {
            if (pair.getName().equalsIgnoreCase(NAME_PATH)) {
                result.setPath(pair.getValue());
            } else if (pair.getName().equalsIgnoreCase(NAME_DOMAIN)) {
                result.setDomain(pair.getValue());
            } else {
                // Unexpected special attribute
                // Silently ignore it as it may have been introduced by new
                // specifications
            }
View Full Code Here

        super(delegate);
    }

    @Override
    public Cookie createEntry(String name, String value) {
        return new Cookie(name, value);
    }
View Full Code Here

    /**
     * Equality tests.
     */
    public void testEquals() throws Exception {
        final Cookie c1 = new Cookie(1, "name1", "value1", "path1", "domain1");
        final Cookie c2 = new Cookie(1, "name1", "value1", "path1", "domain1");

        assertTrue(c1.equals(c2));
        assertTrue(c1.hashCode() == c2.hashCode());
        assertEquals(c1, c2);

        assertTrue(c1.equals(c1));
        assertEquals(c1, c1);
    }
View Full Code Here

    /**
     * Unequality tests.
     */
    public void testUnEquals() throws Exception {
        Cookie c1 = new Cookie(1, "name1", "value1", "path1", "domain1");
        Cookie c2 = new Cookie(2, "name2", "value2", "path2", "domain2");
        assertFalse(c1.equals(c2));
        assertFalse(c1.hashCode() == c2.hashCode());
        assertFalse(c1.equals(null));
        assertFalse(c2.equals(null));

        c1 = new Cookie(1, "name", "value", "path", "domain");
        c2 = new Cookie(2, "name", "value", "path", "domain");
        assertFalse(c1.equals(c2));
        assertFalse(c1.hashCode() == c2.hashCode());
    }
View Full Code Here

     * @throws IOException
     */
    private void testCookie(String headerValue) throws IOException {
        CookieReader cr = new CookieReader(headerValue);
        List<Cookie> cookies = new ArrayList<Cookie>();
        Cookie cookie = cr.readValue();

        while (cookie != null) {
            cookies.add(cookie);
            cookie = cr.readValue();
        }
View Full Code Here

     * @throws IOException
     */
    private void testCookieValues(String headerValue) throws IOException {
        CookieReader cr = new CookieReader(headerValue);
        List<Cookie> cookies = new ArrayList<Cookie>();
        Cookie cookie = cr.readValue();
        while (cookie != null) {
            cookies.add(cookie);
            cookie = cr.readValue();
        }

View Full Code Here

     * @param destination
     *            The cookies map controlling the reading.
     */
    public static void getCookies(List<Cookie> source,
            Map<String, Cookie> destination) {
        Cookie cookie;

        for (final Iterator<Cookie> iter = source.iterator(); iter.hasNext();) {
            cookie = iter.next();

            if (destination.containsKey(cookie.getName())) {
                destination.put(cookie.getName(), cookie);
            }
        }
    }
View Full Code Here

     *            The list of cookies to format.
     * @return This writer.
     */
    public CookieWriter append(List<Cookie> cookies) {
        if ((cookies != null) && !cookies.isEmpty()) {
            Cookie cookie;

            for (int i = 0; i < cookies.size(); i++) {
                cookie = cookies.get(i);

                if (i == 0) {
                    if (cookie.getVersion() > 0) {
                        append("$Version=\"").append(cookie.getVersion())
                                .append("\"; ");
                    }
                } else {
                    append("; ");
                }
View Full Code Here

      this.authService = authService;
      setVerifier(new Verifier() {
         public int verify(Request request, Response response) {
            request.getAttributes().put(App.AUTH_SERVICE_ATTR,UserGuard.this.authService);
            ChallengeResponse cr = request.getChallengeResponse();
            Cookie cookie = request.getCookies().getFirst("I");

            // We must have one of these to check
            if (cr==null && cookie==null) {
               return Verifier.RESULT_MISSING;
            }

            // If we have new credentials, check them first
            if (cr!=null) {
               String identifier = request.getChallengeResponse()
                      .getIdentifier();
               char[] secret = request.getChallengeResponse().getSecret();

               // Check the credentials
               if ((identifier != null) && (secret != null)) {
                  if (getLogger().isLoggable(Level.FINE)) {
                     getLogger().fine("Authenticating " + identifier);
                  }
                  try {
                     User user = UserGuard.this.authService.authenticate(identifier, new String(secret));
                     if (user != null) {
                        if (getLogger().isLoggable(Level.FINE)) {
                           getLogger().fine("Authenticated: " + user.getAlias() + ", checking groups");
                        }
                     }
                     user = checkUser(request, user);
                     if (user!=null) {
                        return Verifier.RESULT_VALID;
                     }
                  } catch (AuthException ex) {
                     getContext().getLogger().log(Level.SEVERE, "Cannot check authentication.", ex);
                  }
               }
            }

            // Check the identity cookie
            if (cookie != null) {
               try {
                  User user = UserGuard.this.authService.verifySession(cookie.getValue());
                  if (user != null) {
                     if (getLogger().isLoggable(Level.FINE)) {
                        getLogger().fine("Valid session for: " + user.getAlias() + ", checking groups");
                     }
                  }
View Full Code Here

TOP

Related Classes of org.restlet.data.Cookie

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.