Skip to content

Commit

Permalink
Merge pull request #8 from uploadcare/issue-1-threading
Browse files Browse the repository at this point in the history
Issue 1 threading
  • Loading branch information
dmitry-mukhin committed Nov 9, 2015
2 parents 85474b8 + 7348054 commit 9fa49f0
Show file tree
Hide file tree
Showing 18 changed files with 475 additions and 122 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
1. Alexander Tchernin @alchernin
2. Dimitry Solovyov @dimituri
3. Yervand Aghababyan @dnavre
4. Raphael Gilyazitdinov @raphaelnew
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# History

## 3.0
- Support Uploadcare REST API v0.4
- Fixed Threading problem with HttpClient.
- Updated some of deprecated classes.
- Added Image Operations to CdnPathBuilder: blur, sharp, preview, format, quality.

## 2.0
- Support Uploadcare REST API v0.3
- Improve error handling
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This is a Java library for Uploadcare.

Supported features:

- Complete file and project API v0.3
- Complete file and project API v0.4
- Paginated resources fetched as `List<T>`
- CDN path builder
- File uploads from disk, byte array, and URL
Expand All @@ -23,7 +23,7 @@ of the project's pom.xml file:
<dependency>
<groupId>com.uploadcare</groupId>
<artifactId>uploadcare</artifactId>
<version>2.0</version>
<version>3.0</version>
</dependency>
```

Expand Down
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.uploadcare</groupId>
<artifactId>uploadcare</artifactId>
<version>2.0</version>
<version>3.0</version>
<packaging>jar</packaging>

<parent>
Expand Down Expand Up @@ -58,18 +58,23 @@
<id>dnavre</id>
<email>[email protected]</email>
</developer>
<developer>
<name>Raphael Gilyazitdinov</name>
<id>raphaelnew</id>
<email>[email protected]</email>
</developer>
</developers>

<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.2</version>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.2.2</version>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
61 changes: 40 additions & 21 deletions src/main/java/com/uploadcare/api/Client.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package com.uploadcare.api;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.uploadcare.data.CopyFileData;
import com.uploadcare.data.FileData;
import com.uploadcare.data.ProjectData;
import com.uploadcare.exceptions.UploadcareApiException;
import com.uploadcare.urls.Urls;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.uploadcare.data.CopyFileData;
import com.uploadcare.data.FileData;
import com.uploadcare.data.ProjectData;
import com.uploadcare.urls.Urls;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
* Uploadcare API client.
Expand All @@ -39,7 +39,7 @@ public class Client {
private final String privateKey;
private final boolean simpleAuth;

private final HttpClient httpClient;
private final CloseableHttpClient httpClient;
private final ObjectMapper objectMapper;
private final RequestHelperProvider requestHelperProvider;

Expand Down Expand Up @@ -77,7 +77,12 @@ public Client(
objectMapper = null;
} else {
this.requestHelperProvider = new DefaultRequestHelperProvider();
httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);
httpClient = HttpClients.custom()
.setConnectionManager(cm)
.build();
objectMapper = new ObjectMapper();
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Expand Down Expand Up @@ -124,7 +129,7 @@ public boolean isSimpleAuth() {
return simpleAuth;
}

HttpClient getHttpClient() {
CloseableHttpClient getHttpClient() {
return httpClient;
}

Expand Down Expand Up @@ -196,10 +201,24 @@ public void saveFile(String fileId) {
}

/**
* Closes client.
*
* @param fileId Resource UUID
* Ensures that all connections kept alive by the manager get closed and system resources
* allocated by those connections are released.
*/
public void close() {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
throw new UploadcareApiException("Error during closing CloseableHttpClient", e);
}
}
}

/**
* @param fileId Resource UUID
* @param storage Target storage name
*
* @return An object containing the results of the copy request
*/
public CopyFileData copyFile(String fileId, String storage) {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/uploadcare/api/FilesQueryBuilder.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.uploadcare.api;

import com.uploadcare.data.FilePageData;
import com.uploadcare.urls.FilesFromParameter;
import com.uploadcare.urls.FilesRemovedParameter;
import com.uploadcare.urls.FilesStoredParameter;
import com.uploadcare.urls.FilesToParameter;
import com.uploadcare.urls.UrlParameter;
import com.uploadcare.urls.Urls;

import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
Expand Down Expand Up @@ -49,6 +52,26 @@ public FilesQueryBuilder stored(boolean stored) {
return this;
}

/**
* Adds a filter for datetime from objects will be returned.
*
* @param from A uploading datetime from which objects will be returned.
*/
public FilesQueryBuilder from(Date from) {
parameters.add(new FilesFromParameter(from));
return this;
}

/**
* Adds a filter for datetime to which objects will be returned.
*
* @param to A uploading datetime to which objects will be returned.
*/
public FilesQueryBuilder to(Date to) {
parameters.add(new FilesToParameter(to));
return this;
}

public Iterable<File> asIterable() {
URI url = Urls.apiFiles();
RequestHelper requestHelper = client.getRequestHelper();
Expand Down
Loading

0 comments on commit 9fa49f0

Please sign in to comment.