Java URI Normalize normalizedSetCookiePath(final String path, final URI originUri)

Here you can find the source of normalizedSetCookiePath(final String path, final URI originUri)

Description

normalized Set Cookie Path

License

Open Source License

Declaration

public static String normalizedSetCookiePath(final String path, final URI originUri) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.URI;

public class Main {
    public static String normalizedSetCookiePath(final String path, final URI originUri) {
        // RFC 6265 Section 5.2.4

        // If the attribute-value is empty or if the first character of the
        // attribute-value is not %x2F ("/"):
        // //from   w  w w  . j a  va 2  s.c  o m
        //    Let cookie-path be the default-path.
        if (path == null || path.equals("") || path.charAt(0) != '/') {
            return defaultSetCookiePath(originUri);
        }

        // 
        // Otherwise:
        // 
        //    Let cookie-path be the attribute-value.
        // 
        return path;
    }

    /**
     * Compute the default Path for a cookie sent from the given originUri 
     * according to RFC 6265 Section 5.1.4
     *
     */
    public static String defaultSetCookiePath(final URI requestUri) {

        // 1.  Let uri-path be the path portion of the request-uri if such a
        //     portion exists (and empty otherwise).  For example, if the
        //     request-uri contains just a path (and optional query string),
        //     then the uri-path is that path (without the %x3F ("?") character
        //     or query string), and if the request-uri contains a full
        //     absoluteURI, the uri-path is the path component of that URI.
        final String uriPath = requestUri.getPath();

        // 2.  If the uri-path is empty or if the first character of the uri-
        //     path is not a %x2F ("/") character, output %x2F ("/") and skip
        //     the remaining steps.
        if (uriPath == null || uriPath.equals("") || uriPath.charAt(0) != '/') {
            return "/";
        }

        // 3.  If the uri-path contains no more than one %x2F ("/") character,
        //     output %x2F ("/") and skip the remaining step.
        int lastSlash = uriPath.lastIndexOf('/');
        if (lastSlash <= 0) { // already verified it starts with "/" so this indicates 1 / only. 
            return "/";
        }

        // 4.  Output the characters of the uri-path from the first character up
        //     to, but not including, the right-most %x2F ("/").
        return uriPath.substring(0, lastSlash);
    }
}

Related

  1. normalize(final String uri)
  2. normalize(String uri, String resourceLocation, String rootLocation)
  3. normalize(URI input)
  4. normalize(URI location)
  5. normalize(URI uri)
  6. normalizedUri(URI uri)
  7. normalizeGitRepoLocation(URI location)
  8. normalizeLink(String link, URI parent, boolean removeParams)
  9. normalizeURI(final URI uri)