-
Notifications
You must be signed in to change notification settings - Fork 13
/
StorageRestClientJersey.java
194 lines (163 loc) · 5.32 KB
/
StorageRestClientJersey.java
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
package org.eclipse.store.storage.restclient.jersey.types;
/*-
* #%L
* EclipseStore Storage REST Client Jersey
* %%
* Copyright (C) 2023 MicroStream Software
* %%
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
* #L%
*/
import static org.eclipse.serializer.util.X.notNull;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import org.eclipse.serializer.persistence.binary.types.BinaryFieldLengthResolver;
import org.eclipse.serializer.persistence.types.PersistenceTypeDescription;
import org.eclipse.serializer.persistence.types.PersistenceTypeDictionaryParser;
import org.eclipse.serializer.persistence.types.PersistenceTypeNameMapper;
import org.eclipse.serializer.persistence.types.PersistenceTypeResolver;
import org.eclipse.serializer.reflect.ClassLoaderProvider;
import org.eclipse.store.storage.restadapter.types.ViewerObjectDescription;
import org.eclipse.store.storage.restadapter.types.ViewerRootDescription;
import org.eclipse.store.storage.restadapter.types.ViewerStorageFileStatistics;
import org.eclipse.store.storage.restclient.types.ObjectRequest;
import org.eclipse.store.storage.restclient.types.StorageRestClient;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
public interface StorageRestClientJersey extends StorageRestClient
{
public void setClientCustomizer(
Consumer<ClientBuilder> clientCustomizer
);
public static StorageRestClientJersey New(
final String baseUrl
)
{
return new Default(
notNull(baseUrl),
Routes.Default()
);
}
public static StorageRestClientJersey New(
final String baseUrl,
final Routes routes
)
{
return new Default(
notNull(baseUrl),
notNull(routes)
);
}
public static class Default implements StorageRestClientJersey
{
private final String baseUrl;
private final Routes routes;
private Consumer<ClientBuilder> clientCustomizer;
private Client client;
private WebTarget storageRestService;
Default(
final String baseUrl,
final Routes routes
)
{
super();
this.baseUrl = baseUrl;
this.routes = routes;
}
@Override
public void setClientCustomizer(
final Consumer<ClientBuilder> clientCustomizer
)
{
this.clientCustomizer = clientCustomizer;
}
private WebTarget storageRestService()
{
if(this.storageRestService == null)
{
final ClientBuilder clientBuilder = ClientBuilder.newBuilder()
.register(GsonReader.class);
if(this.clientCustomizer != null)
{
this.clientCustomizer.accept(clientBuilder);
}
this.client = clientBuilder.build();
this.storageRestService = this.client.target(this.baseUrl);
}
return this.storageRestService;
}
@Override
public Map<Long, PersistenceTypeDescription> requestTypeDictionary()
{
final String data = this.storageRestService()
.path(this.routes.dictionary())
.request(MediaType.APPLICATION_JSON)
.get(String.class);
final PersistenceTypeDictionaryParser parser = PersistenceTypeDictionaryParser.New(
PersistenceTypeResolver.New(ClassLoaderProvider.System()),
new BinaryFieldLengthResolver.Default(),
PersistenceTypeNameMapper.New()
);
final Map<Long, PersistenceTypeDescription> dictionary = new HashMap<>();
parser.parseTypeDictionaryEntries(data).forEach(entry -> dictionary.put(entry.typeId(), entry));
return dictionary;
}
@Override
public ViewerRootDescription requestRoot()
{
return this.storageRestService()
.path(this.routes.root())
.request(MediaType.APPLICATION_JSON)
.get(ViewerRootDescription.class);
}
@Override
public ViewerObjectDescription requestObject(
final ObjectRequest objectRequest
)
{
WebTarget target = this.storageRestService()
.path(this.routes.object())
.path(Long.toString(objectRequest.objectId()));
target = this.optAddParam(target, "valueLength", objectRequest.valueLength ());
target = this.optAddParam(target, "fixedOffset", objectRequest.fixedOffset ());
target = this.optAddParam(target, "fixedLength", objectRequest.fixedLength ());
target = this.optAddParam(target, "variableOffset", objectRequest.variableOffset ());
target = this.optAddParam(target, "variableLength", objectRequest.variableLength ());
target = this.optAddParam(target, "references", objectRequest.references ());
return target
.request(MediaType.APPLICATION_JSON)
.get(ViewerObjectDescription.class);
}
private WebTarget optAddParam(final WebTarget target, final String name, final Object value)
{
return value != null
? target.queryParam(name, value)
: target;
}
@Override
public ViewerStorageFileStatistics requestFileStatistics()
{
return this.storageRestService()
.path(this.routes.filesStatistics())
.request(MediaType.APPLICATION_JSON)
.get(ViewerStorageFileStatistics.class);
}
@Override
public void close()
{
if(this.client != null)
{
this.client.close();
this.client = null;
}
this.storageRestService = null;
}
}
}