forked from gooddata/gooddata-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoodData.java
More file actions
230 lines (202 loc) · 8.92 KB
/
GoodData.java
File metadata and controls
230 lines (202 loc) · 8.92 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
*/
package com.gooddata;
import com.gooddata.account.AccountService;
import com.gooddata.dataset.DatasetService;
import com.gooddata.gdc.DataStoreService;
import com.gooddata.gdc.GdcService;
import com.gooddata.http.client.GoodDataHttpClient;
import com.gooddata.http.client.LoginSSTRetrievalStrategy;
import com.gooddata.http.client.SSTRetrievalStrategy;
import com.gooddata.md.MetadataService;
import com.gooddata.model.ModelService;
import com.gooddata.project.ProjectService;
import com.gooddata.report.ReportService;
import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.VersionInfo;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
import static com.gooddata.Validate.notEmpty;
import static java.util.Collections.singletonMap;
import static org.apache.http.util.VersionInfo.loadVersionInfo;
/**
* Entry point for GoodData SDK usage.
* <p/>
* Configure connection to GoodData using one of constructors. One can then get initialized service he needs from
* the newly constructed instance. This instance can be also used later for logout from GoodData Platform.
* <p/>
* Usage example:
* <pre><code>
* GoodData gd = new GoodData("[email protected]", "Roman1");
* // do something useful like: gd.getSomeService().doSomething()
* gd.logout();
* </code></pre>
*/
public class GoodData {
public static final String GDC_REQUEST_ID_HEADER = "X-GDC-REQUEST";
private static final String PROTOCOL = "https";
private static final int PORT = 443;
private static final String HOSTNAME = "secure.gooddata.com";
private static final String UNKNOWN_VERSION = "UNKNOWN";
private final AccountService accountService;
private final ProjectService projectService;
private final MetadataService metadataService;
private final ModelService modelService;
private final GdcService gdcService;
private final DataStoreService dataStoreService;
private final DatasetService datasetService;
private final ReportService reportService;
/**
* Create instance configured to communicate with GoodData Platform under user with given credentials.
*
* @param login GoodData user's login
* @param password GoodData user's password
*/
public GoodData(String login, String password) {
this(HOSTNAME, login, password);
}
/**
* Create instance configured to communicate with GoodData Platform running on given host using given user's
* credentials.
*
* @param hostname GoodData Platform's host name (e.g. secure.gooddata.com)
* @param login GoodData user's login
* @param password GoodData user's password
*/
public GoodData(String hostname, String login, String password) {
this(hostname, login, password, PORT, PROTOCOL);
}
/**
* Create instance configured to communicate with GoodData Platform running on given host, port and protocol using
* given user's credentials.
*
* @param hostname GoodData Platform's host name (e.g. secure.gooddata.com)
* @param login GoodData user's login
* @param password GoodData user's password
* @param port GoodData Platform's API port (e.g. 443)
* @param protocol GoodData Platform's API protocol (e.g. https)
*/
public GoodData(String hostname, String login, String password, int port, String protocol) {
notEmpty(hostname, "hostname");
notEmpty(login, "login");
notEmpty(password, "password");
notEmpty(protocol, "protocol");
final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
.setUserAgent(getUserAgent());
final RestTemplate restTemplate = createRestTemplate(login, password, hostname, httpClientBuilder, port,
protocol);
accountService = new AccountService(restTemplate);
projectService = new ProjectService(restTemplate, accountService);
metadataService = new MetadataService(restTemplate);
modelService = new ModelService(restTemplate);
gdcService = new GdcService(restTemplate);
dataStoreService = new DataStoreService(httpClientBuilder, gdcService, login, password);
datasetService = new DatasetService(restTemplate, dataStoreService);
reportService = new ReportService(restTemplate);
}
private RestTemplate createRestTemplate(String login, String password, String hostname, HttpClientBuilder builder,
int port, String protocol) {
final HttpClient client = createHttpClient(login, password, hostname, port, protocol, builder);
final UriPrefixingClientHttpRequestFactory factory = new UriPrefixingClientHttpRequestFactory(
new HttpComponentsClientHttpRequestFactory(client), hostname, port, protocol);
final RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList(
new HeaderSettingRequestInterceptor(singletonMap("Accept", MediaType.APPLICATION_JSON_VALUE))));
restTemplate.setErrorHandler(new ResponseErrorHandler(restTemplate.getMessageConverters()));
return restTemplate;
}
protected HttpClient createHttpClient(final String login, final String password, final String hostname,
final int port, final String protocol, final HttpClientBuilder builder) {
final HttpHost host = new HttpHost(hostname, port, protocol);
final HttpClient httpClient = builder.build();
final SSTRetrievalStrategy strategy = new LoginSSTRetrievalStrategy(httpClient, host, login, password);
return new GoodDataHttpClient(httpClient, strategy);
}
private String getUserAgent() {
final Package pkg = Package.getPackage("com.gooddata");
final String clientVersion = pkg != null && pkg.getImplementationVersion() != null
? pkg.getImplementationVersion() : UNKNOWN_VERSION;
final VersionInfo vi = loadVersionInfo("org.apache.http.client", HttpClientBuilder.class.getClassLoader());
final String apacheVersion = vi != null ? vi.getRelease() : UNKNOWN_VERSION;
return String.format("%s/%s (%s; %s) %s/%s", "GoodData-Java-SDK", clientVersion,
System.getProperty("os.name"), System.getProperty("java.specification.version"),
"Apache-HttpClient", apacheVersion);
}
/**
* Logout from GoodData Platform
*/
public void logout() {
getAccountService().logout();
}
/**
* Get initialized service for project management (to list projects, create a project, ...)
*
* @return initialized service for project management
*/
public ProjectService getProjectService() {
return projectService;
}
/**
* Get initialized service for account management (to get current account information, logout, ...)
*
* @return initialized service for account management
*/
public AccountService getAccountService() {
return accountService;
}
/**
* Get initialized service for metadata management (to query, create and update project metadata like attributes,
* facts, metrics, reports, ...)
*
* @return initialized service for metadata management
*/
public MetadataService getMetadataService() {
return metadataService;
}
/**
* Get initialized service for model management (to get model diff, update model, ...)
*
* @return initialized service for model management
*/
public ModelService getModelService() {
return modelService;
}
/**
* Get initialized service for API root management (to get API root links, ...)
*
* @return initialized service for API root management
*/
public GdcService getGdcService() {
return gdcService;
}
/**
* Get initialized service for data store (user staging/WebDAV) management (to upload, download, delete, ...)
*
* @return initialized service for data store management
*/
public DataStoreService getDataStoreService() {
return dataStoreService;
}
/**
* Get initialized service for dataset management (to list manifest, get datasets, load dataset, ...)
*
* @return initialized service for dataset management
*/
public DatasetService getDatasetService() {
return datasetService;
}
/**
* Get initialized service for report management (to execute and export report, ...)
*
* @return initialized service for report management
*/
public ReportService getReportService() {
return reportService;
}
}