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[…]

Ignore SSL Certificate socket factory

import java.security.SecureRandom; import java.security.cert.X509Certificate;   import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager;   …   SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) {} public void checkServerTrusted(X509Certificate[] certs, String authType) {} } }, new SecureRandom()); SSLSocketFactory Read more about Ignore SSL Certificate socket factory[…]

RxJava .reduce()

Observable.from(new Integer[]{1,2,3,4,5}) .reduce(new Func2<Integer, Integer, Integer>() { @Override public Integer call(Integer t1, Integer t2) { return t1+t2; } }).subscribe(new Action1<Integer>() { @Override public void call(Integer t) { // t = 15 } });Observable.from(new Integer[]{1,2,3,4,5}) .reduce(new Func2<Integer, Integer, Integer>() { @Override public Integer call(Integer t1, Integer t2) { return t1+t2; } }).subscribe(new Action1<Integer>() { @Override public Read more about RxJava .reduce()[…]

Observable.zip()

public interface Api {   @GET("x") Observable<X> getX();   @GET("y") Observable<Y> getY();   }public interface Api { @GET("x") Observable<X> getX(); @GET("y") Observable<Y> getY(); } Observable.zip(api.getX(), api.getY(), new Func2<X, Y, Pair>() { @Override public Pair call(X x, Y y) { return new Pair(x, y); } }).subscribe(new Action1<Pair>() { @Override public void call(Pair pair) { // you Read more about Observable.zip()[…]

RxJava – Observable.interval()

package rxjava;   import java.util.Random; import java.util.concurrent.TimeUnit;   import rx.Observable; import rx.Observer; import rx.functions.Func1; import rx.schedulers.Schedulers;   public class RxInterval {   private final Random random = new Random();   public RxInterval() {   Observable<Integer> observable = Observable .interval(5, TimeUnit.SECONDS) /** * startWith() * do Observable dodawany jest element startowy, który emitowy jest przed pierwszym Read more about RxJava – Observable.interval()[…]

Spring Annotation Based Single Class Web Application with JSP View Resolver

Works on Servlet 3.0+ containers. Better not mix Configuration classes with Controller classes. It is only for simplicity 😉 import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration;   import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView;   @Configuration @EnableWebMvc @Controller public Read more about Spring Annotation Based Single Class Web Application with JSP View Resolver[…]

Spring Annotation Based Single Class Web Application

Works on Servlet 3.0+ containers. Better not mix Configuration classes with Controller classes. It is only for simplicity 😉 import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration;   import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Controller; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.config.annotation.EnableWebMvc;   @Configuration @EnableWebMvc @Controller public class SpringWebApp implements WebApplicationInitializer {   @RequestMapping("/") @ResponseBody Read more about Spring Annotation Based Single Class Web Application[…]

Java 8 Lambda expressions

(parameters) -> expression (parameters) -> {statements;} Examples from Java 8 Lambdas: Functional Programming For The Masses Example 1 ActionListener oneArgument = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { System.out.println("button clicked"); } };ActionListener oneArgument = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { System.out.println("button clicked"); } }; ActionListener oneArgument = event -> System.out.println("button Read more about Java 8 Lambda expressions[…]

Android – Save / Load state Fragment

public class SomeFragment extends Fragment {   public final static String OBJECT_ATTR = "object"; Data data;   @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {   /** * restore */ if(savedInstanceState != null) { data = savedInstanceState.getParcelable(OBJECT_ATTR); }   /** * using data */ … }   /** * called when Read more about Android – Save / Load state Fragment[…]

Maven project from archetype – Interactive mode

mvn archetype:generate -Dfilter=org.apache.maven.archetypes:mvn archetype:generate -Dfilter=org.apache.maven.archetypes: Good ones for starting: maven-archetype-quickstart, maven-archetype-webapp mvn archetype:generate -DarchetypeCatalog=http://repo.maven.apache.org/maven2/archetype-catalog.xml -Dfilter=org.codehaus.mojo.archetypes:mvn archetype:generate -DarchetypeCatalog=http://repo.maven.apache.org/maven2/archetype-catalog.xml -Dfilter=org.codehaus.mojo.archetypes: Good ones for starting: ejb-javaee6, ejb-javaee7