Java tutorial
/* * MIT License * * Copyright (c) 2016 Warren Fan * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package me.ixfan.wechatkit.material; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import me.ixfan.wechatkit.WeChatKitComponent; import me.ixfan.wechatkit.common.WeChatApiResult; import me.ixfan.wechatkit.common.WeChatConstants; import me.ixfan.wechatkit.material.model.ArticleForUpload; import me.ixfan.wechatkit.token.TokenManager; import me.ixfan.wechatkit.util.HttpClientUtil; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * ??? * * @author Warren Fan */ public class MaterialManager extends WeChatKitComponent { public MaterialManager(TokenManager tokenManager) { super(tokenManager); } /** * ?? * * @param filename ?? * @param dataBytes ? * @return ?? {@link WeChatApiResult#getMediaId()} {@link WeChatApiResult#getType()} * ? <code>media_id</code> <code>type</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadTemporaryImage(String filename, byte[] dataBytes) { return uploadTemporaryMaterial(MediaType.IMAGE, filename, dataBytes, null); } /** * ?? * * @param image * @return ?? {@link WeChatApiResult#getMediaId()} {@link WeChatApiResult#getType()} * ? <code>media_id</code> <code>type</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadTemporaryImage(File image) { return uploadTemporaryMaterial(MediaType.IMAGE, image.getName(), null, image); } /** * ?? * * @param filename ?? * @param dataBytes ? * @return ?? {@link WeChatApiResult#getMediaId()} {@link WeChatApiResult#getType()} * ? <code>media_id</code> <code>type</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadTemporaryVideo(String filename, byte[] dataBytes) { return uploadTemporaryMaterial(MediaType.VIDEO, filename, dataBytes, null); } /** * ?? * * @param video * @return ?? {@link WeChatApiResult#getMediaId()} {@link WeChatApiResult#getType()} * ? <code>media_id</code> <code>type</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadTemporaryVideo(File video) { return uploadTemporaryMaterial(MediaType.VIDEO, video.getName(), null, video); } /** * ?? * * @param filename ?? * @param dataBytes ? * @return ?? {@link WeChatApiResult#getMediaId()} {@link WeChatApiResult#getType()} * ??? <code>media_id</code> <code>type</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadTemporaryVoice(String filename, byte[] dataBytes) { return uploadTemporaryMaterial(MediaType.VOICE, filename, dataBytes, null); } /** * ?? * * @param voice * @return ?? {@link WeChatApiResult#getMediaId()} {@link WeChatApiResult#getType()} * ??? <code>media_id</code> <code>type</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadTemporaryVoice(File voice) { return uploadTemporaryMaterial(MediaType.VOICE, voice.getName(), null, voice); } /** * * * @param filename ?? * @param dataBytes ? * @return ?? {@link WeChatApiResult#getMediaId()} {@link WeChatApiResult#getType()} * ? <code>media_id</code> <code>type</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadTemporaryThumb(String filename, byte[] dataBytes) { return uploadTemporaryMaterial(MediaType.THUMB, filename, dataBytes, null); } /** * * * @param thumb * @return ?? {@link WeChatApiResult#getMediaId()} {@link WeChatApiResult#getType()} * ? <code>media_id</code> <code>type</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadTemporaryThumb(File thumb) { return uploadTemporaryMaterial(MediaType.THUMB, thumb.getName(), null, thumb); } /** * ?? * * @param articles ?? * @return ?? {@link WeChatApiResult#getMediaId()} ??? <code>media_id</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadPermanentArticles(ArticleForUpload... articles) { Map<String, ArticleForUpload[]> data = new HashMap<>(); data.put("articles", articles); try { Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .create(); JsonObject jsonResponse = HttpClientUtil.sendPostRequestWithJsonBody( WeChatConstants.WECHAT_POST_MATERIAL_UPLOAD_PERMANENT_NEWS.replace("${ACCESS_TOKEN}", super.getTokenManager().getAccessToken()), gson.toJson(data)); return WeChatApiResult.instanceOf(jsonResponse); } catch (IOException e) { throw new RuntimeException(e); } } /** * ?? * * @param articles ?? * @return ?? {@link WeChatApiResult#getMediaId()} ??? <code>media_id</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadArticlesForMassMessage(ArticleForUpload... articles) { Map<String, ArticleForUpload[]> data = new HashMap<>(); data.put("articles", articles); try { Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .create(); JsonObject jsonResponse = HttpClientUtil.sendPostRequestWithJsonBody( WeChatConstants.WECHAT_POST_MESSAGE_UPLOAD_NEWS.replace("${ACCESS_TOKEN}", super.getTokenManager().getAccessToken()), gson.toJson(data)); return WeChatApiResult.instanceOf(jsonResponse); } catch (IOException e) { throw new RuntimeException(e); } } /** * ? * * @param filename ?? * @param dataBytes ? * @return ?? {@link WeChatApiResult#getUrl()} ? <code>URL</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadImageForArticle(String filename, byte[] dataBytes) { return uploadImageForArticle(filename, dataBytes, null); } /** * ? * * @param image * @return ?? {@link WeChatApiResult#getUrl()} ? <code>URL</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadImageForArticle(File image) { return uploadImageForArticle(image.getName(), null, image); } /** * ?? * * @param filename?? * @param dataBytes? * @return ?? {@link WeChatApiResult#getMediaId()} {@link WeChatApiResult#getUrl()} * ? <code>media_id</code> <code>URL</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? * */ public WeChatApiResult uploadPermanentImage(String filename, byte[] dataBytes) { return uploadPermanentMaterial(MediaType.IMAGE, filename, dataBytes, null, null, null); } /** * ?? * * @param image * @return ?? {@link WeChatApiResult#getMediaId()} {@link WeChatApiResult#getUrl()} * ? <code>media_id</code> <code>URL</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? * */ public WeChatApiResult uploadPermanentImage(File image) { return uploadPermanentMaterial(MediaType.IMAGE, image.getName(), null, image, null, null); } /** * ?? * * @param filename ???? * @param dataBytes ? * @return ?? {@link WeChatApiResult#getMediaId()} ??? <code>media_id</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadPermanentVoice(String filename, byte[] dataBytes) { return uploadPermanentMaterial(MediaType.VOICE, filename, dataBytes, null, null, null); } /** * ?? * * @param voice * @return ?? {@link WeChatApiResult#getMediaId()} ??? <code>media_id</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadPermanentVoice(File voice) { return uploadPermanentMaterial(MediaType.VOICE, voice.getName(), null, voice, null, null); } /** * ?? * * @param filename ?? * @param title ?? * @param introduction ?? * @param dataBytes ? * @return ?? {@link WeChatApiResult#getMediaId()} ??? <code>media_id</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadPermanentVideo(String filename, String title, String introduction, byte[] dataBytes) { return uploadPermanentMaterial(MediaType.VIDEO, filename, dataBytes, null, title, introduction); } /** * ?? * * @param title ?? * @param introduction ?? * @param video * @return ?? {@link WeChatApiResult#getMediaId()} ??? <code>media_id</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadPermanentVideo(String title, String introduction, File video) { return uploadPermanentMaterial(MediaType.VIDEO, video.getName(), null, video, title, introduction); } /** * ?? * * @param dataBytes * @return ?? {@link WeChatApiResult#getMediaId()} ? <code>media_id</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadPermanentThumb(String filename, byte[] dataBytes) { return uploadPermanentMaterial(MediaType.THUMB, filename, dataBytes, null, null, null); } /** * ?? * * @param thumb * @return ?? {@link WeChatApiResult#getMediaId()} ? <code>media_id</code> * ? {@link WeChatApiResult#getErrcode()} {@link WeChatApiResult#getErrmsg()} * ?? */ public WeChatApiResult uploadPermanentThumb(File thumb) { return uploadPermanentMaterial(MediaType.THUMB, thumb.getName(), null, thumb, null, null); } /** * ??, ?????{@link #uploadPermanentArticles(ArticleForUpload...)} * ?????????, ?? <code>title</code> <code>introduction</code> * * @param mediaType ? * @param filename ?? * @param dataBytes ? * @param file * @param title ??, ??? * @param introduction ??, ??? * @return {@link WeChatApiResult} */ private WeChatApiResult uploadPermanentMaterial(MediaType mediaType, String filename, byte[] dataBytes, File file, String title, String introduction) { Map<String, String> textPart = new HashMap<>(); String mimeType; switch (mediaType) { case IMAGE: case THUMB: mimeType = "image/*"; break; case VIDEO: mimeType = "video/*"; textPart.put("description", String.format("{\"title\":%s, \"introduction\":%s}", title, introduction)); break; case VOICE: mimeType = "audio/*"; break; default: mimeType = "*/*"; } Map<String, HttpClientUtil.MultipartInput> filePart = new HashMap<>(); HttpClientUtil.MultipartInput multipart; multipart = null == file ? new HttpClientUtil.MultipartInput(filename, mimeType, dataBytes) : new HttpClientUtil.MultipartInput(filename, mimeType, file); filePart.put("media", multipart); try { JsonObject jsonResponse = HttpClientUtil .sendMultipartRequestAndGetJsonResponse(WeChatConstants.WECHAT_POST_MATERIAL_UPLOAD_PERMANENT .replace("${ACCESS_TOKEN}", super.tokenManager.getAccessToken()) .replace("${TYPE}", mediaType.stringValue()), textPart, filePart); return WeChatApiResult.instanceOf(jsonResponse); } catch (IOException e) { throw new RuntimeException(e); } } /** * ??, ??? * * @param mediaType ? * @param filename ?? * @param dataBytes ? * @return {@link WeChatApiResult} */ private WeChatApiResult uploadTemporaryMaterial(MediaType mediaType, String filename, byte[] dataBytes, File file) throws RuntimeException { String mimeType; switch (mediaType) { case IMAGE: case THUMB: mimeType = "image/*"; break; case VIDEO: mimeType = "video/*"; break; case VOICE: mimeType = "audio/*"; break; default: mimeType = "*/*"; } Map<String, HttpClientUtil.MultipartInput> filePart = new HashMap<>(); HttpClientUtil.MultipartInput multipart; multipart = null == dataBytes ? new HttpClientUtil.MultipartInput(filename, mimeType, file) : new HttpClientUtil.MultipartInput(filename, mimeType, dataBytes); filePart.put("media", multipart); try { JsonObject jsonResponse = HttpClientUtil .sendMultipartRequestAndGetJsonResponse(WeChatConstants.WECHAT_POST_MATERIAL_UPLOAD_TEMP .replace("${ACCESS_TOKEN}", super.tokenManager.getAccessToken()) .replace("${TYPE}", mediaType.stringValue()), null, filePart); return WeChatApiResult.instanceOf(jsonResponse); } catch (IOException e) { throw new RuntimeException(e); } } /** * ? * * @param filename ?? * @param image * @param dataBytes ? * @return */ private WeChatApiResult uploadImageForArticle(String filename, byte[] dataBytes, File image) { Map<String, HttpClientUtil.MultipartInput> filePart = new HashMap<>(); HttpClientUtil.MultipartInput multipart; multipart = null == image ? new HttpClientUtil.MultipartInput(filename, "image/*", dataBytes) : new HttpClientUtil.MultipartInput(filename, "image/*", image); filePart.put("media", multipart); try { JsonObject jsonResponse = HttpClientUtil .sendMultipartRequestAndGetJsonResponse(WeChatConstants.WECHAT_POST_MATERIAL_UPLOAD_NEWS_PIC .replace("${ACCESS_TOKEN}", super.tokenManager.getAccessToken()), null, filePart); return WeChatApiResult.instanceOf(jsonResponse); } catch (IOException e) { throw new RuntimeException(e); } } }