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.login;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.component.gae.bind.OutboundBinding;
021    
022    /**
023     * Binds {@link GLoginData} to a Camel {@link Exchange}.
024     */
025    public class GLoginBinding implements OutboundBinding<GLoginEndpoint, GLoginData, GLoginData> {
026    
027        /**
028         * Name of the Camel header defining the host name. Overrides
029         * {@link GLoginEndpoint#getHostName()}.
030         */
031        public static final String GLOGIN_HOST_NAME = "CamelGloginHostName";
032        
033        /**
034         * Name of the Camel header defining the login username. Overrides
035         * {@link GLoginEndpoint#getUserName()}.
036         */
037        public static final String GLOGIN_USER_NAME = "CamelGloginUserName";
038        
039        /**
040         * Name of the Camel header defining the login password. Overrides
041         * {@link GLoginEndpoint#getPassword()}.
042         */
043        public static final String GLOGIN_PASSWORD = "CamelGloginPassword";
044        
045        /**
046         * Name of the Camel header containing the resulting authentication token. 
047         */
048        public static final String GLOGIN_TOKEN = "CamelGloginToken";
049        
050        /**
051         * Name of the Camel header containing the resulting authorization cookie. 
052         */
053        public static final String GLOGIN_COOKIE = "CamelGloginCookie";
054    
055        /**
056         * Creates a {@link GLoginData} object from endpoint and
057         * <code>exchange.getIn()</code> header data. The created object is used to
058         * obtain an authentication token and an authorization cookie.
059         * 
060         * @param endpoint
061         * @param exchange
062         * @param request
063         *            ignored.
064         * @return
065         */
066        public GLoginData writeRequest(GLoginEndpoint endpoint, Exchange exchange, GLoginData request) {
067            String hostName = exchange.getIn().getHeader(GLOGIN_HOST_NAME, String.class); 
068            String userName = exchange.getIn().getHeader(GLOGIN_USER_NAME, String.class); 
069            String password = exchange.getIn().getHeader(GLOGIN_PASSWORD, String.class); 
070            
071            request = new GLoginData();
072            if (hostName == null) {
073                hostName = endpoint.getHostName();
074            }
075            if (userName == null) {
076                userName = endpoint.getUserName();
077            }
078            if (password == null) {
079                password = endpoint.getPassword();
080            }
081            request.setClientName(endpoint.getClientName());
082            request.setDevAdmin(endpoint.isDevAdmin());
083            request.setDevPort(endpoint.getDevPort());
084            request.setDevMode(endpoint.isDevMode());
085            request.setHostName(hostName);
086            request.setUserName(userName);
087            request.setPassword(password);
088            return request;
089        }
090    
091        /**
092         * Creates an <code>exchange.getOut()</code> message with a
093         * {@link #GLOGIN_TOKEN} header containing an authentication token and a
094         * {@link #GLOGIN_COOKIE} header containing an authorization cookie. If the
095         * endpoint is configured to run in development mode, no authentication
096         * token will be set, only an authorization cookie.
097         * 
098         * @param endpoint
099         * @param exchange
100         * @param response
101         * @return
102         */
103        public Exchange readResponse(GLoginEndpoint endpoint, Exchange exchange, GLoginData response) throws Exception {
104            if (response.getAuthenticationToken() != null) {
105                exchange.getOut().setHeader(GLOGIN_TOKEN, response.getAuthenticationToken());
106            }
107            if (response.getAuthorizationCookie() != null) {
108                exchange.getOut().setHeader(GLOGIN_COOKIE, response.getAuthorizationCookie());
109            }
110            return exchange;
111        }
112    
113    }