public static <T> T doGet(String url, Class<T> responseType, Map<String, Object> paramMap) {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(1000);
restTemplate.setRequestFactory(requestFactory);
HttpHeaders headers = new HttpHeaders();
headers.add("X-Access-token",token);
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<String> entity = new HttpEntity<>(headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
builder.queryParam(entry.getKey(), entry.getValue());
}
ResponseEntity<T> responseEntity = restTemplate.exchange(builder.buildAndExpand().toUriString(), HttpMethod.GET, entity, responseType);
if (responseEntity.getStatusCode() == HttpStatus.OK) {
return responseEntity.getBody();
}
throw new RuntimeException("Failed to send get");
}
public static <T> T doPost(String url, Class<T> responseType, Object requestObject) {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(1000);
restTemplate.setRequestFactory(requestFactory);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<Object> requestEntity = new HttpEntity<>(requestObject, headers);
ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType);
return responseEntity.getBody();
}
public static <T> T doPostJson(String url, Class<T> responseType, String params){
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(1000);
restTemplate.setRequestFactory(requestFactory);
HttpHeaders headers = new HttpHeaders();
headers.add("X-Access-token",token);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType);
if (responseEntity.getStatusCode() == HttpStatus.OK) {
return responseEntity.getBody();
}
throw new RuntimeException("Failed to send post");
}
public static JSONObject uploadFile(String url,MultipartFile file) throws IOException {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(1000);
restTemplate.setRequestFactory(requestFactory);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", convert(file));
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity<JSONObject> responseEntity = restTemplate.exchange(
url,
HttpMethod.POST,
requestEntity,
JSONObject.class
);
return responseEntity.getBody();
}
private static ByteArrayResource convert(MultipartFile file) {
ByteArrayResource resource = null;
try {
resource = new ByteArrayResource(file.getBytes()) {
@Override
public String getFilename() {
return file.getOriginalFilename();
}
};
} catch (IOException e) {
e.printStackTrace();
}
return resource;
}
public static MultipartFile downloadFile(String url) throws IOException {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
HttpEntity<String> entity = new HttpEntity<String>(headers);
ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class);
byte[] fileBytes = response.getBody();
ByteArrayResource resource = new ByteArrayResource(fileBytes);
String fileName = getFileName(url, response.getHeaders().getContentType());
Path tempFile = Files.createTempFile(fileName, ".tmp");
Files.write(tempFile, fileBytes);
return new MultipartFile() {
@Override
public String getName() {
return fileName;
}
@Override
public String getOriginalFilename() {
return fileName;
}
@Override
public String getContentType() {
return response.getHeaders().getContentType().toString();
}
@Override
public boolean isEmpty() {
return fileBytes == null || fileBytes.length == 0;
}
@Override
public long getSize() {
return fileBytes.length;
}
@Override
public byte[] getBytes() throws IOException {
return fileBytes;
}
@Override
public InputStream getInputStream() throws IOException {
return resource.getInputStream();
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
Files.copy(tempFile, dest.toPath());
}
};
}
private static String getFileName(String url, MediaType mediaType) {
String extension = "";
String contentType = mediaType.toString();
if (contentType.contains("/")) {
extension = "." + contentType.split("/")[1];
}
String[] parts = url.split("/");
String fileName = parts[parts.length - 1];
if (fileName.contains(".")) {
fileName = fileName.substring(0, fileName.lastIndexOf("."));
}
fileName = fileName.replaceAll("[\\\\/:*?\"<>|]", "_") + extension;
return fileName;
}
public static void downloadFile(String url, String localFilePath) throws IOException {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
RequestCallback requestCallback = request -> {
request.getHeaders().addAll(headers);
};
ResponseExtractor<Void> responseExtractor = response -> {
MediaType contentType = response.getHeaders().getContentType();
String fileExtension = contentType.getSubtype();
String contentDisposition = response.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION);
String filename;
if (contentDisposition != null && contentDisposition.contains("filename=")) {
filename = contentDisposition.substring(contentDisposition.indexOf("filename=") + 9);
filename = filename.replaceAll("\"", "");
} else {
filename = getFileName(url, response.getHeaders().getContentType());
}
FileOutputStream outputStream = new FileOutputStream(new File(localFilePath + filename + "." + fileExtension));
InputStream inputStream = response.getBody();
FileCopyUtils.copy(inputStream, outputStream);
return null;
};
restTemplate.execute(url, HttpMethod.GET, requestCallback, responseExtractor);
}