Posts

Showing posts with the label Example

Spring Boot / Embedded Tomcat - SSL

 @Bean     public ServletWebServerFactory servletContainer() {         TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {             @Override             protected void postProcessContext(Context context) {                 var securityConstraint = new SecurityConstraint();                 securityConstraint.setUserConstraint("CONFIDENTIAL");                 var collection = new SecurityCollection();                 collection.addPattern("/*");                 securityConstraint.addCollection(collection);                 context.addConstraint(securityConstraint);             }       ...

Liquibase in Docker container kimi istifade edilmesi

docker run --rm --network my-test-net -v D:\Workspace\my-app\migration-service\src\main\resources\:/liquibase/changelog liquibase/liquibase --defaultsFile=/liquibase/changelog/liquibase.properties generateChangeLog url : jdbc:postgresql://a54427d8649e/my_db username : postgres password : hamid318 changeLogFile : /liquibase/changelog/mytestv3.yaml Eger evvelceden yaradilmis db container varsa, docker de network yaradib hemin containeri i yaradilan network a elave etmek lazimdir. liquibase i run ederken hemin networka elave edirik docker run --rm --network my-test-net -v D:\Workspace\my-app\migration-service\src\main\resources\:/liquibase/changelog liquibase/liquibase --defaultsFile=/liquibase/changelog/liquibase.properties generateChangeLog --includeObjects="users"

Frontdan gelen base64 file contenti mail de attachment kimi gondermek

String cnt = attachmentDto.getContent(); int index = cnt.indexOf(",") + 1; // cnt = cnt.replaceFirst("^data:image/[^;]*;base64,?", ""); cnt = cnt.substring(index); byte[] bytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(cnt); mimeMessageHelper.addAttachment(    attachmentDto.getName(),    new ByteArrayResource(bytes) ); 

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.

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...

oauth/token request manual

ResponseEntity<String> response = null; RestTemplate restTemplate = new RestTemplate(); String credentials = "clientId:clientSecret"; String encodedCredentials = new String(Base64.encodeBase64(credentials.getBytes())); //        System.out.println(username); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.add("Authorization", "Basic " + encodedCredentials); HttpEntity<String> request = new HttpEntity<String>(headers); String accessTokenUrl = "http://localhost:8080/api/oauth/token" + "?grant_type=password" +         "&username=username" +         "&password=password" +         "&scope=read write"; response = restTemplate.exchange(accessTokenUrl, HttpMethod.POST, request, String.class); //        System.out.println(response.getBody().split(",...

oauth_access_token table

create table oauth_access_token ( token_id VARCHAR(255), token BLOB, authentication_id VARCHAR(255), user_name VARCHAR(255), client_id VARCHAR(255), authentication BLOB, refresh_token VARCHAR(255) ); create table oauth_refresh_token ( token_id VARCHAR(255), token BLOB, authentication BLOB );