Skip to content

Commit d32a8fd

Browse files
committed
new version 0.0.8:
* cache is now url sensitive
1 parent 7dab5e0 commit d32a8fd

15 files changed

Lines changed: 188 additions & 104 deletions

File tree

remotesync-api/pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@
104104
<archive>
105105
<manifest>
106106
<mainClass>org.piwigo.remotesync.api.Main</mainClass>
107+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
108+
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
107109
</manifest>
108110
</archive>
109111
</configuration>
@@ -112,12 +114,17 @@
112114
<artifactId>maven-assembly-plugin</artifactId>
113115
<version>2.4</version>
114116
<configuration>
117+
<finalName>remotesync</finalName>
118+
<outputDirectory>../remotesync/target/</outputDirectory>
119+
<appendAssemblyId>false</appendAssemblyId>
115120
<descriptorRefs>
116121
<descriptorRef>jar-with-dependencies</descriptorRef>
117122
</descriptorRefs>
118123
<archive>
119124
<manifest>
120125
<mainClass>org.piwigo.remotesync.api.Main</mainClass>
126+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
127+
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
121128
</manifest>
122129
</archive>
123130
</configuration>
@@ -136,7 +143,7 @@
136143
<parent>
137144
<groupId>piwigo</groupId>
138145
<artifactId>remotesync</artifactId>
139-
<version>0.0.7</version>
146+
<version>0.0.8</version>
140147
<relativePath>../remotesync</relativePath>
141148
</parent>
142149
</project>

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,24 @@ protected void run(String[] args) {
3838
createConfiguration(parsedSyncConfiguration);
3939

4040
if (help) {
41-
System.out.println("Piwigo Remote Sync : java -jar remotesync.jar");
42-
cmdLineParser.printUsage(System.out);
41+
help(cmdLineParser);
4342
return;
4443
}
4544

4645
start();
4746
} catch (CmdLineException e) {
4847
System.err.println(e.getMessage());
49-
System.err.println("Piwigo Remote Sync : java -jar remotesync.jar");
50-
cmdLineParser.printUsage(System.err);
51-
System.err.println();
52-
System.err.println(" Example: java -jar remotesync.jar" + cmdLineParser.printExample(OptionHandlerFilter.ALL));
48+
System.err.flush();
49+
help(cmdLineParser);
5350
}
5451
}
5552

53+
protected void help(CmdLineParser cmdLineParser) {
54+
System.out.println("Piwigo Remote Sync (version " + getClass().getPackage().getImplementationVersion() + ") : java -jar remotesync.jar");
55+
cmdLineParser.printUsage(System.out);
56+
System.out.println("Example: java -jar remotesync.jar" + cmdLineParser.printExample(OptionHandlerFilter.ALL));
57+
}
58+
5659
protected abstract void start();
5760

5861
@Option(name = "-debug", usage = "enable debug messages")

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.apache.commons.io.FilenameUtils;
1818
import org.apache.commons.io.filefilter.AbstractFileFilter;
1919
import org.apache.commons.io.filefilter.IOFileFilter;
20+
import org.piwigo.remotesync.api.conf.ConfigurationUtil;
2021

2122
public class Constants {
2223

@@ -41,4 +42,10 @@ public boolean accept(File file) {
4142

4243
}
4344

45+
public static final String DIRECTORY_DEFAULT = ConfigurationUtil.INSTANCE.getUserCurrentDirectory().getAbsolutePath();
46+
47+
public static final String CHUNK_SIZE_DEFAULT = "500";
48+
49+
public static final int CHUNK_SIZE_INT_DEFAULT = Integer.parseInt(CHUNK_SIZE_DEFAULT);
50+
4451
}

remotesync-api/src/main/java/org/piwigo/remotesync/api/cache/LegacyCache.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,32 @@ public LegacyCache parseFile() {
106106

107107
public LegacyCache parseContent(String content) {
108108
Matcher matcher = ALBUM_PATTERN.matcher(content);
109-
if (matcher.find()) {
110-
albumCacheElement = new AlbumCacheElement();
111-
albumCacheElement.url = matcher.group(1);
112-
albumCacheElement.id = Integer.parseInt(matcher.group(2));
109+
while (matcher.find()) {
110+
if (isSameUrl(matcher.group(1))) {
111+
albumCacheElement = new AlbumCacheElement();
112+
albumCacheElement.url = matcher.group(1);
113+
albumCacheElement.id = Integer.parseInt(matcher.group(2));
114+
}
113115
}
114116

115117
matcher = IMAGE_PATTERN.matcher(content);
116118
while (matcher.find()) {
117-
ImageCacheElement imageCacheElement = new ImageCacheElement();
118-
imageCacheElement.url = matcher.group(1);
119-
imageCacheElement.filePathMD5 = matcher.group(2);
120-
imageCacheElement.id = Integer.parseInt(matcher.group(3));
121-
imagesCache.add(imageCacheElement);
119+
if (isSameUrl(matcher.group(1))) {
120+
ImageCacheElement imageCacheElement = new ImageCacheElement();
121+
imageCacheElement.url = matcher.group(1);
122+
imageCacheElement.filePathMD5 = matcher.group(2);
123+
imageCacheElement.id = Integer.parseInt(matcher.group(3));
124+
imagesCache.add(imageCacheElement);
125+
}
122126
}
123127

124128
return this;
125129
}
126130

131+
public boolean isSameUrl(String otherUrl) {
132+
return url.replaceAll("https?", "").equals(otherUrl.replaceAll("https?", ""));
133+
}
134+
127135
protected void writeToFile(ILegacyCacheElement abstractCacheElement) {
128136
try {
129137
FileUtils.writeStringToFile(cacheFile, abstractCacheElement.writeToString() + "\n", true);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
public abstract class AbstractClient implements IClient {
2323

2424
@Override
25-
public final <T extends BasicResponse> T sendRequest(AbstractRequest<T> request) throws ClientServerException {
25+
public <T extends BasicResponse> T sendRequest(AbstractRequest<T> request) throws ClientServerException {
2626
return doSendRequest(request);
2727
}
2828

2929
@SuppressWarnings({ "unchecked", "rawtypes" })
3030
@Override
31-
public final <T extends BasicResponse> ComposedResponse<T> sendRequest(ComposedRequest<T> composedRequest) throws ClientServerException {
31+
public <T extends BasicResponse> ComposedResponse<T> sendRequest(ComposedRequest<T> composedRequest) throws ClientServerException {
3232
ComposedResponse<T> composedResponse = new ComposedResponse<T>();
3333
Iterator<AbstractRequest<? extends BasicResponse>> iterator = composedRequest.iterator();
3434
while (iterator.hasNext()) {

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

Lines changed: 80 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.apache.commons.io.IOUtils;
1919
import org.apache.commons.lang.NotImplementedException;
2020
import org.apache.http.HttpHost;
21-
import org.apache.http.HttpResponse;
2221
import org.apache.http.HttpStatus;
2322
import org.apache.http.auth.AuthScope;
2423
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -36,7 +35,10 @@
3635
import org.piwigo.remotesync.api.exception.ClientServerException;
3736
import org.piwigo.remotesync.api.exception.ServerException;
3837
import org.piwigo.remotesync.api.request.AbstractRequest;
38+
import org.piwigo.remotesync.api.request.ComposedRequest;
39+
import org.piwigo.remotesync.api.request.IChunkable;
3940
import org.piwigo.remotesync.api.response.BasicResponse;
41+
import org.piwigo.remotesync.api.response.ComposedResponse;
4042
import org.piwigo.remotesync.api.response.ServerResponse;
4143
import org.piwigo.remotesync.api.xml.PersisterFactory;
4244
import org.slf4j.Logger;
@@ -48,8 +50,6 @@
4850
* System.out.println(response.getStatusLine().getStatusCode());
4951
* System.out.println(response.getStatusLine().getReasonPhrase());
5052
* System.out.println(response.getStatusLine().toString());
51-
*
52-
* TODO handle proxy
5353
*/
5454
public class WSClient extends AbstractClient {
5555

@@ -63,34 +63,69 @@ public WSClient(IClientConfiguration clientConfiguration) {
6363
this.clientConfiguration = clientConfiguration;
6464
}
6565

66-
@SuppressWarnings("unchecked")
66+
@Override
67+
public <T extends BasicResponse> T sendRequest(AbstractRequest<T> request) throws ClientServerException {
68+
handleChunkable(request);
69+
return super.sendRequest(request);
70+
}
71+
72+
@Override
73+
public <T extends BasicResponse> ComposedResponse<T> sendRequest(ComposedRequest<T> composedRequest) throws ClientServerException {
74+
handleChunkable(composedRequest);
75+
return super.sendRequest(composedRequest);
76+
}
77+
78+
protected <T extends BasicResponse> void handleChunkable(AbstractRequest<T> request) {
79+
if (request instanceof IChunkable)
80+
((IChunkable) request).setChunkSize(clientConfiguration.getChunkSize());
81+
}
82+
6783
@Override
6884
protected <T extends BasicResponse> T doSendRequest(AbstractRequest<T> request) throws ClientServerException {
6985
checkRequestAuthorization(request);
7086

71-
if (httpClient == null) {
72-
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
87+
String content = getXmlResponse(request);
7388

74-
if (clientConfiguration.getUsesProxy()) {
75-
String proxyUrl = clientConfiguration.getProxyUrl();
76-
int proxyPort = clientConfiguration.getProxyPort();
77-
78-
String proxyUsername = clientConfiguration.getProxyUsername();
79-
String proxyPassword = clientConfiguration.getProxyPassword();
89+
// basic parsing
90+
ServerResponse errorResponse = parseResponse(content, ServerResponse.class, false);
91+
92+
if ("ok".equals(errorResponse.status)) {
93+
// complete parsing
94+
T response = parseResponse(content, request.getReturnType(), true);
95+
response.setXmlContent(content);
96+
return response;
97+
} else if ("fail".equals(errorResponse.status)) {
98+
logger.debug(content);
99+
throw new ServerException(errorResponse.error.toString());
100+
} else {
101+
throw new NotImplementedException();
102+
}
103+
}
80104

81-
if (proxyUsername != null && proxyUsername.length() > 0 && proxyPassword != null && proxyPassword.length() > 0) {
82-
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
83-
credentialsProvider.setCredentials(new AuthScope(proxyUrl, proxyPort), new UsernamePasswordCredentials(proxyUsername, proxyPassword));
84-
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
85-
}
105+
protected <T extends BasicResponse> String getXmlResponse(AbstractRequest<T> request) throws ClientException, ServerException {
106+
CloseableHttpResponse httpResponse = null;
107+
108+
try {
109+
httpResponse = getHttpResponse(request);
86110

87-
HttpHost proxy = new HttpHost(proxyUrl, proxyPort);
88-
requestConfig = RequestConfig.custom().setProxy(proxy).build();
111+
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
112+
throw new ServerException(httpResponse.getStatusLine().getReasonPhrase() + " (code " + httpResponse.getStatusLine().getStatusCode() + ")");
113+
114+
return IOUtils.toString(httpResponse.getEntity().getContent(), "UTF-8");
115+
} catch (Exception e) {
116+
throw new ClientException("Unable to read response content", e);
117+
} finally {
118+
try {
119+
if (httpResponse != null)
120+
httpResponse.close();
121+
} catch (IOException e) {
122+
logger.error("cannot close post", e);
89123
}
90-
httpClient = httpClientBuilder.build();
91124
}
125+
}
92126

93-
CloseableHttpResponse httpResponse = null;
127+
@SuppressWarnings("unchecked")
128+
protected <T extends BasicResponse> CloseableHttpResponse getHttpResponse(AbstractRequest<T> request) throws ClientException {
94129
try {
95130
HttpPost method = new HttpPost(clientConfiguration.getUrl() + "/ws.php");
96131
method.setConfig(requestConfig);
@@ -115,46 +150,39 @@ else if (value instanceof List) {
115150
}
116151
method.setEntity(multipartEntityBuilder.build());
117152

118-
httpResponse = httpClient.execute(method);
153+
return getHttpClient().execute(method);
119154
} catch (Exception e) {
120155
throw new ClientException("Unable to send request", e);
121156
}
157+
}
122158

123-
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
124-
throw new ServerException(httpResponse.getStatusLine().getReasonPhrase() + " (code " + httpResponse.getStatusLine().getStatusCode() + ")");
125-
126-
String content;
127-
try {
128-
content = IOUtils.toString(httpResponse.getEntity().getContent(), "UTF-8");
129-
} catch (Exception e) {
130-
throw new ClientException("Unable to read response content", e);
131-
}
132-
133-
try {
134-
if (httpResponse != null)
135-
httpResponse.close();
136-
} catch (IOException e) {
137-
logger.error("cannot close post", e);
138-
}
139-
140-
// basic parsing
141-
ServerResponse errorResponse = parseResponse(httpResponse, content, ServerResponse.class, false);
159+
protected CloseableHttpClient getHttpClient() {
160+
if (httpClient == null) {
161+
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
142162

143-
if ("fail".equals(errorResponse.status)) {
144-
logger.debug(content);
145-
throw new ServerException(errorResponse.error.toString());
146-
} else if (!"ok".equals(errorResponse.status))
147-
throw new NotImplementedException();
163+
if (clientConfiguration.getUsesProxy()) {
164+
String proxyUrl = clientConfiguration.getProxyUrl();
165+
int proxyPort = clientConfiguration.getProxyPort();
166+
167+
String proxyUsername = clientConfiguration.getProxyUsername();
168+
String proxyPassword = clientConfiguration.getProxyPassword();
148169

149-
// complete parsing
150-
T response = parseResponse(httpResponse, content, request.getReturnType(), true);
151-
response.setHttpStatusCode(httpResponse.getStatusLine().getStatusCode());
152-
response.setXmlContent(content);
170+
if (proxyUsername != null && proxyUsername.length() > 0 && proxyPassword != null && proxyPassword.length() > 0) {
171+
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
172+
credentialsProvider.setCredentials(new AuthScope(proxyUrl, proxyPort), new UsernamePasswordCredentials(proxyUsername, proxyPassword));
173+
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
174+
}
153175

154-
return response;
176+
HttpHost proxy = new HttpHost(proxyUrl, proxyPort);
177+
requestConfig = RequestConfig.custom().setProxy(proxy).build();
178+
}
179+
httpClient = httpClientBuilder.build();
180+
}
181+
182+
return httpClient;
155183
}
156184

157-
private <T extends BasicResponse> T parseResponse(HttpResponse httpResponse, String content, Class<T> type, boolean strict) throws ClientException {
185+
protected <T extends BasicResponse> T parseResponse(String content, Class<T> type, boolean strict) throws ClientException {
158186
try {
159187
return (T) PersisterFactory.createPersister().read(type, content, strict);
160188
} catch (Exception e) {

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.lang.reflect.Field;
1414

1515
import org.kohsuke.args4j.Option;
16+
import org.piwigo.remotesync.api.Constants;
1617
import org.piwigo.remotesync.api.ISyncConfiguration;
1718
import org.piwigo.remotesync.api.conf.SyncConfigurationValidator.Validator;
1819
import org.piwigo.remotesync.api.conf.SyncConfigurationValidator.ValidatorRequired;
@@ -22,10 +23,6 @@
2223
import org.simpleframework.xml.convert.Convert;
2324

2425
public class SyncConfiguration implements ISyncConfiguration {
25-
private static final String DIRECTORY_DEFAULT = ConfigurationUtil.INSTANCE.getUserCurrentDirectory().getAbsolutePath();
26-
27-
private static final String CHUNK_SIZE_DEFAULT = "500";
28-
2926
@Element(required = false)
3027
@Option(name = "-url", usage = "remote gallery url")
3128
@Validator(type = ValidatorType.url, required = ValidatorRequired.yes)
@@ -46,7 +43,7 @@ public class SyncConfiguration implements ISyncConfiguration {
4643
@Element(required = false)
4744
@Option(name = "-dir", usage = "local directory path")
4845
@Validator(type = ValidatorType.dir)
49-
protected String directory = DIRECTORY_DEFAULT;
46+
protected String directory = Constants.DIRECTORY_DEFAULT;
5047

5148
@Element(required = false)
5249
@Option(name = "-proxy", usage = "use proxy")
@@ -76,7 +73,7 @@ public class SyncConfiguration implements ISyncConfiguration {
7673
@Element(required = false)
7774
@Option(name = "-cs", usage = "chunk size (in Kbytes)")
7875
@Validator(type = ValidatorType.integer)
79-
protected String chunkSize = CHUNK_SIZE_DEFAULT;
76+
protected String chunkSize = Constants.CHUNK_SIZE_DEFAULT;
8077

8178
public String getValue(String fieldName) {
8279
try {
@@ -180,7 +177,7 @@ public int getChunkSize() {
180177
try {
181178
return Integer.parseInt(chunkSize);
182179
} catch (NumberFormatException e) {
183-
return Integer.parseInt(CHUNK_SIZE_DEFAULT);
180+
return Integer.parseInt(Constants.CHUNK_SIZE_DEFAULT);
184181
}
185182
}
186183

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2014 Matthieu Helleboid.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the GNU Public License v2.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7+
*
8+
* Contributors:
9+
* Matthieu Helleboid - initial API and implementation
10+
******************************************************************************/
11+
package org.piwigo.remotesync.api.request;
12+
13+
public interface IChunkable {
14+
public void setChunkSize(int chunkSize);
15+
}

0 commit comments

Comments
 (0)