Extracts a cookie value from a list of cookies based on the name. - Java Network

Java examples for Network:Cookie

Description

Extracts a cookie value from a list of cookies based on the name.

Demo Code

/**/*from  w w w. ja v a2s .c  o  m*/
 * Copyright (c) 2000-2016 Liferay, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

public class Main {
    /**
     * Extracts a cookie value from a list of cookies based on the name.
     *
     * @param   cookieName  Name of the cookie to search for.
     * @param   cookies     List of cookies of the form "name1=value1; name2=value2; ...; nameN=valueN".
     *
     * @return  The cookie value stored as a String object. null if cookie not found.
     */
    public static String getCookie(String cookieName, String cookies) {
        return getParameterValue(cookieName, cookies);
    }

    /**
     * Extracts a parameter value from a list of parameters based on the name.
     *
     * @param   paramName  name of the parameter to search forn
     * @param   params     list of parameters as a String of the form: "name1=value1; name2=value2; ...; nameN=valueN".
     *
     * @return  the parameter value or <code>null</code> if not found
     */
    protected static String getParameterValue(String paramName,
            String params) {
        int paramNameLen;
        int paramsLen;

        // Obvious error in arguments?
        if ((paramName == null)
                || ((paramNameLen = paramName.length()) == 0)) {
            return null;
        }

        if ((params == null) || ((paramsLen = params.length()) == 0)) {
            return null;
        }

        // Try to extract the parameter from the list of parameters
        // Parameters are defined in a string of the form
        // name1=value1; name2=value2; ...; nameN=valueN
        //
        // The following parse copies the behaviour of
        // wpugcinb_GetCookieInNewBuffer in /m/wwg/src/wpu.c
        // and
        // wpdsesgcv_get_cookie_value in /m/wwg/src/wpdses.c
        //

        int index = 0;

        for (;;) {

            // First, eat all the white spaces
            while ((params.charAt(index) == ' ') && (index < paramsLen)) {
                index++;
            }

            if (params.startsWith(paramName, index)) {
                int equalsIndex = params.indexOf('=', index);

                if (equalsIndex != -1) {
                    int i = equalsIndex - 1;

                    while ((params.charAt(i) == ' ') && (i > index)) {
                        i--;
                    }

                    if (paramNameLen == (i - index + 1)) {
                        // We've found our parameter

                        // Move 1 char past '='
                        int beginIndex = equalsIndex + 1;

                        // Check for empty parameter - if so return ""
                        if (beginIndex == paramsLen) {
                            return "";
                        }

                        // Find end of token
                        int endIndex = params.indexOf(';', beginIndex);

                        if (endIndex == -1) {
                            endIndex = paramsLen - 1;
                        } else {

                            // Check for empty parameter - if so return ""
                            if (beginIndex == endIndex) {
                                return "";
                            }

                            endIndex--;
                        }

                        // Eat white spaces out in front
                        while ((params.charAt(beginIndex) == ' ')
                                && (beginIndex < endIndex)) {
                            beginIndex++;
                        }

                        // Eat white spaces out at end
                        while ((params.charAt(endIndex) == ' ')
                                && (endIndex > beginIndex)) {
                            endIndex--;
                        }

                        // Please note that substring takes the endIndex and
                        // subtracts 1
                        // from it. Since our endIndex points to where we want
                        // it to be,
                        // we need to add 1 to it so that everything works out.
                        String paramValue = params.substring(beginIndex,
                                endIndex + 1);

                        // check for empty cookie - if so return ""
                        if (0 == paramValue.length()) {
                            return "";
                        }

                        return paramValue;
                    }
                }
            }

            // no match - advance to next parameter
            index = params.indexOf(';', index);

            // Check for end cases
            if (index == -1) // no more tokens
            {
                return null;
            }

            if (index == (paramsLen - 1)) // nothing following last ';'
            {
                return null;
            }

            index++;
        }
    }
}

Related Tutorials