001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2008 by chenillekit.org
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     */
014    
015    package org.chenillekit.google.services.impl;
016    
017    import org.slf4j.Logger;
018    
019    import java.io.UnsupportedEncodingException;
020    import java.net.ProxySelector;
021    import java.net.URLEncoder;
022    
023    /**
024     * @version $Id: AbstractGoogleService.java 670 2010-07-19 09:22:02Z mlusetti $
025     */
026    abstract public class AbstractGoogleService
027    {
028            private final Logger logger;
029            private final String referer;
030            private final String proxy;
031            private final int timeout;
032            private final String googleKey;
033    
034            private ProxySelector proxySelector;
035    
036            /**
037             * standard constructor.
038             *
039             * @param logger system logger
040             */
041            protected AbstractGoogleService(Logger logger, String googleKey, int timeout, String referer, String proxy)
042            {
043                    assert googleKey != null;
044    
045                    this.googleKey = googleKey;
046                    this.timeout = timeout;
047                    this.referer = referer;
048                    this.proxy = proxy;
049                    this.logger = logger;
050    
051                    initService();
052            }
053    
054            /**
055             * read and check all service parameters.
056             */
057            private void initService()
058            {
059                    proxySelector = ProxySelector.getDefault();
060            }
061    
062            /**
063             * encode string if not null.
064             */
065            protected String getEncodedString(String source) throws UnsupportedEncodingException
066            {
067                    if (source == null)
068                            return "";
069    
070                    return URLEncoder.encode(source.replace(' ', '+'), "UTF-8");
071            }
072    
073            /**
074             * the the default proxy selector.
075             */
076            public ProxySelector getProxySelector()
077            {
078                    return proxySelector;
079            }
080    
081            /**
082             * get the proxy.
083             */
084            public String getProxy()
085            {
086                    return proxy;
087            }
088    
089            /**
090             * the configured referer.
091             */
092            public String getReferer()
093            {
094                    return referer;
095            }
096    
097            /**
098             * timeout for service request.
099             */
100            public int getTimeout()
101            {
102                    return timeout;
103            }
104    
105            /**
106             * the google access key.
107             */
108            public String getKey()
109            {
110                    return googleKey;
111            }
112    }