Boiling frogs 2017 inspirations: teach (1)



It is longer than three weeks since I was attending that software craftsmanship conference in Wrocław. The seven-hour train journey was exhausting, but worth that amazing experience of meeting a lot of great people and learning something new about my job. Here I just want to share some thoughts with hope, that someone gets inspired.

Moore’s law of our times – Tomasz Kaczmarzyk (opening speak)

    • “The software craftsmanship manifest” was reminded. I think that every programmer aspiring to be a professional should recall it more often than on the conference. I would say at least once a month! The thing is to keep the focusing on “raising the bar”.
    • The next fun thing was a prediction of Uncle Bob, that “every 5 years the number of programmers doubles”. And here is my tweet about it:

    • There was nice explanation what “10x programmer” really means in the context of software craftsmanship: maybe 1 senior programmer cannot be as much productive as 10 junior programmers, but he can improve them by teaching and helping. So senior guys step up and don’t hesitate to be mentors! I believe that teaching is a natural way to become an expert.

Thanks for reading!

Cheers, Rafał 🙂

[IntelliJ] Auto-filling Java call arguments like Eclipse did

After changing my IDE from Eclipse to IntelliJ, I suffered from lack of really helpful functionality of auto-completing method call arguments (topic on Stackoverflow).

The standard auto-completition wasn’t enough and “Ctrl+P” shortcut doesn’t satisfy me either.

Hopefully I found this issue: IDEABKL-5496 – Auto-filling the actual Java call arguments, where the last comment written just couple days ago has a great news for me!

Making long story short: some German developer made pretty cool plugin, that fix this problem!

https://github.com/kontext-e/AutoFillingCallArguments

Usage is very simple – you don’t need to know new shortcut, just press “Alt+Enter” and select “Fill …” from suggestions menu. It rocks!

[Guava] Preconditions

Preconditions should be ideal for situations, where there is no specific business exceptions for checking arguments and performance is not an issue.

JavaDoc:

https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/Preconditions.html

Example:

   /**
    * Returns the positive square root of the given value.
    *
    * @throws IllegalArgumentException if the value is negative
    */
   public static double sqrt(double value) {
     Preconditions.checkArgument(value >= 0.0, "negative value: %s", value);
     // calculate the square root
   }

 

Here is more about checking conditions:

https://github.com/google/guava/wiki/ConditionalFailuresExplained

Using Spock for testing code – getting started with syntax (1)

Getting started guide:
http://spockframework.org/spock/docs/1.0/spock_primer.html

Interesting things:

  • cool way to verify throwing exceptions:
    then: 
    EmptyStackException e = thrown() e.cause == null
  • instead of writing, “when-then”, just use “except”:
    expect:
    Math.max(1, 2) == 2
  • to parameterize test with different data, use “where” syntax:
    def "computing the maximum of two numbers"() {
    expect:
    Math.max(a, b) == c
    
    where:
    a << [5, 3]
    b << [1, 9]
    c << [5, 9]
    }
  • in helper method use “assert” keyword:
    assert pc.vendor == "Sunny"

Spring security handle AJAX XHR request for logout users and response 401 instead of 302

I found this article with filter implementation:

Spring Security part VI : Session Timeout handling for Ajax calls

However in comment someone point that we can simply extend “LoginUrlAuthenticationEntryPoint”.

My solution sets only status 401 and doesn’t write anything to response body:

	static class AjaxAwareLoginUrlAuthenticationEntryPoint
			extends LoginUrlAuthenticationEntryPoint {

		public AjaxAwareLoginUrlAuthenticationEntryPoint(String loginFormUrl) {
			super(loginFormUrl);
		}

		public void commence(HttpServletRequest request, HttpServletResponse response,
				AuthenticationException authException) throws IOException, ServletException {

			if (isAjaxRequest(request) && authException != null) {
				response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
				response.flushBuffer();
				return;
			}
			super.commence(request, response, authException);
		}

		private static boolean isAjaxRequest(HttpServletRequest request) {
			return "XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"));
		}
	}

Workaround for “X-Forwarded” headers in Spring UriComponentsBuilder

When our application server user proxy, that doesn’t set “X-Forwarded” headers with original request port, then we can use some hack in our application. Just ignore port when it is equal 80 or 443, cause it is default. For example instead of "http://localhost:80/app", we will get "http://localhost/app". And "https://localhost:443/app" will output "https://localhost/app".

UriComponentsBuilder currentUrl = ServletUriComponentsBuilder
					.fromCurrentRequest();
int port = currentUrl.build().getPort();
currentUrl.port(port != 80 && port != 443 ? port : -1);
String currentUrlString = currentUrl.toUriString();

Inspired by Mat Banik strackoverflow answer – http://stackoverflow.com/a/5212336/5770135.