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.facebook;
017
018import java.util.Properties;
019
020import org.apache.commons.codec.EncoderException;
021import org.apache.commons.codec.net.URLCodec;
022import org.apache.log4j.Logger;
023
024import core.tut.pori.properties.SystemProperty;
025
026/**
027 * System properties for Facebook User Service.
028 */
029public class FacebookProperties extends SystemProperty{
030  /* properties */
031  private static final String PROPERTY_SERVICE_USERS_FACEBOOK = PROPERTY_SERVICE_PORI+".users.facebook";
032  private static final String PROPERTY_SERVICE_USERS_FACEBOOK_APPLICATION_ID = PROPERTY_SERVICE_USERS_FACEBOOK+".facebook_application_id";
033  private static final String PROPERTY_SERVICE_USERS_FACEBOOK_OAUTH2_AUTH_URI = PROPERTY_SERVICE_USERS_FACEBOOK+".facebook_oauth2_auth_uri";
034  private static final String PROPERTY_SERVICE_USERS_FACEBOOK_OAUTH2_REDIRECT_URI = PROPERTY_SERVICE_USERS_FACEBOOK+".facebook_oauth2_redirect_uri";
035  private static final String PROPERTY_SERVICE_USERS_FACEBOOK_OAUTH2_SCOPE = PROPERTY_SERVICE_USERS_FACEBOOK+".facebook_oauth2_scope";
036  private static final String PROPERTY_SERVICE_USERS_FACEBOOK_SHARED_KEY = PROPERTY_SERVICE_USERS_FACEBOOK+".facebook_shared_key";
037  private static final String PROPERTY_SERVICE_USERS_FACEBOOK_OAUTH2_TOKEN_URI = PROPERTY_SERVICE_USERS_FACEBOOK+".facebook_oauth2_token_uri";
038  private static final String PROPERTY_SERVICE_USERS_FACEBOOK_TOKEN_AUTOREFRESH = PROPERTY_SERVICE_USERS_FACEBOOK+".facebook_token_autorefresh";
039  private static final String PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_USER_INFO_URI = PROPERTY_SERVICE_USERS_FACEBOOK+".facebook_oauth2_user_info_uri";
040  private String _applicationId = null;
041  private String _encodedOAuth2RedirectUri = null;
042  private String _oAuth2AuthUri = null;
043  private String _oAuth2RedirectUri = null;
044  private String _oAuth2Scope = null;
045  private String _oAuth2TokenUri = null;
046  private String _oAuth2UserInfoUri = null;
047  private String _sharedKey = null;
048  private long _tokenAutorefresh = -1;
049  
050  @Override
051  public void initialize(Properties properties) throws IllegalArgumentException{
052    _applicationId = properties.getProperty(PROPERTY_SERVICE_USERS_FACEBOOK_APPLICATION_ID);
053    _sharedKey = properties.getProperty(PROPERTY_SERVICE_USERS_FACEBOOK_SHARED_KEY);
054    if(_applicationId == null || _sharedKey == null){
055      throw new IllegalArgumentException(PROPERTY_SERVICE_USERS_FACEBOOK_APPLICATION_ID+" or "+PROPERTY_SERVICE_USERS_FACEBOOK_SHARED_KEY+" is missing.");
056    }
057    _oAuth2Scope = properties.getProperty(PROPERTY_SERVICE_USERS_FACEBOOK_OAUTH2_SCOPE);
058    _oAuth2AuthUri = properties.getProperty(PROPERTY_SERVICE_USERS_FACEBOOK_OAUTH2_AUTH_URI);
059    _oAuth2TokenUri = properties.getProperty(PROPERTY_SERVICE_USERS_FACEBOOK_OAUTH2_TOKEN_URI);
060    _oAuth2RedirectUri = properties.getProperty(PROPERTY_SERVICE_USERS_FACEBOOK_OAUTH2_REDIRECT_URI);
061    _oAuth2UserInfoUri = properties.getProperty(PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_USER_INFO_URI);
062    if(_oAuth2AuthUri == null || _oAuth2Scope == null || _oAuth2RedirectUri == null || _oAuth2TokenUri == null || _oAuth2UserInfoUri == null){
063      throw new IllegalArgumentException(PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_USER_INFO_URI+", "+PROPERTY_SERVICE_USERS_FACEBOOK_OAUTH2_TOKEN_URI+", "+PROPERTY_SERVICE_USERS_FACEBOOK_OAUTH2_AUTH_URI+", "+PROPERTY_SERVICE_USERS_FACEBOOK_OAUTH2_REDIRECT_URI+" or "+PROPERTY_SERVICE_USERS_FACEBOOK_OAUTH2_SCOPE+" is missing.");
064    }
065    try {
066      _encodedOAuth2RedirectUri  = (new URLCodec(core.tut.pori.http.Definitions.ENCODING_UTF8)).encode(_oAuth2RedirectUri);
067    } catch (EncoderException ex) {
068      Logger.getLogger(getClass()).error(ex, ex);
069      throw new IllegalArgumentException("Bad "+PROPERTY_SERVICE_USERS_FACEBOOK_OAUTH2_REDIRECT_URI);
070    }
071    try{
072      _tokenAutorefresh = Long.parseLong(properties.getProperty(PROPERTY_SERVICE_USERS_FACEBOOK_TOKEN_AUTOREFRESH));
073    } catch (NumberFormatException ex){
074      Logger.getLogger(getClass()).error(ex, ex);
075      throw new IllegalArgumentException("Bad "+PROPERTY_SERVICE_USERS_FACEBOOK_TOKEN_AUTOREFRESH);
076    }
077  }
078
079  /**
080   * @return the applicationId
081   */
082  public String getApplicationId() {
083    return _applicationId;
084  }
085
086  /**
087   * @return the sharedKey
088   */
089  public String getSharedKey() {
090    return _sharedKey;
091  }
092
093  /**
094   * @return the oAuth2Scope
095   */
096  public String getoAuth2Scope() {
097    return _oAuth2Scope;
098  }
099
100  /**
101   * @return the oAuth2RedirectUri
102   */
103  public String getoAuth2RedirectUri() {
104    return _oAuth2RedirectUri;
105  }
106
107  /**
108   * @return the encodedOAuth2RedirectUri
109   */
110  public String getEncodedOAuth2RedirectUri() {
111    return _encodedOAuth2RedirectUri;
112  }
113
114  /**
115   * @return the oAuth2AuthUri
116   */
117  public String getoAuth2AuthUri() {
118    return _oAuth2AuthUri;
119  }
120
121  /**
122   * @return the oAuth2TokenUri
123   */
124  public String getoAuth2TokenUri() {
125    return _oAuth2TokenUri;
126  }
127
128  /**
129   * @return the tokenAutorefresh
130   */
131  public long getTokenAutorefresh() {
132    return _tokenAutorefresh;
133  }
134
135  /**
136   * @return the oAuth2UserInfoUri
137   */
138  public String getoAuth2UserInfoUri() {
139    return _oAuth2UserInfoUri;
140  }
141  
142  @Override
143  public String getPropertyFilePath() {
144    return CONFIGURATION_FILE_PATH+"facebook.properties";
145  }
146}