git: file history with diff

Show diff ONLY for src/main/resources/file.xml
git log -p src/main/resources/file.xml

Show diff for ALL FILES that touches src/main/resources/file.xml
git log -p --full-diff src/main/resources/file.xml

Posted in git | Leave a comment

async.js – download multiple files from multiple urls to first success

var async = require('async');
 
var files = ['file1','file2','file3'];
var urls = ['url1','url2','url3'];
 
var down = function(url, file, callback) {
	setTimeout(function(){
		var r = Math.random() >= 0.5;
		console.log('--- end '+file+', '+url+', '+r);
		callback(r);
	}, 1000);
};
 
async.concat(files, function(file, callback){
	async.someSeries(urls, function(url, callback1){
		down(url, file, function(isOk){
			callback1(null, isOk);
		});		
	}, function(err, r){
		callback(err, r);
	});
}, function(err, fullRes){
	console.log('done');
	console.log(fullRes);
});
Posted in JavaScript, async.js | Leave a comment

SSH Tunneling with plink

To make local port 1234 forwarding to remote hostA port 21:
plink -v -C -N -L 1234:hostA:21 user@hostB
Now You can connect to localhost:1234 to access hostA:21 by tunnel created with hostB.

To accept connection from other hosts (not local only):
plink -v -C -N -L 0.0.0.0:1234:hostA:21 user@hostB

Posted in plink, ssh | Leave a comment

HTTPS Redirect with exclude

Example how to redirect to HTTPS, but excluding .well-know/* directory.

<VirtualHost *:80>
   ServerAlias domain.net
   RedirectMatch 301 ^/((?!\.well-known).*)$ https://domain.net/$1
</VirtualHost>
Posted in Uncategorized | Leave a comment

node.js – port test

require('portscanner').checkPortStatus(
80, '192.168.1.234'
, function(error, status) {
  console.log(status)
})
Posted in node.js | Leave a comment

async.js: parallel get

/**
 * jednoczesne wywolanie GET'ow na adresy
 * zwracana jest tablica odpowiedzi w callbacku
 * jako drugi parametr lub error jako pierwszy
 */
var parallelGets = function(urls, callback) {
	var calls = urls.map(function(url){
		return function(callback){
 	    	       $.get(url, function(data){
				callback(null, data);
	    	       }).fail(function(jqXHR){
	    		        callback(jqXHR);
	    	       });				
		};
	});
	async.parallel(calls, function(err, res){
		if(err) {
			callback(err);
		} else {
			callback(null, res);
		}
	});		
}
Posted in JavaScript, async.js | Leave a comment

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
	}
});
Posted in Java, RxJava | Leave a comment

Observable.zip()

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 got pair ;)
    }
});
Posted in Java, Retrofit, RxJava | Leave a comment

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 głównym, 
		 * a między nimi nie ma zwłoki - efekt jest taki, że pierwszego Integer'a dostajemy od razu, 
		 * a nie po 5 sekundach
		 */
		.startWith(0l) 
		.map(new Func1<Long, Integer>() {
			@Override
			public Integer call(Long t) {
				return random.nextInt();
			}
		});
 
		Observer<Integer> observer = new Observer<Integer>() {
 
			/**
			 * observable skonczyl emisje
			 */
			@Override
			public void onCompleted() {
			}
 
			/**
			 * observable gdzies sie wysypal
			 */
			@Override
			public void onError(Throwable e) {
				// TODO Auto-generated method stub
 
			}
 
			/**
			 * nowy element z observable
			 */
			@Override
			public void onNext(Integer t) {
				System.out.println(t);
			}
		};
 
		observable
                .subscribeOn(Schedulers.immediate())
                .observeOn(Schedulers.immediate())		
		.subscribe(observer);
	}
 
	public static void main(String[] args) {
		new RxInterval();
	}
}
Posted in Java, RxJava | Leave a comment

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 class SpringWebApp implements WebApplicationInitializer {
 
	@RequestMapping("/")
	public String helloView(Model model) {
		model.addAttribute("hello", "Hello World!");
		return "hello";
	}
 
	/**
	 * jsp View Resolver
	 * @return
	 */
	@Bean
	public ViewResolver internalViewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setViewClass(JstlView.class);
		resolver.setSuffix(".jsp");
		/**
		 * put Your .jsp in src/main/webapp/WEB-INF/views/ directory
		 */
		resolver.setPrefix("/WEB-INF/views/");
		return resolver;
	}
 
	/**
	 * initialize Spring Dispatcher Servlet 
	 * required Servlet Container 3.0+
	 */
	public void onStartup(ServletContext servletContext) throws ServletException {
		AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
		ctx.register(SpringWebApp.class);
		ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
		registration.setLoadOnStartup(1);
		registration.addMapping("/");			
	}
}

Complete pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>net.marioosh</groupId>
	<artifactId>spring-annotation-based</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.2.RELEASE</version>
		</dependency>
		<dependency>
		    <groupId>javax.servlet</groupId>
		    <artifactId>javax.servlet-api</artifactId>
		    <version>3.1.0</version>
		    <scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
 
			<!-- Jetty testing server -->
			<plugin>
				<groupId>org.eclipse.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>9.2.11.v20150529</version>
				<configuration>
					<scanIntervalSeconds>10</scanIntervalSeconds>
					<webApp>
						<contextPath>/spring</contextPath>
					</webApp>
				</configuration>
			</plugin>			
		</plugins>
	</build>
</project>

WEB-INF/views/hello.jsp

${hello}
Posted in Java, Spring, jetty | Leave a comment