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    import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
020    
021    import org.apache.camel.Exchange;
022    import org.apache.camel.component.gae.bind.OutboundBinding;
023    import org.apache.camel.impl.DefaultProducer;
024    
025    import static org.apache.camel.component.gae.auth.GAuthEndpoint.Name.AUTHORIZE;
026    
027    public class GAuthProducer extends DefaultProducer {
028    
029        public GAuthProducer(GAuthEndpoint endpoint) {
030            super(endpoint);
031        }
032        
033        @Override
034        public GAuthEndpoint getEndpoint() {
035            return (GAuthEndpoint)super.getEndpoint();
036        }
037        
038        public OutboundBinding<GAuthEndpoint, GoogleOAuthParameters, GoogleOAuthParameters> getAuthorizeBinding() {
039            return getEndpoint().getAuthorizeBinding();
040        }
041    
042        public OutboundBinding<GAuthEndpoint, GoogleOAuthParameters, GoogleOAuthParameters> getUpgradeBinding() {
043            return getEndpoint().getUpgradeBinding();
044        }
045    
046        /**
047         * Depending on the {@link GAuthEndpoint.Name}, this method either fetches
048         * an unauthorized request token and creates a redirect response, or
049         * upgrades an authorized request token to an access token.
050         * 
051         * @param exchange
052         * 
053         * @see GAuthAuthorizeBinding
054         * @see GAuthUpgradeBinding
055         * @see GAuthServiceImpl
056         */
057        public void process(Exchange exchange) throws Exception {
058            if (getEndpoint().getName() == AUTHORIZE) {
059                GoogleOAuthParameters params = getAuthorizeBinding().writeRequest(getEndpoint(), exchange, null);
060                getEndpoint().getService().getUnauthorizedRequestToken(params);
061                getAuthorizeBinding().readResponse(getEndpoint(), exchange, params);
062            } else {
063                GoogleOAuthParameters params = getUpgradeBinding().writeRequest(getEndpoint(), exchange, null);
064                getEndpoint().getService().getAccessToken(params);
065                getUpgradeBinding().readResponse(getEndpoint(), exchange, params);
066            }
067            
068        }
069    
070    }