1 package fr.ifremer.quadrige2.ui.swing.common.synchro.action;
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 import com.google.common.base.Joiner;
28 import fr.ifremer.quadrige2.core.security.AuthenticationInfo;
29 import fr.ifremer.quadrige2.synchro.vo.SynchroProgressionStatus;
30 import fr.ifremer.quadrige2.ui.swing.common.action.AbstractWorkerAction;
31 import fr.ifremer.quadrige2.ui.swing.common.synchro.SynchroUI;
32 import fr.ifremer.quadrige2.ui.swing.common.synchro.SynchroUIContext;
33 import fr.ifremer.quadrige2.ui.swing.common.synchro.SynchroUIHandler;
34 import org.apache.commons.lang3.StringUtils;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.http.auth.AuthScope;
38 import org.apache.http.auth.UsernamePasswordCredentials;
39 import org.apache.http.client.CredentialsProvider;
40 import org.apache.http.client.config.RequestConfig;
41 import org.apache.http.client.utils.URIBuilder;
42 import org.apache.http.impl.client.BasicCredentialsProvider;
43
44 import java.net.URI;
45 import java.net.URISyntaxException;
46 import java.net.URL;
47
48
49
50
51
52
53 public abstract class AbstractSynchroAction extends AbstractWorkerAction<SynchroUIContext, SynchroUI, SynchroUIHandler> {
54
55 private static final Log log = LogFactory.getLog(AbstractSynchroAction.class);
56
57 protected URI baseUri;
58 protected Integer baseTimeOut;
59
60
61
62
63
64
65 public AbstractSynchroAction(SynchroUIHandler handler) {
66 super(handler, false);
67 }
68
69
70 @Override
71 public boolean initAction() {
72
73 URL baseUrl = getConfig().getSynchronizationSiteUrl();
74 try {
75 baseUri = baseUrl.toURI();
76 } catch (URISyntaxException ex) {
77 failedAction(ex);
78 return false;
79 }
80 baseTimeOut = getConfig().getSynchronizationSiteTimeout();
81
82
83 getModel().setServerSide(false);
84
85 return true;
86 }
87
88
89 @Override
90 public void failedAction(Throwable ex) {
91
92 String message = ex.getLocalizedMessage();
93 if (StringUtils.isBlank(message)) {
94 message = ex.toString();
95 }
96 log.error(message, ex);
97 getHandler().report(message);
98 getModel().setStatus(SynchroProgressionStatus.FAILED);
99 }
100
101
102 @Override
103 public void disposeAction() {
104
105 }
106
107
108
109
110
111
112
113
114 protected URI getAppendedPath(String... path) throws URISyntaxException {
115
116 String pathToAppend = Joiner.on('/').skipNulls().join(path);
117
118 URIBuilder builder = new URIBuilder(baseUri);
119 String absolutePath = baseUri.getPath() + pathToAppend;
120
121
122 if (absolutePath.contains("//")) {
123 absolutePath = absolutePath.replaceAll("/[/]+", "/");
124 }
125
126 builder.setPath(absolutePath);
127 return builder.build();
128
129 }
130
131
132
133
134
135
136 protected CredentialsProvider getCredentialsProvider() {
137
138
139 if (!getContext().isAuthenticated() || getContext().isAuthenticatedAsLocalUser()) {
140 return null;
141 }
142
143
144 AuthenticationInfo authenticationInfo = getContext().getAuthenticationInfo();
145 if (authenticationInfo == null) {
146 throw new IllegalArgumentException("the authentication information must be not null");
147 }
148
149
150 CredentialsProvider credentialProvider = new BasicCredentialsProvider();
151 credentialProvider.setCredentials(new AuthScope(baseUri.getHost(), baseUri.getPort()),
152 new UsernamePasswordCredentials(authenticationInfo.getLogin(), authenticationInfo.getPassword()));
153
154 return credentialProvider;
155 }
156
157
158
159
160
161
162 protected RequestConfig getRequestConfig() {
163
164
165 return RequestConfig.custom().setSocketTimeout(baseTimeOut).setConnectTimeout(baseTimeOut).build();
166 }
167
168 }