|
| 1 | +package org.gitlab4j.api; |
| 2 | + |
| 3 | +import java.text.ParseException; |
| 4 | +import java.util.Iterator; |
| 5 | + |
| 6 | +import javax.ws.rs.core.Response; |
| 7 | + |
| 8 | +import org.gitlab4j.api.models.Setting; |
| 9 | +import org.gitlab4j.api.models.ApplicationSettings; |
| 10 | +import org.gitlab4j.api.utils.ISO8601; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.databind.JsonNode; |
| 13 | + |
| 14 | +/** |
| 15 | + * This class implements the client side API for the GitLab Application Settings API. |
| 16 | + * See <a href="https://docs.gitlab.com/ee/api/settings.html">Application Settings API at GitLab</a> for more information. |
| 17 | + */ |
| 18 | +public class ApplicationSettingsApi extends AbstractApi { |
| 19 | + |
| 20 | + public ApplicationSettingsApi(GitLabApi gitLabApi) { |
| 21 | + super(gitLabApi); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Get the current application settings of the GitLab instance. |
| 26 | + * |
| 27 | + * <pre><code>GitLab Endpoint: GET /api/v4/application/settings</code></pre> |
| 28 | + * |
| 29 | + * @return an ApplicationSettings instance containing the current application settings of the GitLab instance. |
| 30 | + * @throws GitLabApiException if any exception occurs |
| 31 | + */ |
| 32 | + public ApplicationSettings getApplicationSettings() throws GitLabApiException { |
| 33 | + |
| 34 | + Response response = get(Response.Status.OK, null, "application", "settings"); |
| 35 | + JsonNode root = response.readEntity(JsonNode.class); |
| 36 | + return (parseApplicationSettings(root)); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Update the application settings of the GitLab instance with the settings in the |
| 41 | + * provided ApplicationSettings instance. |
| 42 | + * |
| 43 | + * <pre><code>GitLab Endpoint: PUT /api/v4/application/settings</code></pre> |
| 44 | + * |
| 45 | + * @param appSettings the ApplicationSettings instance holding the settings and values to update |
| 46 | + * @return the updated application settings in an ApplicationSettings instance |
| 47 | + * @throws GitLabApiException if any exception occurs |
| 48 | + */ |
| 49 | + public ApplicationSettings updateApplicationSettings(ApplicationSettings appSettings) throws GitLabApiException { |
| 50 | + |
| 51 | + if (appSettings == null || appSettings.getSettings().isEmpty()) { |
| 52 | + throw new GitLabApiException("ApplicationSettings cannot be null or empty."); |
| 53 | + } |
| 54 | + |
| 55 | + final GitLabApiForm form = new GitLabApiForm(); |
| 56 | + appSettings.getSettings().forEach((s, v) -> form.withParam(s, v)); |
| 57 | + Response response = put(Response.Status.OK, form.asMap(), "application", "settings"); |
| 58 | + JsonNode root = response.readEntity(JsonNode.class); |
| 59 | + return (parseApplicationSettings(root)); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Update a single application setting of the GitLab instance with the provided settings and value. |
| 64 | + * |
| 65 | + * <pre><code>GitLab Endpoint: PUT /api/v4/application/settings</code></pre> |
| 66 | + * |
| 67 | + * @param setting the ApplicationSetting to update |
| 68 | + * @param value the new value for the application setting |
| 69 | + * @return the updated application settings in an ApplicationSettings instance |
| 70 | + * @throws GitLabApiException if any exception occurs |
| 71 | + */ |
| 72 | + public ApplicationSettings updateApplicationSetting(Setting setting, Object value) throws GitLabApiException { |
| 73 | + |
| 74 | + if (setting == null) { |
| 75 | + throw new GitLabApiException("setting cannot be null."); |
| 76 | + } |
| 77 | + |
| 78 | + return (updateApplicationSetting(setting.toString(), value)); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Update a single application setting of the GitLab instance with the provided settings and value. |
| 83 | + * |
| 84 | + * <pre><code>GitLab Endpoint: PUT /api/v4/application/settings</code></pre> |
| 85 | + * |
| 86 | + * @param setting the ApplicationSetting to update |
| 87 | + * @param value the new value for the application setting |
| 88 | + * @return the updated application settings in an ApplicationSettings instance |
| 89 | + * @throws GitLabApiException if any exception occurs |
| 90 | + */ |
| 91 | + public ApplicationSettings updateApplicationSetting(String setting, Object value) throws GitLabApiException { |
| 92 | + |
| 93 | + if (setting == null || setting.trim().isEmpty()) { |
| 94 | + throw new GitLabApiException("setting cannot be null or empty."); |
| 95 | + } |
| 96 | + |
| 97 | + GitLabApiForm form = new GitLabApiForm().withParam(setting, value); |
| 98 | + Response response = put(Response.Status.OK, form.asMap(), "application", "settings"); |
| 99 | + JsonNode root = response.readEntity(JsonNode.class); |
| 100 | + return (parseApplicationSettings(root)); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Pareses the returned JSON and returns an ApplicationSettings instance. |
| 105 | + * |
| 106 | + * @param root the root JsonNode |
| 107 | + * @return the populated ApplicationSettings instance |
| 108 | + * @throws GitLabApiException if any error occurs |
| 109 | + */ |
| 110 | + private final ApplicationSettings parseApplicationSettings(JsonNode root) throws GitLabApiException { |
| 111 | + |
| 112 | + ApplicationSettings appSettings = new ApplicationSettings(); |
| 113 | + |
| 114 | + Iterator<String> fieldNames = root.fieldNames(); |
| 115 | + while (fieldNames.hasNext()) { |
| 116 | + |
| 117 | + String fieldName = fieldNames.next(); |
| 118 | + switch (fieldName) { |
| 119 | + case "id": |
| 120 | + appSettings.setId(root.path(fieldName).asInt()); |
| 121 | + break; |
| 122 | + |
| 123 | + case "created_at": |
| 124 | + try { |
| 125 | + appSettings.setCreatedAt(ISO8601.toDate(root.path(fieldName).asText())); |
| 126 | + } catch (ParseException pe) { |
| 127 | + throw new GitLabApiException(pe); |
| 128 | + } |
| 129 | + break; |
| 130 | + |
| 131 | + case "updated_at": |
| 132 | + try { |
| 133 | + appSettings.setUpdatedAt(ISO8601.toDate(root.path(fieldName).asText())); |
| 134 | + } catch (ParseException pe) { |
| 135 | + throw new GitLabApiException(pe); |
| 136 | + } |
| 137 | + break; |
| 138 | + |
| 139 | + default: |
| 140 | + |
| 141 | + Setting setting = Setting.forValue(fieldName); |
| 142 | + if (setting != null) { |
| 143 | + appSettings.addSetting(setting, root.path(fieldName)); |
| 144 | + } else { |
| 145 | + GitLabApi.getLogger().warning(String.format("Unknown setting: %s, type: %s", |
| 146 | + fieldName, root.path(fieldName).getClass().getSimpleName())); |
| 147 | + appSettings.addSetting(fieldName, root.path(fieldName)); |
| 148 | + } |
| 149 | + |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return (appSettings); |
| 155 | + } |
| 156 | +} |
0 commit comments