Skip to content

Commit 3f8a958

Browse files
committed
.
1 parent 83bc98a commit 3f8a958

File tree

1 file changed

+259
-0
lines changed

1 file changed

+259
-0
lines changed
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
package org.openstack.client.common;
2+
3+
import java.io.Serializable;
4+
import java.util.logging.Logger;
5+
6+
import javax.ws.rs.core.MediaType;
7+
8+
import org.openstack.client.storage.OpenstackStorageClient;
9+
import org.openstack.model.atom.Link;
10+
import org.openstack.model.common.OpenStackSessionData;
11+
import org.openstack.model.compute.NovaFlavor;
12+
import org.openstack.model.compute.NovaImage;
13+
import org.openstack.model.exceptions.OpenstackException;
14+
import org.openstack.model.identity.KeyStoneToken;
15+
16+
public abstract class OpenStackSession implements Serializable {
17+
18+
static final Logger log = Logger.getLogger(OpenStackSession.class.getName());
19+
20+
protected OpenStackSessionData data = new OpenStackSessionData();
21+
22+
protected OpenstackCredentials credentials;
23+
24+
public enum Feature {
25+
26+
VERBOSE(false), FORCE_JSON(false), FORCE_XML(false);
27+
28+
private boolean enabled;
29+
30+
public boolean isEnabled() {
31+
return enabled;
32+
}
33+
34+
public int mask() {
35+
return (1 << ordinal());
36+
}
37+
38+
private Feature(boolean enabled) {
39+
this.enabled = enabled;
40+
}
41+
}
42+
43+
private int features;
44+
45+
private OpenStackIdentityConfig identityConfig;
46+
47+
private OpenStackComputeConfig computeConfig;
48+
49+
private OpenStackImageConfig imageConfig;
50+
51+
transient LinkResolver linkResolver;
52+
53+
public OpenStackSession() {
54+
55+
// calculate the bitmap
56+
for (Feature f : Feature.class.getEnumConstants()) {
57+
if (f.isEnabled()) {
58+
features = features | f.mask();
59+
}
60+
}
61+
62+
computeConfig = new OpenStackComputeConfig();
63+
64+
identityConfig = new OpenStackIdentityConfig();
65+
66+
imageConfig = new OpenStackImageConfig();
67+
68+
}
69+
70+
public OpenStackSession enable(Feature feature) {
71+
features = features | feature.mask();
72+
return this;
73+
}
74+
75+
public OpenStackSession disable(Feature feature) {
76+
features = features & ~feature.mask();
77+
return this;
78+
}
79+
80+
public OpenStackIdentityConfig getIdentityConfig() {
81+
return identityConfig;
82+
}
83+
84+
public void setIdentityConfig(OpenStackIdentityConfig identityConfig) {
85+
this.identityConfig = identityConfig;
86+
}
87+
88+
public OpenStackComputeConfig getComputeConfig() {
89+
return computeConfig;
90+
}
91+
92+
public void setComputeConfig(OpenStackComputeConfig computeConfig) {
93+
this.computeConfig = computeConfig;
94+
}
95+
96+
public OpenStackImageConfig getImageConfig() {
97+
return imageConfig;
98+
}
99+
100+
public void setImageConfig(OpenStackImageConfig imageConfig) {
101+
this.imageConfig = imageConfig;
102+
}
103+
104+
public boolean isEnabled(Feature feature) {
105+
return (features & feature.mask()) == 1;
106+
}
107+
108+
public OpenStackSession with(Feature... features) {
109+
for (Feature feature : features) {
110+
this.features = this.features | feature.mask();
111+
}
112+
return this;
113+
}
114+
115+
public OpenStackSession without(Feature... features) {
116+
for (Feature feature : features) {
117+
this.features = this.features & ~feature.mask();
118+
}
119+
return this;
120+
}
121+
122+
//
123+
124+
/*
125+
* public OpenstackSession(String authURL) { this.authenticationUrl = authURL; }
126+
*
127+
* public OpenstackSession(String authUrl, OpenstackCredentials credentials) { this(authUrl);
128+
* authenticate(credentials); }
129+
*/
130+
131+
public RequestBuilder resource(String resourceUrl) {
132+
RequestBuilder request = createRequestBuilder(resourceUrl);
133+
134+
if (isEnabled(Feature.VERBOSE)) {
135+
request.setVerbose(true);
136+
}
137+
138+
KeyStoneToken token = null;
139+
if (data.isAuthenticated()) {
140+
token = data.getAccess().getToken();
141+
}
142+
143+
String authTokenId = null;
144+
if (token != null) {
145+
authTokenId = token.getId();
146+
}
147+
148+
if (authTokenId != null && !authTokenId.isEmpty()) {
149+
request.putHeader("X-Auth-Token", authTokenId);
150+
}
151+
152+
return request;
153+
}
154+
155+
protected abstract RequestBuilder createRequestBuilder(String resourceUrl);
156+
157+
public OpenstackImageClient getImageClient() {
158+
return new OpenstackImageClient(this);
159+
}
160+
161+
public OpenstackComputeClient getComputeClient() {
162+
return new OpenstackComputeClient(this);
163+
}
164+
165+
public OpenstackAuthenticationClient getAuthenticationClient() {
166+
return new OpenstackAuthenticationClient(this);
167+
}
168+
169+
public OpenstackStorageClient getStorageClient() {
170+
return new OpenstackStorageClient(this);
171+
}
172+
173+
public void authenticate(OpenstackCredentials credentials) throws OpenstackException {
174+
authenticate(credentials, false);
175+
}
176+
177+
public void authenticate(OpenstackCredentials credentials, boolean storeCredentials) throws OpenstackException {
178+
if (storeCredentials) {
179+
this.credentials = credentials;
180+
}
181+
data.setAccess(getAuthenticationClient().authenticate(credentials));
182+
}
183+
184+
185+
186+
public <T> T followLink(Link link, Class<T> clazz) {
187+
return follow(link, null, clazz);
188+
}
189+
190+
public LinkResolver getLinkResolver() {
191+
if (linkResolver == null) {
192+
linkResolver = new SimpleLinkResolver(this);
193+
}
194+
return linkResolver;
195+
}
196+
197+
public void setLinkResolver(LinkResolver linkResolver) {
198+
this.linkResolver = linkResolver;
199+
}
200+
201+
public MediaType getForceContentType() {
202+
if (isEnabled(Feature.FORCE_JSON)) {
203+
return MediaType.APPLICATION_JSON_TYPE;
204+
}
205+
if (isEnabled(Feature.FORCE_XML)) {
206+
return MediaType.APPLICATION_XML_TYPE;
207+
}
208+
return null;
209+
}
210+
211+
public NovaImage resolveImage(NovaImage image) throws OpenstackException {
212+
if (image == null)
213+
return null;
214+
215+
return getLinkResolver().resolveImage(image.getId(), image.getLinks());
216+
}
217+
218+
public NovaFlavor resolveFlavor(NovaFlavor flavor) throws OpenstackException {
219+
if (flavor == null)
220+
return null;
221+
222+
return getLinkResolver().resolveFlavor(flavor.getId(), flavor.getLinks());
223+
}
224+
225+
public static OpenStackSession create() {
226+
return new JerseyOpenstackSession();
227+
}
228+
229+
230+
231+
public void reauthenticate() throws OpenstackException {
232+
if (!hasStoredCredentials()) {
233+
throw new IllegalStateException("Credentials were not saved");
234+
}
235+
authenticate(credentials, false);
236+
}
237+
238+
public <T> T follow(Link link, String method, Class<T> c) {
239+
try {
240+
RequestBuilder request = resource(link.getHref()).addAcceptType(MediaType.APPLICATION_XML_TYPE).setContentType(MediaType.APPLICATION_XML_TYPE);
241+
return request.get(c);
242+
} catch (Exception e) {
243+
throw new OpenstackException(e.getMessage(), e);
244+
}
245+
}
246+
247+
public OpenStackSessionData getData() {
248+
return data;
249+
}
250+
251+
public void setData(OpenStackSessionData data) {
252+
this.data = data;
253+
}
254+
255+
public boolean hasStoredCredentials() {
256+
return credentials != null;
257+
}
258+
259+
}

0 commit comments

Comments
 (0)