Posts

JWT - generate public and private key (YML example)

@SpringBootApplication public class AuthServerApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(AuthServerApplication.class, args); } @Override public void run(String... args) throws Exception { KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA"); keyGenerator.initialize(1024); KeyPair kp = keyGenerator.genKeyPair(); PublicKey publicKey = kp.getPublic(); PrivateKey privateKey = kp.getPrivate(); String encodedPublicKey = Base64.getEncoder().encodeToString(publicKey.getEncoded()); String encodedPrivateKey = Base64.getEncoder().encodeToString(privateKey.getEncoded()); System.out.println(convertToPublicKey(encodedPublicKey)); System.out.println(); System.out.println(convertToPrivateKey(encodedPrivateKey)); } private static String convertToPrivateKey(String key) {         StringBuilder result = new StringBuilder();         result.append(...

Apache Mail - for mail operation

 <dependency> <groupId> org.apache.commons </groupId> <artifactId> commons-email </artifactId> <version> 1.5 </version> </dependency> MimeMessageParser parser = new MimeMessageParser((MimeMessage) message).parse(); "parser" ile body ni hem html, hem de text/plain formatinda chekmek mumkundur.

Gmail, Mail.ru ve Yandex(mail) servislerinde folderler ve maillerin chekilmesinin sade mentiqi

Image
Mail.ru servisi uzerinden gelen folderler UI Language den asili olmayaraq hemishe bir dilde gelir. Lakin Gmail da bu mentiq bashqadir. UI language gore folderler hemin language de gele biler. Eger folderleri chekib bazaya yaziriqsa, aydindir ki, UI terefde dil deyishse, bizim servisimiz duzgun ishlemeyecek. Duzgun ishlemeyecek deyerken, eyni Folderler muxtelif dillerde bazamiza otura biler.  Nece hell etmeli ? IMAPFolder classi var, hansi ki, Folder classini extends edir. Bize gelen Folder classinin obyektini IMAPFolder classina cast eleye bilerik. Bu da bize bu ozellikleri verir ki, folder e aid olan attributelari cekek.  Attributelara \Inbox, \Sent, \Trash ve s gostermek olar. Bu attributelara esasen folderin eslinde hansi folder oldugunu teyin etmek olar.  Birinci if sertinde yazilan kodun izahi : Gmail da custom folder anlayishi olmadigina gore, bele bir kod yazmisham. Gmail da yalniz custom label anlayishi movcuddur. Label a ise mesaji move elemek olur. Bir nov her h...

compare two instants (month or other fields)

Duration duration = Duration.between(LocalDate.now().minusMonths(2).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(),Instant.now()); System.out.println(duration.toDays());

get file from message - JavaMail library

get bytes BASE64DecoderStream base64DecoderStream = (BASE64DecoderStream) obj; byte[] byteArray = IOUtils.toByteArray(base64DecoderStream); byte[] encodeBase64 = Base64.encodeBase64(byteArray); ========= return inputstream for download process Resource <= InputStreamResource return new InputStreamResource(part.getInputStream());

receiver mail from server - IMAP - Java

Ilk once bize host, port, username ve password lazimdir. Host ve port serverden asili olaraq deyishe biler. Hal-hazir ki, numune gmail uchundur. [ get file from message ]

Create cache with expire time

first use @EnableCaching annotation in your Main or configuration class import com.google.common.cache.CacheBuilder; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.cache.concurrent.ConcurrentMapCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration public class CachingConfig {     @Bean     public CacheManager cacheManager() {         return new ConcurrentMapCacheManager() {             @Override             protected Cache createConcurrentMapCache(String name) {                 return new ConcurrentMapCache(                         name, ...

git config - for a single repository

$ git config user.name "John Doe" $ git config user.email johndoe@example.com

send request with body - RestTemplate

HttpHeaders headers = new HttpHeaders();  headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);  MultiValueMap<String, String> data = new LinkedMultiValueMap<String, String>();  data.add("client_id",clientId);  data.add("client_secret",clientSecret);  data.add("grant_type",grantType);  data.add("redirect_uri",redirectUrl);  data.add("code",code.split("#")[0]);  HttpEntity<MultiValueMap<String, String>>  request = new HttpEntity<>(data, headers);  ResponseEntity<String> response = restTemplate.postForEntity(accessTokenUrl,request,String.class);

json to object - object to json methods

import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; public class JsonHelper {     public static String getJsonString(Object object) throws JsonProcessingException {         ObjectMapper mapper = new ObjectMapper();         mapper.registerModule(new JavaTimeModule());         mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);         return mapper.writeValueAsString(object);     }     public static <T> T getObjectFromJson(String json, Class<T> type) throws JsonProcessingException{         ObjectMapper mapper = new ObjectMapper();         mapper.registerModule(new JavaTimeModule());         mapper.disable(SerializationFe...