From 7dab5e0e0a27d1634054a445a5265d48c384017c Mon Sep 17 00:00:00 2001 From: Matthieu Helleboid Date: Tue, 3 Nov 2015 06:40:44 +0100 Subject: [PATCH] new version 0.0.7: * add network connection proxy capability * change user configuration persistence * change cache management * minor refactoring --- ...PiwigoRemoteSync API Junit AllTests.launch | 45 +++-- .../PiwigoRemoteSync API Main.launch | 29 ++- remotesync-api/pom.xml | 2 +- .../piwigo/remotesync/api/AbstractAPI.java | 3 +- .../java/org/piwigo/remotesync/api/API.java | 15 +- .../piwigo/remotesync/api/AbstractMain.java | 24 +-- .../api/{client/Client.java => IClient.java} | 8 +- .../remotesync/api/IClientConfiguration.java | 26 +++ .../remotesync/api/ISyncConfiguration.java | 15 ++ .../java/org/piwigo/remotesync/api/Tasks.java | 2 - .../api/cache/AbstractCacheElement.java | 32 ++++ .../api/cache/AlbumCacheElement.java | 31 +++ .../api/cache/ILegacyCacheElement.java | 17 ++ .../api/cache/ImageCacheElement.java | 39 ++++ .../remotesync/api/cache/LegacyCache.java | 139 ++++++++++++++ .../remotesync/api/client/AbstractClient.java | 3 +- .../api/client/AuthenticatedWSClient.java | 9 +- .../remotesync/api/client/DryRunClient.java | 5 +- .../remotesync/api/client/WSClient.java | 49 ++++- .../piwigo/remotesync/api/conf/Config.java | 45 ----- ...ConfigUtil.java => ConfigurationUtil.java} | 29 +-- ...leryConfig.java => SyncConfiguration.java} | 61 +++--- ...r.java => SyncConfigurationValidator.java} | 45 ++--- .../api/conf/UserConfiguration.java | 45 +++++ .../request/PwgImagesAddAllChunksRequest.java | 6 +- .../PwgImagesAddFileWithChunkRequest.java | 2 +- .../request/PwgImagesAddWithChunkRequest.java | 2 +- .../remotesync/api/sync/ConnectedWalker.java | 94 +++++++++ .../remotesync/api/sync/DummyRunWalker.java | 35 ++++ .../piwigo/remotesync/api/sync/SyncCache.java | 180 ------------------ .../api/sync/SyncDirectoryWalker.java | 87 +++++++++ .../piwigo/remotesync/api/sync/SyncJob.java | 180 +----------------- .../piwigo/remotesync/api/util/FileUtil.java | 37 +++- .../remotesync/api/util/StringUtil.java | 24 ++- .../remotesync/generator/WSJavaAPIWriter.java | 24 ++- .../remotesync/generator/apiTemplate.jmte | 2 +- .../piwigo/remotesync/api/test/APITest.java | 31 ++- .../remotesync/api/test/AbstractTestCase.java | 47 ++--- .../remotesync/api/test/FileUtilTest.java | 33 ++-- .../remotesync/api/test/ImagesTest.java | 10 +- .../remotesync/api/test/RequestsTest.java | 4 +- .../piwigo/remotesync/api/test/SyncTest.java | 49 +++-- .../api/test/{picture.jpg => image.jpg} | Bin .../remotesync/api/test/testConfig.example | 18 +- .../PiwigoRemoteSync UI MainUi Swing.launch | 29 ++- ...wigoRemoteSync UI MainUi TestClient.launch | 29 ++- .../PiwigoRemoteSync UI MainUi.launch | 29 ++- remotesync-ui/pom.xml | 4 +- .../remotesync/ui/pivot/RemotesyncUI.java | 14 +- ...igForm.java => SyncConfigurationForm.java} | 55 +++--- .../ui/pivot/reflection/RequestForm.java | 8 +- .../piwigo/remotesync/ui/swing/ProxyUI.java | 30 +-- .../remotesync/ui/swing/RemotesyncUI.java | 26 +-- ...orm.bxml => galleryConfigurationForm.bxml} | 6 +- remotesync/README.txt | 6 +- remotesync/pom.xml | 2 +- 56 files changed, 1046 insertions(+), 775 deletions(-) rename remotesync-api/src/main/java/org/piwigo/remotesync/api/{client/Client.java => IClient.java} (83%) create mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/IClientConfiguration.java create mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/ISyncConfiguration.java create mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/AbstractCacheElement.java create mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/AlbumCacheElement.java create mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/ILegacyCacheElement.java create mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/ImageCacheElement.java create mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/LegacyCache.java delete mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/Config.java rename remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/{ConfigUtil.java => ConfigurationUtil.java} (73%) rename remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/{GalleryConfig.java => SyncConfiguration.java} (88%) rename remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/{GalleryConfigValidator.java => SyncConfigurationValidator.java} (66%) create mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/UserConfiguration.java create mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/ConnectedWalker.java create mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/DummyRunWalker.java delete mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/SyncCache.java create mode 100644 remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/SyncDirectoryWalker.java rename remotesync-api/src/test/resources/org/piwigo/remotesync/api/test/{picture.jpg => image.jpg} (100%) rename remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/{GalleryConfigForm.java => SyncConfigurationForm.java} (81%) rename remotesync-ui/src/main/resources/org/piwigo/remotesync/ui/pivot/{galleryConfigForm.bxml => galleryConfigurationForm.bxml} (95%) diff --git a/remotesync-api/.settings/PiwigoRemoteSync API Junit AllTests.launch b/remotesync-api/.settings/PiwigoRemoteSync API Junit AllTests.launch index 8d36892..28e9184 100644 --- a/remotesync-api/.settings/PiwigoRemoteSync API Junit AllTests.launch +++ b/remotesync-api/.settings/PiwigoRemoteSync API Junit AllTests.launch @@ -1,23 +1,22 @@ - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + diff --git a/remotesync-api/.settings/PiwigoRemoteSync API Main.launch b/remotesync-api/.settings/PiwigoRemoteSync API Main.launch index e99a1b5..4b17b29 100644 --- a/remotesync-api/.settings/PiwigoRemoteSync API Main.launch +++ b/remotesync-api/.settings/PiwigoRemoteSync API Main.launch @@ -1,15 +1,14 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + diff --git a/remotesync-api/pom.xml b/remotesync-api/pom.xml index fad5735..96f5229 100644 --- a/remotesync-api/pom.xml +++ b/remotesync-api/pom.xml @@ -136,7 +136,7 @@ piwigo remotesync - 0.0.6 + 0.0.7 ../remotesync diff --git a/remotesync-api/src/gen/java/org/piwigo/remotesync/api/AbstractAPI.java b/remotesync-api/src/gen/java/org/piwigo/remotesync/api/AbstractAPI.java index 87757a9..35d3089 100644 --- a/remotesync-api/src/gen/java/org/piwigo/remotesync/api/AbstractAPI.java +++ b/remotesync-api/src/gen/java/org/piwigo/remotesync/api/AbstractAPI.java @@ -10,7 +10,6 @@ ******************************************************************************/ package org.piwigo.remotesync.api; -import org.piwigo.remotesync.api.client.Client; import org.piwigo.remotesync.api.exception.ClientServerException; import org.piwigo.remotesync.api.request.*; import org.piwigo.remotesync.api.response.*; @@ -18,7 +17,7 @@ @org.piwigo.remotesync.generator.Generated public abstract class AbstractAPI { - protected abstract Client getClient(); + protected abstract IClient getClient(); public PwgCaddieAddResponse caddieAdd(PwgCaddieAddRequest request) throws ClientServerException { return getClient().sendRequest(request); diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/API.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/API.java index 3a39328..7fb9a77 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/API.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/API.java @@ -12,7 +12,6 @@ import org.piwigo.remotesync.api.AbstractAPI; import org.piwigo.remotesync.api.client.AuthenticatedWSClient; -import org.piwigo.remotesync.api.client.Client; import org.piwigo.remotesync.api.exception.ClientServerException; import org.piwigo.remotesync.api.request.PwgImagesAddAllChunksRequest; import org.piwigo.remotesync.api.request.PwgImagesAddFileWithChunkRequest; @@ -24,18 +23,18 @@ public class API extends AbstractAPI { - protected Client client; + protected IClient client; - public API(Client client) { + public API(IClient client) { this.client = client; } - public API(String url) { - this.client = new AuthenticatedWSClient(url); + public API(IClientConfiguration clientConfiguration) { + this.client = new AuthenticatedWSClient(clientConfiguration); } - public void login(String username, String password) throws ClientServerException { - client.login(username, password); + public void login() throws ClientServerException { + client.login(); } public void logout() throws ClientServerException { @@ -43,7 +42,7 @@ public void logout() throws ClientServerException { } @Override - protected Client getClient() { + protected IClient getClient() { return client; } diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/AbstractMain.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/AbstractMain.java index d2922ba..e378cb2 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/AbstractMain.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/AbstractMain.java @@ -15,9 +15,9 @@ import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.Option; import org.kohsuke.args4j.OptionHandlerFilter; -import org.piwigo.remotesync.api.conf.Config; -import org.piwigo.remotesync.api.conf.ConfigUtil; -import org.piwigo.remotesync.api.conf.GalleryConfig; +import org.piwigo.remotesync.api.conf.UserConfiguration; +import org.piwigo.remotesync.api.conf.ConfigurationUtil; +import org.piwigo.remotesync.api.conf.SyncConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,15 +27,15 @@ public abstract class AbstractMain { private static final Logger logger = LoggerFactory.getLogger(AbstractMain.class); protected void run(String[] args) { - GalleryConfig parsedGalleryConfig = new GalleryConfig(); - CmdLineParser cmdLineParser = createParser(parsedGalleryConfig); + SyncConfiguration parsedSyncConfiguration = new SyncConfiguration(); + CmdLineParser cmdLineParser = createParser(parsedSyncConfiguration); try { cmdLineParser.parseArgument(args); configureLog(debug); - createConfig(parsedGalleryConfig); + createConfiguration(parsedSyncConfiguration); if (help) { System.out.println("Piwigo Remote Sync : java -jar remotesync.jar"); @@ -61,19 +61,19 @@ protected void run(String[] args) { @Option(name = "-help", usage = "help") protected boolean help = false; - protected CmdLineParser createParser(GalleryConfig galleryConfig) { + protected CmdLineParser createParser(ISyncConfiguration syncConfiguration) { CmdLineParser cmdLineParser = new CmdLineParser(null); new ClassParser().parse(this, cmdLineParser); - new ClassParser().parse(galleryConfig, cmdLineParser); + new ClassParser().parse(syncConfiguration, cmdLineParser); return cmdLineParser; } - protected void createConfig(GalleryConfig parsedGalleryConfig) { - Config config = ConfigUtil.INSTANCE.getUserConfig(); + protected void createConfiguration(SyncConfiguration parsedSyncConfiguration) { + UserConfiguration userConfiguration = ConfigurationUtil.INSTANCE.getUserConfiguration(); //if some gallery configuration was provided through parameters - if (!parsedGalleryConfig.isEmpty()) - config.setCurrentGalleryConfig(parsedGalleryConfig); + if (!parsedSyncConfiguration.isEmpty()) + userConfiguration.setCurrentSyncConfiguration(parsedSyncConfiguration); } protected void configureLog(boolean debug) { diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/Client.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/IClient.java similarity index 83% rename from remotesync-api/src/main/java/org/piwigo/remotesync/api/client/Client.java rename to remotesync-api/src/main/java/org/piwigo/remotesync/api/IClient.java index c0cdfaa..e322905 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/Client.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/IClient.java @@ -8,7 +8,7 @@ * Contributors: * Matthieu Helleboid - initial API and implementation ******************************************************************************/ -package org.piwigo.remotesync.api.client; +package org.piwigo.remotesync.api; import org.piwigo.remotesync.api.exception.ClientServerException; import org.piwigo.remotesync.api.request.AbstractRequest; @@ -16,13 +16,13 @@ import org.piwigo.remotesync.api.response.BasicResponse; import org.piwigo.remotesync.api.response.ComposedResponse; -public interface Client { +public interface IClient { public T sendRequest(AbstractRequest request) throws ClientServerException; public ComposedResponse sendRequest(ComposedRequest composedRequest) throws ClientServerException; - public Client login(String username, String password) throws ClientServerException; + public IClient login() throws ClientServerException; - public Client logout() throws ClientServerException; + public IClient logout() throws ClientServerException; } 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 new file mode 100644 index 0000000..16241e6 --- /dev/null +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/IClientConfiguration.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * 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; + +public interface IClientConfiguration { + public String getUrl(); + + public String getUsername(); + public String getPassword(); + + public boolean getUsesProxy(); + public String getProxyUrl(); + public int getProxyPort(); + public String getProxyUsername(); + public String getProxyPassword(); + + public int getChunkSize(); +} diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/ISyncConfiguration.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/ISyncConfiguration.java new file mode 100644 index 0000000..97ac696 --- /dev/null +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/ISyncConfiguration.java @@ -0,0 +1,15 @@ +/******************************************************************************* + * 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; + +public interface ISyncConfiguration extends IClientConfiguration { + public String getDirectory(); +} diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/Tasks.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/Tasks.java index 0d67243..3e89dd1 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/Tasks.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/Tasks.java @@ -26,7 +26,5 @@ public class Tasks { //TODO implement request text : connecting to / uploading file //TODO implement pause/retry/save of composedrequest - //TODO provide API to create client from galleryconfig - //TODO test upload with community plugin } diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/AbstractCacheElement.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/AbstractCacheElement.java new file mode 100644 index 0000000..ed5e599 --- /dev/null +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/AbstractCacheElement.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * 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.cache; + +public abstract class AbstractCacheElement implements ILegacyCacheElement { + protected String url; + protected Integer id; + + public AbstractCacheElement() { + } + + public AbstractCacheElement(String url, Integer id) { + this.url = url; + this.id = id; + } + + public Integer getId() { + return id; + } + + public String getUrl() { + return url; + } +} diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/AlbumCacheElement.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/AlbumCacheElement.java new file mode 100644 index 0000000..f06ab91 --- /dev/null +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/AlbumCacheElement.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.cache; + +public class AlbumCacheElement extends AbstractCacheElement { + + public AlbumCacheElement() { + super(); + } + + public AlbumCacheElement(String url, Integer id) { + super(url, id); + } + + @Override + public String writeToString() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(url); + stringBuilder.append(" album_id = "); + stringBuilder.append(id); + return stringBuilder.toString(); + } +} diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/ILegacyCacheElement.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/ILegacyCacheElement.java new file mode 100644 index 0000000..61bfa9d --- /dev/null +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/ILegacyCacheElement.java @@ -0,0 +1,17 @@ +/******************************************************************************* + * 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.cache; + +public interface ILegacyCacheElement { + + public String writeToString(); + +} \ No newline at end of file diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/ImageCacheElement.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/ImageCacheElement.java new file mode 100644 index 0000000..9e0022e --- /dev/null +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/ImageCacheElement.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * 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.cache; +public class ImageCacheElement extends AbstractCacheElement { + + protected String filePathMD5; + + public ImageCacheElement() { + } + + public ImageCacheElement(String url, Integer id, String filePathMD5) { + super(url, id); + this.filePathMD5 = filePathMD5; + } + + public String getFilePathMD5() { + return filePathMD5; + } + + @Override + public String writeToString() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(url); + stringBuilder.append(" "); + stringBuilder.append(filePathMD5); + stringBuilder.append(" [id="); + stringBuilder.append(id); + stringBuilder.append("]"); + return stringBuilder.toString(); + } +} diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/LegacyCache.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/LegacyCache.java new file mode 100644 index 0000000..df47a9b --- /dev/null +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/LegacyCache.java @@ -0,0 +1,139 @@ +/******************************************************************************* + * 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.cache; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.io.FileUtils; +import org.piwigo.remotesync.api.util.FileUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LegacyCache { + + public static final String LEGACY_CACHE_FILE_NAME = ".piwigo_import_tree.txt"; + + private static final Pattern ALBUM_PATTERN = Pattern.compile("(.*) album_id = (\\d*)"); + + private static final Pattern IMAGE_PATTERN = Pattern.compile("(.*) (.*) \\[id=(\\d*)\\]"); + + static final Logger logger = LoggerFactory.getLogger(LegacyCache.class); + + protected File cacheFile; + protected String url; + protected AlbumCacheElement albumCacheElement; + protected List imagesCache = new ArrayList(); + + public LegacyCache(String url, File cacheFile) { + this.url = url; + this.cacheFile = cacheFile; + } + + public AlbumCacheElement getAlbumCacheElement() { + return albumCacheElement; + } + + public List getImagesCache() { + return imagesCache; + } + + public AlbumCacheElement addAlbum(Integer id) { + if (id == null) + return null; + + AlbumCacheElement addAlbum = doAddAlbum(id); + writeToFile(addAlbum); + return addAlbum; + } + + public ImageCacheElement addImage(File file, Integer id) { + if (id == null) + return null; + + ImageCacheElement addImage = doAddImage(file, id); + writeToFile(addImage); + return addImage; + } + + protected AlbumCacheElement doAddAlbum(Integer id) { + albumCacheElement = new AlbumCacheElement(url, id); + return albumCacheElement; + } + + protected ImageCacheElement doAddImage(File file, Integer id) { + ImageCacheElement imageCacheElement = new ImageCacheElement(url, id, FileUtil.getFileNameMD5Sum(file)); + imagesCache.add(imageCacheElement); + return imageCacheElement; + } + + public boolean containsImage(File file) { + String md5 = FileUtil.getFileNameMD5Sum(file); + for (ImageCacheElement imageCacheElement : imagesCache) { + if (imageCacheElement.filePathMD5.equals(md5)) + return true; + } + return false; + } + + public LegacyCache parseFile() { + if (!cacheFile.exists()) { + logger.debug(cacheFile + " doesn't exist"); + return this; + } + + try { + String content = FileUtils.readFileToString(cacheFile); + parseContent(content); + } catch (Exception e) { + logger.error("Cannot read file " + cacheFile, e); + } + + return this; + } + + public LegacyCache parseContent(String content) { + Matcher matcher = ALBUM_PATTERN.matcher(content); + if (matcher.find()) { + albumCacheElement = new AlbumCacheElement(); + albumCacheElement.url = matcher.group(1); + albumCacheElement.id = Integer.parseInt(matcher.group(2)); + } + + matcher = IMAGE_PATTERN.matcher(content); + while (matcher.find()) { + ImageCacheElement imageCacheElement = new ImageCacheElement(); + imageCacheElement.url = matcher.group(1); + imageCacheElement.filePathMD5 = matcher.group(2); + imageCacheElement.id = Integer.parseInt(matcher.group(3)); + imagesCache.add(imageCacheElement); + } + + return this; + } + + protected void writeToFile(ILegacyCacheElement abstractCacheElement) { + try { + FileUtils.writeStringToFile(cacheFile, abstractCacheElement.writeToString() + "\n", true); + } catch (IOException e) { + logger.error("Cannot write " + LEGACY_CACHE_FILE_NAME + " in directory " + cacheFile.getParent(), e); + } + } + + public static File getLegacyCacheFile(File directory) { + return new File(directory, LEGACY_CACHE_FILE_NAME); + } + +} diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/AbstractClient.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/AbstractClient.java index 932f826..349a848 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/AbstractClient.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/AbstractClient.java @@ -12,13 +12,14 @@ import java.util.Iterator; +import org.piwigo.remotesync.api.IClient; import org.piwigo.remotesync.api.exception.ClientServerException; import org.piwigo.remotesync.api.request.AbstractRequest; import org.piwigo.remotesync.api.request.ComposedRequest; import org.piwigo.remotesync.api.response.BasicResponse; import org.piwigo.remotesync.api.response.ComposedResponse; -public abstract class AbstractClient implements Client { +public abstract class AbstractClient implements IClient { @Override public final T sendRequest(AbstractRequest request) throws ClientServerException { diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/AuthenticatedWSClient.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/AuthenticatedWSClient.java index 7a91e66..06427c9 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/AuthenticatedWSClient.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/AuthenticatedWSClient.java @@ -10,6 +10,7 @@ ******************************************************************************/ package org.piwigo.remotesync.api.client; +import org.piwigo.remotesync.api.IClientConfiguration; import org.piwigo.remotesync.api.exception.ClientException; import org.piwigo.remotesync.api.exception.ClientServerException; import org.piwigo.remotesync.api.request.AbstractRequest; @@ -24,8 +25,8 @@ public class AuthenticatedWSClient extends WSClient { private PwgSessionGetStatusResponse sessionGetStatusResponse; - public AuthenticatedWSClient(String url) { - super(url); + public AuthenticatedWSClient(IClientConfiguration clientConfiguration) { + super(clientConfiguration); } @Override @@ -45,8 +46,8 @@ private void getSessionStatus() throws ClientServerException { } @Override - public AuthenticatedWSClient login(String username, String password) throws ClientServerException { - doSendRequest(new PwgSessionLoginRequest(username, password)); + public AuthenticatedWSClient login() throws ClientServerException { + doSendRequest(new PwgSessionLoginRequest(clientConfiguration.getUsername(), clientConfiguration.getPassword())); return this; } diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/DryRunClient.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/DryRunClient.java index 6789a68..5a4f5cc 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/DryRunClient.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/client/DryRunClient.java @@ -10,6 +10,7 @@ ******************************************************************************/ package org.piwigo.remotesync.api.client; +import org.piwigo.remotesync.api.IClient; import org.piwigo.remotesync.api.exception.ClientServerException; import org.piwigo.remotesync.api.request.AbstractRequest; import org.piwigo.remotesync.api.response.BasicResponse; @@ -35,13 +36,13 @@ public T doSendRequest(AbstractRequest request) thr } @Override - public Client login(String username, String password) { + public IClient login() { logger.info("DryRun login"); return this; } @Override - public Client logout() { + public IClient logout() { logger.info("DryRun logout"); return this; } 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 89f9936..41b0381 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 @@ -17,13 +17,21 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang.NotImplementedException; +import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; 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.HttpClients; +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.ClientServerException; import org.piwigo.remotesync.api.exception.ServerException; @@ -47,11 +55,12 @@ public class WSClient extends AbstractClient { private static Logger logger = LoggerFactory.getLogger(WSClient.class); - private String url; - private CloseableHttpClient httpClient; + protected IClientConfiguration clientConfiguration; + protected CloseableHttpClient httpClient; + protected RequestConfig requestConfig; - public WSClient(String url) { - this.url = url; + public WSClient(IClientConfiguration clientConfiguration) { + this.clientConfiguration = clientConfiguration; } @SuppressWarnings("unchecked") @@ -59,12 +68,32 @@ public WSClient(String url) { protected T doSendRequest(AbstractRequest request) throws ClientServerException { checkRequestAuthorization(request); - if (httpClient == null) - httpClient = HttpClients.createDefault(); + if (httpClient == null) { + HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); + + if (clientConfiguration.getUsesProxy()) { + String proxyUrl = clientConfiguration.getProxyUrl(); + int proxyPort = clientConfiguration.getProxyPort(); + + String proxyUsername = clientConfiguration.getProxyUsername(); + String proxyPassword = clientConfiguration.getProxyPassword(); + + if (proxyUsername != null && proxyUsername.length() > 0 && proxyPassword != null && proxyPassword.length() > 0) { + CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(new AuthScope(proxyUrl, proxyPort), new UsernamePasswordCredentials(proxyUsername, proxyPassword)); + httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); + } + + HttpHost proxy = new HttpHost(proxyUrl, proxyPort); + requestConfig = RequestConfig.custom().setProxy(proxy).build(); + } + httpClient = httpClientBuilder.build(); + } CloseableHttpResponse httpResponse = null; try { - HttpPost method = new HttpPost(url + "/ws.php"); + HttpPost method = new HttpPost(clientConfiguration.getUrl() + "/ws.php"); + method.setConfig(requestConfig); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); multipartEntityBuilder.addTextBody("method", request.getWSMethodName()); @@ -143,12 +172,12 @@ protected void checkRequestAuthorization(AbstractReque } @Override - public Client login(String username, String password) throws ClientServerException { + public IClient login() throws ClientServerException { throw new IllegalStateException("Cannot login, use " + AuthenticatedWSClient.class.getName()); } @Override - public Client logout() throws ClientServerException { + public IClient logout() throws ClientServerException { throw new IllegalStateException("Cannot logout, use " + AuthenticatedWSClient.class.getName()); } } diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/Config.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/Config.java deleted file mode 100644 index 723a7ae..0000000 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/Config.java +++ /dev/null @@ -1,45 +0,0 @@ -/******************************************************************************* - * 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.conf; - -import java.util.ArrayList; -import java.util.List; - -import org.simpleframework.xml.Attribute; -import org.simpleframework.xml.ElementList; - -public class Config { - - @ElementList - private List galleryConfigs = new ArrayList(); - - @Attribute(required = false) - private Integer currentGalleryConfigId; - - public GalleryConfig getCurrentGalleryConfig() { - if (currentGalleryConfigId == null) { - currentGalleryConfigId = 0; - galleryConfigs.add(new GalleryConfig()); - } - return galleryConfigs.get(currentGalleryConfigId); - } - - public void setCurrentGalleryConfig(GalleryConfig currentGalleryConfig) { - if (!galleryConfigs.contains(currentGalleryConfig)) - galleryConfigs.add(currentGalleryConfig); - currentGalleryConfigId = galleryConfigs.indexOf(currentGalleryConfig); - } - - public List getGalleryConfigs() { - return galleryConfigs; - } - -} diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/ConfigUtil.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/ConfigurationUtil.java similarity index 73% rename from remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/ConfigUtil.java rename to remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/ConfigurationUtil.java index 4218a3d..1bb922c 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/ConfigUtil.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/ConfigurationUtil.java @@ -12,18 +12,25 @@ import java.io.File; +import org.piwigo.remotesync.api.IClientConfiguration; import org.piwigo.remotesync.api.xml.PersisterFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ConfigUtil { +public class ConfigurationUtil { - private static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class); + private static final Logger logger = LoggerFactory.getLogger(ConfigurationUtil.class); - public static final ConfigUtil INSTANCE = new ConfigUtil(); + public static final ConfigurationUtil INSTANCE = new ConfigurationUtil(); - protected Config userConfig; + protected UserConfiguration userConfiguration; + public IClientConfiguration createConfiguration(String url) { + SyncConfiguration configuration = new SyncConfiguration(); + configuration.setUrl(url); + return configuration; + } + public File getUserCurrentDirectory() { return new File(System.getProperty("user.dir")); } @@ -36,10 +43,10 @@ public File getUserConfigFile() { return new File(getUserHomeDirectory(), ".config/piwigo/remotesync"); } - public Config getUserConfig() { - if (userConfig == null) + public UserConfiguration getUserConfiguration() { + if (userConfiguration == null) loadUserConfig(); - return userConfig; + return userConfiguration; } public void loadUserConfig() { @@ -47,14 +54,14 @@ public void loadUserConfig() { } public void saveUserConfig() { - saveConfig(getUserConfigFile(), userConfig); + saveConfig(getUserConfigFile(), userConfiguration); } public void loadConfig(File configFile) { if (configFile.exists()) { logger.debug("found userConfig file " + configFile.getAbsolutePath()); try { - userConfig = PersisterFactory.createPersister().read(Config.class, configFile); + userConfiguration = PersisterFactory.createPersister().read(UserConfiguration.class, configFile); logger.debug("configuration loaded"); return; } catch (Exception e) { @@ -63,10 +70,10 @@ public void loadConfig(File configFile) { } logger.debug("no userConfig file found"); - userConfig = new Config(); + userConfiguration = new UserConfiguration(); } - public void saveConfig(File configFile, Config config) { + public void saveConfig(File configFile, UserConfiguration config) { File configDirectory = configFile.getParentFile(); if (!configDirectory.exists()) diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/GalleryConfig.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/SyncConfiguration.java similarity index 88% rename from remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/GalleryConfig.java rename to remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/SyncConfiguration.java index 4b9c8c1..03a0ddb 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/GalleryConfig.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/SyncConfiguration.java @@ -13,15 +13,16 @@ import java.lang.reflect.Field; import org.kohsuke.args4j.Option; -import org.piwigo.remotesync.api.conf.GalleryConfigValidator.Validator; -import org.piwigo.remotesync.api.conf.GalleryConfigValidator.ValidatorRequired; -import org.piwigo.remotesync.api.conf.GalleryConfigValidator.ValidatorType; +import org.piwigo.remotesync.api.ISyncConfiguration; +import org.piwigo.remotesync.api.conf.SyncConfigurationValidator.Validator; +import org.piwigo.remotesync.api.conf.SyncConfigurationValidator.ValidatorRequired; +import org.piwigo.remotesync.api.conf.SyncConfigurationValidator.ValidatorType; import org.piwigo.remotesync.api.xml.FakePasswordConverter; import org.simpleframework.xml.Element; import org.simpleframework.xml.convert.Convert; -public class GalleryConfig { - private static final String DIRECTORY_DEFAULT = ConfigUtil.INSTANCE.getUserCurrentDirectory().getAbsolutePath(); +public class SyncConfiguration implements ISyncConfiguration { + private static final String DIRECTORY_DEFAULT = ConfigurationUtil.INSTANCE.getUserCurrentDirectory().getAbsolutePath(); private static final String CHUNK_SIZE_DEFAULT = "500"; @@ -67,6 +68,7 @@ public class GalleryConfig { protected String proxyUsername; @Element(required = false) + @Convert(FakePasswordConverter.class) @Option(name = "-ppwd", usage = "proxy password") @Validator(type = ValidatorType.string) protected String proxyPassword; @@ -78,7 +80,7 @@ public class GalleryConfig { public String getValue(String fieldName) { try { - Field field = GalleryConfig.class.getDeclaredField(fieldName); + Field field = SyncConfiguration.class.getDeclaredField(fieldName); return (String) field.get(this); } catch (Exception e) { throw new IllegalStateException(fieldName + " not found", e); @@ -87,7 +89,7 @@ public String getValue(String fieldName) { public void setValue(String fieldName, String value) { try { - Field field = GalleryConfig.class.getDeclaredField(fieldName); + Field field = SyncConfiguration.class.getDeclaredField(fieldName); field.set(this, value); } catch (Exception e) { throw new IllegalStateException(fieldName + " not found", e); @@ -126,8 +128,12 @@ public void setDirectory(String directory) { this.directory = directory; } - public String getUsesProxy() { - return usesProxy; + public boolean getUsesProxy() { + try { + return Boolean.parseBoolean(usesProxy); + } catch (NumberFormatException e) { + return false; + } } public void setUsesProxy(String string) { @@ -142,8 +148,12 @@ public void setProxyUrl(String proxyUrl) { this.proxyUrl = proxyUrl; } - public String getProxyPort() { - return proxyPort; + public int getProxyPort() { + try { + return Integer.parseInt(proxyPort); + } catch (NumberFormatException e) { + return -1; + } } public void setProxyPort(String proxyPort) { @@ -166,24 +176,7 @@ public void setProxyPassword(String proxyPassword) { this.proxyPassword = proxyPassword; } - public String getChunkSize() { - return chunkSize; - } - - public void setChunkSize(String chunkSize) { - this.chunkSize = chunkSize; - } - - public boolean getUsesProxyBoolean() { - try { - return Boolean.parseBoolean(usesProxy); - } catch (NumberFormatException e) { - return false; - } - } - - - public Integer getChunkSizeInteger() { + public int getChunkSize() { try { return Integer.parseInt(chunkSize); } catch (NumberFormatException e) { @@ -191,8 +184,12 @@ public Integer getChunkSizeInteger() { } } + public void setChunkSize(String chunkSize) { + this.chunkSize = chunkSize; + } + public boolean isEmpty() { - return this.equals(new GalleryConfig()); + return this.equals(new SyncConfiguration()); } @Override @@ -220,7 +217,7 @@ public boolean equals(Object obj) { return false; if (getClass() != obj.getClass()) return false; - GalleryConfig other = (GalleryConfig) obj; + SyncConfiguration other = (SyncConfiguration) obj; if (chunkSize == null) { if (other.chunkSize != null) return false; @@ -276,7 +273,7 @@ public boolean equals(Object obj) { @Override public String toString() { - return "GalleryConfig [url=" + url + + return "SyncConfiguration [url=" + url + ", username=" + username + ", password=" + password + ", directory=" + directory + diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/GalleryConfigValidator.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/SyncConfigurationValidator.java similarity index 66% rename from remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/GalleryConfigValidator.java rename to remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/SyncConfigurationValidator.java index 3a298f1..5958c57 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/GalleryConfigValidator.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/SyncConfigurationValidator.java @@ -19,10 +19,11 @@ import java.util.regex.Pattern; import org.apache.commons.lang.NotImplementedException; +import org.piwigo.remotesync.api.ISyncConfiguration; -public class GalleryConfigValidator { +public class SyncConfigurationValidator { - public static final GalleryConfigValidator INSTANCE = new GalleryConfigValidator(); + public static final SyncConfigurationValidator INSTANCE = new SyncConfigurationValidator(); public static enum ValidatorType { url, string, integer, dir @@ -32,13 +33,13 @@ public static enum ValidatorRequired { yes, proxy, no; } - public static class GalleryValidationException extends Exception { + public static class SyncValidationException extends Exception { private static final long serialVersionUID = 2828722578008089789L; public String fieldName; - public GalleryValidationException(String fieldName, String message) { + public SyncValidationException(String fieldName, String message) { super(message); this.fieldName = fieldName; } @@ -53,29 +54,29 @@ public GalleryValidationException(String fieldName, String message) { } public static void main(String[] args) throws Exception { - INSTANCE.validate(new GalleryConfig()); + INSTANCE.validate(new SyncConfiguration()); } - public void validate(GalleryConfig galleryConfig) throws IllegalArgumentException, IllegalAccessException, GalleryValidationException { - for (Field field : GalleryConfig.class.getDeclaredFields()) { - String value = (String) field.get(galleryConfig); - Boolean usesProxy = galleryConfig.getUsesProxyBoolean(); + public void validate(ISyncConfiguration syncConfiguration) throws IllegalArgumentException, IllegalAccessException, SyncValidationException { + for (Field field : SyncConfiguration.class.getDeclaredFields()) { + String value = (String) field.get(syncConfiguration); + Boolean usesProxy = syncConfiguration.getUsesProxy(); validate(field, value, usesProxy); } } - public void validate(String fieldName, String value, Boolean usesProxy) throws GalleryValidationException { - for (Field field : GalleryConfig.class.getDeclaredFields()) { + public void validate(String fieldName, String value, Boolean usesProxy) throws SyncValidationException { + for (Field field : SyncConfiguration.class.getDeclaredFields()) { if (field.getName().equals(fieldName)) { validate(field, value, usesProxy); return; } } - throw new GalleryValidationException(fieldName, "doesn't exist"); + throw new SyncValidationException(fieldName, "doesn't exist"); } - protected void validate(Field field, String value, Boolean usesProxy) throws GalleryValidationException { + protected void validate(Field field, String value, Boolean usesProxy) throws SyncValidationException { Validator validator = field.getAnnotation(Validator.class); if (validator != null) { String fieldName = field.getName(); @@ -85,15 +86,15 @@ protected void validate(Field field, String value, Boolean usesProxy) throws Gal } } - protected void validateRequired(Validator validator, String fieldName, String value, Boolean proxy) throws GalleryValidationException { + protected void validateRequired(Validator validator, String fieldName, String value, Boolean proxy) throws SyncValidationException { switch (validator.required()) { case yes: if (value == null || value.length() == 0) - throw new GalleryValidationException(fieldName, "is required"); + throw new SyncValidationException(fieldName, "is required"); break; case proxy: if (proxy != null && proxy && (value == null || value.length() == 0)) - throw new GalleryValidationException(fieldName, "is required"); + throw new SyncValidationException(fieldName, "is required"); break; case no: break; @@ -102,7 +103,7 @@ protected void validateRequired(Validator validator, String fieldName, String va } } - protected void validateType(Validator validator, String fieldName, String value) throws GalleryValidationException { + protected void validateType(Validator validator, String fieldName, String value) throws SyncValidationException { if (value == null || value.length() == 0) return; @@ -110,25 +111,25 @@ protected void validateType(Validator validator, String fieldName, String value) case url: Pattern pattern = Pattern.compile("https?://.+"); if (!pattern.matcher(value).matches()) - throw new GalleryValidationException(fieldName, "is not an url"); + throw new SyncValidationException(fieldName, "is not an url"); break; case dir: File file; try { file = new File(value); if (!file.exists()) - throw new GalleryValidationException(fieldName, "doesn't exist"); + throw new SyncValidationException(fieldName, "doesn't exist"); if (!file.isDirectory()) - throw new GalleryValidationException(fieldName, "is not a directory"); + throw new SyncValidationException(fieldName, "is not a directory"); } catch (Exception e) { - throw new GalleryValidationException(fieldName, "is invalid"); + throw new SyncValidationException(fieldName, "is invalid"); } break; case integer: try { Integer.parseInt(value); } catch (NumberFormatException e) { - throw new GalleryValidationException(fieldName, "is not an integer"); + throw new SyncValidationException(fieldName, "is not an integer"); } break; case string: diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/UserConfiguration.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/UserConfiguration.java new file mode 100644 index 0000000..cb3c0c6 --- /dev/null +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/conf/UserConfiguration.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * 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.conf; + +import java.util.ArrayList; +import java.util.List; + +import org.simpleframework.xml.Attribute; +import org.simpleframework.xml.ElementList; + +public class UserConfiguration { + + @ElementList + private List syncConfigurations = new ArrayList(); + + @Attribute(required = false) + private Integer currentSyncConfigurationId; + + public SyncConfiguration getCurrentSyncConfiguration() { + if (currentSyncConfigurationId == null) { + currentSyncConfigurationId = 0; + syncConfigurations.add(new SyncConfiguration()); + } + return syncConfigurations.get(currentSyncConfigurationId); + } + + public void setCurrentSyncConfiguration(SyncConfiguration currentSyncConfiguration) { + if (!syncConfigurations.contains(currentSyncConfiguration)) + syncConfigurations.add(currentSyncConfiguration); + currentSyncConfigurationId = syncConfigurations.indexOf(currentSyncConfiguration); + } + + public List getSyncConfigurations() { + return syncConfigurations; + } + +} diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/request/PwgImagesAddAllChunksRequest.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/request/PwgImagesAddAllChunksRequest.java index 1f16990..e2a06e9 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/request/PwgImagesAddAllChunksRequest.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/request/PwgImagesAddAllChunksRequest.java @@ -13,7 +13,7 @@ import java.io.File; import java.util.Iterator; -import org.piwigo.remotesync.api.conf.ConfigUtil; +import org.piwigo.remotesync.api.conf.ConfigurationUtil; import org.piwigo.remotesync.api.exception.ClientException; import org.piwigo.remotesync.api.request.PwgImagesAddChunkRequest; import org.piwigo.remotesync.api.response.BasicResponse; @@ -23,7 +23,7 @@ public class PwgImagesAddAllChunksRequest extends ComposedRequest private File file; //TODO store it - private final int chunkSize = 1024 * ConfigUtil.INSTANCE.getUserConfig().getCurrentGalleryConfig().getChunkSizeInteger(); + private final int chunkSize = 1024 * ConfigurationUtil.INSTANCE.getUserConfiguration().getCurrentSyncConfiguration().getChunkSize(); public PwgImagesAddAllChunksRequest(File file) throws ClientException { this.file = file; @@ -32,7 +32,7 @@ public PwgImagesAddAllChunksRequest(File file) throws ClientException { @Override public Iterator> iterator() { final int chunkNumber = FileUtil.getChunkNumber(file, chunkSize); - final String md5Sum = FileUtil.getMD5Sum(file); + final String md5Sum = FileUtil.getFileContentMD5Sum(file); return new ComposedRequestIterator(requests) { diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/request/PwgImagesAddFileWithChunkRequest.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/request/PwgImagesAddFileWithChunkRequest.java index caaf09f..33c19e4 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/request/PwgImagesAddFileWithChunkRequest.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/request/PwgImagesAddFileWithChunkRequest.java @@ -23,7 +23,7 @@ public class PwgImagesAddFileWithChunkRequest extends ComposedRequest results) throws IOException { + super.handleStart(startDirectory, results); + //TODO only connect when needed + connect(); + } + + @Override + protected void handleEnd(Collection results) throws IOException { + super.handleEnd(results); + disconnect(); + } + + @Override + protected Integer createAlbum(File directory, Integer parentAlbumId) { + try { + return client.sendRequest(new PwgCategoriesAddRequest(directory.getName()).setParent(parentAlbumId)).id; + } catch (ClientServerException e) { + logger.error("Cannot create album for " + directory, e); + return null; + } + } + + @Override + protected Integer createImage(File file, Integer albumId) { + try { + PwgImagesAddSimpleRequest request = new PwgImagesAddSimpleRequest(file); + // FIXME should we upload an image without album? + if (albumId != null) + request.setCategory(albumId); + return client.sendRequest(request).image_id; + } catch (ClientServerException e) { + logger.error("Cannot updload image for " + file, e); + return null; + } + } + +} diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/DummyRunWalker.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/DummyRunWalker.java new file mode 100644 index 0000000..bfa84c5 --- /dev/null +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/DummyRunWalker.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * 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.sync; + +import java.io.File; + +import org.piwigo.remotesync.api.conf.SyncConfiguration; + +public class DummyRunWalker extends SyncDirectoryWalker { + + private int album_id; + private int image_id; + + public DummyRunWalker(SyncConfiguration syncConfiguration) { + super(syncConfiguration); + } + + @Override + protected Integer createAlbum(File directory, Integer parentAlbumId) { + return ++album_id; + } + + @Override + protected Integer createImage(File file, Integer albumId) { + return ++image_id; + } +} \ No newline at end of file diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/SyncCache.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/SyncCache.java deleted file mode 100644 index 48c2566..0000000 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/SyncCache.java +++ /dev/null @@ -1,180 +0,0 @@ -/******************************************************************************* - * 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.sync; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.apache.commons.io.FileUtils; -import org.piwigo.remotesync.api.util.FileUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class SyncCache { - - static final Logger logger = LoggerFactory.getLogger(SyncCache.class); - - public static final String LEGACY_CACHE_FILE_NAME = ".piwigo_import_tree.txt"; - - private static final Pattern ALBUM_PATTERN = Pattern.compile("(.*) album_id = (\\d*)"); - - private static final Pattern PICTURE_PATTERN = Pattern.compile("(.*) (.*) \\[id=(\\d*)\\]"); - - public static abstract class CacheElement { - public String url; - public Integer id; - } - - public static class AlbumCache extends CacheElement { - - @Override - public String toString() { - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(url); - stringBuilder.append(" album_id = "); - stringBuilder.append(id); - return stringBuilder.toString(); - } - } - - public static class PictureCache extends CacheElement { - public String md5; - - @Override - public String toString() { - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(url); - stringBuilder.append(" "); - stringBuilder.append(md5); - stringBuilder.append(" [id="); - stringBuilder.append(id); - stringBuilder.append("]"); - return stringBuilder.toString(); - } - } - - public static class MemoryCache { - public String url; - public AlbumCache albumCache; - public List picturesCache = new ArrayList(); - - public MemoryCache(String url) { - this.url = url; - } - - public MemoryCache parseContent(String content) { - Matcher matcher = ALBUM_PATTERN.matcher(content); - if (matcher.find()) { - albumCache = new AlbumCache(); - albumCache.url = matcher.group(1); - albumCache.id = Integer.parseInt(matcher.group(2)); - } - - matcher = PICTURE_PATTERN.matcher(content); - while (matcher.find()) { - PictureCache pictureCache = new PictureCache(); - pictureCache.url = matcher.group(1); - pictureCache.md5 = matcher.group(2); - pictureCache.id = Integer.parseInt(matcher.group(3)); - picturesCache.add(pictureCache); - } - - return this; - } - - protected AlbumCache addAlbum(Integer id) { - AlbumCache albumCache = new AlbumCache(); - albumCache.id = id; - albumCache.url = url; - this.albumCache = albumCache; - return albumCache; - } - - protected PictureCache addPicture(File file, Integer id) { - PictureCache pictureCache = new PictureCache(); - pictureCache.id = id; - pictureCache.url = url; - pictureCache.md5 = FileUtil.getMD5Sum(file); - picturesCache.add(pictureCache); - return pictureCache; - } - - public boolean containsPicture(File file) { - String md5 = FileUtil.getMD5Sum(file); - for (PictureCache pictureCache : picturesCache) { - if (pictureCache.md5.equals(md5)) - return true; - } - return false; - } - } - - public static class FileCache extends MemoryCache { - private File cacheFile; - - public FileCache(String url, File cacheFile) { - super(url); - this.cacheFile = cacheFile; - } - - public FileCache parseFile() { - if (!cacheFile.exists()) { - logger.debug(cacheFile + " doesn't exist"); - return this; - } - - try { - String content = FileUtils.readFileToString(cacheFile); - parseContent(content); - } catch (Exception e) { - logger.error("Cannot read file " + cacheFile, e); - } - - return this; - } - - @Override - protected AlbumCache addAlbum(Integer id) { - if (id == null) - return null; - - AlbumCache addAlbum = super.addAlbum(id); - writeToFile(addAlbum); - return addAlbum; - } - - @Override - protected PictureCache addPicture(File file, Integer id) { - if (id == null) - return null; - - PictureCache addPicture = super.addPicture(file, id); - writeToFile(addPicture); - return addPicture; - } - - protected void writeToFile(CacheElement cacheElement) { - try { - FileUtils.writeStringToFile(cacheFile, cacheElement.toString() + "\n", true); - } catch (IOException e) { - logger.error("Cannot write " + LEGACY_CACHE_FILE_NAME + " in directory " + cacheFile.getParent(), e); - } - } - } - - public static File getLegacyCacheFile(File directory) { - return new File(directory, LEGACY_CACHE_FILE_NAME); - } -} diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/SyncDirectoryWalker.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/SyncDirectoryWalker.java new file mode 100644 index 0000000..1cdbc5d --- /dev/null +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/SyncDirectoryWalker.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * 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.sync; + +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.DirectoryWalker; +import org.piwigo.remotesync.api.Constants; +import org.piwigo.remotesync.api.ISyncConfiguration; +import org.piwigo.remotesync.api.cache.LegacyCache; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class SyncDirectoryWalker extends DirectoryWalker { + + private static final Logger logger = LoggerFactory.getLogger(SyncDirectoryWalker.class); + + protected ISyncConfiguration syncConfiguration; + protected File startDirectory; + protected Map legacyCaches = new HashMap(); + + protected SyncDirectoryWalker(ISyncConfiguration syncConfiguration) { + super(null, Constants.IMAGE_EXTENSIONS_FILTER, -1); + this.syncConfiguration = syncConfiguration; + startDirectory = new File(syncConfiguration.getDirectory()); + } + + @Override + protected void handleDirectoryStart(File directory, int depth, Collection results) throws IOException { + LegacyCache legacyCache = new LegacyCache(syncConfiguration.getUrl(), LegacyCache.getLegacyCacheFile(directory)).parseFile(); + legacyCaches.put(directory, legacyCache); + + // do not create an album for root directory + if (directory.equals(startDirectory)) + return; + + if (legacyCache.getAlbumCacheElement() != null) { + logger.debug("album already in cache : " + directory); + } else { + Integer parentAlbumId = null; + try { + parentAlbumId = legacyCaches.get(directory.getParentFile()).getAlbumCacheElement().getId(); + } catch (Exception e) { + // FIXME : ignore me + } + logger.info("will create album for " + directory); + legacyCache.addAlbum(createAlbum(directory, parentAlbumId)); + } + } + + protected void handleFile(File file, int depth, java.util.Collection results) throws IOException { + LegacyCache legacyCache = legacyCaches.get(file.getParentFile()); + + if (legacyCache.containsImage(file)) { + logger.debug("image already in cache : " + file); + } else { + Integer albumId = null; + try { + albumId = legacyCache.getAlbumCacheElement().getId(); + } catch (Exception e) { + // FIXME : ignore me + } + logger.info("will upload image for " + file + " in album with id " + albumId); + legacyCache.addImage(file, createImage(file, albumId)); + } + } + + public void walk() throws IOException { + walk(startDirectory, null); + } + + protected abstract Integer createAlbum(File directory, Integer parentAlbumId); + + protected abstract Integer createImage(File file, Integer albumId); +} diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/SyncJob.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/SyncJob.java index 759926a..615c639 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/SyncJob.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/SyncJob.java @@ -10,194 +10,32 @@ ******************************************************************************/ package org.piwigo.remotesync.api.sync; - -import java.io.File; import java.io.IOException; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import org.apache.commons.io.DirectoryWalker; -import org.piwigo.remotesync.api.Constants; +import org.piwigo.remotesync.api.ISyncConfiguration; import org.piwigo.remotesync.api.Job; -import org.piwigo.remotesync.api.client.AuthenticatedWSClient; -import org.piwigo.remotesync.api.conf.ConfigUtil; -import org.piwigo.remotesync.api.conf.GalleryConfig; -import org.piwigo.remotesync.api.exception.ClientServerException; -import org.piwigo.remotesync.api.request.PwgCategoriesAddRequest; -import org.piwigo.remotesync.api.request.PwgImagesAddSimpleRequest; -import org.piwigo.remotesync.api.sync.SyncCache.FileCache; +import org.piwigo.remotesync.api.conf.ConfigurationUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class SyncJob extends Job { - private static abstract class SyncDirectoryWalker extends DirectoryWalker { - - protected GalleryConfig config; - protected File startDirectory; - protected Map fileCaches = new HashMap(); - - private SyncDirectoryWalker(GalleryConfig config) { - super(null, Constants.IMAGE_EXTENSIONS_FILTER, -1); - this.config = config; - startDirectory = new File(config.getDirectory()); - } - - @Override - protected void handleDirectoryStart(File directory, int depth, Collection results) throws IOException { - FileCache fileCache = new SyncCache.FileCache(config.getUrl(), SyncCache.getLegacyCacheFile(directory)).parseFile(); - fileCaches.put(directory, fileCache); - - //do not create an album for root directory - if (directory.equals(startDirectory)) - return; - - if (fileCache.albumCache != null) { - logger.debug("album already in cache : " + directory); - } else { - Integer parentAlbumId = null; - try { - parentAlbumId = fileCaches.get(directory.getParentFile()).albumCache.id; - } catch (Exception e) { - // FIXME : ignore me - } - logger.info("will create album for " + directory); - fileCache.addAlbum(createAlbum(directory, parentAlbumId)); - } - } - - - protected void handleFile(File file, int depth, java.util.Collection results) throws IOException { - FileCache fileCache = fileCaches.get(file.getParentFile()); - - if (fileCache.containsPicture(file)) { - logger.debug("picture already in cache : " + file); - } else { - Integer albumId = null; - try { - albumId = fileCache.albumCache.id; - } catch (Exception e) { - // FIXME : ignore me - } - logger.info("will upload image for " + file + " in album with id " + albumId); - fileCache.addPicture(file, createPicture(file, albumId)); - } - } - - public void walk() throws IOException { - walk(startDirectory, null); - } - - protected abstract Integer createAlbum(File directory, Integer parentAlbumId); - - protected abstract Integer createPicture(File file, Integer albumId); - } - -// private static class DummyRunWalker extends SyncDirectoryWalker { -// -// private int album_id; -// private int picture_id; -// -// public DummyRunWalker(GalleryConfig config) { -// super(config); -// } -// -// @Override -// protected Integer createAlbum(File directory, Integer parentAlbumId) { -// return ++album_id; -// } -// -// @Override -// protected Integer createPicture(File file, Integer albumId) { -// return ++picture_id; -// } -// } - - private static class ConnectedWalker extends SyncDirectoryWalker { - - private AuthenticatedWSClient client; - - public ConnectedWalker(GalleryConfig config) { - super(config); - } - - protected void connect() throws IOException { - try { - logger.info("Connecting... "); - client = new AuthenticatedWSClient(config.getUrl()).login(config.getUsername(), config.getPassword()); - logger.info("Connect successful"); - } catch (ClientServerException e) { - client = null; - logger.error("Unable to connect : " + e.getMessage()); - throw new CancelException("Unable to connect", startDirectory, 0); - } - } - - protected void disconnect() { - try { - logger.info("Disconnecting... "); - client.logout(); - logger.info("Disconnect successful"); - } catch (ClientServerException e) { - logger.error("Unable to disconnect : " + e.getMessage()); - } - } - - @Override - protected void handleStart(File startDirectory, Collection results) throws IOException { - super.handleStart(startDirectory, results); - connect(); - } - - @Override - protected void handleEnd(Collection results) throws IOException { - super.handleEnd(results); - disconnect(); - } - - @Override - protected Integer createAlbum(File directory, Integer parentAlbumId) { - try { - return client.sendRequest(new PwgCategoriesAddRequest(directory.getName()).setParent(parentAlbumId)).id; - } catch (ClientServerException e) { - logger.error("Cannot create album for " + directory, e); - return null; - } - } - - @Override - protected Integer createPicture(File file, Integer albumId) { - try { - PwgImagesAddSimpleRequest request = new PwgImagesAddSimpleRequest(file); - //FIXME should we upload a picture without album? - if (albumId != null) - request.setCategory(albumId); - return client.sendRequest(request).image_id; - } catch (ClientServerException e) { - logger.error("Cannot updload image for " + file, e); - return null; - } - } - - } - private static final Logger logger = LoggerFactory.getLogger(SyncJob.class); protected void doExecute() throws Exception { - GalleryConfig config = ConfigUtil.INSTANCE.getUserConfig().getCurrentGalleryConfig(); + ISyncConfiguration syncConfiguration = ConfigurationUtil.INSTANCE.getUserConfiguration().getCurrentSyncConfiguration(); + + // TODO shoud validate config + // SyncConfigurationValidator.INSTANCE.validate(syncConfiguration); - //TODO shoud validate config -// GalleryConfigValidator.INSTANCE.validate(config); - - logger.info("User {} will sync gallery {} with directory {}", config.getUsername(), config.getUrl(), config.getDirectory()); + logger.info("User {} will sync gallery {} with directory {}", syncConfiguration.getUsername(), syncConfiguration.getUrl(), syncConfiguration.getDirectory()); try { - new ConnectedWalker(config).walk(); + new ConnectedWalker(syncConfiguration).walk(); } catch (IOException e) { logger.error("Error in sync", e); } - + logger.info("Finished"); } } diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/util/FileUtil.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/util/FileUtil.java index 0248b88..2b63c11 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/util/FileUtil.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/util/FileUtil.java @@ -12,8 +12,11 @@ import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; +import java.net.URL; +import java.net.URLDecoder; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.digest.DigestUtils; @@ -21,9 +24,13 @@ import org.piwigo.remotesync.api.exception.IORuntimeException; import org.piwigo.remotesync.api.type.Type; import org.piwigo.remotesync.api.type.ValueValidator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class FileUtil { - public static String getMD5Sum(File file) { + private static final Logger logger = LoggerFactory.getLogger(FileUtil.class); + + public static String getFileContentMD5Sum(File file) { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); @@ -35,6 +42,10 @@ public static String getMD5Sum(File file) { } } + public static String getFileNameMD5Sum(File file) { + return DigestUtils.md5Hex(file.getName()); + } + public static boolean isImage(File file) { try { ValueValidator.checkValue(Type.FILE, null, file); @@ -86,4 +97,28 @@ public static String getBase64String(File file, int chunkSize, int chunkNumber) return Base64.encodeBase64String(getFilePart(file, chunkSize, chunkNumber)); } + public static File getFile(Class clazz, String resourceName) { + return getFile(clazz, resourceName, true); + } + + public static File getFile(Class clazz, String resourceName, boolean checkExistence) { + try { + URL resource = clazz.getResource(resourceName); + + if (resource == null) + throw new IllegalStateException("Unable to find resource with name " + resourceName); + + String filePath = URLDecoder.decode(resource.getPath(), "UTF-8"); + + File file = new File(filePath); + if (checkExistence && !file.exists()) + throw new FileNotFoundException("Unable to find file with name " + resourceName); + + return file; + } catch (Exception exception) { + logger.error(exception.getMessage(), exception); + return null; + } + } + } diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/api/util/StringUtil.java b/remotesync-api/src/main/java/org/piwigo/remotesync/api/util/StringUtil.java index 0177f36..ba88885 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/api/util/StringUtil.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/api/util/StringUtil.java @@ -17,6 +17,8 @@ import org.apache.commons.codec.binary.Base64; public class StringUtil { + private static final String PE = "]-_"; + private static final String PB = "_-["; private static final SimpleDateFormat date_formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static String removeFormatting(String string) { @@ -36,15 +38,33 @@ public static Date parseDate(String value) throws ParseException { public static String fakeEncryptPassword(String value) { if (value == null) return null; + value = PB + rot13(value) + PE; return Base64.encodeBase64String(value.getBytes()); } /** * TODO Password not really encrypted/decrypted : warn users + * @throws ParseException */ - public static String fakeDecryptPassword(String value) { + public static String fakeDecryptPassword(String value) throws ParseException { if (value == null) return null; - return new String(Base64.decodeBase64(value)); + value = new String(Base64.decodeBase64(value)); + if (value.startsWith(PB) && value.endsWith(PE)) + return rot13(value.substring(3, value.length() - 3)); + throw new ParseException("Cannot decrypt password", 0); + } + + public static String rot13(String string) { + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < string.length(); i++) { + char c = string.charAt(i); + if (c >= 'a' && c <= 'm') c += 13; + else if (c >= 'A' && c <= 'M') c += 13; + else if (c >= 'n' && c <= 'z') c -= 13; + else if (c >= 'N' && c <= 'Z') c -= 13; + builder.append(c); + } + return builder.toString(); } } diff --git a/remotesync-api/src/main/java/org/piwigo/remotesync/generator/WSJavaAPIWriter.java b/remotesync-api/src/main/java/org/piwigo/remotesync/generator/WSJavaAPIWriter.java index 2788a0e..5377772 100644 --- a/remotesync-api/src/main/java/org/piwigo/remotesync/generator/WSJavaAPIWriter.java +++ b/remotesync-api/src/main/java/org/piwigo/remotesync/generator/WSJavaAPIWriter.java @@ -18,7 +18,6 @@ import java.io.InputStream; import java.io.StringReader; import java.net.URISyntaxException; -import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -29,21 +28,24 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; -import org.piwigo.remotesync.api.client.Client; +import org.piwigo.remotesync.api.IClient; +import org.piwigo.remotesync.api.IClientConfiguration; import org.piwigo.remotesync.api.client.WSClient; +import org.piwigo.remotesync.api.conf.ConfigurationUtil; import org.piwigo.remotesync.api.reflection.ReflectionCustomization; import org.piwigo.remotesync.api.request.ReflectionGetMethodDetailsRequest; import org.piwigo.remotesync.api.request.ReflectionGetMethodListRequest; import org.piwigo.remotesync.api.response.ReflectionGetMethodDetailsResponse; import org.piwigo.remotesync.api.response.ReflectionGetMethodDetailsResponse.Parameter; +import org.piwigo.remotesync.api.util.FileUtil; import org.piwigo.remotesync.api.xml.PersisterFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import ch.qos.logback.classic.Level; - import com.floreysoft.jmte.Engine; +import ch.qos.logback.classic.Level; + public class WSJavaAPIWriter { private static final Logger logger = LoggerFactory.getLogger(WSJavaAPIWriter.class); @@ -86,8 +88,8 @@ public static void main(String[] args) throws Exception { logger.info("Generation finished"); } - private String projectPath; - private String url; + protected File projectFile; + protected String url; protected void writeAPI(boolean refresh) throws Exception { List methodDetails = null; @@ -160,12 +162,14 @@ protected Map createModel(ReflectionGetMethodDetailsResponse met protected WSJavaAPIWriter(String url) { this.url = url; - URL resource = WSJavaAPIWriter.class.getResource(WSJavaAPIWriter.class.getSimpleName() + ".class"); - this.projectPath = resource.getPath().replaceAll("remotesync-api.*", "remotesync-api"); + File file = FileUtil.getFile(this.getClass(), this.getClass().getSimpleName() + ".class"); + projectFile = new File(file.getAbsolutePath().replaceAll("remotesync-api.*", "remotesync-api")); + assert(projectFile.exists()); } protected List getMethodDetailsFromWS() throws Exception { - Client client = new WSClient(url); + IClientConfiguration createConfiguration = ConfigurationUtil.INSTANCE.createConfiguration(url); + IClient client = new WSClient(createConfiguration); logger.info("get WS methods"); List methodDetailsResponses = new ArrayList(); @@ -226,7 +230,7 @@ protected void applyTemplate(Map model, String templateFileName, } protected File getProjectFile() { - return new File(projectPath); + return projectFile; } protected File getMainJavaFile(String filePath) { diff --git a/remotesync-api/src/main/resources/org/piwigo/remotesync/generator/apiTemplate.jmte b/remotesync-api/src/main/resources/org/piwigo/remotesync/generator/apiTemplate.jmte index 9f40644..f427f19 100644 --- a/remotesync-api/src/main/resources/org/piwigo/remotesync/generator/apiTemplate.jmte +++ b/remotesync-api/src/main/resources/org/piwigo/remotesync/generator/apiTemplate.jmte @@ -10,7 +10,7 @@ ******************************************************************************/ package org.piwigo.remotesync.api; -import org.piwigo.remotesync.api.client.Client; +import org.piwigo.remotesync.api.IClient; import org.piwigo.remotesync.api.exception.ClientServerException; import org.piwigo.remotesync.api.request.*; import org.piwigo.remotesync.api.response.*; diff --git a/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/APITest.java b/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/APITest.java index 59b4d2d..f93f8b1 100644 --- a/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/APITest.java +++ b/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/APITest.java @@ -16,9 +16,9 @@ import java.util.Date; import org.piwigo.remotesync.api.API; -import org.piwigo.remotesync.api.client.Client; +import org.piwigo.remotesync.api.IClient; +import org.piwigo.remotesync.api.ISyncConfiguration; import org.piwigo.remotesync.api.client.WSClient; -import org.piwigo.remotesync.api.conf.GalleryConfig; import org.piwigo.remotesync.api.exception.ClientServerException; import org.piwigo.remotesync.api.reflection.MethodReflection; import org.piwigo.remotesync.api.reflection.ReflectionRegistry; @@ -53,7 +53,7 @@ public void testLoginPossible() { public void testAPI() throws ClientServerException, URISyntaxException { API api = new API(getClient()); int initial = api.galleryGetInfos().nb_elements; - ComposedResponse imagesAddWithChunkRequest = api.imagesAddWithChunkRequest(new PwgImagesAddWithChunkRequest(getPictureFile()).setAuthor("remote sync")); + ComposedResponse imagesAddWithChunkRequest = api.imagesAddWithChunkRequest(new PwgImagesAddWithChunkRequest(getImageFile()).setAuthor("remote sync")); assertEquals(initial + 1, api.galleryGetInfos().nb_elements.intValue()); api.imagesDelete(new PwgImagesDeleteRequest(imagesAddWithChunkRequest.getResponse().image_id)); assertEquals(initial, api.galleryGetInfos().nb_elements.intValue()); @@ -73,15 +73,15 @@ public void testComposedRequest() throws ClientServerException { } public void testEnum() throws Exception { - Client client = getClient(); + IClient client = getClient(); PwgCategoriesAddResponse categoryResponse = client.sendRequest(new PwgCategoriesAddRequest("test").setStatus(Status.PRIVATE)); client.sendRequest(new PwgCategoriesDeleteRequest(categoryResponse.id)); } public void testLoginLogout() throws Exception { - GalleryConfig galleryConfig = getTestConfig().getCurrentGalleryConfig(); - Client client = new WSClient(galleryConfig.getUrl()); - client.sendRequest(new PwgSessionLoginRequest(galleryConfig.getUsername(), galleryConfig.getPassword())); + ISyncConfiguration syncConfiguration = getTestConfiguration().getCurrentSyncConfiguration(); + IClient client = new WSClient(syncConfiguration); + client.sendRequest(new PwgSessionLoginRequest(syncConfiguration.getUsername(), syncConfiguration.getPassword())); assertTrue(client.sendRequest(new PwgSessionGetStatusRequest()).isAdmin()); client.sendRequest(new PwgSessionLogoutRequest()); assertFalse(client.sendRequest(new PwgSessionGetStatusRequest()).isLogged()); @@ -89,8 +89,8 @@ public void testLoginLogout() throws Exception { public void testWrongLogin() { try { - GalleryConfig galleryConfig = getTestConfig().getCurrentGalleryConfig(); - new WSClient(galleryConfig.getUrl()).sendRequest(new PwgSessionLoginRequest("unknown", "unknown")); + ISyncConfiguration syncConfiguration = getTestConfiguration().getCurrentSyncConfiguration(); + new WSClient(syncConfiguration).sendRequest(new PwgSessionLoginRequest("unknown", "unknown")); fail("Should not login with wrong login/password"); } catch (Exception e) { } @@ -116,8 +116,19 @@ public void testDateConverter() throws ParseException { } public void testFakePasswordConverter() throws ParseException { - String password = "password"; + doTestFakePasswordConverter("a"); + doTestFakePasswordConverter("1"); + doTestFakePasswordConverter("password"); + doTestFakePasswordConverter("1234567890&é(-è_çà)^$ù*,;:!?./§µ%MP"); + } + + private void doTestFakePasswordConverter(String password) throws ParseException { assertFalse(password.equals(StringUtil.fakeEncryptPassword(password))); assertEquals(password, StringUtil.fakeDecryptPassword(StringUtil.fakeEncryptPassword(password))); } + + public void testRot13() { + assertEquals("aB123", StringUtil.rot13(StringUtil.rot13("aB123"))); + assertEquals("this is my 1st rot13 automatic test", StringUtil.rot13("guvf vf zl 1fg ebg13 nhgbzngvp grfg")); + } } diff --git a/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/AbstractTestCase.java b/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/AbstractTestCase.java index 9e526ff..e8470a8 100644 --- a/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/AbstractTestCase.java +++ b/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/AbstractTestCase.java @@ -12,56 +12,49 @@ import java.io.File; import java.net.URISyntaxException; -import java.net.URL; - -import junit.framework.TestCase; +import org.piwigo.remotesync.api.IClient; +import org.piwigo.remotesync.api.ISyncConfiguration; +import org.piwigo.remotesync.api.cache.LegacyCache; import org.piwigo.remotesync.api.client.AuthenticatedWSClient; -import org.piwigo.remotesync.api.client.Client; -import org.piwigo.remotesync.api.conf.Config; -import org.piwigo.remotesync.api.conf.ConfigUtil; -import org.piwigo.remotesync.api.conf.GalleryConfig; +import org.piwigo.remotesync.api.conf.ConfigurationUtil; +import org.piwigo.remotesync.api.conf.UserConfiguration; import org.piwigo.remotesync.api.exception.ClientServerException; -import org.piwigo.remotesync.api.sync.SyncCache; -import org.slf4j.Logger; +import org.piwigo.remotesync.api.util.FileUtil; import org.slf4j.LoggerFactory; import ch.qos.logback.classic.Level; +import junit.framework.TestCase; public abstract class AbstractTestCase extends TestCase { - private static final Logger logger = LoggerFactory.getLogger(AbstractTestCase.class); - static { ((ch.qos.logback.classic.Logger) LoggerFactory.getLogger("ROOT")).setLevel(Level.INFO); } - private static Client client; + private static IClient client; - protected Client getClient() throws ClientServerException { + protected IClient getClient() throws ClientServerException { if (client == null) { - GalleryConfig galleryConfig = getTestConfig().getCurrentGalleryConfig(); - client = new AuthenticatedWSClient(galleryConfig.getUrl()).login(galleryConfig.getUsername(), galleryConfig.getPassword()); + ISyncConfiguration syncConfiguration = getTestConfiguration().getCurrentSyncConfiguration(); + client = new AuthenticatedWSClient(syncConfiguration).login(); } return client; } - protected Config getTestConfig() { - URL resource = AbstractTestCase.class.getResource("testConfig"); - if (resource == null) { - IllegalStateException exception = new IllegalStateException("Unable to find testConfig file to configure tests, have a look at testConfig.example"); - logger.error(exception.getMessage(), exception); - throw exception; - } - ConfigUtil.INSTANCE.loadConfig(new File(resource.getPath())); - return ConfigUtil.INSTANCE.getUserConfig(); + protected UserConfiguration getTestConfiguration() { + File file = FileUtil.getFile(this.getClass(), "testConfig", false); + if (file == null) + throw new IllegalStateException("Unable to find testConfig file to configure tests, have a look at testConfig.example");; + ConfigurationUtil.INSTANCE.loadConfig(file); + return ConfigurationUtil.INSTANCE.getUserConfiguration(); } - protected File getPictureFile() throws URISyntaxException { - return new File(AbstractTestCase.class.getResource("picture.jpg").toURI()); + protected File getImageFile() throws URISyntaxException { + return FileUtil.getFile(AbstractTestCase.class, "image.jpg"); } protected File getPiwigoImportTreeFile() throws URISyntaxException { - return new File(AbstractTestCase.class.getResource(SyncCache.LEGACY_CACHE_FILE_NAME).toURI()); + return FileUtil.getFile(AbstractTestCase.class, LegacyCache.LEGACY_CACHE_FILE_NAME); } } diff --git a/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/FileUtilTest.java b/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/FileUtilTest.java index 788adaa..ba1eb58 100644 --- a/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/FileUtilTest.java +++ b/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/FileUtilTest.java @@ -18,46 +18,53 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang.ArrayUtils; import org.piwigo.remotesync.api.util.FileUtil; +import org.piwigo.remotesync.generator.Generated; public class FileUtilTest extends AbstractTestCase { public void testMD5Sum() throws IOException, URISyntaxException { - assertEquals("6c73caff0ed3241365058f0a7f82d7d2", FileUtil.getMD5Sum(getPictureFile())); + assertEquals("6c73caff0ed3241365058f0a7f82d7d2", FileUtil.getFileContentMD5Sum(getImageFile())); + assertEquals("0d5b1c4c7f720f698946c7f6ab08f687", FileUtil.getFileNameMD5Sum(getImageFile())); } public void testChunkNumber() throws URISyntaxException { - assertEquals(1, FileUtil.getChunkNumber(getPictureFile(), Integer.MAX_VALUE)); - assertEquals(3, FileUtil.getChunkNumber(getPictureFile(), (int) (getPictureFile().length() / 2 - 1))); + assertEquals(1, FileUtil.getChunkNumber(getImageFile(), Integer.MAX_VALUE)); + assertEquals(3, FileUtil.getChunkNumber(getImageFile(), (int) (getImageFile().length() / 2 - 1))); } public void testChunkSize() throws URISyntaxException { - assertEquals((int) getPictureFile().length(), FileUtil.getChunkSize(getPictureFile(), (int) getPictureFile().length(), 0)); - assertEquals(1, FileUtil.getChunkSize(getPictureFile(), 1, 0)); - assertEquals(1, FileUtil.getChunkSize(getPictureFile(), (int) getPictureFile().length() - 1, 1)); + assertEquals((int) getImageFile().length(), FileUtil.getChunkSize(getImageFile(), (int) getImageFile().length(), 0)); + assertEquals(1, FileUtil.getChunkSize(getImageFile(), 1, 0)); + assertEquals(1, FileUtil.getChunkSize(getImageFile(), (int) getImageFile().length() - 1, 1)); } public void testChunkSizeAndNumber() throws URISyntaxException { int chunkSize = 123; - int chunkNumber = FileUtil.getChunkNumber(getPictureFile(), chunkSize); + int chunkNumber = FileUtil.getChunkNumber(getImageFile(), chunkSize); long total = 0; for (int i = 0 ; i < chunkNumber ; i++) { - int actualChunkSize = FileUtil.getChunkSize(getPictureFile(), chunkSize, i); + int actualChunkSize = FileUtil.getChunkSize(getImageFile(), chunkSize, i); assertTrue(actualChunkSize > 0); total += actualChunkSize; } - assertEquals(getPictureFile().length(), total); + assertEquals(getImageFile().length(), total); } public void testFilePart() throws IOException, URISyntaxException { int chunkSize = 456; - int chunkNumber = FileUtil.getChunkNumber(getPictureFile(), chunkSize); + int chunkNumber = FileUtil.getChunkNumber(getImageFile(), chunkSize); byte[] result = new byte[0]; for (int i = 0 ; i < chunkNumber ; i++) - result = ArrayUtils.addAll(result, FileUtil.getFilePart(getPictureFile(), chunkSize, i)); - assertTrue(Arrays.equals(IOUtils.toByteArray(new FileInputStream(getPictureFile())), result)); + result = ArrayUtils.addAll(result, FileUtil.getFilePart(getImageFile(), chunkSize, i)); + assertTrue(Arrays.equals(IOUtils.toByteArray(new FileInputStream(getImageFile())), result)); } public void testIsImage() throws URISyntaxException { - assertTrue(FileUtil.isImage(getPictureFile())); + assertTrue(FileUtil.isImage(getImageFile())); assertFalse(FileUtil.isImage(getPiwigoImportTreeFile())); } + + public void testResources() { + assertNull(FileUtil.getFile(this.getClass(), "apiTemplate.jmte")); + assertNotNull(FileUtil.getFile(Generated.class, "apiTemplate.jmte")); + } } diff --git a/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/ImagesTest.java b/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/ImagesTest.java index 69b450e..0e58dbf 100644 --- a/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/ImagesTest.java +++ b/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/ImagesTest.java @@ -12,7 +12,7 @@ import java.io.File; -import org.piwigo.remotesync.api.client.Client; +import org.piwigo.remotesync.api.IClient; import org.piwigo.remotesync.api.request.PwgCategoriesAddRequest; import org.piwigo.remotesync.api.request.PwgCategoriesDeleteRequest; import org.piwigo.remotesync.api.request.PwgImagesAddSimpleRequest; @@ -25,8 +25,8 @@ public class ImagesTest extends AbstractTestCase { public void testAddSimple() throws Exception { - Client client = getClient(); - File image = getPictureFile(); + IClient client = getClient(); + File image = getImageFile(); PwgCategoriesAddResponse categoryResponse = client.sendRequest(new PwgCategoriesAddRequest("test")); PwgImagesAddSimpleResponse imageResponse = client.sendRequest(new PwgImagesAddSimpleRequest(image).setCategory(categoryResponse.id)); client.sendRequest(new PwgImagesDeleteRequest(imageResponse.image_id)); @@ -34,8 +34,8 @@ public void testAddSimple() throws Exception { } public void testAddWithChunk() throws Exception { - Client client = getClient(); - File image = getPictureFile(); + IClient client = getClient(); + File image = getImageFile(); PwgCategoriesAddResponse categoryResponse = client.sendRequest(new PwgCategoriesAddRequest("test")); ComposedResponse composedResponse = client.sendRequest(new PwgImagesAddWithChunkRequest(image).setCategories(categoryResponse.id + "")); PwgImagesAddResponse imagesAddResponse = composedResponse.getResponse(); diff --git a/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/RequestsTest.java b/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/RequestsTest.java index 0008dce..6076298 100644 --- a/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/RequestsTest.java +++ b/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/RequestsTest.java @@ -86,7 +86,7 @@ public void testCaddie() throws ClientServerException { public void testCategories() throws ClientServerException, URISyntaxException { PwgCategoriesAddResponse categoryResponse = getClient().sendRequest(new PwgCategoriesAddRequest("test")); PwgCategoriesAddResponse categoryResponse2 = getClient().sendRequest(new PwgCategoriesAddRequest("test2")); - PwgImagesAddSimpleResponse imageResponse = getClient().sendRequest(new PwgImagesAddSimpleRequest(getPictureFile()).setCategory(categoryResponse.id)); + PwgImagesAddSimpleResponse imageResponse = getClient().sendRequest(new PwgImagesAddSimpleRequest(getImageFile()).setCategory(categoryResponse.id)); try { PwgCategoriesAddResponse category = getClient().sendRequest(new PwgCategoriesAddRequest("newcat")); @@ -166,7 +166,7 @@ public void testTags() throws ClientServerException, URISyntaxException { API api = new API(getClient()); String tagName = "imagetag" + new Date().toString(); - PwgImagesAddSimpleResponse imageResponse = getClient().sendRequest(new PwgImagesAddSimpleRequest(getPictureFile()).setTags(tagName)); + PwgImagesAddSimpleResponse imageResponse = getClient().sendRequest(new PwgImagesAddSimpleRequest(getImageFile()).setTags(tagName)); try { api.tagsAdd(new PwgTagsAddRequest("alonetag" + new Date().toString())); diff --git a/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/SyncTest.java b/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/SyncTest.java index 78d6293..068e4db 100644 --- a/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/SyncTest.java +++ b/remotesync-api/src/test/java/org/piwigo/remotesync/api/test/SyncTest.java @@ -12,38 +12,33 @@ import java.net.URISyntaxException; -import org.piwigo.remotesync.api.sync.SyncCache.AlbumCache; -import org.piwigo.remotesync.api.sync.SyncCache.FileCache; -import org.piwigo.remotesync.api.sync.SyncCache.MemoryCache; -import org.piwigo.remotesync.api.sync.SyncCache.PictureCache; +import org.piwigo.remotesync.api.cache.AlbumCacheElement; +import org.piwigo.remotesync.api.cache.ImageCacheElement; +import org.piwigo.remotesync.api.cache.LegacyCache; public class SyncTest extends AbstractTestCase { - public void testMemoryCache() { - MemoryCache memoryCache = new MemoryCache("test"); - - AlbumCache a = new AlbumCache(); - a.id = 3; - a.url = "url"; - memoryCache.parseContent(a.toString()); - assertEquals(a.toString(), memoryCache.albumCache.toString()); - - PictureCache p = new PictureCache(); - p.id = 3; - p.url = "url"; - p.md5 = "md5"; - memoryCache.parseContent(p.toString()); - assertEquals(p.toString(), memoryCache.picturesCache.get(0).toString()); + public void testLegacyCache() throws URISyntaxException { + LegacyCache legacyCache = new LegacyCache("test", null); + + AlbumCacheElement a = new AlbumCacheElement("url", 3); + legacyCache.parseContent(a.writeToString()); + assertEquals(a.writeToString(), legacyCache.getAlbumCacheElement().writeToString()); + + ImageCacheElement p = new ImageCacheElement("url", 3, "filePathMD5"); + legacyCache.parseContent(p.writeToString()); + assertEquals(p.writeToString(), legacyCache.getImagesCache().get(0).writeToString()); } - public void testFileCache() throws URISyntaxException { + public void testLegacyCache2() throws URISyntaxException { assertTrue(getPiwigoImportTreeFile().exists()); - FileCache fileCache = new FileCache(null, getPiwigoImportTreeFile()); - fileCache.parseFile(); - assertEquals(116, fileCache.albumCache.id.intValue()); - assertEquals(20, fileCache.picturesCache.size()); - assertEquals(502, fileCache.picturesCache.get(0).id.intValue()); - assertEquals(521, fileCache.picturesCache.get(19).id.intValue()); - assertEquals("d2d97dd5727972522adfd82f43daddcd", fileCache.picturesCache.get(1).md5); + + LegacyCache legacyCache = new LegacyCache(null, getPiwigoImportTreeFile()); + legacyCache.parseFile(); + assertEquals(116, legacyCache.getAlbumCacheElement().getId().intValue()); + assertEquals(20, legacyCache.getImagesCache().size()); + assertEquals(502, legacyCache.getImagesCache().get(0).getId().intValue()); + assertEquals(521, legacyCache.getImagesCache().get(19).getId().intValue()); + assertEquals("d2d97dd5727972522adfd82f43daddcd", legacyCache.getImagesCache().get(1).getFilePathMD5()); } } diff --git a/remotesync-api/src/test/resources/org/piwigo/remotesync/api/test/picture.jpg b/remotesync-api/src/test/resources/org/piwigo/remotesync/api/test/image.jpg similarity index 100% rename from remotesync-api/src/test/resources/org/piwigo/remotesync/api/test/picture.jpg rename to remotesync-api/src/test/resources/org/piwigo/remotesync/api/test/image.jpg diff --git a/remotesync-api/src/test/resources/org/piwigo/remotesync/api/test/testConfig.example b/remotesync-api/src/test/resources/org/piwigo/remotesync/api/test/testConfig.example index 529034f..cec5b2a 100644 --- a/remotesync-api/src/test/resources/org/piwigo/remotesync/api/test/testConfig.example +++ b/remotesync-api/src/test/resources/org/piwigo/remotesync/api/test/testConfig.example @@ -1,9 +1,13 @@ - - - + + + http://mygallery.piwigo.com username - Base64password== - - - \ No newline at end of file + Xy1bXS1f + false + http://myproxy.server.com + 8000 + 500 + + + \ No newline at end of file diff --git a/remotesync-ui/.settings/PiwigoRemoteSync UI MainUi Swing.launch b/remotesync-ui/.settings/PiwigoRemoteSync UI MainUi Swing.launch index 109ac77..d74e184 100644 --- a/remotesync-ui/.settings/PiwigoRemoteSync UI MainUi Swing.launch +++ b/remotesync-ui/.settings/PiwigoRemoteSync UI MainUi Swing.launch @@ -1,15 +1,14 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + diff --git a/remotesync-ui/.settings/PiwigoRemoteSync UI MainUi TestClient.launch b/remotesync-ui/.settings/PiwigoRemoteSync UI MainUi TestClient.launch index a58d32c..085de27 100644 --- a/remotesync-ui/.settings/PiwigoRemoteSync UI MainUi TestClient.launch +++ b/remotesync-ui/.settings/PiwigoRemoteSync UI MainUi TestClient.launch @@ -1,15 +1,14 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + diff --git a/remotesync-ui/.settings/PiwigoRemoteSync UI MainUi.launch b/remotesync-ui/.settings/PiwigoRemoteSync UI MainUi.launch index 8b0e830..4db4a0d 100644 --- a/remotesync-ui/.settings/PiwigoRemoteSync UI MainUi.launch +++ b/remotesync-ui/.settings/PiwigoRemoteSync UI MainUi.launch @@ -1,15 +1,14 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + diff --git a/remotesync-ui/pom.xml b/remotesync-ui/pom.xml index 034f171..d3dba8b 100644 --- a/remotesync-ui/pom.xml +++ b/remotesync-ui/pom.xml @@ -17,7 +17,7 @@ piwigo remotesync-api - 0.0.6 + 0.0.7 org.apache.pivot @@ -66,7 +66,7 @@ piwigo remotesync - 0.0.6 + 0.0.7 ../remotesync diff --git a/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/RemotesyncUI.java b/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/RemotesyncUI.java index 5545027..c13ee9e 100644 --- a/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/RemotesyncUI.java +++ b/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/RemotesyncUI.java @@ -16,25 +16,25 @@ import org.apache.pivot.wtk.DesktopApplicationContext; import org.apache.pivot.wtk.Display; import org.apache.pivot.wtk.Window; -import org.piwigo.remotesync.api.conf.ConfigUtil; +import org.piwigo.remotesync.api.conf.ConfigurationUtil; public class RemotesyncUI extends Window implements Application { - private GalleryConfigForm galleryConfigForm; + private SyncConfigurationForm syncConfigurationForm; public void startup(final Display display, final Map properties) throws Exception { - galleryConfigForm = (GalleryConfigForm) new BXMLSerializer().readObject(GalleryConfigForm.class, "galleryConfigForm.bxml"); - galleryConfigForm.loadAccessors(); + syncConfigurationForm = (SyncConfigurationForm) new BXMLSerializer().readObject(SyncConfigurationForm.class, "syncConfigurationForm.bxml"); + syncConfigurationForm.loadAccessors(); - this.setContent(galleryConfigForm); + this.setContent(syncConfigurationForm); this.setTitle("Piwigo Remote Sync"); this.setMaximized(true); this.open(display); } public boolean shutdown(final boolean optional) throws Exception { - galleryConfigForm.saveAccessors(); - ConfigUtil.INSTANCE.saveUserConfig(); + syncConfigurationForm.saveAccessors(); + ConfigurationUtil.INSTANCE.saveUserConfig(); this.close(); return false; diff --git a/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/GalleryConfigForm.java b/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/SyncConfigurationForm.java similarity index 81% rename from remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/GalleryConfigForm.java rename to remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/SyncConfigurationForm.java index 4d434e8..118c3e6 100644 --- a/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/GalleryConfigForm.java +++ b/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/SyncConfigurationForm.java @@ -40,16 +40,17 @@ import org.apache.pivot.wtk.content.ListButtonDataRenderer; import org.apache.pivot.wtk.content.ListViewItemRenderer; import org.apache.pivot.wtk.validation.Validator; -import org.piwigo.remotesync.api.conf.Config; -import org.piwigo.remotesync.api.conf.ConfigUtil; -import org.piwigo.remotesync.api.conf.GalleryConfig; -import org.piwigo.remotesync.api.conf.GalleryConfigValidator; -import org.piwigo.remotesync.api.conf.GalleryConfigValidator.GalleryValidationException; +import org.piwigo.remotesync.api.conf.UserConfiguration; +import org.piwigo.remotesync.api.ISyncConfiguration; +import org.piwigo.remotesync.api.conf.ConfigurationUtil; +import org.piwigo.remotesync.api.conf.SyncConfiguration; +import org.piwigo.remotesync.api.conf.SyncConfigurationValidator; +import org.piwigo.remotesync.api.conf.SyncConfigurationValidator.SyncValidationException; -public class GalleryConfigForm extends Border implements Bindable { +public class SyncConfigurationForm extends Border implements Bindable { @BXML private Form form; - @BXML private ListButton configListButton; + @BXML private ListButton configurationListButton; @BXML private TextInput urlTextInput; @BXML private TextInput loginTextInput; @@ -102,23 +103,23 @@ public void enabledChanged(Component component) { accessor.component.getComponentStateListeners().add(componentStateListener); } - configListButton.setItemRenderer(new ListViewItemRenderer() { + configurationListButton.setItemRenderer(new ListViewItemRenderer() { @Override public String toString(Object item) { - if (item instanceof GalleryConfig) { - GalleryConfig galleryConfig = (GalleryConfig) item; - return galleryConfig.getUsername() + "@" + galleryConfig.getUrl(); + if (item instanceof SyncConfiguration) { + ISyncConfiguration syncConfiguration = (ISyncConfiguration) item; + return syncConfiguration.getUsername() + "@" + syncConfiguration.getUrl(); } return super.toString(item); } }); - configListButton.setDataRenderer(new ListButtonDataRenderer() { + configurationListButton.setDataRenderer(new ListButtonDataRenderer() { @Override public String toString(Object data) { - if (data instanceof GalleryConfig) { - GalleryConfig galleryConfig = (GalleryConfig) data; - return galleryConfig.getUsername() + "@" + galleryConfig.getUrl(); + if (data instanceof SyncConfiguration) { + ISyncConfiguration syncConfiguration = (ISyncConfiguration) data; + return syncConfiguration.getUsername() + "@" + syncConfiguration.getUrl(); } return super.toString(data); } @@ -156,7 +157,7 @@ protected File findRootDirectory() { while (rootDirectory != null && !rootDirectory.exists() && !rootDirectory.isDirectory()) rootDirectory = rootDirectory.getParentFile(); if (rootDirectory == null) - return ConfigUtil.INSTANCE.getUserCurrentDirectory(); + return ConfigurationUtil.INSTANCE.getUserCurrentDirectory(); return rootDirectory; } }); @@ -180,7 +181,7 @@ public void buttonPressed(Button button) { reloadButton.getButtonPressListeners().add(new ButtonPressListener() { @Override public void buttonPressed(Button button) { - ConfigUtil.INSTANCE.loadUserConfig(); + ConfigurationUtil.INSTANCE.loadUserConfig(); loadAccessors(); } }); @@ -198,27 +199,27 @@ protected void updateProxyState() { } public void loadAccessors() { - Config config = ConfigUtil.INSTANCE.getUserConfig(); - GalleryConfig galleryConfig = config.getCurrentGalleryConfig(); + UserConfiguration userConfiguration = ConfigurationUtil.INSTANCE.getUserConfiguration(); + SyncConfiguration syncConfiguration = userConfiguration.getCurrentSyncConfiguration(); - configListButton.setListData(new ListAdapter(config.getGalleryConfigs())); - configListButton.setSelectedItem(galleryConfig); + configurationListButton.setListData(new ListAdapter(userConfiguration.getSyncConfigurations())); + configurationListButton.setSelectedItem(syncConfiguration); for (FieldAccessor accessor : accessors) { - accessor.setValue(galleryConfig.getValue(accessor.getFieldName())); + accessor.setValue(syncConfiguration.getValue(accessor.getFieldName())); } updateChunkSizeLabel(); updateProxyState(); } public void saveAccessors() { - Config config = ConfigUtil.INSTANCE.getUserConfig(); - GalleryConfig galleryConfig = config.getCurrentGalleryConfig(); + UserConfiguration userConfiguration = ConfigurationUtil.INSTANCE.getUserConfiguration(); + SyncConfiguration syncConfiguration = userConfiguration.getCurrentSyncConfiguration(); for (FieldAccessor accessor : accessors) { - galleryConfig.setValue(accessor.getFieldName(), accessor.getValue()); + syncConfiguration.setValue(accessor.getFieldName(), accessor.getValue()); } } @@ -228,8 +229,8 @@ public void validateAccessors() { for (FieldAccessor accessor : accessors) { try { - GalleryConfigValidator.INSTANCE.validate(accessor.getFieldName(), accessor.getValue(), proxyCheckbox.isSelected()); - } catch (GalleryValidationException e) { + SyncConfigurationValidator.INSTANCE.validate(accessor.getFieldName(), accessor.getValue(), proxyCheckbox.isSelected()); + } catch (SyncValidationException e) { Component component = accessor.getComponent(); if (component.isEnabled() && component.isVisible() && component instanceof TextInput) { TextInput input = (TextInput) component; diff --git a/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/reflection/RequestForm.java b/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/reflection/RequestForm.java index b634041..d48207b 100644 --- a/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/reflection/RequestForm.java +++ b/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/pivot/reflection/RequestForm.java @@ -31,9 +31,9 @@ import org.apache.pivot.wtk.PushButton; import org.apache.pivot.wtk.SplitPane; import org.apache.pivot.wtk.TextArea; +import org.piwigo.remotesync.api.ISyncConfiguration; import org.piwigo.remotesync.api.client.AuthenticatedWSClient; -import org.piwigo.remotesync.api.conf.ConfigUtil; -import org.piwigo.remotesync.api.conf.GalleryConfig; +import org.piwigo.remotesync.api.conf.ConfigurationUtil; import org.piwigo.remotesync.api.reflection.MethodReflection; import org.piwigo.remotesync.api.reflection.ReflectiveRequest; import org.piwigo.remotesync.api.response.BasicResponse; @@ -78,8 +78,8 @@ public void buttonPressed(Button button) { } if (client == null) { - GalleryConfig galleryConfig = ConfigUtil.INSTANCE.getUserConfig().getCurrentGalleryConfig(); - client = new AuthenticatedWSClient(galleryConfig.getUrl()).login(galleryConfig.getUsername(), galleryConfig.getPassword()); + ISyncConfiguration syncConfiguration = ConfigurationUtil.INSTANCE.getUserConfiguration().getCurrentSyncConfiguration(); + client = new AuthenticatedWSClient(syncConfiguration).login(); } BasicResponse response = client.sendRequest(request); diff --git a/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/swing/ProxyUI.java b/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/swing/ProxyUI.java index 7c2ae59..27e48f0 100644 --- a/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/swing/ProxyUI.java +++ b/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/swing/ProxyUI.java @@ -24,7 +24,7 @@ import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; -import org.piwigo.remotesync.api.conf.GalleryConfig; +import org.piwigo.remotesync.api.conf.SyncConfiguration; public class ProxyUI extends JFrame { @@ -37,11 +37,11 @@ public class ProxyUI extends JFrame { private JTextField proxyPasswordtextField; private JCheckBox chckbxUseProxy; - public static void run(final GalleryConfig config) { + public static void run(final SyncConfiguration syncConfiguration) { EventQueue.invokeLater(new Runnable() { public void run() { try { - ProxyUI frame = new ProxyUI(config); + ProxyUI frame = new ProxyUI(syncConfiguration); frame.chckbxUseProxy.setSelected(false); frame.disableTextFields(); frame.setVisible(true); @@ -55,9 +55,9 @@ public void run() { /** * Create the frame. * - * @param config + * @param syncConfiguration */ - public ProxyUI(final GalleryConfig config) { + public ProxyUI(final SyncConfiguration syncConfiguration) { setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); @@ -114,21 +114,21 @@ public void stateChanged(ChangeEvent e) { @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); - config.setUsesProxy(Boolean.toString(chckbxUseProxy.isSelected())); - config.setProxyUrl(proxyUrltextField.getText()); - config.setProxyPort(proxyPorttextField.getText()); - config.setProxyUsername(proxyLogintextField.getText()); - config.setProxyPassword(proxyPasswordtextField.getText()); + syncConfiguration.setUsesProxy(Boolean.toString(chckbxUseProxy.isSelected())); + syncConfiguration.setProxyUrl(proxyUrltextField.getText()); + syncConfiguration.setProxyPort(proxyPorttextField.getText()); + syncConfiguration.setProxyUsername(proxyLogintextField.getText()); + syncConfiguration.setProxyPassword(proxyPasswordtextField.getText()); } @Override public void windowOpened(WindowEvent e) { super.windowOpened(e); - chckbxUseProxy.setSelected(config.getUsesProxyBoolean()); - proxyUrltextField.setText(config.getProxyUrl()); - proxyPorttextField.setText(config.getProxyPort() + ""); - proxyLogintextField.setText(config.getProxyUsername()); - proxyPasswordtextField.setText(config.getProxyPassword()); + chckbxUseProxy.setSelected(syncConfiguration.getUsesProxy()); + proxyUrltextField.setText(syncConfiguration.getProxyUrl()); + proxyPorttextField.setText(syncConfiguration.getProxyPort() + ""); + proxyLogintextField.setText(syncConfiguration.getProxyUsername()); + proxyPasswordtextField.setText(syncConfiguration.getProxyPassword()); } }); diff --git a/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/swing/RemotesyncUI.java b/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/swing/RemotesyncUI.java index 0da3e29..cd2ce68 100644 --- a/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/swing/RemotesyncUI.java +++ b/remotesync-ui/src/main/java/org/piwigo/remotesync/ui/swing/RemotesyncUI.java @@ -27,8 +27,8 @@ import javax.swing.JTextField; import javax.swing.text.BadLocationException; -import org.piwigo.remotesync.api.conf.ConfigUtil; -import org.piwigo.remotesync.api.conf.GalleryConfig; +import org.piwigo.remotesync.api.conf.ConfigurationUtil; +import org.piwigo.remotesync.api.conf.SyncConfiguration; import org.piwigo.remotesync.api.sync.SyncJob; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,7 +52,7 @@ public class RemotesyncUI { private JTextArea textArea; private JButton proxyButton; - private GalleryConfig config = ConfigUtil.INSTANCE.getUserConfig().getCurrentGalleryConfig(); + private SyncConfiguration syncConfiguration = ConfigurationUtil.INSTANCE.getUserConfiguration().getCurrentSyncConfiguration(); private JScrollPane scrollPane; /** @@ -70,7 +70,7 @@ public void run() { window.frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ window.save(); - ConfigUtil.INSTANCE.saveUserConfig(); + ConfigurationUtil.INSTANCE.saveUserConfig(); } }); @@ -198,7 +198,7 @@ public void actionPerformed(ActionEvent e) { protected void proxy() { logger.debug("RemotesyncUI proxy button pressed"); - ProxyUI.run(config); + ProxyUI.run(syncConfiguration); } protected void selectDirectory() { @@ -226,16 +226,16 @@ protected void sync() { } protected void load() { - urlText.setText(config.getUrl()); - usernameText.setText(config.getUsername()); - passwordText.setText(config.getPassword()); - directoryText.setText(config.getDirectory()); + urlText.setText(syncConfiguration.getUrl()); + usernameText.setText(syncConfiguration.getUsername()); + passwordText.setText(syncConfiguration.getPassword()); + directoryText.setText(syncConfiguration.getDirectory()); } protected void save() { - config.setUrl(urlText.getText()); - config.setUsername(usernameText.getText()); - config.setPassword(passwordText.getText()); - config.setDirectory(directoryText.getText()); + syncConfiguration.setUrl(urlText.getText()); + syncConfiguration.setUsername(usernameText.getText()); + syncConfiguration.setPassword(passwordText.getText()); + syncConfiguration.setDirectory(directoryText.getText()); } } diff --git a/remotesync-ui/src/main/resources/org/piwigo/remotesync/ui/pivot/galleryConfigForm.bxml b/remotesync-ui/src/main/resources/org/piwigo/remotesync/ui/pivot/galleryConfigurationForm.bxml similarity index 95% rename from remotesync-ui/src/main/resources/org/piwigo/remotesync/ui/pivot/galleryConfigForm.bxml rename to remotesync-ui/src/main/resources/org/piwigo/remotesync/ui/pivot/galleryConfigurationForm.bxml index b6159ae..33b35e1 100644 --- a/remotesync-ui/src/main/resources/org/piwigo/remotesync/ui/pivot/galleryConfigForm.bxml +++ b/remotesync-ui/src/main/resources/org/piwigo/remotesync/ui/pivot/galleryConfigurationForm.bxml @@ -8,7 +8,7 @@ Contributors: Matthieu Helleboid - initial API and implementation --> - @@ -18,7 +18,7 @@
- + @@ -96,4 +96,4 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/remotesync/README.txt b/remotesync/README.txt index 32abfd3..aad6c67 100644 --- a/remotesync/README.txt +++ b/remotesync/README.txt @@ -21,8 +21,8 @@ cd remotesync mvn -Dmaven.test.skip=true clean package #show help -java -jar ../remotesync-api/target/remotesync-api-0.0.6-jar-with-dependencies.jar -help -java -jar ../remotesync-ui/target/remotesync-ui-0.0.6-jar-with-dependencies.jar -help +java -jar ../remotesync-api/target/remotesync-api-0.0.7-jar-with-dependencies.jar -help +java -jar ../remotesync-ui/target/remotesync-ui-0.0.7-jar-with-dependencies.jar -help #launch remote sync ui -java -jar ../remotesync-ui/target/remotesync-ui-0.0.6-jar-with-dependencies.jar +java -jar ../remotesync-ui/target/remotesync-ui-0.0.7-jar-with-dependencies.jar diff --git a/remotesync/pom.xml b/remotesync/pom.xml index 84a6403..f9b1e6d 100644 --- a/remotesync/pom.xml +++ b/remotesync/pom.xml @@ -14,7 +14,7 @@ piwigo remotesync Piwigo Remote Sync - 0.0.6 + 0.0.7 pom ../remotesync-api