1
|
package eu.dnetlib.parthenos.registry;
|
2
|
|
3
|
import eu.dnetlib.parthenos.publisher.ParthenosPublisherException;
|
4
|
import eu.dnetlib.parthenos.publisher.SaxonHelper;
|
5
|
import org.apache.commons.lang3.StringUtils;
|
6
|
import org.apache.commons.logging.Log;
|
7
|
import org.apache.commons.logging.LogFactory;
|
8
|
import org.gcube.common.authorization.client.Constants;
|
9
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
10
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
11
|
import org.gcube.common.scope.api.ScopeProvider;
|
12
|
import org.gcube.informationsystem.resourceregistry.client.ParthenosRegistryClientSetter;
|
13
|
import org.gcube.informationsystem.resourceregistry.publisher.ParthenosRegistryPublisherSetter;
|
14
|
import org.springframework.beans.factory.annotation.Autowired;
|
15
|
import org.springframework.beans.factory.annotation.Value;
|
16
|
import org.springframework.stereotype.Component;
|
17
|
|
18
|
/**
|
19
|
* Created by Alessia Bardi on 26/09/2017.
|
20
|
*
|
21
|
* @author Alessia Bardi
|
22
|
*/
|
23
|
@Component
|
24
|
public class RegistryClientFactory {
|
25
|
|
26
|
private static final Log log = LogFactory.getLog(RegistryClientFactory.class);
|
27
|
|
28
|
@Value("${gcube.registry.baseurl}")
|
29
|
private String registryBaseURL;
|
30
|
@Value("${gcube.registry.uri.base.default}")
|
31
|
private String defaultBaseURI;
|
32
|
@Value("${gcube.registry.application.token}")
|
33
|
private String applicationToken;
|
34
|
|
35
|
@Autowired
|
36
|
private SaxonHelper saxonHelper;
|
37
|
@Autowired
|
38
|
private GCubeResourceGenerator resourceGenerator;
|
39
|
|
40
|
public RegistryClient getRegistryClient() throws ParthenosPublisherException {
|
41
|
log.debug("Creating RegistryClient for "+registryBaseURL);
|
42
|
return new RegistryClient(registryBaseURL, getSaxonHelper(), defaultBaseURI, getResourceGenerator());
|
43
|
}
|
44
|
|
45
|
public RegistryClientFactory() throws Exception {
|
46
|
log.info("Creating RegistryClientFactory");
|
47
|
ParthenosRegistryClientSetter.forceToURL(registryBaseURL);
|
48
|
ParthenosRegistryPublisherSetter.forceToURL(registryBaseURL);
|
49
|
log.info("Registry URL forced to "+registryBaseURL);
|
50
|
if(StringUtils.isNotBlank(getApplicationToken())) {
|
51
|
RegistryClientFactory.setContext(getApplicationToken());
|
52
|
log.info("GCube Context set");
|
53
|
}
|
54
|
else{
|
55
|
log.fatal("GCube context cannot be configured without a value in gcube.registry.application.token");
|
56
|
}
|
57
|
}
|
58
|
|
59
|
public static String getCurrentScope(String token) throws Exception {
|
60
|
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
61
|
String context = authorizationEntry.getContext();
|
62
|
log.info("Context of token "+token+" is: "+context);
|
63
|
return context;
|
64
|
}
|
65
|
|
66
|
public static void setContext(String token) throws Exception {
|
67
|
SecurityTokenProvider.instance.set(token);
|
68
|
ScopeProvider.instance.set(getCurrentScope(token));
|
69
|
}
|
70
|
|
71
|
public String getApplicationToken() {
|
72
|
return applicationToken;
|
73
|
}
|
74
|
|
75
|
public void setApplicationToken(final String applicationToken) {
|
76
|
this.applicationToken = applicationToken;
|
77
|
}
|
78
|
|
79
|
|
80
|
public String getRegistryBaseURL() {
|
81
|
return registryBaseURL;
|
82
|
}
|
83
|
|
84
|
public void setRegistryBaseURL(final String registryBaseURL) {
|
85
|
this.registryBaseURL = registryBaseURL;
|
86
|
}
|
87
|
|
88
|
public SaxonHelper getSaxonHelper() {
|
89
|
return saxonHelper;
|
90
|
}
|
91
|
|
92
|
public void setSaxonHelper(final SaxonHelper saxonHelper) {
|
93
|
this.saxonHelper = saxonHelper;
|
94
|
}
|
95
|
|
96
|
public String getDefaultBaseURI() {
|
97
|
return defaultBaseURI;
|
98
|
}
|
99
|
|
100
|
public void setDefaultBaseURI(final String defaultBaseURI) {
|
101
|
this.defaultBaseURI = defaultBaseURI;
|
102
|
}
|
103
|
|
104
|
public GCubeResourceGenerator getResourceGenerator() {
|
105
|
return resourceGenerator;
|
106
|
}
|
107
|
|
108
|
public void setResourceGenerator(final GCubeResourceGenerator resourceGenerator) {
|
109
|
this.resourceGenerator = resourceGenerator;
|
110
|
}
|
111
|
}
|