MobileTokenCloudClient.java
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.ndkey.im.product.mobiletoken; import com.fasterxml.jackson.databind.ObjectMapper; import com.ndkey.security.SymmetricTicket; import com.ndkey.web.ResponseData; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.impl.client.CloseableHttpClient; import org.springframework.web.multipart.MultipartFile; /** * * @author zxf */ public class MobileTokenCloudClient { private final String BASE_URL = "http://mtc.ndkey.com/mtc/rest/app/1"; private final static ObjectMapper _objectMapper = new ObjectMapper(); private MobileTokenCloudSecurityService mobileTokenCloudSecurityService; public MobileTokenCloudClient() { } public ResponseData uploadLogo(String serviceId, MultipartFile file) throws IOException { try { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(BASE_URL + "/logo/update"); SymmetricTicket ticket = mobileTokenCloudSecurityService.generateTicket(); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); File sigleFile = new File(file.getOriginalFilename()); FileUtils.copyInputStreamToFile(file.getInputStream(), sigleFile); builder.addTextBody("serviceId", serviceId); builder.addPart("file", new FileBody(sigleFile)); builder.addTextBody("signature", ticket.getSignature()); builder.addTextBody("timestamp", Long.toString(ticket.getTimestamp())); builder.addTextBody("nonce", ticket.getNonce()); HttpEntity httpEntity = builder.build(); httpPost.setEntity(httpEntity); HttpResponse response = httpClient.execute(httpPost); return _objectMapper.readValue(response.getEntity().getContent(), ResponseData.class); } catch (Exception e) { return ResponseData.errorData(e.getLocalizedMessage()); } } public ResponseData uploadApp(String platform, String version, MultipartFile file) throws IOException { try { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(BASE_URL + "/update"); SymmetricTicket ticket = mobileTokenCloudSecurityService.generateTicket(); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); File sigleFile = new File(file.getOriginalFilename()); FileUtils.copyInputStreamToFile(file.getInputStream(), sigleFile); builder.addTextBody("platform", platform); builder.addTextBody("version", version); builder.addPart("file", new FileBody(sigleFile)); builder.addTextBody("signature", ticket.getSignature()); builder.addTextBody("timestamp", Long.toString(ticket.getTimestamp())); builder.addTextBody("nonce", ticket.getNonce()); HttpEntity httpEntity = builder.build(); httpPost.setEntity(httpEntity); HttpResponse response = httpClient.execute(httpPost); return _objectMapper.readValue(response.getEntity().getContent(), ResponseData.class); } catch (Exception e) { return ResponseData.errorData(e.getLocalizedMessage()); } } public void setMobileTokenCloudSecurityService(MobileTokenCloudSecurityService mobileTokenCloudSecurityService) { this.mobileTokenCloudSecurityService = mobileTokenCloudSecurityService; } }