All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.axway.apim.users.adapter.UserConfigAdapter Maven / Gradle / Ivy

There is a newer version: 1.14.7
Show newest version
package com.axway.apim.users.adapter;

import com.axway.apim.api.model.CustomProperties.Type;
import com.axway.apim.api.model.Image;
import com.axway.apim.api.model.User;
import com.axway.apim.lib.error.AppException;
import com.axway.apim.lib.error.ErrorCode;
import com.axway.apim.lib.utils.Utils;
import com.axway.apim.users.lib.UserImportParams;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UserConfigAdapter extends UserAdapter {
    private static final Logger LOG = LoggerFactory.getLogger(UserConfigAdapter.class);

    public UserConfigAdapter(UserImportParams params) {
        super(params);
    }

    public void readConfig() throws AppException {
        ObjectMapper mapper = null;
        String config = importParams.getConfig();
        String stage = importParams.getStage();

        File configFile = Utils.locateConfigFile(config);
        if (!configFile.exists()) return;
        File stageConfig = Utils.getStageConfig(stage, importParams.getStageConfig(), configFile);
        List baseUsers;
        // Try to read a list of users
        try {
            mapper = Utils.createObjectMapper(configFile);
            baseUsers = mapper.readValue(Utils.substituteVariables(configFile), new TypeReference>() {
            });
            if (stageConfig != null) {
                throw new AppException("Stage overrides are not supported for users lists.", ErrorCode.CANT_READ_CONFIG_FILE);
            } else {
                this.users = baseUsers;
            }
            // Try to read single user
        } catch (MismatchedInputException me) {
            try {
                User user = readUser(mapper, configFile, stageConfig, stage);
                this.users = new ArrayList<>();
                this.users.add(user);
            } catch (Exception pe) {
                throw new AppException("Cannot read user(s) from config file: " + config, ErrorCode.UNKNOWN_USER, pe);
            }
        } catch (Exception e) {
            throw new AppException("Cannot read user(s) from config file: " + config, ErrorCode.UNKNOWN_USER, e);
        }
        try {
            addImage(users, configFile.getCanonicalFile().getParentFile());
        } catch (Exception e) {
            throw new AppException("Cannot read image for user(s) from config file: " + config, ErrorCode.UNKNOWN_USER, e);
        }
        validateCustomProperties(users);
        setInternalUser(users);
    }

    public void addImage(List users, File parentFolder) throws AppException {
        for (User user : users) {
            String imageUrl = user.getImageUrl();
            if (imageUrl == null || imageUrl.isEmpty()) continue;
            if (imageUrl.startsWith("data:")) {
                user.setImage(Image.createImageFromBase64(imageUrl));
            } else {
                user.setImage(Image.createImageFromFile(new File(parentFolder + File.separator + imageUrl)));
            }
        }
    }

    private void validateCustomProperties(List users) throws AppException {
        for (User user : users) {
            Utils.validateCustomProperties(user.getCustomProperties(), Type.user);
        }
    }

    private void setInternalUser(List users) {
        for (User user : users) {
            user.setType("internal"); // Default to internal, as external makes no sense using the CLI
        }
    }

    public User readUser(ObjectMapper mapper, File configFile, File stageConfig, String stage) throws IOException {
        User user = mapper.readValue(Utils.substituteVariables(configFile), User.class);
        if (stageConfig != null) {
            try {
                ObjectReader updater = mapper.readerForUpdating(user);
                user = updater.readValue(Utils.substituteVariables(stageConfig));
            } catch (FileNotFoundException e) {
                LOG.warn("No config file found for stage: {}", stage);
            }
        }
        return user;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy