Skip to content

Commit

Permalink
Possibility to define cacheItems manually
Browse files Browse the repository at this point in the history
  • Loading branch information
Hi-Fi committed Aug 3, 2018
1 parent c19d9a4 commit a2a390a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -81,8 +83,9 @@ public ResponseEntity<InputStreamResource> getCachedItems() throws FileNotFoundE
File cacheFile = fileService.exportCacheToFile();
HttpHeaders respHeaders = new HttpHeaders();
respHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
String fileName = "cacheExport_"+new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())+".json";
respHeaders.set(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=cacheExport.dat");
"attachment; filename="+fileName);
respHeaders.setContentLength(cacheFile.length());

InputStreamResource isr = new InputStreamResource(new FileInputStream(cacheFile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public void appendToMessageDatas(Map<String, MessageData> messageDatas) {
this.messageDatas.putAll(messageDatas);
}

public void appendToMessageDatas(MessageData messageData) {
this.messageDatas.put(messageData.getHash(), messageData);
}

public void removeCachedInformation(String requestHash) {
this.messageDatas.remove(requestHash);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -45,10 +46,15 @@ public void importCacheFromFile(MultipartFile file) {
ObjectMapper objectMapper = new ObjectMapper();
TypeReference<Map<String, MessageData>> typeRef
= new TypeReference<Map<String, MessageData>>() {};
cache.appendToMessageDatas(objectMapper.readValue(file.getInputStream(), typeRef));
Map<String, MessageData> importedData = objectMapper.readValue(file.getInputStream(), typeRef);
for (Map.Entry<String, MessageData> dataItem : importedData.entrySet()) {
if (dataItem.getValue().getHash().isEmpty()) {
dataItem.getValue().setHash(DigestUtils.sha256Hex(dataItem.getValue().getRequestContent()));
}
cache.appendToMessageDatas(dataItem.getValue());
}
} catch (IOException e) {
log.error("Reading of import YAML failed", e);
}
}

}

0 comments on commit a2a390a

Please # to comment.