Example usage for com.google.common.primitives Booleans join

List of usage examples for com.google.common.primitives Booleans join

Introduction

In this page you can find the example usage for com.google.common.primitives Booleans join.

Prototype

public static String join(String separator, boolean... array) 

Source Link

Document

Returns a string containing the supplied boolean values separated by separator .

Usage

From source file:com.baasbox.controllers.Push.java

public static Result sendUsers() throws Exception {
    boolean verbose = false;
    try {/*from   w  w w .  j a v  a2  s.  co  m*/

        if (BaasBoxLogger.isTraceEnabled())
            BaasBoxLogger.trace("Method Start");
        PushLogger pushLogger = PushLogger.getInstance().init();
        if (UserService.isAnAdmin(DbHelper.currentUsername())) {
            pushLogger.enable();
        } else {
            pushLogger.disable();
        }
        if (request().getQueryString("verbose") != null
                && request().getQueryString("verbose").equalsIgnoreCase("true"))
            verbose = true;

        Http.RequestBody body = request().body();
        JsonNode bodyJson = body.asJson(); //{"message":"Text"}
        if (BaasBoxLogger.isTraceEnabled())
            BaasBoxLogger.trace("send bodyJson: " + bodyJson);
        if (bodyJson == null)
            return status(CustomHttpCode.JSON_PAYLOAD_NULL.getBbCode(),
                    CustomHttpCode.JSON_PAYLOAD_NULL.getDescription());
        pushLogger.addMessage("Payload received: %s", bodyJson.toString());
        JsonNode messageNode = bodyJson.findValue("message");
        pushLogger.addMessage("Payload message received: %s",
                messageNode == null ? "null" : messageNode.asText());
        if (messageNode == null)
            return status(CustomHttpCode.PUSH_MESSAGE_INVALID.getBbCode(),
                    CustomHttpCode.PUSH_MESSAGE_INVALID.getDescription());
        if (!messageNode.isTextual())
            return status(CustomHttpCode.PUSH_MESSAGE_INVALID.getBbCode(),
                    CustomHttpCode.PUSH_MESSAGE_INVALID.getDescription());

        String message = messageNode.asText();

        JsonNode usernamesNodes = bodyJson.get("users");
        pushLogger.addMessage("users: %s", usernamesNodes == null ? "null" : usernamesNodes.toString());

        List<String> usernames = new ArrayList<String>();

        if (!(usernamesNodes == null)) {

            if (!(usernamesNodes.isArray()))
                return status(CustomHttpCode.PUSH_USERS_FORMAT_INVALID.getBbCode(),
                        CustomHttpCode.PUSH_USERS_FORMAT_INVALID.getDescription());

            for (JsonNode usernamesNode : usernamesNodes) {
                usernames.add(usernamesNode.asText());
            }

            HashSet<String> hs = new HashSet<String>();
            hs.addAll(usernames);
            usernames.clear();
            usernames.addAll(hs);
            pushLogger.addMessage("Users extracted: %s", Joiner.on(",").join(usernames));
        } else {
            return status(CustomHttpCode.PUSH_NOTFOUND_KEY_USERS.getBbCode(),
                    CustomHttpCode.PUSH_NOTFOUND_KEY_USERS.getDescription());
        }

        JsonNode pushProfilesNodes = bodyJson.get("profiles");
        pushLogger.addMessage("profiles: %s",
                pushProfilesNodes == null ? "null" : pushProfilesNodes.toString());

        List<Integer> pushProfiles = new ArrayList<Integer>();
        if (!(pushProfilesNodes == null)) {
            if (!(pushProfilesNodes.isArray()))
                return status(CustomHttpCode.PUSH_PROFILE_FORMAT_INVALID.getBbCode(),
                        CustomHttpCode.PUSH_PROFILE_FORMAT_INVALID.getDescription());
            for (JsonNode pushProfileNode : pushProfilesNodes) {
                if (pushProfileNode.isTextual())
                    return status(CustomHttpCode.PUSH_PROFILE_FORMAT_INVALID.getBbCode(),
                            CustomHttpCode.PUSH_PROFILE_FORMAT_INVALID.getDescription());
                pushProfiles.add(pushProfileNode.asInt());
            }

            HashSet<Integer> hs = new HashSet<Integer>();
            hs.addAll(pushProfiles);
            pushProfiles.clear();
            pushProfiles.addAll(hs);
        } else {
            pushProfiles.add(1);
        }
        pushLogger.addMessage("Profiles computed: %s", Joiner.on(",").join(pushProfiles));

        boolean[] withError = new boolean[6];
        PushService ps = new PushService();
        Result toRet = null;
        try {
            boolean isValid = (ps.validate(pushProfiles));
            pushLogger.addMessage("Profiles validation: %s", isValid);
            if (isValid)
                withError = ps.send(message, usernames, pushProfiles, bodyJson, withError);
            pushLogger.addMessage("Service result: %s", Booleans.join(", ", withError));
        } catch (UserNotFoundException e) {
            return notFound("Username not found");
        } catch (SqlInjectionException e) {
            return badRequest("The supplied name appears invalid (Sql Injection Attack detected)");
        } catch (PushNotInitializedException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_CONFIG_INVALID.getBbCode(),
                    CustomHttpCode.PUSH_CONFIG_INVALID.getDescription());
        } catch (PushProfileDisabledException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_PROFILE_DISABLED.getBbCode(),
                    CustomHttpCode.PUSH_PROFILE_DISABLED.getDescription());
        } catch (PushProfileInvalidException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_PROFILE_FORMAT_INVALID.getBbCode(),
                    CustomHttpCode.PUSH_PROFILE_FORMAT_INVALID.getDescription());
        } catch (PushInvalidApiKeyException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_INVALID_APIKEY.getBbCode(),
                    CustomHttpCode.PUSH_INVALID_APIKEY.getDescription());
        } catch (UnknownHostException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_HOST_UNREACHABLE.getBbCode(),
                    CustomHttpCode.PUSH_HOST_UNREACHABLE.getDescription());
        } catch (InvalidRequestException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_INVALID_REQUEST.getBbCode(),
                    CustomHttpCode.PUSH_INVALID_REQUEST.getDescription());
        } catch (IOException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return badRequest(ExceptionUtils.getMessage(e));
        } catch (PushSoundKeyFormatException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_SOUND_FORMAT_INVALID.getBbCode(),
                    CustomHttpCode.PUSH_SOUND_FORMAT_INVALID.getDescription());
        } catch (PushBadgeFormatException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_BADGE_FORMAT_INVALID.getBbCode(),
                    CustomHttpCode.PUSH_BADGE_FORMAT_INVALID.getDescription());
        } catch (PushActionLocalizedKeyFormatException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_ACTION_LOCALIZED_KEY_FORMAT_INVALID.getBbCode(),
                    CustomHttpCode.PUSH_ACTION_LOCALIZED_KEY_FORMAT_INVALID.getDescription());
        } catch (PushLocalizedKeyFormatException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_LOCALIZED_KEY_FORMAT_INVALID.getBbCode(),
                    CustomHttpCode.PUSH_LOCALIZED_ARGUMENTS_FORMAT_INVALID.getDescription());
        } catch (PushLocalizedArgumentsFormatException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_LOCALIZED_ARGUMENTS_FORMAT_INVALID.getBbCode(),
                    CustomHttpCode.PUSH_LOCALIZED_ARGUMENTS_FORMAT_INVALID.getDescription());
        } catch (PushCollapseKeyFormatException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_COLLAPSE_KEY_FORMAT_INVALID.getBbCode(),
                    CustomHttpCode.PUSH_COLLAPSE_KEY_FORMAT_INVALID.getDescription());
        } catch (PushTimeToLiveFormatException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_TIME_TO_LIVE_FORMAT_INVALID.getBbCode(),
                    CustomHttpCode.PUSH_TIME_TO_LIVE_FORMAT_INVALID.getDescription());
        } catch (PushContentAvailableFormatException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_CONTENT_AVAILABLE_FORMAT_INVALID.getBbCode(),
                    CustomHttpCode.PUSH_CONTENT_AVAILABLE_FORMAT_INVALID.getDescription());
        } catch (PushCategoryFormatException e) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(e));
            return status(CustomHttpCode.PUSH_CATEGORY_FORMAT_INVALID.getBbCode(),
                    CustomHttpCode.PUSH_CATEGORY_FORMAT_INVALID.getDescription());
        }
        if (BaasBoxLogger.isTraceEnabled())
            BaasBoxLogger.trace("Method End");

        for (int i = 0; i < withError.length; i++) {
            if (withError[i] == true)
                return status(CustomHttpCode.PUSH_SENT_WITH_ERROR.getBbCode(),
                        CustomHttpCode.PUSH_SENT_WITH_ERROR.getDescription());
        }
        PushLogger.getInstance().messages();
        if (UserService.isAnAdmin(DbHelper.currentUsername()) && verbose) {
            return ok(toJson(PushLogger.getInstance().messages()));
        } else {
            return ok("Push Notification(s) has been sent");
        }
    } finally {
        if (UserService.isAnAdmin(DbHelper.currentUsername()) && verbose) {
            return ok(toJson(PushLogger.getInstance().messages()));
        }
        BaasBoxLogger.debug("Push execution flow:\n{}", PushLogger.getInstance().toString());
    }
}