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 package co.stateful.rest;
31
32 import co.stateful.spi.Counters;
33 import com.google.common.collect.Iterables;
34 import com.rexsl.page.JaxbBundle;
35 import com.rexsl.page.Link;
36 import com.rexsl.page.PageBuilder;
37 import java.io.IOException;
38 import java.util.logging.Level;
39 import javax.ws.rs.FormParam;
40 import javax.ws.rs.GET;
41 import javax.ws.rs.POST;
42 import javax.ws.rs.Path;
43 import javax.ws.rs.QueryParam;
44 import javax.ws.rs.core.Response;
45 import org.apache.commons.io.IOUtils;
46
47
48
49
50
51
52
53
54 @Path("/counters")
55 public final class CountersRs extends BaseRs {
56
57
58
59
60 private static final String PARAM = "name";
61
62
63
64
65
66
67 @GET
68 @Path("/")
69 public Response index() throws Exception {
70 return new PageBuilder()
71 .stylesheet("/xsl/counters.xsl")
72 .build(StPage.class)
73 .init(this)
74 .append(
75 new JaxbBundle(
76 "documentation",
77 IOUtils.toString(
78 this.getClass().getResourceAsStream(
79 "doc-counters.html"
80 )
81 )
82 )
83 )
84 .append(this.list())
85 .append(new JaxbBundle("menu", "counters"))
86 .link(new Link("add", "./add"))
87 .render()
88 .build();
89 }
90
91
92
93
94
95
96 @POST
97 @Path("/add")
98 public void add(@FormParam(CountersRs.PARAM) final String name)
99 throws IOException {
100 if (!name.matches("[0-9a-zA-Z\\-]{1,32}")) {
101 throw this.flash().redirect(
102 this.uriInfo().getBaseUriBuilder()
103 .clone()
104 .path(CountersRs.class)
105 .build(),
106 "1-32 letters, numbers or dashes",
107 Level.WARNING
108 );
109 }
110 if (Iterables.size(this.user().counters().names()) > Counters.MAX) {
111 throw this.flash().redirect(
112 this.uriInfo().getBaseUriBuilder()
113 .clone()
114 .path(CountersRs.class)
115 .build(),
116 "too many counters in your account",
117 Level.SEVERE
118 );
119 }
120 this.user().counters().create(name);
121 throw this.flash().redirect(
122 this.uriInfo().getBaseUriBuilder()
123 .clone()
124 .path(CountersRs.class)
125 .build(),
126 String.format("counter %s created successfully", name),
127 Level.INFO
128 );
129 }
130
131
132
133
134
135
136 @GET
137 @Path("/delete")
138 public void delete(@QueryParam(CountersRs.PARAM) final String name)
139 throws IOException {
140 this.user().counters().delete(name);
141 throw this.flash().redirect(
142 this.uriInfo().getBaseUriBuilder()
143 .clone()
144 .path(CountersRs.class)
145 .build(),
146 String.format("counter %s deleted successfully", name),
147 Level.INFO
148 );
149 }
150
151
152
153
154
155
156 private JaxbBundle list() throws IOException {
157 return new JaxbBundle("counters").add(
158
159 new JaxbBundle.Group<String>(this.user().counters().names()) {
160 @Override
161 public JaxbBundle bundle(final String name) {
162 return new JaxbBundle("counter")
163 .add("name", name)
164 .up()
165 .link(
166 new Link(
167 "set",
168 CountersRs.this.uriInfo().getBaseUriBuilder()
169 .clone()
170 .path(CounterRs.class)
171 .path(CounterRs.class, "set")
172 .build(name)
173 )
174 )
175 .link(
176 new Link(
177 "increment",
178 CountersRs.this.uriInfo().getBaseUriBuilder()
179 .clone()
180 .path(CounterRs.class)
181 .path(CounterRs.class, "increment")
182 .build(name)
183 )
184 )
185 .link(
186 new Link(
187 "delete",
188 CountersRs.this.uriInfo().getBaseUriBuilder()
189 .clone()
190 .path(CountersRs.class)
191 .path(CountersRs.class, "delete")
192 .queryParam(CountersRs.PARAM, "{x}")
193 .build(name)
194 )
195 );
196 }
197 }
198 );
199 }
200
201 }