From 04cd43c16c74eb6b2b33fac0d58a24d46e8e1df8 Mon Sep 17 00:00:00 2001 From: Matthieu Helleboid Date: Wed, 3 Aug 2016 23:05:56 +0200 Subject: [PATCH] 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) --- remotesync-api/pom.xml | 2 +- .../remotesync/api/IClientConfiguration.java | 2 +- .../client/TrustSSLCertificatesStrategy.java | 25 +++++++++++++ .../remotesync/api/client/WSClient.java | 36 +++++++++++++++---- .../api/conf/SyncConfiguration.java | 12 +++---- .../exception/ClientRedirectException.java | 31 ++++++++++++++++ .../response/PwgSessionGetStatusResponse.java | 3 ++ remotesync-ui/pom.xml | 4 +-- .../piwigo/remotesync/ui/swing/OptionsUI.java | 12 +++---- remotesync/pom.xml | 2 +- 10 files changed, 106 insertions(+), 23 deletions(-) create mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/client/TrustSSLCertificatesStrategy.java create mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/exception/ClientRedirectException.java diff --git a/remotesync-api/pom.xml b/remotesync-api/pom.xml index 87304c3..4f2ccd1 100644 --- a/remotesync-api/pom.xml +++ b/remotesync-api/pom.xml @@ -143,7 +143,7 @@ piwigo remotesync - 0.0.12 + 0.0.13 ../remotesync diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/IClientConfiguration.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/IClientConfiguration.java index 0f6f0be..10c131f 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/IClientConfiguration.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/IClientConfiguration.java @@ -22,7 +22,7 @@ public interface IClientConfiguration { public String getProxyUsername(); public String getProxyPassword(); - public boolean getTrustSelfSignedSSLCertificate(); + public boolean getTrustSSLCertificates(); public int getChunkSize(); } diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/TrustSSLCertificatesStrategy.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/TrustSSLCertificatesStrategy.java new file mode 100644 index 0000000..53fbf57 --- /dev/null +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/TrustSSLCertificatesStrategy.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2014 Matthieu Helleboid. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v2.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * + * Contributors: + * Matthieu Helleboid - initial API and implementation + ******************************************************************************/ +package org.piwigo.remotesync.api.client; + +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + +import org.apache.http.conn.ssl.TrustStrategy; + +public class TrustSSLCertificatesStrategy implements TrustStrategy { + + @Override + public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { + return true; + } + +} diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/WSClient.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/WSClient.java index 2f49f4f..c4cb698 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/WSClient.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/WSClient.java @@ -19,6 +19,8 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang.NotImplementedException; +import org.apache.http.Header; +import org.apache.http.HttpHeaders; import org.apache.http.HttpHost; import org.apache.http.HttpStatus; import org.apache.http.auth.AuthScope; @@ -29,7 +31,6 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLContextBuilder; -import org.apache.http.conn.ssl.TrustSelfSignedStrategy; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; @@ -37,6 +38,7 @@ import org.piwigo.remotesync.api.IClient; import org.piwigo.remotesync.api.IClientConfiguration; import org.piwigo.remotesync.api.exception.ClientException; +import org.piwigo.remotesync.api.exception.ClientRedirectException; import org.piwigo.remotesync.api.exception.ClientSSLException; import org.piwigo.remotesync.api.exception.ClientServerException; import org.piwigo.remotesync.api.exception.ServerException; @@ -114,8 +116,8 @@ protected String getXmlResponse(AbstractRequest req try { httpResponse = getHttpResponse(request); - if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) - throw new ServerException(httpResponse.getStatusLine().getReasonPhrase() + " (code " + httpResponse.getStatusLine().getStatusCode() + ")"); + checkMovedUrl(httpResponse); + checkStatusCode(httpResponse); return IOUtils.toString(httpResponse.getEntity().getContent(), "UTF-8"); } catch (ClientServerException e) { @@ -132,6 +134,28 @@ protected String getXmlResponse(AbstractRequest req } } + protected void checkStatusCode(CloseableHttpResponse httpResponse) throws ServerException { + if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) + throw new ServerException(httpResponse.getStatusLine().getReasonPhrase() + " (code " + httpResponse.getStatusLine().getStatusCode() + ")"); + } + + protected void checkMovedUrl(CloseableHttpResponse httpResponse) throws ClientRedirectException { + if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || + httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) { + + String newLocation = "new location"; + + Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION); + if (headers.length > 0) { + newLocation = headers[0].getValue(); + } + + ClientRedirectException clientRedirectException = new ClientRedirectException("Remote site moved to " + newLocation); + clientRedirectException.setDestination(newLocation); + throw clientRedirectException; + } + } + @SuppressWarnings("unchecked") protected CloseableHttpResponse getHttpResponse(AbstractRequest request) throws ClientException { try { @@ -160,7 +184,7 @@ else if (value instanceof List) { return getHttpClient().execute(method); } catch (SSLException e) { - throw new ClientSSLException("SSL certificate exception (Please use option 'Trust SSL certificates')", e); + throw new ClientSSLException("SSL certificate exception (Please try option 'Trust SSL certificates')", e); } catch (Exception e) { throw new ClientException("Unable to send request", e); } @@ -187,9 +211,9 @@ protected CloseableHttpClient getHttpClient() throws Exception { requestConfig = RequestConfig.custom().setProxy(proxy).build(); } - if (clientConfiguration.getTrustSelfSignedSSLCertificate()) { + if (clientConfiguration.getTrustSSLCertificates()) { SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); - sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); + sslContextBuilder.loadTrustMaterial(null, new TrustSSLCertificatesStrategy()); httpClientBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContextBuilder.build())); } diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/SyncConfiguration.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/SyncConfiguration.java index 900870d..fa3b4ac 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/SyncConfiguration.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/SyncConfiguration.java @@ -71,8 +71,8 @@ public class SyncConfiguration implements ISyncConfiguration { protected String proxyPassword; @Element(required = false) - @Option(name = "-tsssc", usage = "trust self signed ssl certificates") - protected String trustSelfSignedSSLCertificates = Boolean.FALSE.toString(); + @Option(name = "-tsslc", usage = "trust ssl certificates") + protected String trustSSLCertificates = Boolean.FALSE.toString(); @Element(required = false) @Option(name = "-cs", usage = "chunk size (in Kbytes)") @@ -177,16 +177,16 @@ public void setProxyPassword(String proxyPassword) { this.proxyPassword = proxyPassword; } - public boolean getTrustSelfSignedSSLCertificate() { + public boolean getTrustSSLCertificates() { try { - return Boolean.parseBoolean(trustSelfSignedSSLCertificates); + return Boolean.parseBoolean(trustSSLCertificates); } catch (Exception e) { return false; } } - public void setTrustSelfSignedSSLCertificate(String string) { - this.trustSelfSignedSSLCertificates = string; + public void setTrustSSLCertificates(String string) { + this.trustSSLCertificates = string; } public int getChunkSize() { diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/exception/ClientRedirectException.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/exception/ClientRedirectException.java new file mode 100644 index 0000000..626647b --- /dev/null +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/exception/ClientRedirectException.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2014 Matthieu Helleboid. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v2.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * + * Contributors: + * Matthieu Helleboid - initial API and implementation + ******************************************************************************/ +package org.piwigo.remotesync.api.exception; + + +public class ClientRedirectException extends ClientException { + + private static final long serialVersionUID = -4061416823576651051L; + + private String destination; + + public ClientRedirectException(String message) { + super(message); + } + + public void setDestination(String destination) { + this.destination = destination; + } + + public String getDestination() { + return destination; + } +} \ No newline at end of file diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/response/PwgSessionGetStatusResponse.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/response/PwgSessionGetStatusResponse.java index fff8622..1f78aa1 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/response/PwgSessionGetStatusResponse.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/response/PwgSessionGetStatusResponse.java @@ -41,6 +41,9 @@ public class PwgSessionGetStatusResponse extends BasicResponse { @Element(required=false) public String upload_file_types; + @Element(required=false) + public String upload_form_chunk_size; + public boolean isAdmin() { return Constants.UserType.admin.toString().equals(status) || Constants.UserType.webmaster.toString().equals(status); } diff --git a/remotesync-ui/pom.xml b/remotesync-ui/pom.xml index 44d3ec8..95fc728 100644 --- a/remotesync-ui/pom.xml +++ b/remotesync-ui/pom.xml @@ -17,7 +17,7 @@ piwigo remotesync-api - 0.0.12 + 0.0.13 org.apache.pivot @@ -73,7 +73,7 @@ piwigo remotesync - 0.0.12 + 0.0.13 ../remotesync diff --git a/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/swing/OptionsUI.java b/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/swing/OptionsUI.java index 30981d9..97e6735 100644 --- a/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/swing/OptionsUI.java +++ b/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/swing/OptionsUI.java @@ -36,7 +36,7 @@ public class OptionsUI extends JFrame { private JTextField proxyLogintextField; private JTextField proxyPasswordtextField; private JCheckBox chckbxUseProxy; - private JCheckBox chckbxTSSSC; + private JCheckBox chckbxTSSLC; public static void run(final SyncConfiguration syncConfiguration) { EventQueue.invokeLater(new Runnable() { @@ -111,9 +111,9 @@ public void stateChanged(ChangeEvent e) { contentPane.add(proxyPasswordtextField); proxyPasswordtextField.setColumns(10); - chckbxTSSSC = new JCheckBox("Trust self signed SSL certificates"); - chckbxTSSSC.setBounds(8, 170, 300, 23); - contentPane.add(chckbxTSSSC); + chckbxTSSLC = new JCheckBox("Trust SSL certificates"); + chckbxTSSLC.setBounds(8, 170, 300, 23); + contentPane.add(chckbxTSSLC); addWindowListener(new WindowAdapter() { @Override @@ -124,7 +124,7 @@ public void windowClosing(WindowEvent e) { syncConfiguration.setProxyPort(proxyPorttextField.getText()); syncConfiguration.setProxyUsername(proxyLogintextField.getText()); syncConfiguration.setProxyPassword(proxyPasswordtextField.getText()); - syncConfiguration.setTrustSelfSignedSSLCertificate(Boolean.toString(chckbxTSSSC.isSelected())); + syncConfiguration.setTrustSSLCertificates(Boolean.toString(chckbxTSSLC.isSelected())); } @Override @@ -135,7 +135,7 @@ public void windowOpened(WindowEvent e) { proxyPorttextField.setText(syncConfiguration.getProxyPort() + ""); proxyLogintextField.setText(syncConfiguration.getProxyUsername()); proxyPasswordtextField.setText(syncConfiguration.getProxyPassword()); - chckbxTSSSC.setSelected(syncConfiguration.getTrustSelfSignedSSLCertificate()); + chckbxTSSLC.setSelected(syncConfiguration.getTrustSSLCertificates()); } }); diff --git a/remotesync/pom.xml b/remotesync/pom.xml index 4b39ee8..f38ff41 100644 --- a/remotesync/pom.xml +++ b/remotesync/pom.xml @@ -14,7 +14,7 @@ piwigo remotesync Piwigo Remote Sync - 0.0.12 + 0.0.13 pom UTF-8