Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,34 @@ try {
}
```

### Image Client 样例

> 以下片断来自项目代码里面的文件:example / cn.jpush.api.examples.ImageExample
* 支持通过URL或者文件来上传图片
```Java
public static void testUploadImageByUrl() throws APIConnectionException, APIRequestException {
ImageClient client = new ImageClient(MASTER_SECRET, APP_KEY);
ImageUrlPayload payload = ImageUrlPayload.newBuilder()
.setImageType(ImageType.LARGE_ICON)
.setImageUrl("http://xxx.com/image/a.jpg")
.build();
ImageUploadResult imageUploadResult = client.uploadImage(payload);
String mediaId = imageUploadResult.getMediaId();
}

public static void testUploadImageByFile() {
ImageClient client = new ImageClient(MASTER_SECRET, APP_KEY);
ImageFilePayload payload = ImageFilePayload.newBuilder()
.setImageType(ImageType.BIG_PICTURE)
// 本地文件路径
.setOppoFileName("/MyDir/a.jpg")
.setXiaomiFileName("/MyDir/a.jpg")
.build();
ImageUploadResult imageUploadResult = client.uploadImage(payload);
String mediaId = imageUploadResult.getMediaId();
}
```

### Weblogic 使用Java SDK

Weblogic在使用jpush-api-java-client时需要注意的一些事项。
Expand Down
56 changes: 56 additions & 0 deletions example/main/java/cn/jpush/api/examples/ImageExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cn.jpush.api.image;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.image.model.ImageFilePayload;
import cn.jpush.api.image.model.ImageType;
import cn.jpush.api.image.model.ImageUploadResult;
import cn.jpush.api.image.model.ImageUrlPayload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImageExample {
protected static final Logger LOG = LoggerFactory.getLogger(ImageExample.class);

// demo App defined in resources/jpush-api.conf
protected static final String APP_KEY = "e4ceeaf7a53ad745dd4728f2";
protected static final String MASTER_SECRET = "1582b986adeaf48ceec1e354";
protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c";
protected static final String GROUP_MASTER_SECRET = "b11314807507e2bcfdeebe2e";

public static final String TITLE = "Test from API example";
public static final String ALERT = "Test from API Example - alert";
public static final String MSG_CONTENT = "Test from API Example - msgContent";
public static final String REGISTRATION_ID = "0900e8d85ef";
public static final String TAG = "tag_api";
public static long sendCount = 0;
private static long sendTotalTime = 0;

public static void main(String[] args) throws APIConnectionException, APIRequestException {
testUploadImageByFile();
testUploadImageByUrl();
}

public static void testUploadImageByUrl() throws APIConnectionException, APIRequestException {
ImageClient client = new ImageClient(MASTER_SECRET, APP_KEY);
ImageUrlPayload payload = ImageUrlPayload.newBuilder()
.setImageType(ImageType.LARGE_ICON)
.setImageUrl("http://xxx.com/image/a.jpg")
.build();
ImageUploadResult imageUploadResult = client.uploadImage(payload);
String mediaId = imageUploadResult.getMediaId();
}

public static void testUploadImageByFile() {
ImageClient client = new ImageClient(MASTER_SECRET, APP_KEY);
ImageFilePayload payload = ImageFilePayload.newBuilder()
.setImageType(ImageType.BIG_PICTURE)
// 本地文件路径
.setOppoFileName("/MyDir/a.jpg")
.setXiaomiFileName("/MyDir/a.jpg")
.build();
ImageUploadResult imageUploadResult = client.uploadImage(payload);
String mediaId = imageUploadResult.getMediaId();
}
}

17 changes: 14 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.4.7</version>
<version>3.4.8</version>
<packaging>jar</packaging>
<url>https://github.com/jpush/jpush-api-java-client</url>
<name>JPush API Java Client</name>
Expand Down Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.1.9</version>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down Expand Up @@ -117,6 +117,18 @@
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -294,7 +306,6 @@

</plugins>
</build>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/jpush/api/file/FileClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public FileUploadResult uploadFile(FileType type, String filename)
String url = _baseUrl + _filesPath + "/" + typeStr;
Map<String, String> fileMap = new HashMap<>();
fileMap.put("filename", filename);
String response = client.formUpload(url, null, fileMap, null);
String response = client.formUploadByPost(url, null, fileMap, null);
LOG.info("uploadFile:{}", response);
return _gson.fromJson(response,
new TypeToken<FileUploadResult>() {
Expand Down
162 changes: 162 additions & 0 deletions src/main/java/cn/jpush/api/image/ImageClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package cn.jpush.api.image;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.ServiceHelper;
import cn.jiguang.common.connection.HttpProxy;
import cn.jiguang.common.connection.IHttpClient;
import cn.jiguang.common.connection.NativeHttpClient;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jiguang.common.resp.ResponseWrapper;
import cn.jiguang.common.utils.Preconditions;
import cn.jiguang.common.utils.StringUtils;
import cn.jpush.api.image.model.ImageFilePayload;
import cn.jpush.api.image.model.ImageSource;
import cn.jpush.api.image.model.ImageUploadResult;
import cn.jpush.api.image.model.ImageUrlPayload;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
* Provide the ability to upload images to the Jiguang server. Only images in JPG, JPEG and PNG format are supported.
*
* @author fuyx
* @version 2020-12-14
*/
public class ImageClient {

protected static final Logger LOG = LoggerFactory.getLogger(ImageClient.class);

private IHttpClient _httpClient;
private String _baseUrl;
private String _imagesPath;
private Gson _gson = new Gson();

public ImageClient(String masterSecret, String appKey) {
this(masterSecret, appKey, null, ClientConfig.getInstance());
}

public ImageClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) {
_baseUrl = (String) conf.get(ClientConfig.PUSH_HOST_NAME);
_imagesPath = (String) conf.get(ClientConfig.V3_IMAGES_PATH);
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
this._httpClient = new NativeHttpClient(authCode, proxy, conf);
}

/**
* Upload image by url. Require at least one non-null url.
*/
public ImageUploadResult uploadImage(ImageUrlPayload imageUrlPayload)
throws APIConnectionException, APIRequestException {
Preconditions.checkArgument(imageUrlPayload.getImageType() != null, "Image type should not be null");
checkImageUrlPayload(imageUrlPayload);
NativeHttpClient client = (NativeHttpClient) _httpClient;
String url = _baseUrl + _imagesPath + "/" + ImageSource.URL.value();
JsonElement jsonElement = imageUrlPayload.toJSON();
String content = _gson.toJson(jsonElement);
ResponseWrapper responseWrapper = client.sendPost(url, content);
if (responseWrapper.responseCode != 200) {
LOG.error("upload image failed: {}", responseWrapper);
}
ImageUploadResult imageUploadResult = _gson.fromJson(responseWrapper.responseContent, ImageUploadResult.class);

LOG.info("upload image result:{}", imageUploadResult);
return imageUploadResult;
}

/**
* Upload image by file. Require at least 1 non-null fileName. Currently only support Xiaomi and OPPO
*/
public ImageUploadResult uploadImage(ImageFilePayload imageFilePayload) {
Preconditions.checkArgument(imageFilePayload.getImageType() != null, "Image type should not be null");
checkImageFilePayload(imageFilePayload);
NativeHttpClient client = (NativeHttpClient) _httpClient;
String url = _baseUrl + _imagesPath + "/" + ImageSource.FILE.value();

Map<String, String> textMap = new HashMap<>();
textMap.put("image_type", String.valueOf(imageFilePayload.getImageType().value()));

Map<String, String> fileMap = imageFilePayload.toFileMap();
LOG.debug("upload fileMap: {}", fileMap);
String response = client.formUploadByPost(url, textMap, fileMap, null);
LOG.debug("upload image result: {}", response);
ImageUploadResult imageUploadResult;
try {
imageUploadResult = _gson.fromJson(response, ImageUploadResult.class);
} catch (JsonSyntaxException e) {
LOG.error("could not parse response: {}", response);
throw new IllegalStateException("could not parse response", e);
}
LOG.info("upload image result:{}", imageUploadResult);
return imageUploadResult;
}

/**
* Modify image by url. Require at least one non-null url.
*/
public ImageUploadResult modifyImage(String mediaId, ImageUrlPayload imageUrlPayload)
throws APIConnectionException, APIRequestException {
Preconditions.checkArgument(StringUtils.isNotEmpty(mediaId), "mediaId should not be empty");
checkImageUrlPayload(imageUrlPayload);
NativeHttpClient client = (NativeHttpClient) _httpClient;
String url = _baseUrl + _imagesPath + "/" + ImageSource.URL.value() + "/" + mediaId;
JsonElement jsonElement = imageUrlPayload.toJSON();
String content = _gson.toJson(jsonElement);
ResponseWrapper responseWrapper = client.sendPut(url, content);
if (responseWrapper.responseCode != 200) {
LOG.error("upload image failed: {}", responseWrapper);
}
ImageUploadResult imageUploadResult = _gson.fromJson(responseWrapper.responseContent, ImageUploadResult.class);

LOG.info("upload image result:{}", imageUploadResult);
return imageUploadResult;
}

/**
* Modify image by file. Require at least 1 non-null fileName. Currently only support Xiaomi and OPPO
*/
public ImageUploadResult modifyImage(String mediaId, ImageFilePayload imageFilePayload) {
Preconditions.checkArgument(StringUtils.isNotEmpty(mediaId), "mediaId should not be empty");
checkImageFilePayload(imageFilePayload);
NativeHttpClient client = (NativeHttpClient) _httpClient;
String url = _baseUrl + _imagesPath + "/" + ImageSource.FILE.value() + "/" + mediaId;

Map<String, String> fileMap = imageFilePayload.toFileMap();
LOG.debug("upload image fileMap: {}", fileMap);
String response = client.formUploadByPut(url, null, fileMap, null);
LOG.debug("upload image result: {}", response);
ImageUploadResult imageUploadResult;
try {
imageUploadResult = _gson.fromJson(response, ImageUploadResult.class);
} catch (JsonSyntaxException e) {
LOG.error("could not parse response: {}", response);
throw new IllegalStateException("could not parse response", e);
}
LOG.info("upload image result:{}", imageUploadResult);
return imageUploadResult;
}

private void checkImageUrlPayload(ImageUrlPayload imageUrlPayload) {
boolean anyUrlNotEmpty = StringUtils.isNotEmpty(imageUrlPayload.getImageUrl())
|| StringUtils.isNotEmpty(imageUrlPayload.getFcmImageUrl())
|| StringUtils.isNotEmpty(imageUrlPayload.getHuaweiImageUrl())
|| StringUtils.isNotEmpty(imageUrlPayload.getOppoImageUrl())
|| StringUtils.isNotEmpty(imageUrlPayload.getXiaomiImageUrl())
|| StringUtils.isNotEmpty(imageUrlPayload.getJiguangImageUrl()) ;
Preconditions.checkArgument(anyUrlNotEmpty, "Require at least 1 non-empty url");
}

private void checkImageFilePayload(ImageFilePayload imageFilePayload) {
boolean anyFileNotEmpty = StringUtils.isNotEmpty(imageFilePayload.getOppoFileName() )
|| StringUtils.isNotEmpty(imageFilePayload.getXiaomiFileName() );
Preconditions.checkArgument(anyFileNotEmpty, "Require at least 1 non-empty fileName. Currently only support Xiaomi and OPPO");
}


}
Loading