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 java.util.Map;
020    
021    import org.apache.camel.CamelContext;
022    import org.apache.camel.Endpoint;
023    import org.apache.camel.component.gae.auth.GAuthComponent;
024    import org.apache.camel.component.gae.bind.OutboundBinding;
025    import org.apache.camel.impl.DefaultComponent;
026    
027    /**
028     * The <a href="http://camel.apache.org/glogin.html">GLogin Component</a>
029     * encapsulates the required steps needed to login to an Google App Engine (GAE)
030     * application. This component uses <a
031     * href="http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html"
032     * >ClientLogin</a> for authentication and a GAE-specific mechanism for
033     * authorization. Result of the login procedure is an authorization cookie that
034     * should be included in HTTP requests targeted at GAE applications. This
035     * component is intended for being used by Java client applications that want to
036     * do a programmatic login to GAE applications. Web applications should use the
037     * {@link GAuthComponent} for access authorization to other web applications.
038     */
039    public class GLoginComponent extends DefaultComponent {
040    
041        public GLoginComponent() {
042            this(null);
043        }
044    
045        public GLoginComponent(CamelContext context) {
046            super(context);
047        }
048    
049        @Override
050        @SuppressWarnings("unchecked")
051        protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
052            OutboundBinding outboundBinding = resolveAndRemoveReferenceParameter(
053                    parameters, "outboundBindingRef", GLoginBinding.class, new GLoginBinding());
054            GLoginService service = resolveAndRemoveReferenceParameter(
055                    parameters, "serviceRef", GLoginService.class, new GLoginServiceImpl());
056            GLoginEndpoint endpoint = new GLoginEndpoint(uri, this, 
057                    getHostName(remaining),
058                    getDevPort(remaining));
059            endpoint.setOutboundBinding(outboundBinding);
060            endpoint.setService(service);
061            return endpoint;
062        }
063    
064        private static String getHostName(String remaining) {
065            if (remaining.contains(":")) {
066                return remaining.split(":")[0];
067            } else {
068                return remaining;
069            }
070        }
071        
072        private static int getDevPort(String remaining) {
073            if (remaining.contains(":")) {
074                return Integer.parseInt(remaining.split(":")[1]);
075            } else {
076                return 8080;
077            }
078        }
079        
080    }