001/**
002 * Copyright 2014 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package service.tut.pori.users.twitter;
017
018import java.util.Properties;
019
020import org.apache.commons.codec.EncoderException;
021import org.apache.commons.codec.net.URLCodec;
022import org.apache.commons.lang3.BooleanUtils;
023import org.apache.commons.lang3.StringUtils;
024import org.apache.log4j.Logger;
025
026import core.tut.pori.properties.SystemProperty;
027
028/**
029 * Twitter User Service properties.
030 */
031public class TwitterProperties extends SystemProperty {
032  /* properties */
033  private static final String PROPERTY_SERVICE_USERS_TWITTER = PROPERTY_SERVICE_PORI+".users.twitter";
034  private static final String PROPERTY_SERVICE_USERS_TWITTER_API_KEY = PROPERTY_SERVICE_USERS_TWITTER+".twitter_api_key";
035  private static final String PROPERTY_SERVICE_USERS_TWITTER_CLIENT_SECRET = PROPERTY_SERVICE_USERS_TWITTER+".twitter_client_secret";
036  private static final String PROPERTY_SERVICE_USERS_TWITTER_OAUTH_AUTHORIZE_REDIRECT_URI = PROPERTY_SERVICE_USERS_TWITTER+".twitter_oauth_authorize_redirect_uri";
037  private static final String PROPERTY_SERVICE_USERS_TWITTER_OAUTH_LOGIN_REDIRECT_URI = PROPERTY_SERVICE_USERS_TWITTER+".twitter_oauth_login_redirect_uri";
038  private static final String PROPERTY_SERVICE_USERS_TWITTER_OAUTH_REGISTER_REDIRECT_URI = PROPERTY_SERVICE_USERS_TWITTER+".twitter_oauth_register_redirect_uri";
039  private static final String PROPERTY_SERVICE_USERS_TWITTER_OAUTH_URI = PROPERTY_SERVICE_USERS_TWITTER+".twitter_oauth_uri";
040  private static final String PROPERTY_SERVICE_USERS_TWITTER_USER_INFO_URI = PROPERTY_SERVICE_USERS_TWITTER+".twitter_user_info_uri";
041  private static final String PROPERTY_SERVICE_USERS_TWITTER_DEBUG_ENABLED = PROPERTY_SERVICE_USERS_TWITTER+".twitter_debug_enabled";
042  /* members */
043  private String _apiKey = null;
044  private String _clientSecret = null;
045  private boolean _debugEnabled = false;
046  private String _encodedApiKey = null;
047  private String _encodedClientSecret = null;
048  private String _encodedOAuthAuthorizeRedirectUri = null;
049  private String _encodedOAuthLoginRedirectUri = null;
050  private String _encodedOAuthRegisterRedirectUri = null;
051  private String _oAuthAuthorizeRedirectUri = null;
052  private String _oAuthLoginRedirectUri = null;
053  private String _oAuthRegisterRedirectUri = null;
054  private String _oAuthUri = null;
055  private String _userInfoUri = null;
056
057  @Override
058  public void initialize(Properties properties) throws IllegalArgumentException {
059    _apiKey = properties.getProperty(PROPERTY_SERVICE_USERS_TWITTER_API_KEY);
060    _clientSecret = properties.getProperty(PROPERTY_SERVICE_USERS_TWITTER_CLIENT_SECRET);
061    if(_apiKey == null || _clientSecret == null){
062      throw new IllegalArgumentException(PROPERTY_SERVICE_USERS_TWITTER_API_KEY+" or "+PROPERTY_SERVICE_USERS_TWITTER_CLIENT_SECRET+" is missing.");
063    }
064    _oAuthUri = properties.getProperty(PROPERTY_SERVICE_USERS_TWITTER_OAUTH_URI);
065    _oAuthLoginRedirectUri = properties.getProperty(PROPERTY_SERVICE_USERS_TWITTER_OAUTH_LOGIN_REDIRECT_URI);
066    _oAuthRegisterRedirectUri = properties.getProperty(PROPERTY_SERVICE_USERS_TWITTER_OAUTH_REGISTER_REDIRECT_URI);
067    _oAuthAuthorizeRedirectUri = properties.getProperty(PROPERTY_SERVICE_USERS_TWITTER_OAUTH_AUTHORIZE_REDIRECT_URI);
068    _userInfoUri = properties.getProperty(PROPERTY_SERVICE_USERS_TWITTER_USER_INFO_URI);
069    if(_oAuthAuthorizeRedirectUri == null || _oAuthUri == null || _oAuthRegisterRedirectUri == null || _oAuthAuthorizeRedirectUri == null || _userInfoUri == null){
070      throw new IllegalArgumentException(PROPERTY_SERVICE_USERS_TWITTER_USER_INFO_URI+", "+PROPERTY_SERVICE_USERS_TWITTER_OAUTH_LOGIN_REDIRECT_URI+", "+PROPERTY_SERVICE_USERS_TWITTER_OAUTH_REGISTER_REDIRECT_URI+", "+PROPERTY_SERVICE_USERS_TWITTER_OAUTH_AUTHORIZE_REDIRECT_URI+" or "+PROPERTY_SERVICE_USERS_TWITTER_OAUTH_URI+" is missing.");
071    }
072    try {
073      URLCodec codec = new URLCodec(core.tut.pori.http.Definitions.ENCODING_UTF8);
074      _encodedOAuthLoginRedirectUri = codec.encode(_oAuthLoginRedirectUri);
075      _encodedOAuthRegisterRedirectUri = codec.encode(_oAuthRegisterRedirectUri);
076      _encodedOAuthAuthorizeRedirectUri = codec.encode(_oAuthAuthorizeRedirectUri);
077      _encodedApiKey = codec.encode(_apiKey);
078      _encodedClientSecret = codec.encode(_clientSecret);
079    } catch (EncoderException ex) {
080      Logger.getLogger(getClass()).error(ex);
081      throw new IllegalArgumentException("Bad "+PROPERTY_SERVICE_USERS_TWITTER_API_KEY+", "+PROPERTY_SERVICE_USERS_TWITTER_CLIENT_SECRET+", "+PROPERTY_SERVICE_USERS_TWITTER_OAUTH_LOGIN_REDIRECT_URI+", "+PROPERTY_SERVICE_USERS_TWITTER_OAUTH_REGISTER_REDIRECT_URI+" or "+PROPERTY_SERVICE_USERS_TWITTER_OAUTH_AUTHORIZE_REDIRECT_URI);
082    }
083    String debugEnabled = properties.getProperty(PROPERTY_SERVICE_USERS_TWITTER_DEBUG_ENABLED);
084    if(StringUtils.isBlank(debugEnabled)){
085      throw new IllegalArgumentException("Bad "+PROPERTY_SERVICE_USERS_TWITTER_DEBUG_ENABLED);
086    }
087    _debugEnabled = BooleanUtils.toBoolean(debugEnabled);
088  }
089
090  /**
091   * @return the apiKey
092   */
093  public String getApiKey() {
094    return _apiKey;
095  }
096
097  /**
098   * @return the clientSecret
099   */
100  public String getClientSecret() {
101    return _clientSecret;
102  }
103
104  /**
105   * @return the encodedOAuthAuthorizeRedirectUri
106   */
107  public String getEncodedOAuthAuthorizeRedirectUri() {
108    return _encodedOAuthAuthorizeRedirectUri;
109  }
110
111  /**
112   * @return the oAuthAuthorizeRedirectUri
113   */
114  public String getoAuthAuthorizeRedirectUri() {
115    return _oAuthAuthorizeRedirectUri;
116  }
117
118  /**
119   * @return the oAuthUri
120   */
121  public String getoAuthUri() {
122    return _oAuthUri;
123  }
124
125  /**
126   * @return the userInfoUri
127   */
128  public String getUserInfoUri() {
129    return _userInfoUri;
130  }
131
132  /**
133   * @return the encodedOAuthLoginRedirectUri
134   */
135  public String getEncodedOAuthLoginRedirectUri() {
136    return _encodedOAuthLoginRedirectUri;
137  }
138
139  /**
140   * @return the encodedOAuthRegisterRedirectUri
141   */
142  public String getEncodedOAuthRegisterRedirectUri() {
143    return _encodedOAuthRegisterRedirectUri;
144  }
145
146  /**
147   * @return the debugEnabled
148   */
149  public boolean isDebugEnabled() {
150    return _debugEnabled;
151  }
152
153  /**
154   * @return the oAuthLoginRedirectUri
155   */
156  public String getoAuthLoginRedirectUri() {
157    return _oAuthLoginRedirectUri;
158  }
159
160  /**
161   * @return the oAuthRegisterRedirectUri
162   */
163  public String getoAuthRegisterRedirectUri() {
164    return _oAuthRegisterRedirectUri;
165  }
166
167  /**
168   * @return the encodedApiKey
169   */
170  public String getEncodedApiKey() {
171    return _encodedApiKey;
172  }
173
174  /**
175   * @return the encodedClientSecret
176   */
177  public String getEncodedClientSecret() {
178    return _encodedClientSecret;
179  }
180  
181  @Override
182  public String getPropertyFilePath() {
183    return CONFIGURATION_FILE_PATH+"twitter.properties";
184  }
185}