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.Base; |
33 | |
import co.stateful.spi.User; |
34 | |
import com.jcabi.manifests.Manifests; |
35 | |
import com.jcabi.urn.URN; |
36 | |
import com.rexsl.page.BasePage; |
37 | |
import com.rexsl.page.BaseResource; |
38 | |
import com.rexsl.page.Inset; |
39 | |
import com.rexsl.page.JaxbBundle; |
40 | |
import com.rexsl.page.Link; |
41 | |
import com.rexsl.page.Resource; |
42 | |
import com.rexsl.page.auth.AuthInset; |
43 | |
import com.rexsl.page.auth.Facebook; |
44 | |
import com.rexsl.page.auth.Github; |
45 | |
import com.rexsl.page.auth.Google; |
46 | |
import com.rexsl.page.auth.Identity; |
47 | |
import com.rexsl.page.auth.Provider; |
48 | |
import com.rexsl.page.inset.FlashInset; |
49 | |
import com.rexsl.page.inset.LinksInset; |
50 | |
import com.rexsl.page.inset.VersionInset; |
51 | |
import java.io.IOException; |
52 | |
import java.net.URI; |
53 | |
import java.util.logging.Level; |
54 | |
import javax.ws.rs.core.HttpHeaders; |
55 | |
import javax.ws.rs.core.MediaType; |
56 | |
import javax.ws.rs.core.Response; |
57 | |
import org.apache.commons.lang3.Validate; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
@Resource.Forwarded |
69 | |
@Inset.Default(LinksInset.class) |
70 | |
@SuppressWarnings("PMD.TooManyMethods") |
71 | 2 | public class BaseRs extends BaseResource { |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | 1 | private static final String VERSION_LABEL = String.format( |
77 | |
"%s/%s built on %s", |
78 | |
|
79 | |
Manifests.read("Stateful-Version"), |
80 | |
Manifests.read("Stateful-Revision"), |
81 | |
Manifests.read("Stateful-Date") |
82 | |
); |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | 1 | private static final Provider TESTER = new Provider() { |
88 | |
@Override |
89 | |
public Identity identity() { |
90 | |
final Identity identity; |
91 | 1 | if ("1234567".equals(Manifests.read("Stateful-Revision"))) { |
92 | 0 | identity = new Identity.Simple( |
93 | |
URN.create("urn:test:123456"), |
94 | |
"Localhost", |
95 | |
URI.create("http://img.stateful.com/unknown.png") |
96 | |
); |
97 | |
} else { |
98 | 1 | identity = Identity.ANONYMOUS; |
99 | |
} |
100 | 1 | return identity; |
101 | |
} |
102 | |
}; |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
@Inset.Runtime |
109 | |
public final FlashInset flash() { |
110 | 1 | return new FlashInset(this); |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
@Inset.Runtime |
118 | |
public final Inset supplementary() { |
119 | 1 | return new Inset() { |
120 | |
@Override |
121 | |
public void render(final BasePage<?, ?> page, |
122 | |
final Response.ResponseBuilder builder) { |
123 | 1 | builder.header("X-Sttc-Version", BaseRs.VERSION_LABEL); |
124 | 1 | builder.type(MediaType.TEXT_XML); |
125 | 1 | builder.header(HttpHeaders.VARY, "Cookie"); |
126 | 1 | } |
127 | |
}; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
@Inset.Runtime |
135 | |
public final Inset menu() { |
136 | 1 | return new Inset() { |
137 | |
@Override |
138 | |
public void render(final BasePage<?, ?> page, |
139 | |
final Response.ResponseBuilder builder) { |
140 | 1 | if (!BaseRs.this.auth().identity().equals(Identity.ANONYMOUS)) { |
141 | 0 | page.link(new Link("menu:home", "/")); |
142 | 0 | page.link(new Link("menu:counters", "/counters")); |
143 | 0 | page.link(new Link("menu:locks", "/k")); |
144 | |
} |
145 | 1 | } |
146 | |
}; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
@Inset.Runtime |
154 | |
public final Inset token() { |
155 | 1 | return new Inset() { |
156 | |
@Override |
157 | |
public void render(final BasePage<?, ?> page, |
158 | |
final Response.ResponseBuilder builder) { |
159 | 1 | if (!BaseRs.this.auth().identity().equals(Identity.ANONYMOUS)) { |
160 | |
try { |
161 | 0 | page.append( |
162 | |
new JaxbBundle("token", BaseRs.this.user().token()) |
163 | |
); |
164 | 0 | } catch (final IOException ex) { |
165 | 0 | throw new IllegalStateException(ex); |
166 | 0 | } |
167 | 0 | page.link(new Link("user:refresh", "/u/refresh")); |
168 | |
} |
169 | 1 | } |
170 | |
}; |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
@Inset.Runtime |
178 | |
public final Inset version() { |
179 | 1 | return new VersionInset( |
180 | |
Manifests.read("Stateful-Version"), |
181 | |
Manifests.read("Stateful-Revision"), |
182 | |
Manifests.read("Stateful-Date") |
183 | |
); |
184 | |
} |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
@Inset.Runtime |
191 | |
public final AuthInset auth() { |
192 | 3 | return new AuthInset(this, Manifests.read("Stateful-SecurityKey")) |
193 | |
.with(new Auth(this, this.base())) |
194 | |
|
195 | |
.with(new Facebook(this, Manifests.read("Stateful-FbId"), Manifests.read("Stateful-FbSecret"))) |
196 | |
.with(new Google(this, Manifests.read("Stateful-GoogleId"), Manifests.read("Stateful-GoogleSecret"))) |
197 | |
.with(new Github(this, Manifests.read("Stateful-GithubId"), Manifests.read("Stateful-GithubSecret"))) |
198 | |
.with(BaseRs.TESTER); |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
protected final User user() { |
206 | 0 | final Identity identity = this.auth().identity(); |
207 | 0 | if (identity.equals(Identity.ANONYMOUS)) { |
208 | 0 | throw FlashInset.forward( |
209 | |
this.uriInfo().getBaseUriBuilder().clone() |
210 | |
.path(IndexRs.class) |
211 | |
.build(), |
212 | |
"please login first", |
213 | |
Level.SEVERE |
214 | |
); |
215 | |
} |
216 | 0 | return this.base().user(identity.urn()); |
217 | |
} |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
protected final Base base() { |
224 | 3 | final Base base = Base.class.cast( |
225 | |
this.servletContext().getAttribute(Base.class.getName()) |
226 | |
); |
227 | 3 | Validate.notNull(base, "spi is not initialized"); |
228 | 3 | return base; |
229 | |
} |
230 | |
|
231 | |
} |