View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common.synchro.action;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * Abstract Synchronization Action
50   *
51   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
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       * <p>Constructor for AbstractSynchroAction.</p>
62       *
63       * @param handler a {@link SynchroUIHandler} object.
64       */
65      public AbstractSynchroAction(SynchroUIHandler handler) {
66          super(handler, false);
67      }
68  
69      /** {@inheritDoc} */
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          // by default by set by
83          getModel().setServerSide(false);
84  
85          return true;
86      }
87  
88      /** {@inheritDoc} */
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     /** {@inheritDoc} */
102     @Override
103     public void disposeAction() {
104         // if something to dispose after work
105     }
106 
107     /**
108      * <p>getAppendedPath.</p>
109      *
110      * @param path a {@link String} object.
111      * @return a {@link URI} object.
112      * @throws URISyntaxException if any.
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         // Remove redundant slash
122         if (absolutePath.contains("//")) {
123             absolutePath = absolutePath.replaceAll("/[/]+", "/");
124         }
125 
126         builder.setPath(absolutePath);
127         return builder.build();
128 
129     }
130 
131     /**
132      * <p>getCredentialsProvider.</p>
133      *
134      * @return a {@link CredentialsProvider} object.
135      */
136     protected CredentialsProvider getCredentialsProvider() {
137 
138         // No auth if user not authenticated, or if local user
139         if (!getContext().isAuthenticated() || getContext().isAuthenticatedAsLocalUser()) {
140             return null;
141         }
142 
143         // get context credential
144         AuthenticationInfo authenticationInfo = getContext().getAuthenticationInfo();
145         if (authenticationInfo == null) {
146             throw new IllegalArgumentException("the authentication information must be not null");
147         }
148 
149         // build credentials information
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      * <p>getRequestConfig.</p>
159      *
160      * @return a {@link RequestConfig} object.
161      */
162     protected RequestConfig getRequestConfig() {
163 
164         // build request config for timeout 
165         return RequestConfig.custom().setSocketTimeout(baseTimeOut).setConnectTimeout(baseTimeOut).build();
166     }
167 
168 }