Java Code Examples for com.tw.go.plugin.util.JSONUtils#fromJSON()

The following examples show how to use com.tw.go.plugin.util.JSONUtils#fromJSON() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private GoPluginApiResponse handleSearchUserRequest(GoPluginApiRequest goPluginApiRequest) {
    Map<String, String> requestBodyMap = (Map<String, String>) JSONUtils.fromJSON(goPluginApiRequest.requestBody());
    String searchTerm = requestBodyMap.get("search-term");
    PluginSettings pluginSettings = getPluginSettings();
    List<User> users = provider.searchUser(pluginSettings, searchTerm);
    if (users == null || users.isEmpty()) {
        return renderJSON(SUCCESS_RESPONSE_CODE, null);
    } else {
        List<Map> searchResults = new ArrayList<Map>();
        for (User user : users) {
            searchResults.add(getUserMap(user));
        }
        return renderJSON(SUCCESS_RESPONSE_CODE, searchResults);
    }
}
 
Example 2
private SocialAuthManager read() {
    Map<String, Object> requestMap = new HashMap<String, Object>();
    requestMap.put("plugin-id", provider.getPluginId());
    GoApiRequest goApiRequest = createGoApiRequest(GO_REQUEST_SESSION_GET, JSONUtils.toJSON(requestMap));
    GoApiResponse response = goApplicationAccessor.submit(goApiRequest);
    // handle error
    String responseBody = response.responseBody();
    Map<String, String> sessionData = (Map<String, String>) JSONUtils.fromJSON(responseBody);
    String socialAuthManagerStr = sessionData.get("social-auth-manager");
    return deserializeObject(socialAuthManagerStr);
}
 
Example 3
Source Project: gocd-oauth-login   File: GitLabProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<User> searchUser(GitLabPluginSettings pluginSettings, String searchTerm) {
    HttpUrl searchBaseUrl = HttpUrl.parse(fullUrl(pluginSettings, "/api/v3/users"));

    HttpUrl url = new HttpUrl.Builder()
            .scheme(searchBaseUrl.scheme())
            .host(searchBaseUrl.host())
            .addPathSegments(searchBaseUrl.encodedPath())
            .addQueryParameter("private_token", pluginSettings.getOauthToken())
            .addQueryParameter("search", searchTerm)
            .build();

    Request request = new Request.Builder().url(url.url()).build();

    try {
        Response response = client.newCall(request).execute();
        List<User> users = new ArrayList<>();

        for (Map<String, String> userParams : (List<Map<String, String>>) JSONUtils.fromJSON(response.body().string())) {
            users.add(new User(userParams.get("email"), userParams.get("name"), userParams.get("email")));
        }
        return users;

    } catch (IOException e) {
        LOGGER.warn("Error occurred while trying to perform user search", e);
        return new ArrayList<>();
    }
}
 
Example 4
public PluginSettings getPluginSettings() {
    Map<String, Object> requestMap = new HashMap<>();
    requestMap.put("plugin-id", PLUGIN_ID);
    GoApiResponse response = goApplicationAccessor.submit(createGoApiRequest(GET_PLUGIN_SETTINGS, JSONUtils.toJSON(requestMap)));
    if (response.responseBody() == null || response.responseBody().trim().isEmpty()) {
        throw new RuntimeException("plugin is not configured. please provide plugin settings.");
    }
    Map<String, String> responseBodyMap = (Map<String, String>) JSONUtils.fromJSON(response.responseBody());
    return new PluginSettings(responseBodyMap.get(PLUGIN_SETTINGS_SMTP_HOST), Integer.parseInt(responseBodyMap.get(PLUGIN_SETTINGS_SMTP_PORT)),
            Boolean.parseBoolean(responseBodyMap.get(PLUGIN_SETTINGS_IS_TLS)), responseBodyMap.get(PLUGIN_SETTINGS_SENDER_EMAIL_ID),
            responseBodyMap.get(PLUGIN_SETTINGS_SMTP_USERNAME),
            responseBodyMap.get(PLUGIN_SETTINGS_SENDER_PASSWORD), responseBodyMap.get(PLUGIN_SETTINGS_RECEIVER_EMAIL_ID),
            responseBodyMap.get(PLUGIN_SETTINGS_FILTER));
}
 
Example 5
public PluginSettings getPluginSettings() {
    Map<String, Object> requestMap = new HashMap<String, Object>();
    requestMap.put("plugin-id", provider.pluginId());
    GoApiResponse response = goApplicationAccessor.submit(createGoApiRequest(GET_PLUGIN_SETTINGS, JSONUtils.toJSON(requestMap)));
    Map<String, String> responseBodyMap = response.responseBody() == null ? new HashMap<String, String>() : (Map<String, String>) JSONUtils.fromJSON(response.responseBody());
    return provider.pluginSettings(responseBodyMap);
}
 
Example 6
private GoPluginApiResponse handleStageNotification(GoPluginApiRequest goPluginApiRequest) {
    Map<String, Object> dataMap = (Map<String, Object>) JSONUtils.fromJSON(goPluginApiRequest.requestBody());

    int responseCode = SUCCESS_RESPONSE_CODE;
    Map<String, Object> response = new HashMap<>();
    List<String> messages = new ArrayList<>();
    try {
        Map<String, Object> pipelineMap = (Map<String, Object>) dataMap.get("pipeline");
        Map<String, Object> stageMap = (Map<String, Object>) pipelineMap.get("stage");


        String pipelineName = (String) pipelineMap.get("name");
        String stageName = (String) stageMap.get("name");
        String stageState = (String) stageMap.get("state");

        String subject = String.format("%s/%s is/has %s", pipelineName, stageName, stageState);
        String body = String.format("State: %s\nResult: %s\nCreate Time: %s\nLast Transition Time: %s", stageState, stageMap.get("result"), stageMap.get("create-time"), stageMap.get("last-transition-time"));

        PluginSettings pluginSettings = getPluginSettings();


        boolean matchesFilter = false;

        List<Filter> filterList = pluginSettings.getFilterList();

        if(filterList.isEmpty()) {
            matchesFilter = true;
        } else {
            for(Filter filter : filterList) {
                if(filter.matches(pipelineName, stageName, stageState)) {
                    matchesFilter = true;
                }
            }
        }

        if(matchesFilter) {
            LOGGER.info("Sending Email for " + subject);

            String receiverEmailIdString = pluginSettings.getReceiverEmailId();

            String[] receiverEmailIds = new String[]{receiverEmailIdString};

            if (receiverEmailIdString.contains(",")) {
                receiverEmailIds = receiverEmailIdString.split(",");
            }

            for (String receiverEmailId : receiverEmailIds) {
                SMTPSettings settings = new SMTPSettings(pluginSettings.getSmtpHost(), pluginSettings.getSmtpPort(), pluginSettings.isTls(), pluginSettings.getSenderEmailId(), pluginSettings.getSmtpUsername(), pluginSettings.getSenderPassword());
                new SMTPMailSender(settings, sessionFactory).send(subject, body, receiverEmailId);
            }

            LOGGER.info("Successfully delivered an email.");
        } else {
            LOGGER.info("Skipped email as no filter matched this pipeline/stage/state");
        }

        response.put("status", "success");
    } catch (Exception e) {
        LOGGER.warn("Error occurred while trying to deliver an email.", e);

        responseCode = INTERNAL_ERROR_RESPONSE_CODE;
        response.put("status", "failure");
        if (!isEmpty(e.getMessage())) {
            messages.add(e.getMessage());
        }
    }

    if (!messages.isEmpty()) {
        response.put("messages", messages);
    }
    return renderJSON(responseCode, response);
}
 
Example 7
private GoPluginApiResponse handleValidatePluginSettingsConfiguration(GoPluginApiRequest goPluginApiRequest) {
    Map<String, Object> fields = (Map<String, Object>) JSONUtils.fromJSON(goPluginApiRequest.requestBody());
    List<Map<String, Object>> response = provider.validateConfig((Map<String, Object>) fields.get("plugin-settings"));
    return renderJSON(SUCCESS_RESPONSE_CODE, response);
}