This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
ServerConfigurationService.java
214 lines (199 loc) · 8.02 KB
/
ServerConfigurationService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package org.zanata.rest.service;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.ws.Service;
import org.jboss.resteasy.annotations.providers.jaxb.Wrapped;
import org.jboss.resteasy.util.GenericType;
import javax.inject.Inject;
import javax.inject.Named;
import org.jboss.seam.annotations.Transactional;
import org.zanata.security.annotations.CheckLoggedIn;
import org.zanata.security.annotations.CheckPermission;
import org.zanata.security.annotations.CheckRole;
import org.zanata.common.Namespaces;
import org.zanata.dao.ApplicationConfigurationDAO;
import org.zanata.events.ConfigurationChanged;
import org.zanata.model.HApplicationConfiguration;
import org.zanata.rest.MediaTypes;
import org.zanata.rest.dto.Configuration;
import org.zanata.rest.dto.Link;
import com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import javax.enterprise.event.Event;
import org.zanata.util.ServiceLocator;
/**
* This API is experimental only and subject to change or even removal.
*
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Named("serverConfigurationResource")
@Path("/configurations")
@Produces({"application/xml", "application/json"})
@Consumes({"application/xml", "application/json"})
@Transactional
@CheckRole("admin")
@Slf4j
@Beta
public class ServerConfigurationService {
private static List<String> availableKeys;
/** Type of media requested. */
@HeaderParam("Accept")
@DefaultValue(MediaType.APPLICATION_JSON)
@Context
private MediaType accept;
@Inject
private ApplicationConfigurationDAO applicationConfigurationDAO;
/**
* Retrieves all existing server configurations.
*
* @return The following response status codes will be returned from this
* operation:<br>
* OK(200) - Response containing value for the config key.<br>
* INTERNAL SERVER ERROR(500) - If there is an unexpected error in
* the server while performing this operation.
*/
@GET
@Wrapped(element = "configurations", namespace = Namespaces.ZANATA_API)
public Response get() {
List<HApplicationConfiguration> all =
applicationConfigurationDAO.findAll();
List<Configuration> allConfig =
Lists.transform(all, new ToConfigurationFunction(accept));
Type genericType = new GenericType<List<Configuration>>() {
}.getGenericType();
Object entity =
new GenericEntity<List<Configuration>>(allConfig, genericType);
return Response.ok().entity(entity).build();
}
/**
* Retrieves a specific server configuration.
*
* @return The following response status codes will be returned from this
* operation:<br>
* OK(200) - Response containing value for the config key.<br>
* NOT_FOUND(404) - If server does not have given configuration set.<br>
* INTERNAL SERVER ERROR(500) - If there is an unexpected error in
* the server while performing this operation.
*/
@GET
@Path("c/{configKey}")
public Response get(@PathParam("configKey") @Nonnull String configKey) {
HApplicationConfiguration config =
applicationConfigurationDAO.findByKey(configKey);
if (config == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
Configuration configuration =
new ToConfigurationFunction(accept).apply(config);
return Response.ok().entity(configuration).build();
}
/**
* Creates or updates a server configuration. If a configuration with the
* given key already exists, the value will be overwritten with the provided
* data. Otherwise, a new config will be created.
*
* @param configKey
* The configuration item to be created/updated.
* @return The following response status codes will be returned from this
* operation:<br>
* OK(200) - If an existing configuration was modified.<br>
* CREATED(201) - If a new configuration was created.<br>
* UNAUTHORIZED(401) - If the user does not have the proper
* permissions to perform this operation.<br>
* INTERNAL SERVER ERROR(500) - If there is an unexpected error in
* the server while performing this operation.
*/
@PUT
@Path("/c/{configKey}")
public Response put(@PathParam("configKey") @Nonnull String configKey,
String configValue) {
if (!isConfigKeyValid(configKey)) {
return Response.status(Response.Status.BAD_REQUEST)
.entity("config key not supported: " + configKey).build();
}
HApplicationConfiguration configuration =
applicationConfigurationDAO.findByKey(configKey);
boolean created = configuration == null;
persistApplicationConfig(configKey, configuration, configValue,
applicationConfigurationDAO);
if (created) {
return Response.created(URI.create("c/" + configKey)).build();
} else {
return Response.ok().build();
}
}
/**
* This method is shared by REST service and JSF backend bean. It will raise
* event to notify configuration change.
*
* @see org.zanata.action.ServerConfigurationBean
* @param key
* config key
* @param appConfig
* configuration entity
* @param newValue
* new config value (if null or empty will remove the config
* entity)
* @param applicationConfigurationDAO
* dao
*/
public static void persistApplicationConfig(String key,
@Nullable HApplicationConfiguration appConfig, String newValue,
ApplicationConfigurationDAO applicationConfigurationDAO) {
if (appConfig != null) {
if (newValue == null || newValue.isEmpty()) {
applicationConfigurationDAO.makeTransient(appConfig);
} else {
appConfig.setValue(newValue);
}
} else if (newValue != null && !newValue.isEmpty()) {
appConfig = new HApplicationConfiguration(key, newValue);
applicationConfigurationDAO.makePersistent(appConfig);
}
Event<ConfigurationChanged> event =
ServiceLocator.instance().getInstance(Event.class);
event.fire(new ConfigurationChanged(key));
}
private boolean isConfigKeyValid(String configKey) {
return HApplicationConfiguration.getAvailableKeys().contains(configKey);
}
/**
* Converts HApplicationConfiguration to dto Configuration. It also contains
* a link to the configuration itself.
*/
@RequiredArgsConstructor
private class ToConfigurationFunction implements
Function<HApplicationConfiguration, Configuration> {
private final MediaType accept;
@Override
public Configuration apply(HApplicationConfiguration input) {
Configuration config =
new Configuration(input.getKey(), input.getValue());
config.getLinks(true).add(
// a link to get this specific configuration details
new Link(URI.create("c/" + input.getKey()), "self",
MediaTypes.createFormatSpecificType(
MediaType.APPLICATION_XML, accept)));
return config;
}
}
}