Example usage for org.springframework.web.util UriUtils encodeScheme

List of usage examples for org.springframework.web.util UriUtils encodeScheme

Introduction

In this page you can find the example usage for org.springframework.web.util UriUtils encodeScheme.

Prototype

public static String encodeScheme(String scheme, Charset charset) 

Source Link

Document

Encode the given URI scheme with the given encoding.

Usage

From source file:fr.gael.dhus.sync.impl.ODataUserSynchronizer.java

@Override
public boolean synchronize() throws InterruptedException {
    int created = 0, updated = 0;
    try {//from w  w w  . ja  v  a  2s . c o m
        // Makes query parameters
        Map<String, String> query_param = new HashMap<>();

        if (skip != 0) {
            query_param.put("$skip", String.valueOf(skip));
        }

        query_param.put("$top", String.valueOf(pageSize));

        log(Level.DEBUG, "Querying users from " + skip + " to " + skip + pageSize);
        ODataFeed userfeed = readFeedLogPerf("/Users", query_param);

        // For each entry, creates a DataBase Object
        for (ODataEntry pdt : userfeed.getEntries()) {
            String username = null;
            try {
                Map<String, Object> props = pdt.getProperties();

                username = (String) props.get(UserEntitySet.USERNAME);
                String email = (String) props.get(UserEntitySet.EMAIL);
                String firstname = (String) props.get(UserEntitySet.FIRSTNAME);
                String lastname = (String) props.get(UserEntitySet.LASTNAME);
                String country = (String) props.get(UserEntitySet.COUNTRY);
                String domain = (String) props.get(UserEntitySet.DOMAIN);
                String subdomain = (String) props.get(UserEntitySet.SUBDOMAIN);
                String usage = (String) props.get(UserEntitySet.USAGE);
                String subusage = (String) props.get(UserEntitySet.SUBUSAGE);
                String phone = (String) props.get(UserEntitySet.PHONE);
                String address = (String) props.get(UserEntitySet.ADDRESS);
                String hash = (String) props.get(UserEntitySet.HASH);
                String password = (String) props.get(UserEntitySet.PASSWORD);
                Date creation = ((GregorianCalendar) props.get(UserEntitySet.CREATED)).getTime();

                // Uses the Scheme encoder as it is the most restrictives, it only allows
                // '+' (forbidden char in usernames, so it's ok)
                // '-' (forbidden char in usernames, so it's ok)
                // '.' (allowed char in usernames, not problematic)
                // the alphanumeric character class (a-z A-Z 0-9)
                String encoded_username = UriUtils.encodeScheme(username, "UTF-8");

                // Retrieves Roles
                String roleq = String.format("/Users('%s')/SystemRoles", encoded_username);
                ODataFeed userrole = readFeedLogPerf(roleq, null);

                List<ODataEntry> roles = userrole.getEntries();
                List<Role> new_roles = new ArrayList<>();
                for (ODataEntry role : roles) {
                    String rolename = (String) role.getProperties().get(SystemRoleEntitySet.NAME);
                    new_roles.add(Role.valueOf(rolename));
                }

                // Has restriction?
                String restricq = String.format("/Users('%s')/Restrictions", encoded_username);
                ODataFeed userrestric = readFeedLogPerf(restricq, null);
                boolean has_restriction = !userrestric.getEntries().isEmpty();

                // Reads user in database, may be null
                User user = USER_SERVICE.getUserNoCheck(username);

                // Updates existing user
                if (user != null && (force || creation.equals(user.getCreated()))) {
                    boolean changed = false;

                    // I wish users had their `Updated` field exposed on OData
                    if (!username.equals(user.getUsername())) {
                        user.setUsername(username);
                        changed = true;
                    }
                    if (email == null && user.getEmail() != null
                            || email != null && !email.equals(user.getEmail())) {
                        user.setEmail(email);
                        changed = true;
                    }
                    if (firstname == null && user.getFirstname() != null
                            || firstname != null && !firstname.equals(user.getFirstname())) {
                        user.setFirstname(firstname);
                        changed = true;
                    }
                    if (lastname == null && user.getLastname() != null
                            || lastname != null && !lastname.equals(user.getLastname())) {
                        user.setLastname(lastname);
                        changed = true;
                    }
                    if (country == null && user.getCountry() != null
                            || country != null && !country.equals(user.getCountry())) {
                        user.setCountry(country);
                        changed = true;
                    }
                    if (domain == null && user.getDomain() != null
                            || domain != null && !domain.equals(user.getDomain())) {
                        user.setDomain(domain);
                        changed = true;
                    }
                    if (subdomain == null && user.getSubDomain() != null
                            || subdomain != null && !subdomain.equals(user.getSubDomain())) {
                        user.setSubDomain(subdomain);
                        changed = true;
                    }
                    if (usage == null && user.getUsage() != null
                            || usage != null && !usage.equals(user.getUsage())) {
                        user.setUsage(usage);
                        changed = true;
                    }
                    if (subusage == null && user.getSubUsage() != null
                            || subusage != null && !subusage.equals(user.getSubUsage())) {
                        user.setSubUsage(subusage);
                        changed = true;
                    }
                    if (phone == null && user.getPhone() != null
                            || phone != null && !phone.equals(user.getPhone())) {
                        user.setPhone(phone);
                        changed = true;
                    }
                    if (address == null && user.getAddress() != null
                            || address != null && !address.equals(user.getAddress())) {
                        user.setAddress(address);
                        changed = true;
                    }

                    if (password == null && user.getPassword() != null
                            || password != null && !password.equals(user.getPassword())) {
                        if (hash == null)
                            hash = PasswordEncryption.NONE.getAlgorithmKey();

                        user.setEncryptedPassword(password, User.PasswordEncryption.valueOf(hash));
                        changed = true;
                    }

                    //user.setPasswordEncryption(User.PasswordEncryption.valueOf(hash));

                    if (!SetUtils.isEqualSet(user.getRoles(), new_roles)) {
                        user.setRoles(new_roles);
                        changed = true;
                    }

                    if (has_restriction != !user.getRestrictions().isEmpty()) {
                        if (has_restriction) {
                            user.addRestriction(new LockedAccessRestriction());
                        } else {
                            user.setRestrictions(Collections.EMPTY_SET);
                        }
                        changed = true;
                    }

                    if (changed) {
                        log(Level.DEBUG, "Updating user " + user.getUsername());
                        USER_SERVICE.systemUpdateUser(user);
                        updated++;
                    }
                }
                // Creates new user
                else if (user == null) {
                    user = new User();

                    user.setUsername(username);
                    user.setEmail(email);
                    user.setFirstname(firstname);
                    user.setLastname(lastname);
                    user.setCountry(country);
                    user.setDomain(domain);
                    user.setSubDomain(subdomain);
                    user.setUsage(usage);
                    user.setSubUsage(subusage);
                    user.setPhone(phone);
                    user.setAddress(address);
                    user.setPassword(password);
                    //user.setPasswordEncryption(User.PasswordEncryption.valueOf(hash));
                    user.setCreated(creation);
                    user.setRoles(new_roles);
                    if (has_restriction) {
                        user.addRestriction(new LockedAccessRestriction());
                    }

                    log(Level.DEBUG, "Creating new user " + user.getUsername());
                    USER_SERVICE.systemCreateUser(user);
                    created++;
                } else {
                    log(Level.ERROR, "Namesake '" + username + "' detected!");
                }
            } catch (RootNotModifiableException e) {
            } // Ignored exception
            catch (RequiredFieldMissingException ex) {
                log(Level.ERROR, "Cannot create user '" + username + "'", ex);
            } catch (IOException | ODataException ex) {
                log(Level.ERROR, "OData failure on user '" + username + "'", ex);
            }

            this.skip++;
        }

        // This is the end, resets `skip` to 0
        if (userfeed.getEntries().size() < pageSize) {
            this.skip = 0;
        }
    } catch (IOException | ODataException ex) {
        log(Level.ERROR, "OData failure", ex);
    } catch (LockAcquisitionException | CannotAcquireLockException e) {
        throw new InterruptedException(e.getMessage());
    } finally {
        StringBuilder sb = new StringBuilder("done:    ");
        sb.append(created).append(" new Users,    ");
        sb.append(updated).append(" updated Users,    ");
        sb.append("    from ").append(this.client.getServiceRoot());
        log(Level.INFO, sb.toString());

        this.syncConf.setConfig("skip", String.valueOf(skip));
        SYNC_SERVICE.saveSynchronizer(this);
    }
    return false;
}