Skip to content

Commit

Permalink
add more check for push notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
lupatellimatteo authored and lupatellimatteo committed Aug 20, 2014
1 parent 0f48368 commit a9bf605
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 42 deletions.
4 changes: 2 additions & 2 deletions app/com/baasbox/configuration/IosCertificateHandler.java
Expand Up @@ -110,8 +110,8 @@ public static void init(){
if(!f.exists()){
f.mkdirs();
}
ConfigurationFileContainer prod = Push.DEFAULT_PRODUCTION_IOS_CERTIFICATE.getValueAsFileContainer();
ConfigurationFileContainer sandbox = Push.DEFAULT_SANDBOX_IOS_CERTIFICATE.getValueAsFileContainer();
ConfigurationFileContainer prod = Push.PROFILE1_PRODUCTION_IOS_CERTIFICATE.getValueAsFileContainer();
ConfigurationFileContainer sandbox = Push.PROFILE1_SANDBOX_IOS_CERTIFICATE.getValueAsFileContainer();

ConfigurationFileContainer prod2 = Push.PROFILE2_PRODUCTION_IOS_CERTIFICATE.getValueAsFileContainer();
ConfigurationFileContainer sandbox2 = Push.PROFILE2_SANDBOX_IOS_CERTIFICATE.getValueAsFileContainer();
Expand Down
Expand Up @@ -35,6 +35,8 @@
import play.Logger;

import com.baasbox.exception.ConfigurationException;
import com.baasbox.service.push.PushSwitchException;
import com.baasbox.service.push.providers.PushNotInitializedException;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMap;

Expand Down Expand Up @@ -287,15 +289,20 @@ public static Object findByKey(String completeKey) throws ConfigurationException
* @param iKey
* @param value
* @throws ConfigurationException
* @throws PushNotInitializedException
* @throws PushSwitchException
* @throws Exception
*/
public static void setByKey(Class en,String iKey,Object value) throws IllegalStateException,ConfigurationException {
public static void setByKey(Class en,String iKey,Object value) throws IllegalStateException,ConfigurationException, PushNotInitializedException, PushSwitchException {
Object enumValue = findByKey(en,iKey);
try {
en.getMethod("setValue",Object.class).invoke(enumValue,value);

}catch (Exception e) {
if (e.getCause() instanceof IllegalStateException) throw new IllegalStateException(e.getCause());
if (e.getCause() instanceof PushNotInitializedException) throw new PushNotInitializedException(e.getCause());
if (e.getCause() instanceof PushSwitchException) throw new PushSwitchException(e.getCause());

throw new ConfigurationException ("Invalid key -" +iKey+ "- or value -" +value+"-" ,e );
}
} //setByKey
Expand Down
74 changes: 61 additions & 13 deletions app/com/baasbox/configuration/Push.java
Expand Up @@ -27,21 +27,22 @@
import play.Logger;

import com.baasbox.configuration.index.IndexPushConfiguration;
import com.baasbox.service.push.PushSwitchException;
import com.baasbox.service.push.providers.PushNotInitializedException;
import com.baasbox.util.ConfigurationFileContainer;


public enum Push implements IProperties {
//DEFAULT PROFILE or FIRST
DEFAULT_PUSH_SANDBOX_ENABLE("default.push.sandbox.enable", "The value to verify if BaasBox needs to contact the SANDBOX server or the PRODUCTION server for default profile", Boolean.class),
DEFAULT_PUSH_APPLE_TIMEOUT("default.push.apple.timeout", "The timeout for push notifications on Apple devices for default profile", Integer.class),
DEFAULT_SANDBOX_ANDROID_API_KEY("default.sandbox.android.api.key", "The key to send push notifications to Android devices in SANDBOX mode for default profile", String.class),
DEFAULT_SANDBOX_IOS_CERTIFICATE("default.sandbox.ios.certificate", "The Apple certificate in SANDBOX mode for default profile", ConfigurationFileContainer.class,new IosCertificateHandler()),
DEFAULT_SANDBOX_IOS_CERTIFICATE_PASSWORD("default.sandbox.ios.certificate.password", "The password of the Apple certificate in SANDBOX mode for default profile", String.class),
DEFAULT_PRODUCTION_ANDROID_API_KEY("default.production.android.api.key", "The key to send push notifications to Android devices in PRODUCTION mode for default profile", String.class),
DEFAULT_PRODUCTION_IOS_CERTIFICATE("default.production.ios.certificate", "The Apple certificate in PRODUCTION mode for default profile", ConfigurationFileContainer.class,new IosCertificateHandler()),
DEFAULT_PRODUCTION_IOS_CERTIFICATE_PASSWORD("default.production.ios.certificate.password", "The password of the Apple certificate in PRODUCTION mode for default profile", String.class),
DEFAULT_PUSH_PROFILE_ENABLE("default.push.profile.enable","Enable this profile",Boolean.class),
PROFILE1_PUSH_SANDBOX_ENABLE("profile1.push.sandbox.enable", "The value to verify if BaasBox needs to contact the SANDBOX server or the PRODUCTION server for first profile", Boolean.class),
PROFILE1_PUSH_APPLE_TIMEOUT("profile1.push.apple.timeout", "The timeout for push notifications on Apple devices for first profile", Integer.class),
PROFILE1_SANDBOX_ANDROID_API_KEY("profile1.sandbox.android.api.key", "The key to send push notifications to Android devices in SANDBOX mode for first profile", String.class),
PROFILE1_SANDBOX_IOS_CERTIFICATE("profile1.sandbox.ios.certificate", "The Apple certificate in SANDBOX mode for first profile", ConfigurationFileContainer.class,new IosCertificateHandler()),
PROFILE1_SANDBOX_IOS_CERTIFICATE_PASSWORD("profile1.sandbox.ios.certificate.password", "The password of the Apple certificate in SANDBOX mode for first profile", String.class),
PROFILE1_PRODUCTION_ANDROID_API_KEY("profile1.production.android.api.key", "The key to send push notifications to Android devices in PRODUCTION mode for first profile", String.class),
PROFILE1_PRODUCTION_IOS_CERTIFICATE("profile1.production.ios.certificate", "The Apple certificate in PRODUCTION mode for first profile", ConfigurationFileContainer.class,new IosCertificateHandler()),
PROFILE1_PRODUCTION_IOS_CERTIFICATE_PASSWORD("profile1.production.ios.certificate.password", "The password of the Apple certificate in PRODUCTION mode for first profile", String.class),
PROFILE1_PUSH_PROFILE_ENABLE("profile1.push.profile.enable","Enable this profile",Boolean.class),

//SECOND PROFILE
PROFILE2_PUSH_SANDBOX_ENABLE("profile2.push.sandbox.enable", "The value to verify if BaasBox needs to contact the SANDBOX server or the PRODUCTION server for second profile", Boolean.class),
Expand Down Expand Up @@ -94,21 +95,67 @@ public enum Push implements IProperties {
@Override
public void setValue(Object newValue) throws Exception{
if (!editable) throw new IllegalStateException("The value cannot be changed");
if(this.key.equals("default.push.profile.enable")) {
if(Push.DEFAULT_PUSH_SANDBOX_ENABLE.getValueAsBoolean()){
if((!Push.DEFAULT_SANDBOX_ANDROID_API_KEY.getValue().equals(""))||((Push.DEFAULT_SANDBOX_IOS_CERTIFICATE.getValue()!=null) && (!Push.DEFAULT_SANDBOX_IOS_CERTIFICATE_PASSWORD.getValue().equals("")))){

if(this.key.equals("profile1.push.sandbox.enable")) {
if(this.getValue()==null) _setValue(newValue);
if(!this.getValueAsBoolean()) {
if((!Push.PROFILE1_SANDBOX_ANDROID_API_KEY.getValue().equals(""))||((Push.PROFILE1_SANDBOX_IOS_CERTIFICATE.getValue()!=null) && (!Push.PROFILE1_SANDBOX_IOS_CERTIFICATE_PASSWORD.getValue().equals("")))){
_setValue(newValue);
}
else throw new PushSwitchException("Cannot switch, because settings are missing");
}
else if((!Push.PROFILE1_PRODUCTION_ANDROID_API_KEY.getValue().equals(""))||((Push.PROFILE1_PRODUCTION_IOS_CERTIFICATE.getValue()!=null) && (!Push.PROFILE1_PRODUCTION_IOS_CERTIFICATE_PASSWORD.getValue().equals("")))){
_setValue(newValue);
}
else throw new PushSwitchException("Cannot switch, because settings are missing");
}

else if(this.key.equals("profile2.push.sandbox.enable")) {
if(this.getValue()==null) _setValue(newValue);
if(!this.getValueAsBoolean()) {
if((!Push.PROFILE2_SANDBOX_ANDROID_API_KEY.getValue().equals(""))||((Push.PROFILE2_SANDBOX_IOS_CERTIFICATE.getValue()!=null) && (!Push.PROFILE2_SANDBOX_IOS_CERTIFICATE_PASSWORD.getValue().equals("")))){
_setValue(newValue);
}
else throw new PushSwitchException("Cannot switch, because settings are missing");
}
else if((!Push.PROFILE2_PRODUCTION_ANDROID_API_KEY.getValue().equals(""))||((Push.PROFILE2_PRODUCTION_IOS_CERTIFICATE.getValue()!=null) && (!Push.PROFILE2_PRODUCTION_IOS_CERTIFICATE_PASSWORD.getValue().equals("")))){
_setValue(newValue);
}
else throw new PushSwitchException("Cannot switch, because settings are missing");
}

else if(this.key.equals("profile3.push.sandbox.enable")) {
if(this.getValue()==null) _setValue(newValue);
if(!this.getValueAsBoolean()) {
if((!Push.PROFILE3_SANDBOX_ANDROID_API_KEY.getValue().equals(""))||((Push.PROFILE3_SANDBOX_IOS_CERTIFICATE.getValue()!=null) && (!Push.PROFILE3_SANDBOX_IOS_CERTIFICATE_PASSWORD.getValue().equals("")))){
_setValue(newValue);
}
else throw new PushSwitchException("Cannot switch, because settings are missing");
}
else if((!Push.PROFILE3_PRODUCTION_ANDROID_API_KEY.getValue().equals(""))||((Push.PROFILE3_PRODUCTION_IOS_CERTIFICATE.getValue()!=null) && (!Push.PROFILE3_PRODUCTION_IOS_CERTIFICATE_PASSWORD.getValue().equals("")))){
_setValue(newValue);
}
else throw new PushSwitchException("Cannot switch, because settings are missing");
}


else if(this.key.equals("profile1.push.profile.enable")) {
if(this.getValue()==null) _setValue(newValue);
if(Push.PROFILE1_PUSH_SANDBOX_ENABLE.getValueAsBoolean()){
if((!Push.PROFILE1_SANDBOX_ANDROID_API_KEY.getValue().equals(""))||((Push.PROFILE1_SANDBOX_IOS_CERTIFICATE.getValue()!=null) && (!Push.PROFILE1_SANDBOX_IOS_CERTIFICATE_PASSWORD.getValue().equals("")))){
_setValue(newValue);
}
else throw new PushNotInitializedException("Configuration not initialized");
}
else if((!Push.DEFAULT_PRODUCTION_ANDROID_API_KEY.getValue().equals(""))||((Push.DEFAULT_PRODUCTION_IOS_CERTIFICATE.getValue()!=null) && (!Push.DEFAULT_PRODUCTION_IOS_CERTIFICATE_PASSWORD.getValue().equals("")))){
else if((!Push.PROFILE1_PRODUCTION_ANDROID_API_KEY.getValue().equals(""))||((Push.PROFILE1_PRODUCTION_IOS_CERTIFICATE.getValue()!=null) && (!Push.PROFILE1_PRODUCTION_IOS_CERTIFICATE_PASSWORD.getValue().equals("")))){
_setValue(newValue);
}
else throw new PushNotInitializedException("Configuration not initialized");

}

else if(this.key.equals("profile2.push.profile.enable")) {
if(this.getValue()==null) _setValue(newValue);
if(Push.PROFILE2_PUSH_SANDBOX_ENABLE.getValueAsBoolean()){
if((!Push.PROFILE2_SANDBOX_ANDROID_API_KEY.getValue().equals(""))||((Push.PROFILE2_SANDBOX_IOS_CERTIFICATE.getValue()!=null) && (!Push.PROFILE2_SANDBOX_IOS_CERTIFICATE_PASSWORD.getValue().equals("")))){
_setValue(newValue);
Expand All @@ -123,6 +170,7 @@ else if((!Push.PROFILE2_PRODUCTION_ANDROID_API_KEY.getValue().equals(""))||((Pus
}

else if(this.key.equals("profile3.push.profile.enable")) {
if(this.getValue()==null) _setValue(newValue);
if(Push.PROFILE3_PUSH_SANDBOX_ENABLE.getValueAsBoolean()){
if((!Push.PROFILE3_SANDBOX_ANDROID_API_KEY.getValue().equals(""))||((Push.PROFILE3_SANDBOX_IOS_CERTIFICATE.getValue()!=null) && (!Push.PROFILE3_SANDBOX_IOS_CERTIFICATE_PASSWORD.getValue().equals("")))){
_setValue(newValue);
Expand Down
8 changes: 7 additions & 1 deletion app/com/baasbox/controllers/Admin.java
Expand Up @@ -72,6 +72,8 @@
import com.baasbox.exception.UserNotFoundException;
import com.baasbox.service.dbmanager.DbManagerService;
import com.baasbox.service.permissions.PermissionTagService;
import com.baasbox.service.push.PushSwitchException;
import com.baasbox.service.push.providers.PushNotInitializedException;
import com.baasbox.service.storage.CollectionService;
import com.baasbox.service.storage.StatisticsService;
import com.baasbox.service.user.RoleService;
Expand Down Expand Up @@ -501,7 +503,7 @@ public static Result dumpConfiguration(String returnType){
return ok(dump);
}

public static Result setConfiguration(String section, String subSection, String key, String value){
public static Result setConfiguration(String section, String subSection, String key, String value) throws PushNotInitializedException, PushSwitchException{

Class conf = PropertiesConfigurationHelper.CONFIGURATION_SECTIONS.get(section);
if (conf==null) return notFound(section + " is not a valid configuration section");
Expand Down Expand Up @@ -548,6 +550,10 @@ public static Result setConfiguration(String section, String subSection, String

} catch (ConfigurationException e) {
return badRequest(e.getMessage());
} catch (PushNotInitializedException e) {
return status(CustomHttpCode.PUSH_CONFIG_INVALID.getBbCode(), CustomHttpCode.PUSH_CONFIG_INVALID.getDescription());
} catch (PushSwitchException e) {
return status(CustomHttpCode.PUSH_SWITCH_EXCEPTION.getBbCode(),CustomHttpCode.PUSH_SWITCH_EXCEPTION.getDescription());
}catch (IllegalStateException e) {
return badRequest("This configuration value is not editable");
}
Expand Down
2 changes: 2 additions & 0 deletions app/com/baasbox/controllers/CustomHttpCode.java
Expand Up @@ -32,6 +32,8 @@ public enum CustomHttpCode {
SESSION_TOKEN_EXPIRED (40101,401,"Authentication info not valid or not provided. HINT: is your session expired?","error"),
PUSH_PROFILE_DISABLED(40011,400,"Push profile disabled","error"),
PUSH_PROFILE_INVALID(40012,400,"Push profile invalid. Accepted values are 1,2 or 3","error"),
PUSH_PROFILE_ARRAY_EXCEPTION(40013,400,"Push profile array too big. Max three values are accepted","error"),
PUSH_SWITCH_EXCEPTION(40014,400,"Cannot switch, because settings for the selected mode are missing","error"),
PUSH_CONFIG_INVALID (50301,503,"Push settings are not properly configured. HINT: go to administration console and check the settings","error"),
PUSH_HOST_UNREACHABLE(50302,503,"Could not resolve host. HINT: check your internet connection","error"),
PUSH_INVALID_REQUEST (50303,503,"Could not send push notifications. HINT: Check your API Key(Google)","error");
Expand Down
5 changes: 5 additions & 0 deletions app/com/baasbox/controllers/Push.java
Expand Up @@ -52,6 +52,7 @@
import com.baasbox.exception.UserNotFoundException;
import com.baasbox.security.SessionKeys;
import com.baasbox.security.SessionTokenProvider;
import com.baasbox.service.push.PushProfileArrayException;
import com.baasbox.service.push.PushProfileDisabledException;
import com.baasbox.service.push.PushProfileInvalidException;
import com.baasbox.service.push.PushService;
Expand Down Expand Up @@ -111,6 +112,10 @@ public static Result send(String username) throws Exception {
Logger.error(e.getMessage());
return status(CustomHttpCode.PUSH_PROFILE_INVALID.getBbCode(),CustomHttpCode.PUSH_PROFILE_INVALID.getDescription());
}
catch (PushProfileArrayException e) {
Logger.error(e.getMessage());
return status(CustomHttpCode.PUSH_PROFILE_ARRAY_EXCEPTION.getBbCode(),CustomHttpCode.PUSH_PROFILE_ARRAY_EXCEPTION.getDescription());
}
catch (UnknownHostException e){
Logger.error(e.getMessage());
return status(CustomHttpCode.PUSH_HOST_UNREACHABLE.getBbCode(),CustomHttpCode.PUSH_HOST_UNREACHABLE.getDescription());
Expand Down
24 changes: 24 additions & 0 deletions app/com/baasbox/service/push/PushProfileArrayException.java
@@ -0,0 +1,24 @@
package com.baasbox.service.push;

import com.baasbox.exception.BaasBoxPushException;

public class PushProfileArrayException extends BaasBoxPushException {
public PushProfileArrayException(String message){
super(message);
}

public PushProfileArrayException() {
super();
// TODO Auto-generated constructor stub
}

public PushProfileArrayException(String arg0, Throwable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}

public PushProfileArrayException(Throwable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
}
46 changes: 35 additions & 11 deletions app/com/baasbox/service/push/PushService.java
Expand Up @@ -88,22 +88,22 @@ else if(pushProfile==3){
}

}
else if (Push.DEFAULT_PUSH_SANDBOX_ENABLE.getValueAsBoolean()){
else if (Push.PROFILE1_PUSH_SANDBOX_ENABLE.getValueAsBoolean()){
if (Logger.isDebugEnabled()) Logger.debug("Push profile choosen for sandbox environment: 1(default)");
response = ImmutableMap.of(
ConfigurationKeys.ANDROID_API_KEY, ""+Push.DEFAULT_SANDBOX_ANDROID_API_KEY.getValueAsString(),
ConfigurationKeys.APPLE_TIMEOUT, ""+Push.DEFAULT_PUSH_APPLE_TIMEOUT.getValueAsString(),
ConfigurationKeys.IOS_CERTIFICATE, ""+Push.DEFAULT_SANDBOX_IOS_CERTIFICATE.getValueAsString(),
ConfigurationKeys.IOS_CERTIFICATE_PASSWORD, ""+Push.DEFAULT_SANDBOX_IOS_CERTIFICATE_PASSWORD.getValueAsString(),
ConfigurationKeys.ANDROID_API_KEY, ""+Push.PROFILE1_SANDBOX_ANDROID_API_KEY.getValueAsString(),
ConfigurationKeys.APPLE_TIMEOUT, ""+Push.PROFILE1_PUSH_APPLE_TIMEOUT.getValueAsString(),
ConfigurationKeys.IOS_CERTIFICATE, ""+Push.PROFILE1_SANDBOX_IOS_CERTIFICATE.getValueAsString(),
ConfigurationKeys.IOS_CERTIFICATE_PASSWORD, ""+Push.PROFILE1_SANDBOX_IOS_CERTIFICATE_PASSWORD.getValueAsString(),
ConfigurationKeys.IOS_SANDBOX,""+Boolean.TRUE.toString()
);
}else{
if (Logger.isDebugEnabled()) Logger.debug("Push profile choosen for production environment: 1(default)");
response = ImmutableMap.of(
ConfigurationKeys.ANDROID_API_KEY, ""+Push.DEFAULT_PRODUCTION_ANDROID_API_KEY.getValueAsString(),
ConfigurationKeys.APPLE_TIMEOUT, ""+Push.DEFAULT_PUSH_APPLE_TIMEOUT.getValueAsString(),
ConfigurationKeys.IOS_CERTIFICATE,""+ Push.DEFAULT_PRODUCTION_IOS_CERTIFICATE.getValueAsString(),
ConfigurationKeys.IOS_CERTIFICATE_PASSWORD, ""+Push.DEFAULT_PRODUCTION_IOS_CERTIFICATE_PASSWORD.getValueAsString(),
ConfigurationKeys.ANDROID_API_KEY, ""+Push.PROFILE1_PRODUCTION_ANDROID_API_KEY.getValueAsString(),
ConfigurationKeys.APPLE_TIMEOUT, ""+Push.PROFILE1_PUSH_APPLE_TIMEOUT.getValueAsString(),
ConfigurationKeys.IOS_CERTIFICATE,""+ Push.PROFILE1_PRODUCTION_IOS_CERTIFICATE.getValueAsString(),
ConfigurationKeys.IOS_CERTIFICATE_PASSWORD, ""+Push.PROFILE1_PRODUCTION_IOS_CERTIFICATE_PASSWORD.getValueAsString(),
ConfigurationKeys.IOS_SANDBOX,""+Boolean.FALSE.toString()
);
}
Expand Down Expand Up @@ -144,10 +144,34 @@ public void send(String message, String username, List<Integer> pushProfiles, Js
}//send

public boolean validate(List<Integer> pushProfiles) throws IOException, BaasBoxPushException {
if (pushProfiles.size()>3) throw new IOException("Too many push profiles");
if (pushProfiles.size()>3) throw new PushProfileArrayException("Too many push profiles");
for(Integer pushProfile : pushProfiles) {
if((pushProfile!=1) && (pushProfile!=2) && (pushProfile!=3)) throw new PushProfileInvalidException("Error with profiles (accepted values are:1,2 or 3)");
if((pushProfile==1) && (!Push.DEFAULT_PUSH_PROFILE_ENABLE.getValueAsBoolean())) throw new PushProfileDisabledException("Profile not enabled");
if((pushProfile==1) && (!Push.PROFILE1_PUSH_PROFILE_ENABLE.getValueAsBoolean())) throw new PushProfileDisabledException("Profile not enabled");
if((pushProfile==2) && (!Push.PROFILE2_PUSH_PROFILE_ENABLE.getValueAsBoolean())) throw new PushProfileDisabledException("Profile not enabled");
if((pushProfile==3) && (!Push.PROFILE3_PUSH_PROFILE_ENABLE.getValueAsBoolean())) throw new PushProfileDisabledException("Profile not enabled");
}

return true;
}

public boolean validateUser(List<Integer> pushProfiles, VendorOS vendor) throws PushProfileInvalidException, PushProfileDisabledException, PushNotInitializedException {
for(Integer pushProfile : pushProfiles) {
if((pushProfile!=1) && (pushProfile!=2) && (pushProfile!=3)) throw new PushProfileInvalidException("Error with profiles (accepted values are:1,2 or 3)");
switch(vendor) {
case IOS:
if(pushProfile==1) {
if(Push.PROFILE1_PUSH_SANDBOX_ENABLE.getValueAsBoolean()){
if((Push.PROFILE1_SANDBOX_IOS_CERTIFICATE.getValue()==null) && (Push.PROFILE1_SANDBOX_IOS_CERTIFICATE_PASSWORD.getValue().equals(""))){
throw new PushNotInitializedException("Configuration not initialized");
}
}
else if((Push.PROFILE1_PRODUCTION_IOS_CERTIFICATE.getValue()==null) && (Push.PROFILE1_PRODUCTION_IOS_CERTIFICATE_PASSWORD.getValue().equals(""))){
throw new PushNotInitializedException("Configuration not initialized");
}
}
}

if((pushProfile==2) && (!Push.PROFILE2_PUSH_PROFILE_ENABLE.getValueAsBoolean())) throw new PushProfileDisabledException("Profile not enabled");
if((pushProfile==3) && (!Push.PROFILE3_PUSH_PROFILE_ENABLE.getValueAsBoolean())) throw new PushProfileDisabledException("Profile not enabled");
}
Expand Down

0 comments on commit a9bf605

Please sign in to comment.