Created
February 13, 2018 01:15
-
-
Save israeleriston/e1ca4429148b247fa9efbb74c23f6fcc to your computer and use it in GitHub Desktop.
ResourceLoader for loading e writings file .json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.gleac.seguranca; | |
import br.com.gleac.entidade.seguranca.RecursoSistema; | |
import br.com.gleac.negocio.RecursoSistemaRepository; | |
import com.fasterxml.jackson.core.JsonEncoding; | |
import com.fasterxml.jackson.core.JsonFactory; | |
import com.fasterxml.jackson.core.JsonGenerator; | |
import com.fasterxml.jackson.core.JsonParseException; | |
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; | |
import com.google.common.collect.Lists; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.event.ContextRefreshedEvent; | |
import org.springframework.context.event.EventListener; | |
import org.springframework.core.annotation.Order; | |
import org.springframework.stereotype.Component; | |
import org.springframework.util.ResourceUtils; | |
import javax.xml.bind.DatatypeConverter; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URL; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.security.MessageDigest; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
@Component | |
public class ResourceLoader { | |
public static final Logger log = LoggerFactory.getLogger(RecursoSistemaLoader.class); | |
public static final String RAIZ = "/recursos"; | |
public static final String SHA1 = "SHA1"; | |
@Autowired | |
private RecursoSistemaRepository recursoSistemaRepository; | |
private List<RecursoSistema> recursos; | |
private List<RecursoSistema> recursosSistemaJson; | |
public RecursoSistemaLoader() { | |
setRecursos(Lists.newArrayList()); | |
setRecursosSistemaJson(Lists.newArrayList()); | |
} | |
@EventListener(ContextRefreshedEvent.class) | |
@Order(Integer.MIN_VALUE) | |
public void onApplicationLoaderRecursos(ContextRefreshedEvent event) { | |
converterJsonToRecursoSistema(); | |
} | |
private void converterJsonToRecursoSistema() { | |
List<File> recursosJson = buscarRecursosJson(getRaizRecursos()); | |
recursosJson.forEach(arquivo -> { | |
try { | |
// nao preciso disso, pq recursosJson, já é um file | |
File file = ResourceUtils.getFile(arquivo.getName()); | |
FileOutputStream stream = new FileOutputStream(file); | |
JsonFactory factory = new JsonFactory(); | |
JsonGenerator jsonGenerator = factory.createGenerator(stream, JsonEncoding.UTF8); | |
jsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter()); | |
jsonGenerator.writeStartObject(); | |
jsonGenerator.writeStringField("checksum", calcularChecksum(file.getAbsolutePath())); | |
jsonGenerator.writeEndObject(); | |
jsonGenerator.flush(); | |
jsonGenerator.close(); | |
} catch (JsonParseException jpe) { | |
jpe.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
}); | |
} | |
public List<File> buscarRecursosJson(String path) { | |
try { | |
return Files.walk(Paths.get(path)) | |
.filter(Files::isRegularFile) | |
.map(Path::toFile) | |
.collect(Collectors.toList()); | |
} catch (IOException e) { | |
log.error(" erro ao carregar os recursos do sistema {} ", e.getCause()); | |
} | |
return Lists.newArrayList(); | |
} | |
public String getRaizRecursos() { | |
ClassLoader classLoader = this.getClass().getClassLoader(); | |
URL resource = classLoader.getResource(RAIZ); | |
return resource.getPath(); | |
} | |
public String calcularChecksum(String file) { | |
try { | |
InputStream stream = Files.newInputStream(Paths.get(file)); | |
MessageDigest digest = MessageDigest.getInstance(SHA1); | |
byte[] block = new byte[4096]; | |
int length; | |
while ((length = stream.read(block)) > 0) { | |
digest.update(block, 0, length); | |
} | |
return converterHex(digest.digest()); | |
}catch (Exception e) { | |
log.error("não foi possível calcular o checksum {} ", e.getCause()); | |
} | |
return null; | |
} | |
public String converterHex(byte[] bytes) { | |
return DatatypeConverter.printHexBinary(bytes); | |
} | |
public List<RecursoSistema> getRecursos() { | |
return recursos; | |
} | |
public void setRecursos(List<RecursoSistema> recursos) { | |
this.recursos = recursos; | |
} | |
public List<RecursoSistema> getRecursosSistemaJson() { | |
return recursosSistemaJson; | |
} | |
public void setRecursosSistemaJson(List<RecursoSistema> recursosSistemaJson) { | |
this.recursosSistemaJson = recursosSistemaJson; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment