Skip to content

Commit

Permalink
new version 0.0.13:
Browse files Browse the repository at this point in the history
* new trust ssl strategy (issue #1)
* new exception for site redirection error
* begin support for piwigo version 2.8.x (issue #3)
  • Loading branch information
mhelleboid committed Aug 3, 2016
1 parent 43aa1c3 commit 04cd43c
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 23 deletions.
2 changes: 1 addition & 1 deletion remotesync-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
<parent>
<groupId>piwigo</groupId>
<artifactId>remotesync</artifactId>
<version>0.0.12</version>
<version>0.0.13</version>
<relativePath>../remotesync</relativePath>
</parent>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface IClientConfiguration {
public String getProxyUsername();
public String getProxyPassword();

public boolean getTrustSelfSignedSSLCertificate();
public boolean getTrustSSLCertificates();

public int getChunkSize();
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,14 +31,14 @@
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;
import org.apache.http.impl.client.HttpClientBuilder;
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;
Expand Down Expand Up @@ -114,8 +116,8 @@ protected <T extends BasicResponse> String getXmlResponse(AbstractRequest<T> 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) {
Expand All @@ -132,6 +134,28 @@ protected <T extends BasicResponse> String getXmlResponse(AbstractRequest<T> 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 <T extends BasicResponse> CloseableHttpResponse getHttpResponse(AbstractRequest<T> request) throws ClientException {
try {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions remotesync-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<dependency>
<groupId>piwigo</groupId>
<artifactId>remotesync-api</artifactId>
<version>0.0.12</version>
<version>0.0.13</version>
</dependency>
<dependency>
<groupId>org.apache.pivot</groupId>
Expand Down Expand Up @@ -73,7 +73,7 @@
<parent>
<groupId>piwigo</groupId>
<artifactId>remotesync</artifactId>
<version>0.0.12</version>
<version>0.0.13</version>
<relativePath>../remotesync</relativePath>
</parent>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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());
}

});
Expand Down
2 changes: 1 addition & 1 deletion remotesync/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<groupId>piwigo</groupId>
<artifactId>remotesync</artifactId>
<name>Piwigo Remote Sync</name>
<version>0.0.12</version>
<version>0.0.13</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down

0 comments on commit 04cd43c

Please sign in to comment.