List to Map (streams)

@Getter @Setter class Directory { Long id; String name; }   List<Directory> l = repo.allDirectories();   Map<Long, String> m = l.stream() .collect(Collectors.toMap(d -> d.getId(), d -> d.getName()));@Getter @Setter class Directory { Long id; String name; } List<Directory> l = repo.allDirectories(); Map<Long, String> m = l.stream() .collect(Collectors.toMap(d -> d.getId(), d -> d.getName()));

@PreAuthorize and SpEL params

If @PreAuthorize is used on interface, parameter name (id) in SpEL must match parameter name in implementation (not required on interface – fileId). @Transactional public interface FileService {   @PreAuthorize("@securityService.canAccessFile(#id)") File getFile(Long fileId); }   /** * GOOD */ @Service public class FileServiceImpl implements FileService {   @Override public File getFile(Long id) { /* … Read more about @PreAuthorize and SpEL params[…]

SpEL in @Query (Spring Data)

@Query("select t from Test t where name = :#{#data.name} and type = :#{#data.type}") List<User> findByData(@Param("data") Data data);   public class Data { private String name; private DataType type; }@Query("select t from Test t where name = :#{#data.name} and type = :#{#data.type}") List<User> findByData(@Param("data") Data data); public class Data { private String name; private DataType type; Read more about SpEL in @Query (Spring Data)[…]

Map<String,String> in JPA Entity

@Entity @Table(name="product") public class Product extends AbstractEntity {   @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id;   @ElementCollection(fetch = FetchType.EAGER) @JoinTable(name="product_attribute", joinColumns=@JoinColumn(name="product_id")) @MapKeyColumn(name="key") @Column(name="value") private Map<String, String> attributes = new HashMap<String, String>();   // …   }@Entity @Table(name="product") public class Product extends AbstractEntity { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @ElementCollection(fetch = FetchType.EAGER) @JoinTable(name="product_attribute", joinColumns=@JoinColumn(name="product_id")) @MapKeyColumn(name="key") @Column(name="value") private Read more about Map<String,String> in JPA Entity[…]

Active Directory/LDAP authentication with Spring Boot

package net.marioosh.ldapdemo;   import java.util.Arrays; import java.util.Collection;   import org.springframework.context.annotation.Configuration; import org.springframework.ldap.core.DirContextOperations; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;   @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {   @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest() .fullyAuthenticated() .and().formLogin(); }   @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { Read more about Active Directory/LDAP authentication with Spring Boot[…]

XML – check is well formed

public boolean isValid(String xmlFilePath) throws FileNotFoundException { FileInputStream in = new FileInputStream(xmlFilePath); try { XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader staxXmlReader = factory.createXMLStreamReader(in); while(staxXmlReader.next() != XMLStreamConstants.END_DOCUMENT) {} return true; } catch (XMLStreamException e) { e.printStackTrace(); return false; } }public boolean isValid(String xmlFilePath) throws FileNotFoundException { FileInputStream in = new FileInputStream(xmlFilePath); try { XMLInputFactory factory = XMLInputFactory.newInstance(); Read more about XML – check is well formed[…]

Simple AES-128 encryption in Java

import java.io.ByteArrayOutputStream;   import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec;   /** * AES-128 */ public class EncryptionUtils {   /** * initialisation vector */ private final static byte[] IV = "thai8bai9voo1haV".getBytes();   /** * algorithm/mode/padding */ private final static String TRANSFORMATION = "AES/CBC/PKCS5Padding";   /** * * @param text encrypted text * @param password need Read more about Simple AES-128 encryption in Java[…]

Mockito: doReturn() vs when()

import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import org.junit.Assert; import org.junit.Test; public class MockitoTest { @Test public void test() { X x = spy(X.class); doReturn("ace").when(x).foo(); Assert.assertEquals(x.foo(), "ace"); } @Test(expected = NullPointerException.class) public void test2() { X x = spy(X.class); /** * Using Mockito.when(…).thenReturn(…) * the real method will still be invoked !!! * Read more about Mockito: doReturn() vs when()[…]