1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.admin.resources.controller;
17
18 import java.net.URI;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
23
24 import javax.annotation.security.RolesAllowed;
25 import javax.ws.rs.FormParam;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.core.Context;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
33 import javax.ws.rs.core.UriInfo;
34
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import de.smartics.properties.admin.domain.model.ConfigurationKeys;
39 import de.smartics.properties.admin.domain.model.ManagedApplication;
40 import de.smartics.properties.admin.domain.model.Paths;
41 import de.smartics.properties.admin.domain.model.Roles;
42 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
43
44
45
46
47 @Path("")
48 @RolesAllowed(Roles.VIEW)
49 public class ConfigurationKeysResource
50 {
51
52
53
54
55
56
57
58 private static final Logger LOG = LoggerFactory
59 .getLogger(ConfigurationKeysResource.class);
60
61
62
63
64
65
66 @Context
67 private UriInfo uriInfo;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 @GET
89 @Path(Paths.PATH_CONFIGURATIONS)
90 @Produces(MediaType.TEXT_PLAIN)
91 public String getAsText()
92 {
93 final ConfigurationKeys keys = fetchConfigurationKeys();
94
95 final StringBuilder buffer = new StringBuilder(512);
96 for (final ConfigurationKey<?> key : keys.getKeys())
97 {
98 buffer.append(key).append('\n');
99 }
100 return buffer.toString();
101 }
102
103
104
105
106
107
108 @GET
109 @Path(Paths.PATH_CONFIGURATIONS)
110 @Produces(MediaType.TEXT_HTML)
111 public ConfigurationKeys getAsHtml()
112 {
113 final ConfigurationKeys keys = fetchConfigurationKeys();
114 return keys;
115 }
116
117
118
119
120
121
122 @GET
123 @Path(Paths.PATH_CONFIGURATIONS)
124 @Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
125 public ConfigurationKeys getAsXml()
126 {
127 final ConfigurationKeys keys = fetchConfigurationKeys();
128 return keys;
129 }
130
131 @SuppressWarnings({ "rawtypes", "unchecked" })
132 private ConfigurationKeys fetchConfigurationKeys()
133 {
134 if (LOG.isDebugEnabled())
135 {
136 LOG.debug("Fetching all configuration keys.");
137 }
138
139 final ManagedApplication application = ManagedApplication.getApplication();
140 final Collection<ConfigurationKey<?>> keys =
141 application.getConfigurationKeys();
142 final List sorted = new ArrayList<ConfigurationKey<?>>(keys);
143 Collections.sort(sorted);
144
145 final ConfigurationKeys configurationKeys =
146 new ConfigurationKeys(application, sorted);
147 return configurationKeys;
148 }
149
150
151
152
153
154
155
156
157
158 @POST
159 @Path(Paths.PATH_CONFIGURATIONS)
160 @Produces(MediaType.TEXT_HTML)
161 @RolesAllowed(Roles.CREATE_CONFIG)
162 public Response postAsHtml(
163 @FormParam(Paths.PARAM_CONFIGURATION_KEY) final String configurationKey)
164 {
165 final ManagedApplication application = ManagedApplication.getApplication();
166 application.getConfiguration(configurationKey);
167
168 final ConfigurationKeys keys = fetchConfigurationKeys();
169 final URI self = uriInfo.getRequestUriBuilder().build();
170 final Response response = Response.seeOther(self).entity(keys).build();
171
172 return response;
173 }
174
175
176
177 }