Skip to content

Commit 04cd43c

Browse files
committed
new version 0.0.13:
* new trust ssl strategy (issue #1) * new exception for site redirection error * begin support for piwigo version 2.8.x (issue #3)
1 parent 43aa1c3 commit 04cd43c

10 files changed

Lines changed: 106 additions & 23 deletions

File tree

remotesync-api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
<parent>
144144
<groupId>piwigo</groupId>
145145
<artifactId>remotesync</artifactId>
146-
<version>0.0.12</version>
146+
<version>0.0.13</version>
147147
<relativePath>../remotesync</relativePath>
148148
</parent>
149149
</project>

remotesync-api/src/main/java/org/piwigo/remotesync/api/IClientConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public interface IClientConfiguration {
2222
public String getProxyUsername();
2323
public String getProxyPassword();
2424

25-
public boolean getTrustSelfSignedSSLCertificate();
25+
public boolean getTrustSSLCertificates();
2626

2727
public int getChunkSize();
2828
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2014 Matthieu Helleboid.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the GNU Public License v2.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7+
*
8+
* Contributors:
9+
* Matthieu Helleboid - initial API and implementation
10+
******************************************************************************/
11+
package org.piwigo.remotesync.api.client;
12+
13+
import java.security.cert.CertificateException;
14+
import java.security.cert.X509Certificate;
15+
16+
import org.apache.http.conn.ssl.TrustStrategy;
17+
18+
public class TrustSSLCertificatesStrategy implements TrustStrategy {
19+
20+
@Override
21+
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
22+
return true;
23+
}
24+
25+
}

remotesync-api/src/main/java/org/piwigo/remotesync/api/client/WSClient.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import org.apache.commons.io.IOUtils;
2121
import org.apache.commons.lang.NotImplementedException;
22+
import org.apache.http.Header;
23+
import org.apache.http.HttpHeaders;
2224
import org.apache.http.HttpHost;
2325
import org.apache.http.HttpStatus;
2426
import org.apache.http.auth.AuthScope;
@@ -29,14 +31,14 @@
2931
import org.apache.http.client.methods.HttpPost;
3032
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
3133
import org.apache.http.conn.ssl.SSLContextBuilder;
32-
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
3334
import org.apache.http.entity.mime.MultipartEntityBuilder;
3435
import org.apache.http.impl.client.BasicCredentialsProvider;
3536
import org.apache.http.impl.client.CloseableHttpClient;
3637
import org.apache.http.impl.client.HttpClientBuilder;
3738
import org.piwigo.remotesync.api.IClient;
3839
import org.piwigo.remotesync.api.IClientConfiguration;
3940
import org.piwigo.remotesync.api.exception.ClientException;
41+
import org.piwigo.remotesync.api.exception.ClientRedirectException;
4042
import org.piwigo.remotesync.api.exception.ClientSSLException;
4143
import org.piwigo.remotesync.api.exception.ClientServerException;
4244
import org.piwigo.remotesync.api.exception.ServerException;
@@ -114,8 +116,8 @@ protected <T extends BasicResponse> String getXmlResponse(AbstractRequest<T> req
114116
try {
115117
httpResponse = getHttpResponse(request);
116118

117-
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
118-
throw new ServerException(httpResponse.getStatusLine().getReasonPhrase() + " (code " + httpResponse.getStatusLine().getStatusCode() + ")");
119+
checkMovedUrl(httpResponse);
120+
checkStatusCode(httpResponse);
119121

120122
return IOUtils.toString(httpResponse.getEntity().getContent(), "UTF-8");
121123
} catch (ClientServerException e) {
@@ -132,6 +134,28 @@ protected <T extends BasicResponse> String getXmlResponse(AbstractRequest<T> req
132134
}
133135
}
134136

137+
protected void checkStatusCode(CloseableHttpResponse httpResponse) throws ServerException {
138+
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
139+
throw new ServerException(httpResponse.getStatusLine().getReasonPhrase() + " (code " + httpResponse.getStatusLine().getStatusCode() + ")");
140+
}
141+
142+
protected void checkMovedUrl(CloseableHttpResponse httpResponse) throws ClientRedirectException {
143+
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY ||
144+
httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
145+
146+
String newLocation = "new location";
147+
148+
Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
149+
if (headers.length > 0) {
150+
newLocation = headers[0].getValue();
151+
}
152+
153+
ClientRedirectException clientRedirectException = new ClientRedirectException("Remote site moved to " + newLocation);
154+
clientRedirectException.setDestination(newLocation);
155+
throw clientRedirectException;
156+
}
157+
}
158+
135159
@SuppressWarnings("unchecked")
136160
protected <T extends BasicResponse> CloseableHttpResponse getHttpResponse(AbstractRequest<T> request) throws ClientException {
137161
try {
@@ -160,7 +184,7 @@ else if (value instanceof List) {
160184

161185
return getHttpClient().execute(method);
162186
} catch (SSLException e) {
163-
throw new ClientSSLException("SSL certificate exception (Please use option 'Trust SSL certificates')", e);
187+
throw new ClientSSLException("SSL certificate exception (Please try option 'Trust SSL certificates')", e);
164188
} catch (Exception e) {
165189
throw new ClientException("Unable to send request", e);
166190
}
@@ -187,9 +211,9 @@ protected CloseableHttpClient getHttpClient() throws Exception {
187211
requestConfig = RequestConfig.custom().setProxy(proxy).build();
188212
}
189213

190-
if (clientConfiguration.getTrustSelfSignedSSLCertificate()) {
214+
if (clientConfiguration.getTrustSSLCertificates()) {
191215
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
192-
sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
216+
sslContextBuilder.loadTrustMaterial(null, new TrustSSLCertificatesStrategy());
193217
httpClientBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContextBuilder.build()));
194218
}
195219

remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/SyncConfiguration.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public class SyncConfiguration implements ISyncConfiguration {
7171
protected String proxyPassword;
7272

7373
@Element(required = false)
74-
@Option(name = "-tsssc", usage = "trust self signed ssl certificates")
75-
protected String trustSelfSignedSSLCertificates = Boolean.FALSE.toString();
74+
@Option(name = "-tsslc", usage = "trust ssl certificates")
75+
protected String trustSSLCertificates = Boolean.FALSE.toString();
7676

7777
@Element(required = false)
7878
@Option(name = "-cs", usage = "chunk size (in Kbytes)")
@@ -177,16 +177,16 @@ public void setProxyPassword(String proxyPassword) {
177177
this.proxyPassword = proxyPassword;
178178
}
179179

180-
public boolean getTrustSelfSignedSSLCertificate() {
180+
public boolean getTrustSSLCertificates() {
181181
try {
182-
return Boolean.parseBoolean(trustSelfSignedSSLCertificates);
182+
return Boolean.parseBoolean(trustSSLCertificates);
183183
} catch (Exception e) {
184184
return false;
185185
}
186186
}
187187

188-
public void setTrustSelfSignedSSLCertificate(String string) {
189-
this.trustSelfSignedSSLCertificates = string;
188+
public void setTrustSSLCertificates(String string) {
189+
this.trustSSLCertificates = string;
190190
}
191191

192192
public int getChunkSize() {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2014 Matthieu Helleboid.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the GNU Public License v2.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7+
*
8+
* Contributors:
9+
* Matthieu Helleboid - initial API and implementation
10+
******************************************************************************/
11+
package org.piwigo.remotesync.api.exception;
12+
13+
14+
public class ClientRedirectException extends ClientException {
15+
16+
private static final long serialVersionUID = -4061416823576651051L;
17+
18+
private String destination;
19+
20+
public ClientRedirectException(String message) {
21+
super(message);
22+
}
23+
24+
public void setDestination(String destination) {
25+
this.destination = destination;
26+
}
27+
28+
public String getDestination() {
29+
return destination;
30+
}
31+
}

remotesync-api/src/main/java/org/piwigo/remotesync/api/response/PwgSessionGetStatusResponse.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class PwgSessionGetStatusResponse extends BasicResponse {
4141
@Element(required=false)
4242
public String upload_file_types;
4343

44+
@Element(required=false)
45+
public String upload_form_chunk_size;
46+
4447
public boolean isAdmin() {
4548
return Constants.UserType.admin.toString().equals(status) || Constants.UserType.webmaster.toString().equals(status);
4649
}

remotesync-ui/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<dependency>
1818
<groupId>piwigo</groupId>
1919
<artifactId>remotesync-api</artifactId>
20-
<version>0.0.12</version>
20+
<version>0.0.13</version>
2121
</dependency>
2222
<dependency>
2323
<groupId>org.apache.pivot</groupId>
@@ -73,7 +73,7 @@
7373
<parent>
7474
<groupId>piwigo</groupId>
7575
<artifactId>remotesync</artifactId>
76-
<version>0.0.12</version>
76+
<version>0.0.13</version>
7777
<relativePath>../remotesync</relativePath>
7878
</parent>
7979
</project>

remotesync-ui/src/main/java/org/piwigo/remotesync/ui/swing/OptionsUI.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class OptionsUI extends JFrame {
3636
private JTextField proxyLogintextField;
3737
private JTextField proxyPasswordtextField;
3838
private JCheckBox chckbxUseProxy;
39-
private JCheckBox chckbxTSSSC;
39+
private JCheckBox chckbxTSSLC;
4040

4141
public static void run(final SyncConfiguration syncConfiguration) {
4242
EventQueue.invokeLater(new Runnable() {
@@ -111,9 +111,9 @@ public void stateChanged(ChangeEvent e) {
111111
contentPane.add(proxyPasswordtextField);
112112
proxyPasswordtextField.setColumns(10);
113113

114-
chckbxTSSSC = new JCheckBox("Trust self signed SSL certificates");
115-
chckbxTSSSC.setBounds(8, 170, 300, 23);
116-
contentPane.add(chckbxTSSSC);
114+
chckbxTSSLC = new JCheckBox("Trust SSL certificates");
115+
chckbxTSSLC.setBounds(8, 170, 300, 23);
116+
contentPane.add(chckbxTSSLC);
117117

118118
addWindowListener(new WindowAdapter() {
119119
@Override
@@ -124,7 +124,7 @@ public void windowClosing(WindowEvent e) {
124124
syncConfiguration.setProxyPort(proxyPorttextField.getText());
125125
syncConfiguration.setProxyUsername(proxyLogintextField.getText());
126126
syncConfiguration.setProxyPassword(proxyPasswordtextField.getText());
127-
syncConfiguration.setTrustSelfSignedSSLCertificate(Boolean.toString(chckbxTSSSC.isSelected()));
127+
syncConfiguration.setTrustSSLCertificates(Boolean.toString(chckbxTSSLC.isSelected()));
128128
}
129129

130130
@Override
@@ -135,7 +135,7 @@ public void windowOpened(WindowEvent e) {
135135
proxyPorttextField.setText(syncConfiguration.getProxyPort() + "");
136136
proxyLogintextField.setText(syncConfiguration.getProxyUsername());
137137
proxyPasswordtextField.setText(syncConfiguration.getProxyPassword());
138-
chckbxTSSSC.setSelected(syncConfiguration.getTrustSelfSignedSSLCertificate());
138+
chckbxTSSLC.setSelected(syncConfiguration.getTrustSSLCertificates());
139139
}
140140

141141
});

remotesync/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<groupId>piwigo</groupId>
1515
<artifactId>remotesync</artifactId>
1616
<name>Piwigo Remote Sync</name>
17-
<version>0.0.12</version>
17+
<version>0.0.13</version>
1818
<packaging>pom</packaging>
1919
<properties>
2020
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

0 commit comments

Comments
 (0)