001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.gae.auth;
018    
019    /**
020     * A request token secret container with marshalling/unmarshalling methods to
021     * and from a cookie.
022     * 
023     * @see GAuthAuthorizeBinding
024     * @see GAuthUpgradeBinding
025     */
026    public class GAuthTokenSecret {
027    
028        /**
029         * Name of the request token secret cookie.
030         */
031        public static final String COOKIE_NAME = "gauth-token-secret";
032    
033        private String value;
034    
035        /**
036         * Creates a new {@link GAuthTokenSecret}
037         * 
038         * @param value
039         *            request token secret.
040         */
041        public GAuthTokenSecret(String value) {
042            this.value = value;
043        }
044    
045        /**
046         * Returns the request token secret.
047         */
048        public String getValue() {
049            return value;
050        }
051    
052        /**
053         * Creates a cookie from this {@link GAuthTokenSecret}.
054         */
055        public String toCookie() {
056            return COOKIE_NAME + "=" + value;
057        }
058    
059        /**
060         * Create a {@link GAuthTokenSecret} from a cookies string.
061         * 
062         * @param cookies
063         *            cookies string.
064         * @return either an {@link GAuthTokenSecret} instance or <code>null</code>
065         *         if there's no cookie with name {@link #COOKIE_NAME}.
066         */
067        public static GAuthTokenSecret fromCookie(String cookies) {
068            if (cookies == null) {
069                return null;
070            }
071            for (String cookie : cookies.split(";")) {
072                String[] pair = cookie.split("=");
073                if (pair[0].trim().equals(COOKIE_NAME)) {
074                    return new GAuthTokenSecret(pair[1].trim());
075                }
076            }
077            return null;
078        }
079        
080    }
081