Are you the fan of Harry Potter?
Do you want to listen to audios of Harry Potter Series?
Here are two easy options for you
1. DOWNLOAD JAR file (This is to be run with Java 8 Update 121 or above)
2. DOWNLOAD APK file to run in your Android Device
Sunday, January 29, 2017
Tuesday, January 24, 2017
Too Many Redirect with SSL in NGINX and Jetty Server
Recently I faced a very nasty problem.
I am using Nginx as my web server and Java Web Application as App Server hosted in Jetty.
I was required to host all the pages using SSL i.e. HTTPS. I enabled Nginx to handle the HTTPS requests at the port 443. Nginx did handle the https requests however Jetty server received all the HTTPS requests as HTTP. There I had the logic to redirect all HTTP to HTTPS. So I fell into the problem that it was redirecting again and again, therefore causing Too Many Redirect error.
I had to spent a lot of time and finally I found the error and hence solution.
While the problem was HTTPS scheme was getting lost at load balancer. This could be solved using the following snippet into the web.xml
I am using Nginx as my web server and Java Web Application as App Server hosted in Jetty.
I was required to host all the pages using SSL i.e. HTTPS. I enabled Nginx to handle the HTTPS requests at the port 443. Nginx did handle the https requests however Jetty server received all the HTTPS requests as HTTP. There I had the logic to redirect all HTTP to HTTPS. So I fell into the problem that it was redirecting again and again, therefore causing Too Many Redirect error.
I had to spent a lot of time and finally I found the error and hence solution.
While the problem was HTTPS scheme was getting lost at load balancer. This could be solved using the following snippet into the web.xml
<context-param>
<param-name>org.mortbay.jetty.servlet.SessionPath</param-name>
<param-value>/</param-value>
</context-param>
<filter>
<filter-name>XForwardedFilter</filter-name>
<filter-class>fr.xebia.servlet.filter.XForwardedFilter</filter-class>
<init-param>
<param-name>protocolHeader</param-name>
<param-value>x-forwarded-proto</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>XForwardedFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
Labels:
http,
https,
java,
jetty,
load balancer,
nginx,
ssl,
too many redirects,
web application,
XForwardedFilter
Thursday, January 5, 2017
Best Loop Idiom for special casing the last element
References: StackOverflow1 StackOverflow2
Very often it happens that you need to iterate through the elements of a list/array and treat the first/last element in a special way.
For example: if you have an array {"tom", "jerry", "dog"} and want to print in a comma separated fashion.
Normally I end up appending Comma (",") after each element and finally after the loop exits, I just delete the last element which is unnecessary comma.
Turns out there are few elegant way to achieve this. One of the way is:
This method is in fact used by Joiner class in Google Collections.
My initial suspicion was on Arrays.asList() method. I thought that the complexity of this method was O(n).
However the time complexity of this method is actually O(1) because list is merely a wrapper around the array.
Java API Documentation: Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)
Very often it happens that you need to iterate through the elements of a list/array and treat the first/last element in a special way.
For example: if you have an array {"tom", "jerry", "dog"} and want to print in a comma separated fashion.
Normally I end up appending Comma (",") after each element and finally after the loop exits, I just delete the last element which is unnecessary comma.
Turns out there are few elegant way to achieve this. One of the way is:
Iterator itemIterator = Arrays.asList(items).iterator();
if (itemIterator.hasNext()) {
// special-case first item. in this case, no comma
while (itemIterator.hasNext()) {
// process the rest
}
}
Complexity of Arrays.asList(items)
My initial suspicion was on Arrays.asList() method. I thought that the complexity of this method was O(n).
However the time complexity of this method is actually O(1) because list is merely a wrapper around the array.
Java API Documentation: Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)
Subscribe to:
Posts (Atom)