Commit 3ad3070c authored by renjie's avatar renjie

ip购买,查询信息更新

parent 82e3b5a0
......@@ -88,6 +88,11 @@
<artifactId>ehcache</artifactId>
<version>2.10.5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
......
package com.edgec.browserbackend.account.config;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MongoConfig {
@Value("${spring.data.mongodb.uri}")
String dbUri;
@Bean
public GridFSBucket getGridFSBucket(MongoClient mongoClient){
String db = dbUri.substring(dbUri.lastIndexOf("/") + 1);
MongoDatabase database = mongoClient.getDatabase(db);
GridFSBucket bucket = GridFSBuckets.create(database);
return bucket;
}
}
......@@ -24,6 +24,10 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.security.Principal;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
......@@ -396,7 +400,7 @@ public class AccountController {
return accountService.deletePreOrder(username);
}
@RequestMapping(path = "/authorize", method = RequestMethod.POST)
@RequestMapping(path = "/authorize/add", method = RequestMethod.POST)
public ResultDto companyAuthorize(Principal principal,
@RequestParam(value = "type", defaultValue = "0") int type,
@RequestParam(value = "companyName") String companyName,
......@@ -439,4 +443,53 @@ public class AccountController {
return resultDto;
}
@RequestMapping(path = "/authorize/details")
public ResultDto getAuthorizeDetails(Principal principal) {
ResultDto resultDto = new ResultDto();
try {
resultDto.setData(accountService.getAuthorizeDetails(principal.getName()));
resultDto.setStatus(0);
}catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
}
return resultDto;
}
@RequestMapping(path = "/authorize/files")
public ResultDto getAuthorizeFiles(Principal principal, HttpServletResponse response) {
ResultDto resultDto = new ResultDto();
try {
File tempFile = accountService.getAuthorizeFiles(principal.getName());
try(OutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(tempFile)){
response.setHeader("content-type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("压缩包.zip", "UTF-8"));
int len =0;
byte[] bt = new byte[5*1024];
while((len = fis.read(bt)) != -1) {
os.write(bt,0,len);
}
}catch(Exception e) {
e.printStackTrace();
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}finally {
tempFile.deleteOnExit();
}
accountService.getAuthorizeFiles(principal.getName());
resultDto.setStatus(0);
}catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
}
return resultDto;
}
}
package com.edgec.browserbackend.account.dto;
import com.edgec.browserbackend.account.domain.CompanyAuthorize;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.Id;
import org.springframework.web.multipart.MultipartFile;
@JsonIgnoreProperties(ignoreUnknown = true)
public class CompanyAuthorizeDto {
private String username;
private int type; //0 -- 企业, 1 -- 个体工商户
......
......@@ -5,5 +5,5 @@ import com.edgec.browserbackend.account.dto.CompanyAuthorizeDto;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface CompanyAuthorizeRepository extends MongoRepository<CompanyAuthorize, String>, CompanyAuthorizeRepositoryCustom {
CompanyAuthorize findByUsername(String username);
}
package com.edgec.browserbackend.account.repository;
import com.mongodb.client.gridfs.model.GridFSFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
public interface CompanyAuthorizeRepositoryCustom {
String saveFile(String collectionName, MultipartFile file, String filename);
String saveFile(MultipartFile file, String filename);
void deleteFile(String fileId);
File getFile(String fileId);
}
......@@ -4,14 +4,21 @@ import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import com.mongodb.DB;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.web.multipart.MultipartFile;
......@@ -31,24 +38,51 @@ public class CompanyAuthorizeRepositoryCustomImpl implements CompanyAuthorizeRep
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
GridFSBucket gridFSBucket;
@Override
public String saveFile(String collectionName, MultipartFile file, String filename) {
public String saveFile(MultipartFile file, String filename) {
try {
GridFS gridFS = new GridFS((DB) mongoTemplate.getDb());
InputStream in = file.getInputStream();
ObjectId id = gridFsTemplate.store(in, filename, file.getContentType());
return id.toString();
} catch (Exception e) {
logger.error("存储文件时发生错误!!!", e);
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}
}
GridFSInputFile gridFSInputFile = gridFS.createFile(in);
gridFSInputFile.setFilename(filename);
gridFSInputFile.setContentType(file.getContentType());
gridFSInputFile.save();
@Override
public void deleteFile(String fileId) {
Query query = Query.query(Criteria.where("_id").is(fileId));
// 查询单个文件
GridFSFile gfsfile = gridFsTemplate.findOne(query);
if (gfsfile == null) {
return;
}
gridFsTemplate.delete(query);
}
return (String) gridFSInputFile.getId();
@Override
public File getFile(String fileId) {
Query query = Query.query(Criteria.where("_id").is(fileId));
// 查询单个文件
GridFSFile gridFSFile = gridFsTemplate.findOne(query);
if (gridFSFile == null)
return null;
try {
GridFSDownloadStream gridFSDownloadStream =
gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
GridFsResource gridFsResource = new GridFsResource(gridFSFile,gridFSDownloadStream);
return gridFsResource.getFile();
} catch (Exception e) {
logger.error("存储文件时发生错误!!!", e);
logger.error(e.getMessage());
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}
}
}
......@@ -2,9 +2,13 @@ package com.edgec.browserbackend.account.service;
import com.edgec.browserbackend.account.domain.*;
import com.edgec.browserbackend.account.dto.*;
import com.mongodb.client.gridfs.model.GridFSFile;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
......@@ -102,4 +106,8 @@ public interface AccountService {
void authorizeCompany(String username, CompanyAuthorizeDto companyAuthorizeDto);
CompanyAuthorizeDto getAuthorizeDetails(String username);
File getAuthorizeFiles(String username);
}
......@@ -13,6 +13,7 @@ import com.edgec.browserbackend.account.utils.AccountServicePool;
import com.edgec.browserbackend.auth.exception.AuthErrorCode;
import com.edgec.browserbackend.auth.repository.UserRepository;
import com.edgec.browserbackend.auth.service.UserAuthService;
import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode;
import com.edgec.browserbackend.browser.domain.IpSummary;
import com.edgec.browserbackend.browser.domain.ShopSummary;
import com.edgec.browserbackend.browser.dto.PageInfo;
......@@ -22,12 +23,17 @@ import com.edgec.browserbackend.browser.service.IpResourceService;
import com.edgec.browserbackend.browser.service.ShopService;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import com.edgec.browserbackend.common.commons.utils.CommonStringUtils;
import com.edgec.browserbackend.common.utils.FileUtil;
import com.mongodb.DB;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSInputFile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -43,12 +49,11 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.StringUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.*;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.YearMonth;
......@@ -1015,25 +1020,74 @@ public class AccountServiceImpl implements AccountService {
@Override
public void authorizeCompany(String username, CompanyAuthorizeDto companyAuthorizeDto) {
Account account = repository.findByName(username);
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
String companyLicenseName = "companyLicense-" + username;
String companyLicenseId = companyAuthorizeRepository.saveFile("companylicense", companyAuthorizeDto.getCompanyLicense(), companyLicenseName);
String coporationLicenseName = "coporationLicense-" + username;
String coporationLicenseId = companyAuthorizeRepository.saveFile("coporationlicense", companyAuthorizeDto.getCoporationLicense(), companyLicenseName);
String companyLicenseId = null;
String coporationLicenseId = null;
String agencyId = null;
if (companyAuthorizeDto.getAgency() != null) {
String agencyName = "agencyLicense-" + username;
agencyId = companyAuthorizeRepository.saveFile("coporationlicense", companyAuthorizeDto.getAgency(), agencyName);
CompanyAuthorize existing = companyAuthorizeRepository.findByUsername(username);
if (existing != null) {
throw new ClientRequestException(BrowserErrorCode.COMPANYAUTHORIZEEXIST);
}
CompanyAuthorize companyAuthorize = new CompanyAuthorize(companyAuthorizeDto);
companyAuthorize.setUsername(username);
companyAuthorize.setCompanyLicenseId(companyLicenseId);
companyAuthorize.setCoporationLicenseId(coporationLicenseId);
if (agencyId != null)
companyAuthorize.setAgencyId(agencyId);
companyAuthorizeRepository.save(companyAuthorize);
try {
Account account = repository.findByName(username);
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
String companyLicenseName = "companyLicense-" + username;
companyLicenseId = companyAuthorizeRepository.saveFile(companyAuthorizeDto.getCompanyLicense(), companyLicenseName);
String coporationLicenseName = "coporationLicense-" + username;
coporationLicenseId = companyAuthorizeRepository.saveFile(companyAuthorizeDto.getCoporationLicense(), coporationLicenseName);
if (companyAuthorizeDto.getAgency() != null) {
String agencyName = "agencyLicense-" + username;
agencyId = companyAuthorizeRepository.saveFile(companyAuthorizeDto.getAgency(), agencyName);
}
CompanyAuthorize companyAuthorize = new CompanyAuthorize(companyAuthorizeDto);
companyAuthorize.setUsername(username);
companyAuthorize.setCompanyLicenseId(companyLicenseId);
companyAuthorize.setCoporationLicenseId(coporationLicenseId);
if (agencyId != null)
companyAuthorize.setAgencyId(agencyId);
companyAuthorizeRepository.save(companyAuthorize);
} catch (Exception e) {
if (agencyId != null)
companyAuthorizeRepository.deleteFile(agencyId);
if (coporationLicenseId != null)
companyAuthorizeRepository.deleteFile(coporationLicenseId);
if (companyLicenseId != null)
companyAuthorizeRepository.deleteFile(companyLicenseId);
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}
}
@Override
public CompanyAuthorizeDto getAuthorizeDetails(String username) {
CompanyAuthorize companyAuthorize = companyAuthorizeRepository.findByUsername(username);
if (companyAuthorize == null)
return null;
CompanyAuthorizeDto companyAuthorizeDto = new CompanyAuthorizeDto(companyAuthorize);
return companyAuthorizeDto;
}
@Override
public File getAuthorizeFiles(String username) {
CompanyAuthorize companyAuthorize = companyAuthorizeRepository.findByUsername(username);
if (companyAuthorize == null)
return null;
File companyLicense = companyAuthorizeRepository.getFile(companyAuthorize.getCompanyLicenseId());
log.error("3");
File coporationLicense = companyAuthorizeRepository.getFile(companyAuthorize.getCoporationLicenseId());
File agencyLicense = null;
if (companyAuthorize.getAgencyId() != null)
agencyLicense = companyAuthorizeRepository.getFile(companyAuthorize.getAgencyId());
try {
File tempFile = File.createTempFile("tempFile", "zip");
List<File> files = Arrays.asList(companyLicense, coporationLicense);
if (agencyLicense != null)
files.add(agencyLicense);
tempFile = FileUtil.putBatchFilesInZip(files, tempFile);
return tempFile;
} catch (Exception e) {
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}
}
}
......@@ -9,15 +9,18 @@ public enum BrowserErrorCode implements ErrorCode {
/* client errors */
UNKNOWN(ErrorCode.COMMON_UNKNOWN,"unknown"),
INFORMATIONNOTCOMPELETE(BROWSER_BASE+100, "The information about shop do not complete"),
SHOPNOTEXIST(BROWSER_BASE+101, "The shop do not exist"),
INFORMATIONNOTCOMPELETE(BROWSER_BASE+100, "The information about shop does not complete"),
SHOPNOTEXIST(BROWSER_BASE+101, "The shop does not exist"),
IPNOTEXIST(BROWSER_BASE+201, "The ip do not exist"),
IPNOTBINDTOSHOP(BROWSER_BASE+202, "The ip do not bind this shop."),
IPNOTBINDTOSHOP(BROWSER_BASE+202, "The ip does not bind this shop."),
GROUPNOTEXIST(BROWSER_BASE+301, "The group do not exist"),
GROUPNOTEXIST(BROWSER_BASE+301, "The group does not exist"),
IPTRANSACTIONNOTEXIST(BROWSER_BASE+401, "The ipTransaction do not exist");
IPTRANSACTIONNOTEXIST(BROWSER_BASE+401, "The ipTransaction does not exist"),
COMPANYAUTHORIZEEXIST(BROWSER_BASE+501, "The authority exists"),
COMPANYAUTHORIZENOTEXIST(BROWSER_BASE+502, "The authority dose not exist");
......
......@@ -20,7 +20,7 @@ public class IpResource implements Serializable {
private Vendor vendor;
private String region;
private String regionCn;
//0:正常, 1:已过期, 2:即将过期, 3:正在分配, 4:未使用, 5:已失效;
//0:正常, 1:已过期, 2:即将过期, 3:正在分配, 4:未使用, 5:已失效, 6:未分配
private int status;
private List<String> port;
private long purchasedTime;
......@@ -35,6 +35,15 @@ public class IpResource implements Serializable {
private String shopName;
private List<String> protocol;
private String password;
private double price;
private boolean isLocked;
private long lockTimestamp;
private String unit;
private int period;
private String tid;
public String getDetails() {
return details;
......@@ -195,4 +204,52 @@ public class IpResource implements Serializable {
public void setOwner(String owner) {
this.owner = owner;
}
public boolean isLocked() {
return isLocked;
}
public void setLocked(boolean locked) {
isLocked = locked;
}
public long getLockTimestamp() {
return lockTimestamp;
}
public void setLockTimestamp(long lockTimestamp) {
this.lockTimestamp = lockTimestamp;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public int getPeriod() {
return period;
}
public void setPeriod(int period) {
this.period = period;
}
public String getTid() {
return tid;
}
public void setTid(String tid) {
this.tid = tid;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
......@@ -8,7 +8,7 @@ import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface IpResourceRepository extends MongoRepository<IpResource, String> {
public interface IpResourceRepository extends MongoRepository<IpResource, String>, IpResourceRepositoryCustom {
IpResource findByAddr(String addr);
IpResource findByAddrAndIsDeleted(String addr, boolean isDeleted);
IpResource findByIdAndIsDeleted(String id, boolean isDeleted);
......@@ -22,6 +22,8 @@ public interface IpResourceRepository extends MongoRepository<IpResource, String
Page<IpResource> findByVendorLikeAndIdInAndIsDeletedOrderByPurchasedTimeDesc(String vendor, List<String> ipIds, boolean isDeleted, Pageable pageable);
Page<IpResource> findByRegionLikeAndIdInAndIsDeletedOrderByPurchasedTimeDesc(String region, List<String> ipIds, boolean isDeleted, Pageable pageable);
List<IpResource> findByStatusAndLockedAndLockTimestampLessThan(int status, boolean locked, long timestamp);
List<IpResource> findByValidTimeBetween(long beginTime, long endTime);
int countByStatusAndIdInAndIsDeleted(int status, List<String> ipIds, boolean isDeleted);
......
package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.IpResource;
import java.util.List;
public interface IpResourceRepositoryCustom {
boolean lockTask(IpResource ipResource);
boolean unLockTask(String id);
List<IpResource> sampleTasks(int status, long timestamp);
}
package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.IpResource;
import com.mongodb.client.result.UpdateResult;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.MatchOperation;
import org.springframework.data.mongodb.core.aggregation.SampleOperation;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Update;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.data.mongodb.core.query.Criteria.where;
public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCustom {
@Autowired
MongoTemplate mongoTemplate;
@Override
public boolean lockTask(IpResource ipResource) {
Document doc = new Document();
BasicQuery basicQuery = new BasicQuery(doc);
Criteria criteria = new Criteria();
criteria.orOperator(where("id").is(ipResource.getId()).and("isLocked").is(false).and("status").is(ipResource.getStatus()),
where("lockTimestamp").lte(Instant.now().minusSeconds(300).toEpochMilli()).and("status").is(ipResource.getStatus()));
basicQuery.addCriteria(criteria);
Update update = new Update();
update.set("isLocked", true).set("lockTimestamp", Instant.now().toEpochMilli());
UpdateResult result = mongoTemplate.updateFirst(basicQuery, update, IpResource.class);
if (result.getModifiedCount() < 1)
return false;
else
return true;
}
@Override
public boolean unLockTask(String id) {
Document doc = new Document();
BasicQuery basicQuery = new BasicQuery(doc);
basicQuery.addCriteria(where("id").is(id));
Update update = new Update();
update.set("isLocked", false).set("lockTimestamp", Instant.now().toEpochMilli());
UpdateResult result = mongoTemplate.updateFirst(basicQuery, update, IpResource.class);
if (result.getModifiedCount() < 1)
return false;
else
return true;
}
@Override
public List<IpResource> sampleTasks(int status, long timestamp) {
Criteria matchCriteria = new Criteria();
matchCriteria.orOperator(where("status").is(status).and("isLocked").is(false),
where("isLocked").is(true).and("lockTimeStamp").lte(timestamp));
MatchOperation match = Aggregation.match(matchCriteria);
SampleOperation sample = Aggregation.sample(20);
AggregationResults<IpResource> results = mongoTemplate.aggregate(Aggregation.newAggregation(match, sample), IpResource.class, IpResource.class);
List<IpResource> mappedResults = results.getMappedResults();
return mappedResults;
}
}
......@@ -54,8 +54,6 @@ public class IpResourceServiceImpl implements IpResourceService {
private static List<String> protocol = Arrays.asList("socks5", "proxy");
private static String startscript = "";
@Value("${spring.profiles.active}")
private String profiles;
......@@ -163,7 +161,6 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override
public IpTransactionDto buyIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception {
String URL = (profiles.equals("dev") || profiles.equals("staging"))? TESTURL : CLOUDAMURL;
Account account = accountRepository.findByName(username);
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
......@@ -205,7 +202,7 @@ public class IpResourceServiceImpl implements IpResourceService {
ipResource.setAddr("");
ipResource.setIpType(IpType.VENDOR);
ipResource.setVendor(Vendor.valueOf(ipResourceRequestDto.getVendor()));
ipResource.setStatus(3);
ipResource.setStatus(6);
ipResource.setValidTime(Instant.now().plusSeconds(3600*24*30).toEpochMilli());
} else {
ipResource.setAddr("本地Ip未使用");
......@@ -227,99 +224,27 @@ public class IpResourceServiceImpl implements IpResourceService {
ipResource.setProtocol(protocol);
ipResource.setPassword(password);
ipResource.setOwner(username);
ipResource.setLocked(false);
ipResource.setLockTimestamp(Instant.now().toEpochMilli());
ipResource.setPeriod(ipResourceRequestDto.getPeriod());
ipResource.setUnit(ipResourceRequestDto.getUnit());
ipResource.setPrice(newprice);
IpResource ipResource1 = ipResourceRepository.save(ipResource);
ipResourceDtos.add(new IpResourceDto(ipResource1, null, false));
ipIds.add(ipResource1.getId());
}
if (ipResourceRequestDto.getVendor().equals("local")) {
ipTransaction.setStatus(1);
ipTransactionRepository.save(ipTransaction);
}
ipTransaction.setIpIds(ipIds);
ipTransactionRepository.save(ipTransaction);
IpTransactionDto ipTransactionDto = new IpTransactionDto(ipTransaction);
ipTransactionDto.setIpResourceDtos(ipResourceDtos);
if (!ipResourceRequestDto.getVendor().equals("local")) {
AtomicBoolean result = new AtomicBoolean(false);
CompletableFuture.runAsync(() -> {
PollerUtils.poll(300, 15, () -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
RestTemplate restTemplate = new RestTemplate();
HttpHeaders header = buildPostHeader();
HashMap<String, Object> map = new HashMap<>();
map.put("name", ipResourceRequestDto.getName());
map.put("region", ipResourceRequestDto.getRegion());
if (ipResourceRequestDto.getUnit().equals("month") && ipResourceRequestDto.getPeriod() == 6)
ipResourceRequestDto.setPeriod(7);
else if (ipResourceRequestDto.getUnit().equals("month") && ipResourceRequestDto.getPeriod() == 12)
ipResourceRequestDto.setPeriod(14);
map.put("period", String.valueOf(ipResourceRequestDto.getPeriod()));
map.put("provider", ipResourceRequestDto.getVendor());
map.put("unit", ipResourceRequestDto.getUnit());
map.put("amount", String.valueOf(ipResourceRequestDto.getAmount()));
map.put("loginPassword", password);
map.put("startscript", startscript);
map.put("ipkeptperiod", ipResourceRequestDto.getIpkeptperiod());
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(map, header);
IpBuyResultDto ipBuyResultDto = null;
try {
ipBuyResultDto = restTemplate.postForObject(URL + "/intelligroup/ipresources?accountId=browser", httpEntity, IpBuyResultDto.class);
if (StringUtils.isNotBlank(ipBuyResultDto.getErrorCode())) {
logger.error("fail to buy ip");
logger.error(ipBuyResultDto.getErrorCode());
return false;
}
if (ipBuyResultDto != null && ipBuyResultDto.getIplist() != null && ipBuyResultDto.getIplist().size() >= 1) {
AtomicInteger index = new AtomicInteger();
ipBuyResultDto.getIplist().forEach(x -> {
IpResource ipResource = ipResourceRepository.findById(ipResourceDtos.get(index.get()).getId()).orElse(null);
if (ipResource != null) {
ipResource.setAddr(x.getIp());
ipResource.setStatus(0);
ipResource.setPurchasedTime(Instant.now().toEpochMilli());
ipResource.setValidTime(Instant.parse(x.getValidTill()).toEpochMilli());
ipResourceRepository.save(ipResource);
} else {
logger.error("no ipResource");
}
index.getAndIncrement();
});
if (ipBuyResultDto.getIplist().size() < ipResourceDtos.size()) {
IpChargeRequestDto ipChargeRequestDto = buildIpChargeRequestDto(ipResourceRequestDto, 3, 0);
accountService.chargeByMoney(username, -newprice * (ipResourceDtos.size() - ipBuyResultDto.getIplist().size()), ipChargeRequestDto);
for (IpResourceDto ipResourceDto : ipResourceDtos) {
IpResource ipResource = ipResourceRepository.findById(ipResourceDto.getId()).orElse(null);
if (ipResource != null && StringUtils.isBlank(ipResource.getAddr())) {
ipResourceRepository.deleteById(ipResource.getId());
}
}
}
}
ipTransaction.setStatus(1);
ipTransactionRepository.save(ipTransaction);
result.set(true);
return true;
} catch (Exception e) {
logger.error(e.getMessage());
return false;
}
});
if (result.get() == false) {
IpChargeRequestDto ipChargeRequestDto = buildIpChargeRequestDto(ipResourceRequestDto, 3, 0);
accountService.chargeByMoney(username, -newprice * ipChargeRequestDto.getAmount(), ipChargeRequestDto);
for (IpResourceDto ipResourceDto : ipResourceDtos) {
ipResourceRepository.deleteById(ipResourceDto.getId());
}
}
}, ThreadPoolUtils.taskExecutorPool);
} else {
ipTransaction.setStatus(1);
ipTransactionRepository.save(ipTransaction);
}
IpChargeRequestDto ipChargeRequestDto = buildIpChargeRequestDto(ipResourceRequestDto, 1, ipResourceRequestDto.getPayMethod());
accountService.chargeByMoney(username, newprice * ipChargeRequestDto.getAmount(), ipChargeRequestDto);
return ipTransactionDto;
......@@ -541,7 +466,7 @@ public class IpResourceServiceImpl implements IpResourceService {
notUsed = ipResourceRepository.findByOwnerAndStatusAndIsDeletedAndShopIdIsNull(username, 1, false);
break;
case 4:
notUsed = ipResourceRepository.findByOwnerAndStatusIsNotInAndIsDeletedAndShopIdIsNull(username, Arrays.asList(3, 5),false);
notUsed = ipResourceRepository.findByOwnerAndStatusIsNotInAndIsDeletedAndShopIdIsNull(username, Arrays.asList(3, 5, 6),false);
break;
}
if (notUsed != null)
......@@ -605,6 +530,11 @@ public class IpResourceServiceImpl implements IpResourceService {
}
}
}
if (x.getStatus() == 6)
x.setStatus(3);
if (x.getStatus() == 3) {
x.setAddr("");
}
ipResourceDtos.add(new IpResourceDto(x, shopDto, false));
});
}
......@@ -667,18 +597,7 @@ public class IpResourceServiceImpl implements IpResourceService {
ipTransactionDto.getIpResourceDtos().add(ipResourceDto);
}
}
// IpInfoResultDto ipInfoResultDto = new IpInfoResultDto();
// Map<String, String> params = new HashMap<String, String>();
// params.put("accountId", "browser");
// params.put("ip", x.getIp());
// HttpHeaders headers = buildGetHeader();
// HttpEntity<Map<String, String>> entity = new HttpEntity<>(params, headers);
// ResponseEntity<String> result = restTemplate.exchange(TESTURL + "/ecc/ipinfo?accountId={accountId}&ip={ip}", HttpMethod.GET, entity, String.class);
// JSONObject jsonObject = JSON.parseObject(result.getBody());
// if (ipInfoResultDto != null && StringUtils.isBlank(ipInfoResultDto.getErrorCode())) {
// ipResource.setPurchasedTime(Long.valueOf((String) jsonObject.get("createdWhen")));
// ipResource.setValidTime(Long.valueOf((String)jsonObject.get("validTill")));
// }
return ipTransactionDto;
}
......
package com.edgec.browserbackend.browser.task;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.edgec.browserbackend.account.domain.IpChargeRequestDto;
import com.edgec.browserbackend.account.repository.AccountRepository;
import com.edgec.browserbackend.account.service.AccountService;
import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode;
import com.edgec.browserbackend.browser.domain.IpResource;
import com.edgec.browserbackend.browser.domain.IpTransaction;
import com.edgec.browserbackend.browser.domain.IpType;
import com.edgec.browserbackend.browser.dto.IpBuyResultDto;
import com.edgec.browserbackend.browser.dto.IpInfoResultDto;
import com.edgec.browserbackend.browser.dto.IpResourceDto;
import com.edgec.browserbackend.browser.dto.IpResourceRequestDto;
import com.edgec.browserbackend.browser.repository.IpResourceRepository;
import com.edgec.browserbackend.browser.repository.IpTransactionRepository;
import com.edgec.browserbackend.common.utils.PollerUtils;
import com.edgec.browserbackend.common.utils.ThreadPoolUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.time.Instant;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
@Component
public class BrowserTask {
private static final Logger log = LoggerFactory.getLogger(BrowserTask.class);
private static String CLOUDAMURL = "https://www.cloudam.cn";
private static String TESTURL = "http://112.74.13.2";
private static String USERNAME = "fangguanlianbrowser";
@Autowired
private AccountRepository accountRepository;
@Autowired
private IpResourceRepository ipResourceRepository;
@Scheduled(cron = "0 0 1 * * ?")
public void clearnull() {
@Autowired
private IpTransactionRepository ipTransactionRepository;
@Autowired
private AccountService accountService;
@Value("${spring.profiles.active}")
private String profiles;
public HttpHeaders buildGetHeader() {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
if (profiles.equals("dev") || profiles.equals("staging"))
headers.setBearerAuth("oq5tg3gMsflHcK5iZ2741G5R30XYd9blyOqH9qeBItKtrzfTsGIoy8AsxqqNXdcm");
else if (profiles.equals("prod"))
headers.setBearerAuth("tKWsuHzcngf0RQPMss70f9jgymDIwgQ9zbLfESJdcou3pZSNWl7lNTzto8VQgwaO");
return headers;
}
public HttpHeaders buildPostHeader() {
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON);
if (profiles.equals("dev") || profiles.equals("staging")){
header.setBearerAuth("oq5tg3gMsflHcK5iZ2741G5R30XYd9blyOqH9qeBItKtrzfTsGIoy8AsxqqNXdcm");
}
else if (profiles.equals("prod")) {
header.setBearerAuth("tKWsuHzcngf0RQPMss70f9jgymDIwgQ9zbLfESJdcou3pZSNWl7lNTzto8VQgwaO");
}
return header;
}
private IpChargeRequestDto buildIpChargeRequestDto(IpResource request, int chargeType, int payMethod) {
IpChargeRequestDto ipChargeRequestDto = new IpChargeRequestDto();
ipChargeRequestDto.setAmount(1);
ipChargeRequestDto.setChargeType(chargeType);
ipChargeRequestDto.setRegion(request.getRegion());
ipChargeRequestDto.setPeriod(request.getPeriod());
ipChargeRequestDto.setUnit(request.getUnit());
ipChargeRequestDto.setPayMethod(0);
return ipChargeRequestDto;
}
@Scheduled(cron = "0 0/1 * * * ?")
public void buyIpTasks() {
String URL = (profiles.equals("dev") || profiles.equals("staging")) ? TESTURL : CLOUDAMURL;
long time = Instant.now().minusSeconds(300).toEpochMilli();
List<IpResource> ipResources = ipResourceRepository.sampleTasks(6, time);
AtomicBoolean result = new AtomicBoolean(false);
for (IpResource ipResource : ipResources) {
long start = System.currentTimeMillis();
CompletableFuture.runAsync(() -> {
if (ipResourceRepository.lockTask(ipResource)) {
try {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders header = buildPostHeader();
HashMap<String, Object> map = new HashMap<>();
map.put("name", ipResource.getUsername());
map.put("region", ipResource.getRegion());
if (ipResource.getUnit().equals("month") && ipResource.getPeriod() == 6)
ipResource.setPeriod(7);
else if (ipResource.getUnit().equals("month") && ipResource.getPeriod() == 12)
ipResource.setPeriod(14);
map.put("period", String.valueOf(ipResource.getPeriod()));
map.put("provider", ipResource.getVendor());
map.put("unit", ipResource.getUnit());
map.put("amount", 1);
map.put("loginPassword", ipResource.getPassword());
map.put("startscript", "");
map.put("ipkeptperiod", 7);
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(map, header);
IpBuyResultDto ipBuyResultDto = null;
try {
ipBuyResultDto = restTemplate.postForObject(URL + "/intelligroup/ipresources?accountId=browser", httpEntity, IpBuyResultDto.class);
if (StringUtils.isNotBlank(ipBuyResultDto.getErrorCode())) {
log.error("fail to buy ip");
log.error(ipBuyResultDto.getErrorCode());
}
if (ipBuyResultDto != null && ipBuyResultDto.getIplist() != null && ipBuyResultDto.getIplist().size() >= 1) {
AtomicInteger index = new AtomicInteger();
ipBuyResultDto.getIplist().forEach(x -> {
if (ipResource != null) {
ipResource.setAddr(x.getIp());
ipResource.setStatus(3);
ipResource.setValidTime(Instant.parse(x.getValidTill()).toEpochMilli());
ipResourceRepository.save(ipResource);
} else {
log.error("no ipResource");
}
index.getAndIncrement();
});
}
// ipTransaction.setStatus(1);
// ipTransactionRepository.save(ipTransaction);
result.set(true);
} catch (Exception e) {
log.error(e.getMessage());
}
if (result.get() == false && (ipResource.getPurchasedTime() < Instant.now().minusSeconds(7200).toEpochMilli())) {
IpChargeRequestDto ipChargeRequestDto = buildIpChargeRequestDto(ipResource, 3, 0);
accountService.chargeByMoney(ipResource.getUsername(), -ipResource.getPrice(), ipChargeRequestDto);
ipResourceRepository.deleteById(ipResource.getId());
}
} finally {
long end = System.currentTimeMillis();
log.debug("buyIpTask {} execution time is: " + (end - start) / 1000 + "s", ipResource.getId());
try {
ipResourceRepository.unLockTask(ipResource.getId());
} catch (Throwable th) {
log.error("unlock failed", th);
//try again
ipResourceRepository.unLockTask(ipResource.getId());
}
}
}
}, ThreadPoolUtils.buyIpTasksPool);
}
}
@Scheduled(cron = "0 0/1 * * * ?")
public void queryIpTasks() {
String URL = (profiles.equals("dev") || profiles.equals("staging")) ? TESTURL : CLOUDAMURL;
long time = Instant.now().minusSeconds(300).toEpochMilli();
List<IpResource> ipResources = ipResourceRepository.sampleTasks(3, time);
for (IpResource ipResource : ipResources) {
long start = System.currentTimeMillis();
CompletableFuture.runAsync(() -> {
if (ipResourceRepository.lockTask(ipResource)) {
try {
RestTemplate restTemplate = new RestTemplate();
Map<String, Object> params = new HashMap<>();
HttpHeaders headers = buildGetHeader();
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(params, headers);
ResponseEntity<IpInfoResultDto> result = restTemplate.exchange(URL + "/ecc/ipinfo?accountId={accountId}&ip={ip}",
HttpMethod.GET, entity, IpInfoResultDto.class, "browser", ipResource.getAddr());
IpInfoResultDto ipInfoResultDto = result.getBody();
if (ipInfoResultDto != null && StringUtils.isBlank(ipInfoResultDto.getErrorCode())) {
if (ipInfoResultDto.getStatus().equals("Running")) {
ipResource.setStatus(0);
ipResourceRepository.save(ipResource);
}
}
} catch (Exception e) {
log.error(e.getMessage());
} finally {
long end = System.currentTimeMillis();
log.debug("queryIpTask {} execution time is: " + (end - start) / 1000 + "s", ipResource.getId());
try {
ipResourceRepository.unLockTask(ipResource.getId());
} catch (Throwable th) {
log.error("unlock failed", th);
//try again
ipResourceRepository.unLockTask(ipResource.getId());
}
}
}
}, ThreadPoolUtils.buyIpTasksPool);
}
}
}
......@@ -8,10 +8,13 @@ import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @Desc
......@@ -160,5 +163,35 @@ public class FileUtil {
return cellValue;
}
/**
* 方法说明将files打包放到一个zip中。
* @return
* @throws IOException
*/
public static File putBatchFilesInZip(List<File> filePaths,File tempFile) throws IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tempFile));
for(File inputFile : filePaths) {
try(FileInputStream fis = new FileInputStream(inputFile);){
//压缩文件中写入名称
ZipEntry entry = new ZipEntry(inputFile.getName());
zos.putNextEntry(entry);
// 向压缩文件中输出数据
int len = 0;
byte[] bt = new byte[5*1024];
while ((len = fis.read(bt)) != -1) {
//压缩文件中写入真正的文件流
zos.write(bt, 0, len);
}
}catch(Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
zos.flush();
zos.close();
return tempFile;
}
}
......@@ -19,26 +19,26 @@ public abstract class ThreadPoolUtils {
public static final BlockingQueue<Runnable> schedulerQueue = new UniquePriorityBlockingQueue<>(50);
private static final int TASK_POOL_COUNT = 80;
private static final int TASK_POOL_COUNT = 20;
public static final ExecutorService taskExecutorPool = Executors.newFixedThreadPool(TASK_POOL_COUNT, new ThreadFactory() {
public static final ExecutorService buyIpTasksPool = Executors.newFixedThreadPool(TASK_POOL_COUNT, new ThreadFactory() {
int count = 1;
@Override
public Thread newThread(Runnable runnable) {
return new Thread(runnable, "intelligroup-taskexec-" + count++);
return new Thread(runnable, "browser-buyIp-task-" + count++);
}
});
private static final int TIME_SCHEDULER_POOL_COUNT = 10;
private static final int TIME_SCHEDULER_POOL_COUNT = 20;
public static final ScheduledExecutorService timeSchedulerPool = Executors.newScheduledThreadPool(TIME_SCHEDULER_POOL_COUNT, new ThreadFactory() {
public static final ScheduledExecutorService queryIpTasksPool = Executors.newScheduledThreadPool(TIME_SCHEDULER_POOL_COUNT, new ThreadFactory() {
int count = 1;
@Override
public Thread newThread(Runnable runnable) {
return new Thread(runnable, "intelligroup-time-scheduler-" + count++);
return new Thread(runnable, "browser-queyrIp-taks-" + count++);
}
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment