gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.lib / org.gvsig.vcsgis.lib.impl / src / main / java / org / gvsig / vcsgis / lib / repository / localdb / VCSGisRepositoryLocaldb.java @ 3748
History | View | Annotate | Download (23 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
18 |
*
|
19 |
* For any additional information, do not hesitate to contact us
|
20 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
21 |
*/
|
22 |
|
23 |
package org.gvsig.vcsgis.lib.repository.localdb; |
24 |
|
25 |
import java.time.LocalDateTime; |
26 |
import java.util.Date; |
27 |
import java.util.HashMap; |
28 |
import java.util.List; |
29 |
import java.util.Map; |
30 |
import java.util.zip.CRC32; |
31 |
import javax.json.JsonObject; |
32 |
import org.apache.commons.lang3.BooleanUtils; |
33 |
import org.apache.commons.lang3.StringUtils; |
34 |
import org.gvsig.fmap.dal.DALLocator; |
35 |
import org.gvsig.fmap.dal.DataManager; |
36 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
37 |
import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
38 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.CONFIG_NAME_STORESREPOSITORYID; |
39 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_CONFIGURATION; |
40 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_REPOSITORY; |
41 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES; |
42 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
43 |
import org.gvsig.fmap.dal.feature.FeatureType; |
44 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
45 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
46 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
47 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
48 |
import org.gvsig.json.Json; |
49 |
import org.gvsig.json.JsonObjectBuilder; |
50 |
import org.gvsig.tools.ToolsLocator; |
51 |
import static org.gvsig.tools.dataTypes.DataTypeUtils.toTimestamp; |
52 |
import org.gvsig.tools.dispose.DisposeUtils; |
53 |
import org.gvsig.tools.task.SimpleTaskStatus; |
54 |
import org.gvsig.vcsgis.lib.VCSGisCodeGenerator; |
55 |
import org.gvsig.vcsgis.lib.VCSGisEntity; |
56 |
import org.gvsig.vcsgis.lib.VCSGisLocator; |
57 |
import org.gvsig.vcsgis.lib.VCSGisManager; |
58 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_AUTHENTICATION_EXPIRED; |
59 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_INVALID_AUTHENTICATION_TOKEN; |
60 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_INVALID_USERCODE; |
61 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK; |
62 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_USER_NOT_AUTHORIZED; |
63 |
import org.gvsig.vcsgis.lib.VCSGisRuntimeException; |
64 |
import org.gvsig.vcsgis.lib.VCSGisUser; |
65 |
import org.gvsig.vcsgis.lib.VCSGisUtils; |
66 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepository; |
67 |
import org.gvsig.vcsgis.lib.repository.localdb.requests.AbstractRequestLocaldb; |
68 |
import org.gvsig.vcsgis.lib.repository.localdb.requests.AuthenticateRequestLocaldb; |
69 |
import org.gvsig.vcsgis.lib.repository.localdb.requests.CheckoutRequestLocaldb; |
70 |
import org.gvsig.vcsgis.lib.repository.localdb.requests.CommitRequestLocaldb; |
71 |
import org.gvsig.vcsgis.lib.repository.localdb.requests.EntitiesRequestLocaldb; |
72 |
import org.gvsig.vcsgis.lib.repository.localdb.requests.HistoryRequestLocaldb; |
73 |
import org.gvsig.vcsgis.lib.repository.localdb.requests.RowCreateRequestLocaldb; |
74 |
import org.gvsig.vcsgis.lib.repository.localdb.requests.RowDeleteRequestLocaldb; |
75 |
import org.gvsig.vcsgis.lib.repository.localdb.requests.RowUpdateRequestLocaldb; |
76 |
import org.gvsig.vcsgis.lib.repository.localdb.requests.TopologyPlansRequestLocaldb; |
77 |
import org.gvsig.vcsgis.lib.repository.localdb.requests.UpdateRequestLocaldb; |
78 |
import org.gvsig.vcsgis.lib.repository.localdb.requests.UsersRequestLocaldb; |
79 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.ConfigRepoTable; |
80 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.DataRepoTable; |
81 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.EntitiesRepoTable; |
82 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.HooksRepoTable; |
83 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.RevisionsRepoTable; |
84 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.TopologyplanRepoTable; |
85 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.UsersRepoTable; |
86 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisAuthenticateRequest; |
87 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisCheckoutRequest; |
88 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisCommitRequest; |
89 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisEntitiesRequest; |
90 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisHistoryRequest; |
91 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRequest; |
92 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRowCreateRequest; |
93 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRowDeleteRequest; |
94 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRowUpdateRequest; |
95 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisTopologyPlansRequest; |
96 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisUpdateRequest; |
97 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisUsersRequest; |
98 |
import org.gvsig.vcsgis.lib.requests.AbstractRequest; |
99 |
import org.slf4j.Logger; |
100 |
import org.slf4j.LoggerFactory; |
101 |
|
102 |
/**
|
103 |
*
|
104 |
* @author gvSIG Team
|
105 |
*/
|
106 |
@SuppressWarnings("UseSpecificCatch") |
107 |
public class VCSGisRepositoryLocaldb implements VCSGisRepository { |
108 |
private static final Logger LOGGER = LoggerFactory.getLogger(VCSGisRepositoryLocaldb.class); |
109 |
|
110 |
|
111 |
public static int create(JDBCServerExplorerParameters serverParameters, VCSGisCodeGenerator codeGenerator, SimpleTaskStatus status) { |
112 |
if( status == null ) { |
113 |
status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("vcsgis init");
|
114 |
status.add(); |
115 |
} |
116 |
int r = 3; |
117 |
status.push(); |
118 |
DataManager dataManager = DALLocator.getDataManager(); |
119 |
JDBCServerExplorer server = null;
|
120 |
try {
|
121 |
server = (JDBCServerExplorer) dataManager.openServerExplorer( |
122 |
serverParameters.getProviderName(), |
123 |
serverParameters |
124 |
); |
125 |
} catch (Exception ex) { |
126 |
throw new VCSGisRuntimeException(VCSGisManager.ERR_INVALID_REPOSITORY, "Can't open server explorer."); |
127 |
} |
128 |
|
129 |
try {
|
130 |
status.setTitle("vcsgis init");
|
131 |
|
132 |
DatabaseWorkspaceManager databaseWorkspaceManager = dataManager.createDatabaseWorkspaceManager(serverParameters); |
133 |
|
134 |
if(!databaseWorkspaceManager.existsTable(TABLE_CONFIGURATION)){
|
135 |
databaseWorkspaceManager.createTable(TABLE_CONFIGURATION); |
136 |
} |
137 |
if(!databaseWorkspaceManager.existsTable(TABLE_RESOURCES)){
|
138 |
databaseWorkspaceManager.createTable(TABLE_RESOURCES); |
139 |
} |
140 |
if(!databaseWorkspaceManager.existsTable(TABLE_REPOSITORY)){
|
141 |
databaseWorkspaceManager.createTable(TABLE_REPOSITORY); |
142 |
databaseWorkspaceManager.set(CONFIG_NAME_STORESREPOSITORYID, "VCSGIS");
|
143 |
} |
144 |
databaseWorkspaceManager.connect(); |
145 |
|
146 |
FeatureType[] tables = new FeatureType[] { |
147 |
ConfigRepoTable.featureType(), |
148 |
UsersRepoTable.featureType(), |
149 |
TopologyplanRepoTable.featureType(), |
150 |
EntitiesRepoTable.featureType(), |
151 |
RevisionsRepoTable.featureType(), |
152 |
DataRepoTable.featureType(), |
153 |
// ResourcesRepoTable.featureType(),
|
154 |
HooksRepoTable.featureType(), |
155 |
}; |
156 |
r = 4;
|
157 |
status.setRangeOfValues(0, tables.length);
|
158 |
int step = 1; |
159 |
for (FeatureType table : tables) {
|
160 |
status.message(table.getLabel()); |
161 |
status.setCurValue(step++); |
162 |
r = 4+step;
|
163 |
JDBCNewStoreParameters table_params = server.getAddParameters(); |
164 |
table_params.setDefaultFeatureType(table.getEditable()); |
165 |
String tableName = table.getTags().getString("ID"); |
166 |
table_params.setTable(tableName); |
167 |
server.add(server.getProviderName(), table_params, false);
|
168 |
|
169 |
JDBCStoreParameters openParams = server.get(tableName); |
170 |
databaseWorkspaceManager.writeStoresRepositoryEntry(tableName, openParams); |
171 |
} |
172 |
|
173 |
try {
|
174 |
server.execute(VCSGisUtils.getSqlTemplate(serverParameters.getProviderName(), "createRepositoryIndex1"));
|
175 |
server.execute(VCSGisUtils.getSqlTemplate(serverParameters.getProviderName(), "createRepositoryIndex2"));
|
176 |
} catch (Exception ex) { |
177 |
LOGGER.warn("Can't create index in workspace", ex);
|
178 |
} |
179 |
|
180 |
VCSGisRepositoryLocaldb repo = new VCSGisRepositoryLocaldb();
|
181 |
repo.initialize(serverParameters, codeGenerator); |
182 |
DisposeUtils.disposeQuietly(repo); |
183 |
status.terminate(); |
184 |
} catch(Exception ex) { |
185 |
status.abort(); |
186 |
LOGGER.warn("Can't init repository.",ex);
|
187 |
status.message(ex.getMessage()); |
188 |
return r;
|
189 |
} finally {
|
190 |
status.pop(); |
191 |
} |
192 |
return 0; |
193 |
} |
194 |
|
195 |
private JDBCServerExplorer server;
|
196 |
private JDBCServerExplorerParameters serverParameters;
|
197 |
private Map<String,FeatureStore> stores; |
198 |
private VCSGisCodeGenerator codeGenerator;
|
199 |
private String code; |
200 |
private HooksRepoTable hooksTable;
|
201 |
private boolean allowAssignTheRevisionDate; |
202 |
// private boolean authenticationRequired;
|
203 |
// private boolean authorizationRequired;
|
204 |
private String token; |
205 |
private String userCode; |
206 |
|
207 |
public VCSGisRepositoryLocaldb() {
|
208 |
// For persistence
|
209 |
this.codeGenerator = null; |
210 |
this.stores = null; |
211 |
this.server = null; |
212 |
this.serverParameters = null; |
213 |
this.code = null; |
214 |
this.allowAssignTheRevisionDate = false; |
215 |
// this.authenticationRequired = false;
|
216 |
// this.authorizationRequired = false;
|
217 |
} |
218 |
|
219 |
public VCSGisRepositoryLocaldb(JDBCServerExplorerParameters serverParameters, VCSGisCodeGenerator codeGenerator) {
|
220 |
this.stores = null; |
221 |
this.codeGenerator = codeGenerator;
|
222 |
this.serverParameters = serverParameters;
|
223 |
DataManager dataManager = DALLocator.getDataManager(); |
224 |
try {
|
225 |
this.server = (JDBCServerExplorer) dataManager.openServerExplorer(
|
226 |
serverParameters.getProviderName(), |
227 |
serverParameters |
228 |
); |
229 |
} catch (Exception ex) { |
230 |
throw new VCSGisRuntimeException(VCSGisManager.ERR_INVALID_REPOSITORY, "Can't open server explorer."); |
231 |
} |
232 |
|
233 |
ConfigRepoTable varsTable = new ConfigRepoTable();
|
234 |
this.code = varsTable.get(this, "REPOSITORY_CODE"); |
235 |
if (this.code == null){ |
236 |
throw new VCSGisRuntimeException(VCSGisManager.ERR_INVALID_REPOSITORY, "The database can't have REPOSITORY_CODE variable."); |
237 |
} |
238 |
// this.authenticationRequired = BooleanUtils.toBoolean(varsTable.get(this, "AUTHENTICATION"));
|
239 |
// this.authorizationRequired = BooleanUtils.toBoolean(varsTable.get(this, "AUTHORIZATION"));
|
240 |
|
241 |
this.hooksTable = new HooksRepoTable(); |
242 |
hooksTable.initHooks(this);
|
243 |
} |
244 |
|
245 |
private void initialize(JDBCServerExplorerParameters serverParameters, VCSGisCodeGenerator codeGenerator) { |
246 |
this.stores = null; |
247 |
this.codeGenerator = codeGenerator;
|
248 |
this.serverParameters = serverParameters;
|
249 |
DataManager dataManager = DALLocator.getDataManager(); |
250 |
try {
|
251 |
this.server = (JDBCServerExplorer) dataManager.openServerExplorer(
|
252 |
serverParameters.getProviderName(), |
253 |
serverParameters |
254 |
); |
255 |
} catch (Exception ex) { |
256 |
throw new VCSGisRuntimeException(VCSGisManager.ERR_INVALID_REPOSITORY, "Can't open server explorer."); |
257 |
} |
258 |
this.code = this.createUniqueCode(); |
259 |
ConfigRepoTable varsTable = new ConfigRepoTable();
|
260 |
varsTable.set(this, "REPOSITORY_CODE", this.code); |
261 |
} |
262 |
|
263 |
@Override
|
264 |
public void dispose() { |
265 |
if(this.stores != null){ |
266 |
for (FeatureStore store : this.stores.values()) { |
267 |
DisposeUtils.disposeQuietly(store); |
268 |
} |
269 |
this.stores = null; |
270 |
} |
271 |
DisposeUtils.disposeQuietly(this.server);
|
272 |
this.server = null; |
273 |
} |
274 |
|
275 |
@Override
|
276 |
public String getUserCode() { |
277 |
return this.userCode; |
278 |
} |
279 |
|
280 |
@Override
|
281 |
public String getLabel() { |
282 |
return this.server.getParameters().getUrl(); |
283 |
} |
284 |
|
285 |
@Override
|
286 |
public String createUniqueCode() { |
287 |
return this.codeGenerator.generateCode(); |
288 |
} |
289 |
|
290 |
public JDBCServerExplorer getServerExplorer() {
|
291 |
return this.server; |
292 |
} |
293 |
|
294 |
public static void selfRegister() { |
295 |
Json.registerSerializer(VCSGisRepositoryLocaldb.class); |
296 |
} |
297 |
|
298 |
@Override
|
299 |
public VCSGisCommitRequest createCommitRequest() {
|
300 |
return new CommitRequestLocaldb(this); |
301 |
} |
302 |
|
303 |
@Override
|
304 |
public VCSGisUpdateRequest createUpdateRequest(String entityName) { |
305 |
return new UpdateRequestLocaldb(this, entityName); |
306 |
} |
307 |
|
308 |
|
309 |
@Override
|
310 |
public VCSGisCheckoutRequest createCheckoutRequest(String entityName) { |
311 |
return new CheckoutRequestLocaldb(this, entityName); |
312 |
} |
313 |
|
314 |
@Override
|
315 |
public void fromJson(JsonObject json) { |
316 |
try {
|
317 |
DataManager dataManager = DALLocator.getDataManager(); |
318 |
DataServerExplorerParameters explorerParams = (DataServerExplorerParameters) Json.toObject(json, "explorerParams");
|
319 |
this.server = (JDBCServerExplorer) dataManager.openServerExplorer(
|
320 |
explorerParams.getProviderName(), |
321 |
explorerParams |
322 |
); |
323 |
this.codeGenerator = VCSGisLocator.getVCSGisManager().getCodeGenerator();
|
324 |
this.stores = new HashMap<>(); |
325 |
this.code = json.getString("code", null); |
326 |
this.allowAssignTheRevisionDate = json.getBoolean("allowAssignTheRevisionDate", false); |
327 |
this.hooksTable = new HooksRepoTable(); |
328 |
hooksTable.initHooks(this);
|
329 |
// this.authenticationRequired = json.getBoolean("authenticationRequired", false);
|
330 |
|
331 |
} catch (Exception ex) { |
332 |
throw new RuntimeException("Can't restore repository from json object.", ex); |
333 |
} |
334 |
} |
335 |
|
336 |
@Override
|
337 |
public JsonObject toJson() {
|
338 |
return this.toJsonBuilder().build(); |
339 |
} |
340 |
|
341 |
@Override
|
342 |
public JsonObjectBuilder toJsonBuilder() {
|
343 |
JsonObjectBuilder builder = Json.createObjectBuilder(); |
344 |
builder.add_class(this);
|
345 |
builder.add("explorerParams", this.server.getParameters()); |
346 |
builder.add("code", this.code); |
347 |
builder.add("allowAssignTheRevisionDate", this.allowAssignTheRevisionDate); |
348 |
// builder.add("authenticationRequired", this.authenticationRequired);
|
349 |
return builder;
|
350 |
} |
351 |
|
352 |
public FeatureStore getFeatureStore(String tableName) { |
353 |
if( this.stores==null ) { |
354 |
this.stores = new HashMap<>(); |
355 |
} |
356 |
FeatureStore store = this.stores.get(tableName);
|
357 |
if( store==null ) { |
358 |
store = this.openFeatureStore(tableName, null); |
359 |
this.stores.put(tableName, store);
|
360 |
} |
361 |
DisposeUtils.bind(store); |
362 |
return store;
|
363 |
} |
364 |
|
365 |
public FeatureStore openFeatureStore(String tableName, String sql) { |
366 |
FeatureStore store; |
367 |
try {
|
368 |
JDBCStoreParameters params = this.server.get(tableName);
|
369 |
params.setSQL(sql); |
370 |
store = (FeatureStore) DALLocator.getDataManager().openStore(params.getProviderName(), params); |
371 |
return store;
|
372 |
} catch (Exception ex) { |
373 |
throw new RuntimeException("Can't open store '"+tableName+"'.", ex); |
374 |
} |
375 |
} |
376 |
|
377 |
@Override
|
378 |
public VCSGisEntitiesRequest createEntitiesRequest() {
|
379 |
return new EntitiesRequestLocaldb(this); |
380 |
} |
381 |
|
382 |
@Override
|
383 |
public VCSGisHistoryRequest createHistoryRequest(String entityName) { |
384 |
return new HistoryRequestLocaldb(this, entityName); |
385 |
} |
386 |
|
387 |
@Override
|
388 |
public VCSGisEntity getEntityByName(String entityName) { |
389 |
VCSGisEntitiesRequest request = this.createEntitiesRequest();
|
390 |
if(request.execute() != ERR_OK){
|
391 |
return null; |
392 |
} |
393 |
for (VCSGisEntity repositoryEntity : request.getRepositoryEntities()) {
|
394 |
if(StringUtils.equalsIgnoreCase(repositoryEntity.getEntityName(), entityName)){
|
395 |
return repositoryEntity;
|
396 |
} |
397 |
} |
398 |
return null; |
399 |
} |
400 |
|
401 |
@Override
|
402 |
public VCSGisRowCreateRequest createRowCreateRequest(String entityName, String localRevisionCode) { |
403 |
VCSGisRowCreateRequest request = new RowCreateRequestLocaldb(this, entityName, localRevisionCode); |
404 |
return request;
|
405 |
} |
406 |
|
407 |
@Override
|
408 |
public VCSGisRowDeleteRequest createRowDeleteRequest(String entityName, String localRevisionCode) { |
409 |
VCSGisRowDeleteRequest request = new RowDeleteRequestLocaldb(this, entityName, localRevisionCode); |
410 |
return request;
|
411 |
} |
412 |
|
413 |
@Override
|
414 |
public VCSGisRowUpdateRequest createRowUpdateRequest(String entityName, String localRevisionCode) { |
415 |
VCSGisRowUpdateRequest request = new RowUpdateRequestLocaldb(this, entityName, localRevisionCode); |
416 |
return request;
|
417 |
} |
418 |
|
419 |
@Override
|
420 |
public VCSGisTopologyPlansRequest createTopologyPlansRequest() {
|
421 |
VCSGisTopologyPlansRequest request = new TopologyPlansRequestLocaldb(this); |
422 |
return request;
|
423 |
} |
424 |
|
425 |
@Override
|
426 |
public VCSGisUsersRequest createUsersRequest() {
|
427 |
VCSGisUsersRequest request = new UsersRequestLocaldb(this); |
428 |
return request;
|
429 |
} |
430 |
|
431 |
@Override
|
432 |
public void addObserver(String tableName, String type, String operation, String command) { |
433 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
434 |
} |
435 |
|
436 |
public void notifyObservers(VCSGisRequest request) { |
437 |
this.hooksTable.after_request(this.getUserCode(), request.getRequestName(), null, null); |
438 |
} |
439 |
|
440 |
public void notifyObservers(VCSGisRequest request, String tableName, String revisionCode) { |
441 |
this.hooksTable.after_request(this.getUserCode(), request.getRequestName(), tableName, revisionCode); |
442 |
} |
443 |
|
444 |
public boolean isAllowedToAssignTheRevisionDate() { |
445 |
return this.allowAssignTheRevisionDate; |
446 |
} |
447 |
|
448 |
public void setAllowAssignTheRevisionDate(boolean allowAssignTheRevisionDate) { |
449 |
this.allowAssignTheRevisionDate = allowAssignTheRevisionDate;
|
450 |
} |
451 |
|
452 |
public String getAuthenticationToken(String userCode, int minutesToExpire) { |
453 |
CRC32 crc = new CRC32(); |
454 |
crc.update(userCode.getBytes()); |
455 |
|
456 |
long expireTime = 0; |
457 |
if( minutesToExpire>0 ) { |
458 |
expireTime = toTimestamp(LocalDateTime.now().plusMinutes(minutesToExpire)).getTime(); |
459 |
} |
460 |
|
461 |
String token = String.format("%016x%016x", |
462 |
crc.getValue(), |
463 |
expireTime |
464 |
); |
465 |
|
466 |
return token;
|
467 |
} |
468 |
|
469 |
public boolean isAuthenticationRequired() { |
470 |
ConfigRepoTable varsTable = new ConfigRepoTable();
|
471 |
return BooleanUtils.toBoolean(varsTable.get(this, "AUTHENTICATION")); |
472 |
} |
473 |
|
474 |
public boolean isAuthorizationRequired() { |
475 |
ConfigRepoTable varsTable = new ConfigRepoTable();
|
476 |
return BooleanUtils.toBoolean(varsTable.get(this, "AUTHORIZATION")); |
477 |
} |
478 |
|
479 |
public boolean isAuthenticated(AbstractRequest request) { |
480 |
if( !this.isAuthenticationRequired() ) { |
481 |
LOGGER.info("Authentication not required for this repository.");
|
482 |
return true; |
483 |
} |
484 |
if( StringUtils.isBlank(request.getUserCode()) ) {
|
485 |
request.error(ERR_INVALID_USERCODE,"User is not authenticated.");
|
486 |
return false; |
487 |
} |
488 |
String authenticationToken = request.getAuthenticationToken();
|
489 |
if( StringUtils.isBlank(authenticationToken) || authenticationToken.length()!=32 ) { |
490 |
request.error(ERR_INVALID_AUTHENTICATION_TOKEN,"User '"+request.getUserCode()+"' is not authenticated."); |
491 |
return false; |
492 |
} |
493 |
|
494 |
// TODO: check expire time and CRC
|
495 |
long expireTime_l = Long.parseLong(authenticationToken.substring(17), 16); |
496 |
if( expireTime_l>0 ) { |
497 |
long now_l = new java.util.Date().getTime(); |
498 |
if( now_l > expireTime_l ) {
|
499 |
request.error(ERR_AUTHENTICATION_EXPIRED,"User '"+request.getUserCode()+"' is not authorized to '"+request.getRequestName()+"'."); |
500 |
return false; |
501 |
} |
502 |
} |
503 |
|
504 |
Date d = new java.util.Date(expireTime_l); |
505 |
Date now = new java.util.Date(); |
506 |
|
507 |
LOGGER.info("Repository authentication for user with userCode '"+request.getUserCode()+"' expire at '"+d.toString()+"' (now is '"+now.toString()+"'."); |
508 |
return true; |
509 |
} |
510 |
|
511 |
public boolean isAuthorized(AbstractRequestLocaldb request) { |
512 |
return this.isAuthorized(request, null); |
513 |
} |
514 |
|
515 |
public boolean isAuthorized(AbstractRequestLocaldb request, String operation) { |
516 |
if( !this.isAuthenticated(request) ) { |
517 |
return false; |
518 |
} |
519 |
if( !this.isAuthorizationRequired() ) { |
520 |
LOGGER.info("Repository authorization not required.");
|
521 |
return true; |
522 |
} |
523 |
if( operation == null ) { |
524 |
operation = request.getRequestName().toLowerCase(); |
525 |
} |
526 |
|
527 |
VCSGisUser user = request.getUser(); |
528 |
if( user == null ) { |
529 |
request.error(ERR_USER_NOT_AUTHORIZED,"User '"+request.getUserCode()+"' is not authorized to '"+request.getRequestName()+"'."); |
530 |
return false; |
531 |
} |
532 |
List<String> operations = user.getAllowedOperationsAsList(); |
533 |
if( operations == null ) { |
534 |
LOGGER.info("All repository operations are allowed for user '"+user.getIdentifier()+"' ("+user.getUserCode()+")."); |
535 |
return true; |
536 |
} |
537 |
if( !operations.contains(operation) ) {
|
538 |
request.error(ERR_USER_NOT_AUTHORIZED,"User '"+request.getUserCode()+"' is not authorized to '"+request.getRequestName()+"'."); |
539 |
return false; |
540 |
} |
541 |
LOGGER.info("Repository authorization for '"+operation + "' operation is allowed for user '"+user.getIdentifier()+"' ("+user.getUserCode()+")."); |
542 |
return true; |
543 |
} |
544 |
|
545 |
@Override
|
546 |
public VCSGisAuthenticateRequest createAuthenticateRequest(String userId, String password) { |
547 |
VCSGisAuthenticateRequest request = new AuthenticateRequestLocaldb(this); |
548 |
request.setUserId(userId); |
549 |
request.setPassword(password); |
550 |
return request;
|
551 |
} |
552 |
|
553 |
@Override
|
554 |
public String getAuthenticationToken() { |
555 |
return this.token; |
556 |
} |
557 |
|
558 |
@Override
|
559 |
public void setAuthenticationToken(String token) { |
560 |
this.token = token;
|
561 |
} |
562 |
|
563 |
@Override
|
564 |
public void setUserCode(String userCode) { |
565 |
this.userCode = userCode;
|
566 |
} |
567 |
|
568 |
|
569 |
} |