Revision 9515 org.gvsig.online/trunk/org.gvsig.online/org.gvsig.online.lib/org.gvsig.online.lib.impl/src/main/java/org/gvsig/online/lib/impl/OnlineManagerImpl.java
OnlineManagerImpl.java | ||
---|---|---|
25 | 25 |
|
26 | 26 |
import java.io.File; |
27 | 27 |
import java.io.IOException; |
28 |
import java.io.InputStream; |
|
29 |
import java.net.URI; |
|
28 | 30 |
import java.net.URL; |
29 | 31 |
import java.nio.file.Files; |
30 | 32 |
import java.nio.file.Path; |
... | ... | |
35 | 37 |
import java.util.List; |
36 | 38 |
import java.util.Map; |
37 | 39 |
import java.util.Objects; |
40 |
import javax.json.JsonObject; |
|
38 | 41 |
import org.apache.commons.lang3.StringUtils; |
42 |
import org.apache.commons.lang3.mutable.Mutable; |
|
43 |
import org.apache.commons.lang3.mutable.MutableInt; |
|
44 |
import org.apache.commons.lang3.mutable.MutableObject; |
|
45 |
import org.apache.http.HttpResponse; |
|
46 |
import org.apache.http.NameValuePair; |
|
47 |
import org.apache.http.client.ClientProtocolException; |
|
48 |
import org.apache.http.client.ResponseHandler; |
|
49 |
import org.apache.http.client.entity.UrlEncodedFormEntity; |
|
50 |
import org.apache.http.client.methods.HttpPost; |
|
51 |
import org.apache.http.impl.client.CloseableHttpClient; |
|
52 |
import org.apache.http.impl.client.HttpClientBuilder; |
|
53 |
import org.apache.http.message.BasicNameValuePair; |
|
39 | 54 |
import org.gvsig.fmap.dal.DALLocator; |
40 | 55 |
import org.gvsig.fmap.dal.DataManager; |
41 | 56 |
import org.gvsig.fmap.dal.DataServerExplorer; |
... | ... | |
53 | 68 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
54 | 69 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
55 | 70 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
71 |
import org.gvsig.json.Json; |
|
56 | 72 |
import org.gvsig.online.lib.api.OnlineCodeGenerator; |
57 | 73 |
import org.gvsig.online.lib.api.OnlineManager; |
58 | 74 |
import org.gvsig.online.lib.api.OnlineProject; |
59 | 75 |
import org.gvsig.online.lib.api.OnlineSite; |
60 | 76 |
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester; |
77 |
import static org.gvsig.online.lib.api.OnlineUserIdentificationRequester.CONFIG_FACTORYNAME_NAME; |
|
78 |
import static org.gvsig.online.lib.api.OnlineUserIdentificationRequester.CONFIG_URLONLINE_NAME; |
|
61 | 79 |
import org.gvsig.online.lib.api.workingcopy.OnlineWorkingcopy; |
62 | 80 |
import org.gvsig.online.lib.api.workingcopy.OnlineWorkingcopyDescriptor; |
63 | 81 |
import org.gvsig.online.lib.api.workingcopy.WorkingArea; |
82 |
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester.OnlineUserIdentificationRequesterConfig; |
|
83 |
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester.OnlineUserIdentificationRequesterFactory; |
|
64 | 84 |
import org.gvsig.online.lib.impl.workspace.OnlineWorkingcopyDescriptorImpl; |
65 | 85 |
import org.gvsig.online.lib.impl.workspace.OnlineWorkspaceImpl; |
66 | 86 |
import org.gvsig.online.lib.impl.workspace.StoreProperties; |
... | ... | |
77 | 97 |
import org.gvsig.tools.util.ListBuilder; |
78 | 98 |
import org.slf4j.Logger; |
79 | 99 |
import org.slf4j.LoggerFactory; |
100 |
import org.gvsig.tools.util.Factory; |
|
80 | 101 |
|
81 | 102 |
@SuppressWarnings("UseSpecificCatch") |
82 | 103 |
public class OnlineManagerImpl implements OnlineManager { |
... | ... | |
93 | 114 |
DatabaseWorkspaceManager.TABLE_REPOSITORY_NAME |
94 | 115 |
); |
95 | 116 |
|
96 |
private OnlineUserIdentificationRequester userIdentificationRequester;
|
|
117 |
private Map<String,OnlineUserIdentificationRequesterFactory> userIdentificationRequesters;
|
|
97 | 118 |
private Map<String,OnlineWorkingcopyDescriptor> workspaces; |
98 | 119 |
|
99 | 120 |
public OnlineManagerImpl() { |
100 | 121 |
this.workspaces = new HashMap<>(); |
101 |
this.userIdentificationRequester = null;
|
|
122 |
this.userIdentificationRequesters = new HashMap<>();
|
|
102 | 123 |
} |
103 | 124 |
|
104 | 125 |
@Override |
... | ... | |
107 | 128 |
} |
108 | 129 |
|
109 | 130 |
@Override |
110 |
public void setUserIdentificationRequester(OnlineUserIdentificationRequester userIdentificationRequester) {
|
|
111 |
this.userIdentificationRequester = userIdentificationRequester;
|
|
131 |
public void addUserIdentificationRequester(OnlineUserIdentificationRequesterFactory factory) {
|
|
132 |
this.userIdentificationRequesters.put(factory.getName(), factory);
|
|
112 | 133 |
} |
113 | 134 |
|
114 | 135 |
@Override |
115 |
public OnlineUserIdentificationRequester getUserIdentificationRequester() {
|
|
116 |
return this.userIdentificationRequester; |
|
136 |
public OnlineUserIdentificationRequesterFactory getUserIdentificationRequester(String mode) {
|
|
137 |
return this.userIdentificationRequesters.get(mode);
|
|
117 | 138 |
} |
139 |
|
|
140 |
@Override |
|
141 |
public OnlineUserIdentificationRequesterConfig getUserIdentificationRequester(JsonObject json) { |
|
142 |
String factoryName = json.getString(CONFIG_FACTORYNAME_NAME, null); |
|
143 |
String urlOnline = json.getString(CONFIG_URLONLINE_NAME, null); |
|
144 |
OnlineUserIdentificationRequesterFactory factory = this.getUserIdentificationRequester(factoryName); |
|
145 |
OnlineUserIdentificationRequesterConfig config = factory.create(urlOnline); |
|
146 |
config.fromJson(json); |
|
147 |
return config; |
|
148 |
} |
|
149 |
|
|
150 |
@Override |
|
151 |
public Iterable<OnlineUserIdentificationRequesterFactory> getUserIdentificationRequesters() { |
|
152 |
return this.userIdentificationRequesters.values(); |
|
153 |
} |
|
118 | 154 |
|
119 | 155 |
@Override |
120 | 156 |
public OnlineWorkingcopyDescriptor getWorkingcopyDescriptor(String code) { |
... | ... | |
141 | 177 |
DisposeUtils.disposeQuietly(workspace); |
142 | 178 |
} |
143 | 179 |
this.workspaces = new HashMap<>(); |
144 |
this.userIdentificationRequester = null; |
|
180 |
this.userIdentificationRequesters = null;
|
|
145 | 181 |
} |
146 | 182 |
|
147 | 183 |
public OnlineWorkingcopy openWorkingcopy(JDBCServerExplorer wsexplorer, String label) { |
... | ... | |
155 | 191 |
params = wsexplorer.getParameters(); |
156 | 192 |
|
157 | 193 |
workspace = new OnlineWorkspaceImpl(wsexplorer, this.getCodeGenerator(), label); |
158 |
workspace.setUserIdentificationRequester(this.userIdentificationRequester); |
|
159 | 194 |
this.registerWorkingcopy(workspace); |
160 | 195 |
dropExpiredCaches(); |
161 | 196 |
return workspace; |
... | ... | |
486 | 521 |
// |
487 | 522 |
status.setTitle("vcsgis retrieving entities"); |
488 | 523 |
workspace = new OnlineWorkspaceImpl(wsexplorer, this.getCodeGenerator(), project, label); |
489 |
if (workspace.getUserIdentificationRequester() == null) { |
|
490 |
workspace.setUserIdentificationRequester(this.userIdentificationRequester); |
|
491 |
} |
|
492 | 524 |
workspace.initialize(); |
493 | 525 |
LOGGER.info("Workspace initialized "+workspace.getCode()); |
494 | 526 |
|
... | ... | |
557 | 589 |
} |
558 | 590 |
if (value.isWorkspaceInitialized() && StringUtils.equals(url, value.getExplorerParameters().getUrl())) { |
559 | 591 |
workspace = value.getWorkspace();//FIXME: lo siguiente solo si es null |
560 |
//workspace.getCode(); value.getCode(); |
|
561 |
if (workspace.getUserIdentificationRequester() == null) { |
|
562 |
workspace.setUserIdentificationRequester(this.userIdentificationRequester); |
|
563 |
} |
|
564 | 592 |
dropExpiredCaches(); |
565 | 593 |
// LOGGER.debug("===: OPEN WORKSPACE "+ hexId(workspace)+ " from registry (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"')"); |
566 | 594 |
return workspace; |
... | ... | |
568 | 596 |
} |
569 | 597 |
|
570 | 598 |
workspace = new OnlineWorkspaceImpl(wsexplorer, this.getCodeGenerator(), null); |
571 |
workspace.setUserIdentificationRequester(this.userIdentificationRequester); |
|
572 | 599 |
this.registerWorkingcopy(workspace); |
573 | 600 |
dropExpiredCaches(); |
574 | 601 |
return workspace; |
... | ... | |
601 | 628 |
} |
602 | 629 |
|
603 | 630 |
@Override |
604 |
public OnlineSite connectSite(URL siteurl) throws IOException { |
|
631 |
public OnlineSite connectSite(OnlineUserIdentificationRequesterConfig userIdentificationRequesterConfig, URL siteurl) throws IOException {
|
|
605 | 632 |
OnlineSiteImpl site = new OnlineSiteImpl(); |
606 | 633 |
if( siteurl!=null) { |
607 |
site.connect(siteurl); |
|
634 |
site.connect(userIdentificationRequesterConfig, siteurl);
|
|
608 | 635 |
} |
609 | 636 |
return site; |
610 | 637 |
} |
611 | 638 |
|
639 |
@SuppressWarnings("Convert2Lambda") |
|
612 | 640 |
@Override |
641 |
public String authenticate(String urlbase, String userId, String password) { |
|
642 |
try { |
|
643 |
MutableInt statusCode = new MutableInt(); |
|
644 |
Mutable<String> token = new MutableObject<>(); |
|
645 |
|
|
646 |
if( !StringUtils.endsWith("/", urlbase) ) { |
|
647 |
urlbase += "/"; |
|
648 |
} |
|
649 |
URI uri = new URI(urlbase+"auth/api-token-auth/"); |
|
650 |
int numretries = 3; |
|
651 |
for (int retries = 0; retries < numretries; retries++) { |
|
652 |
HttpPost request = new HttpPost(uri); |
|
653 |
List<NameValuePair> params = new ArrayList<>(); |
|
654 |
params.add(new BasicNameValuePair("username", userId)); |
|
655 |
params.add(new BasicNameValuePair("password", password)); |
|
656 |
UrlEncodedFormEntity formdata = new UrlEncodedFormEntity(params); |
|
657 |
request.setEntity(formdata); |
|
658 |
request.setHeader("Content-type","application/x-www-form-urlencoded"); |
|
659 |
CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
|
660 |
ResponseHandler responseHandler = new ResponseHandler() { |
|
661 |
@Override |
|
662 |
public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException { |
|
663 |
statusCode.setValue(response.getStatusLine().getStatusCode()); |
|
664 |
InputStream content = response.getEntity().getContent(); |
|
665 |
JsonObject resp = Json.createObject(content); |
|
666 |
token.setValue(resp.getString("token", null)); |
|
667 |
return null; |
|
668 |
} |
|
669 |
}; |
|
670 |
httpClient.execute(request,responseHandler); |
|
671 |
if( statusCode.getValue()==200 ) { |
|
672 |
break; |
|
673 |
} |
|
674 |
} |
|
675 |
if( statusCode.getValue()!=200 ) { |
|
676 |
LOGGER.warn("Toomany retries for authenticate (HTTP status "+statusCode.toString()+")"); |
|
677 |
return null; |
|
678 |
} |
|
679 |
String authorization = "JWT " + token.getValue(); |
|
680 |
return authorization; |
|
681 |
|
|
682 |
} catch (Exception ex) { |
|
683 |
LOGGER.warn("", ex); |
|
684 |
throw new RuntimeException("Can't get authorization from '"+urlbase+"' for user '"+userId+"'.", ex); |
|
685 |
} |
|
686 |
} |
|
687 |
@Override |
|
613 | 688 |
public WorkingArea createWorkingArea() { |
614 | 689 |
return new WorkingAreaImpl(); |
615 | 690 |
} |
Also available in: Unified diff