Skip to content

Commit 8c0952a

Browse files
committed
Initial commit (#340).
1 parent c2e8d57 commit 8c0952a

File tree

5 files changed

+1233
-0
lines changed

5 files changed

+1233
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package org.gitlab4j.api.models;
2+
3+
import java.util.Date;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import org.gitlab4j.api.GitLabApiException;
8+
import org.gitlab4j.api.utils.JacksonJson;
9+
10+
import com.fasterxml.jackson.annotation.JsonIgnore;
11+
import com.fasterxml.jackson.databind.JsonNode;
12+
import com.fasterxml.jackson.databind.node.ArrayNode;
13+
import com.fasterxml.jackson.databind.node.BooleanNode;
14+
import com.fasterxml.jackson.databind.node.DoubleNode;
15+
import com.fasterxml.jackson.databind.node.FloatNode;
16+
import com.fasterxml.jackson.databind.node.IntNode;
17+
import com.fasterxml.jackson.databind.node.NullNode;
18+
import com.fasterxml.jackson.databind.node.TextNode;
19+
20+
public class ApplicationSettings {
21+
22+
private Integer id;
23+
private Date createdAt;
24+
private Date updatedAt;
25+
private Map<String, Object> settings = new HashMap<>();
26+
27+
public Integer getId() {
28+
return id;
29+
}
30+
31+
public void setId(Integer id) {
32+
this.id = id;
33+
}
34+
35+
public Date getCreatedAt() {
36+
return createdAt;
37+
}
38+
39+
public void setCreatedAt(Date createdAt) {
40+
this.createdAt = createdAt;
41+
}
42+
43+
public Date getUpdatedAt() {
44+
return updatedAt;
45+
}
46+
47+
public void setUpdatedAt(Date updatedAt) {
48+
this.updatedAt = updatedAt;
49+
}
50+
51+
public Map<String, Object> getSettings() {
52+
return settings;
53+
}
54+
55+
public void setSettings(Map<String, Object> settings) {
56+
this.settings = settings;
57+
}
58+
59+
@JsonIgnore
60+
public Object getSetting(Setting setting) {
61+
62+
if (setting == null) {
63+
return (null);
64+
}
65+
66+
String name = setting.toString();
67+
return (settings.get(name));
68+
}
69+
70+
@JsonIgnore
71+
public Object getSetting(String setting) {
72+
73+
if (setting == null) {
74+
return (null);
75+
}
76+
77+
return (settings.get(setting));
78+
}
79+
80+
public Object addSetting(String setting, Object value) throws GitLabApiException {
81+
82+
Setting appSetting = Setting.forValue(setting);
83+
if (appSetting != null) {
84+
return (addSetting(appSetting, value));
85+
}
86+
87+
settings.put(setting, value);
88+
return (value);
89+
}
90+
91+
public Object addSetting(Setting setting, Object value) throws GitLabApiException {
92+
93+
if (value instanceof JsonNode) {
94+
value = jsonNodeToValue((JsonNode)value);
95+
}
96+
97+
setting.validate(value);
98+
settings.put(setting.toString(), value);
99+
return (value);
100+
}
101+
102+
public Object removeSetting(Setting setting) {
103+
return settings.remove(setting.toString());
104+
}
105+
106+
public Object removeSetting(String setting) {
107+
return settings.remove(setting);
108+
}
109+
110+
public void clearSettings() {
111+
settings.clear();
112+
}
113+
114+
private Object jsonNodeToValue(JsonNode node) {
115+
116+
Object value = node;
117+
if (node instanceof NullNode) {
118+
value = null;
119+
} else if (node instanceof TextNode) {
120+
value = node.asText();
121+
} else if (node instanceof BooleanNode) {
122+
value = node.asBoolean();
123+
} else if (node instanceof IntNode) {
124+
value = node.asInt();
125+
} else if (node instanceof FloatNode) {
126+
value = (float)((FloatNode)node).asDouble();
127+
} else if (node instanceof DoubleNode) {
128+
value = (float)((DoubleNode)node).asDouble();
129+
} else if (node instanceof ArrayNode) {
130+
131+
int numItems = node.size();
132+
String[] values = new String[numItems];
133+
for (int i = 0; i < numItems; i++) {
134+
values[i] = node.path(i).asText();
135+
}
136+
137+
value = values;
138+
}
139+
140+
return (value);
141+
}
142+
143+
@Override
144+
public String toString() {
145+
return (JacksonJson.toJsonString(this));
146+
}
147+
}

0 commit comments

Comments
 (0)