Detecting IE version modes and trident token

First solution

Did you tried uing Conditional Comments
If you are specifically interested in Internet Explorer, conditional comments might be a more appropriate choice. The following example shows an effective way to use conditional comments to display custom content.

1
2
3
4
5
6
7
8
9
10
11
12
13
<!--[if gte IE 8]>
 
You're using a recent version of Internet Explorer.
 
<![endif]-->
 
<!--[if lt IE 7]>
 
Hm. You should upgrade your copy of Internet Explorer.
 
<![endif]-->
 
<![if !IE]>

You’re not using Internet Explorer.

<![endif]>

Second Solution

Two things to remember for IE, one is different modes for e.g. IE 10 can run in IE 9 mode or IE 8 mode and so on,
second compatiblity mode (http://stackoverflow.com/questions/17675520/difference-between-ie9-and-ie9-compatibility-view-browser-mode)
>>
Websites that are designed for older versions of Windows Internet Explorer don’t always display as expected in the current version. We addressed this in Windows Internet Explorer 8 by adding the Compatibility View function that allows users to “revert” to a previous browser version of the platform, which emulates IE7 Standards mode.
<<

I used the browserstack.com to use different browsers and hit the request on http://request.urih.com/ to get the request headers for every browser version with its different modes and one compatiblity mode.
For compatiblity mode IE always send MSIE 7.0 as the user agent but Trident tokens tells the actual version.

Trident token, which was introduced in IE8

See below the user agent string for all browsers

See the Trident token is same for IE 11 i.e. Trident/7.0 means IE 11
IE 11 Default
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko

10 mode
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
9 mode
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
8 mode
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko

compatiblity mode in IE 11
    Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729;     .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; .NET4.0C; InfoPath.2; .NET4.0E; GWX:QUALIFIED)

 

In IE 10 Trident is changed per mode
10 mode
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
9 mode
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
8 mode
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)
7 mode
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)
IE 10 Compatiblity mode (see Trident is 6)
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)

IE 9 (see Trident is 5)
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)
8 mode
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)
7 mode
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)
9 Compatibility View (see Trident is 5)
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/5.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)

IE8 (see Trident is 4)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)
Check this link

http://stackoverflow.com/questions/5825385/javascript-can-i-detect-ie9-if-its-in-ie7-or-ie8-compatibility-mode

 

 

Jackson Annotation Cheat Sheet

Jackson Json Annotation Cheat sheet

 

NOTE – To avoid combinatorial explosion, I have used the following notation: If a property has more than one possible option, the options are comma separated, enclosed in double braces.

@JsonIgnoreProperties

  • Used at the class level, it ignores certain properties when serializing/deserializing an object.
    • @JsonIgnoreProperties(ignoreUnknown=true) – Used at the class level, it ensures that properties that are in the JSON string with no mapping to the object are ignored and no error is thrown on deserialization.
    • @JsonIgnoreProperties({“prop1″, “prop2″}) – Used at the class level, it ensures that listed properties (prop1, prop2) are ignored on serialization.

Example:

@JsonIgnoreProperties({“name”, “age”}}

class DemoPerson {

public String name;

public int age;

public double height;

}

When a DemoPerson object is serialized or deserialized, the name and age PIVs are not serialized and deserialized i.e. only height is serialized and deserialized.

@JsonIgnore

  • Used at the method or field level, it completely ignores the property during serialization and deserialization regardless of other annotations (such as @JsonAutoDetect).

Example:

class DemoPerson {

@JsonIgnore

public String name;

@JsonIgnore

public int age;

public double height;

}

When a DemoPerson object is serialized or deserialized, the name and age PIVs are not serialized and deserialized i.e. only height is serialized and deserialized. (Equivalent to @JsonIgnoreProperties({“name”, “age”}} used at the class level).

Read full story »

Spring Cache configurations to remember

Things to remember while configuring Spring Cache

For this e.g. I am using ehcache

Maven Dependencies

<properties>
       <spring.framework.version>4.1.5.RELEASE</spring.framework.version>
</properties>
<dependency>
      <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
     <version>${spring.framework.version}</version>
 </dependency>
 <dependency>
     <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
     <version>${spring.framework.version}</version>
 </dependency>
<!-- Ehcache --> 
         <dependency>
               <groupId>javax.cache</groupId>
              <artifactId>cache-api</artifactId>
              <version>1.0.0</version>
       </dependency>

    <dependency>
         <groupId>net.sf.ehcache</groupId>
       <artifactId>ehcache</artifactId>
        <version>2.9.0</version>
   </dependency>

Spring App-Context configuration

Enable caching annotations in Spring application context and configure ehcache

<?xml version="1.0" encoding="UTF-8"?> Read full story »

Java DNS Cache and JVM TTL

When ever your java program performs a network operation the Domain name service (DNS) settings get cached inside JVM, meaning the IP address of the other machine get cached in the JVM for a period of time usually called TTL (a.k.a Time to Live) . During this time Java uses the cached IP address, even if the IP address gets changed for the machine your are trying to connecting to. Therefore if TTL is set to 300 secs and  the server or resource you are trying to connect to changes its IP address the java application won’t be able to connect to it. This can be solved if we restart the JVM which will flush out the cache.

We face this issue all the time when ever we are trying to move our  server cluster A to server cluster B, we need to reset or restart the clients connecting to those server clusters. For JDK 1.4 clients the default TTL settings is that it will cache the DNS for ever. The TTL value differs in version to version.

 

We can disable Java/JVM DNS caching by adding

1
-Dsun.net.inetaddr.ttl=0

on the command line starting the JVM or we can set TTL  in the file java.security, which is located in the directory %JRE%\lib\security.

following shows how to configure the TTL to 30 seconds.

1
networkaddress.cache.ttl=30

 

IP address is a unique address/number assigned to each computer/printer/device on internet. It is used to identify a computer on the internet. IPv4 is the most  commonly used Internet Protocol Version, its format is 0.0.0.0 (127.0.0.1) for local address.

The latest version of Internet Protocol to date  (Sept 2014) is IPv6. IPv6 is intended to replace IPv4 but still 96% of the internet traffic is still using IPv4.

If your java program (HttpServletRequest.getRemoteAddr() ) is returning IPv6 format of IP address and you need it to return IPv4 format then you need to add the following JVM param at your server startup.

1
-Djava.net.preferIPv4Stack=true

 

Quickly Maven

Maven

Maven

Quickly Maven

In short, maven is a build automation tool describing the structure of the Java project and its dependencies. Maven addresses dependency management, artifact versioning and project comprehension.

We already went through a brief steps to convert a ANT based project to maven in this article Convert an ANT project to Maven

Dependency management and transitive dependency

Dependency management allows combining and centralizing the management of dependencies and their versions via single configuration file (pom.xml).

Transitive Dependency can be simply explained as a functional dependency between dependencies for e.g. if Aà B and Bà C then AàC therefore A is transitive dependent on C. If project A has a dependency project B and project B has a dependency on project C, then while building the project A maven with automatically download all the dependencies of project A i.e. project B and will also download all the dependencies of project B and this will go on till nth level.

Make sure you don’t end up with cyclic dependency like AàB BàC CàA

Dependency Scope defines when these dependencies are needed for e.g compile means this dependency is needed at the Read full story »

Java DateUtils methods

Some Java Utility methods for date conversions

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private String getUTCTimeAfter(int hours)
{
 
SimpleDateFormat sdfUTC = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
sdfUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = new Date(System.currentTimeMillis()
+ (hours * 60 * 60 * 1000));
return sdfUTC.format(date);
}
 
private Date getLocalTime(String utcDateStr, SimpleDateFormat sdfUTC)
throws Exception
{
SimpleDateFormat sdfLocal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdfLocal.setTimeZone(TimeZone.getDefault());
 
Date date = sdfUTC.parse(utcDateStr);
String localTimeStr = sdfLocal.format(date.getTime());
return sdfLocal.parse(localTimeStr);
 
}

Java Convert LocalTime to UTC Time

Converts Local future/previous date in String to UTC date

A simple utility method to convert local time into UTC time. The important thing in this method is, you will find a lot of examples on the internet that changes the “current” local time to UTC time, but you will hardly find an example where you will change an old local time stored as string and change it to UTC time for that moment of time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 public static String convertLocalTimeToUTC(String saleTimeZone, String p_localDateTime) throws Exception{
 
  String dateFormateInUTC="";
  Date localDate = null;
  String localTimeZone ="";
  SimpleDateFormat formatter;
  SimpleDateFormat parser;
  localTimeZone = saleTimeZone;
 
  //create a new Date object using the timezone of the specified city
  parser = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
  parser.setTimeZone(TimeZone.getTimeZone(localTimeZone));
  localDate = parser.parse(p_localDateTime);
  formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss z'('Z')'");
  formatter.setTimeZone(TimeZone.getTimeZone(localTimeZone));
  System.out.println("convertLocalTimeToUTC: "+saleTimeZone+": "+" The Date in the local time zone " +   formatter.format(localDate));
 
  //Convert the date from the local timezone to UTC timezone
  formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
  dateFormateInUTC = formatter.format(localDate);
  System.out.println("convertLocalTimeToUTC: "+saleTimeZone+": "+" The Date in the UTC time zone " +  dateFormateInUTC);
 
 return dateFormateInUTC;
 }
 
  public static void main(String arg[]) throws Exception
    {
 
    	convertLocalTimeToUTC("EST", "12-03-2013 10:30:00");
    	convertLocalTimeToUTC("PST", "12-03-2013 10:30:00");
    }

Hope this helps.

Java Heap Dump

After JDK6 its easy to generate Java Heap dumps using jmap.
here is a quick example, make sure your user has the permission to write on the dump folder.

./jmap -dump:live,format=b,file=/var/tmp/abc.hprof pid
where pid is the process id, live option ensures to dump only live objects (yes JVM executes FULL GC before dumping heap)

you can search your java process pid using
ps -ef | grep java

once done you might need to give permission right to download the file, I just simply change the mode to all permission.
chmod 777 /var/tmp/abc.hprof

Once downloaded, I use visualvm or jvisualvm to analyze the heap dumps.

General Contract for Equals and Hashcode

Java Equals and Hashcode general Contract

Overiding BOTH, equals and hashcode method is not a common practice among java developers, but is strongly recommended in order to avoid the followings.

1) duplicate Objects in a set
2) list’s contains method returns false even you have added that object in the list
3) Can not find same content object in a hashtable even the contents of the objects are same

First thing first:
The first thing you must remember is when ever you override equals method, you must override the hashcode method as well.
The reason for doing so is the default implementation of equals method is to compares the memory addresses of the objects and if you override the equals() and you dont override hashcode you are violating the general contract for Object.hashCode, which can have unexpected consequences when your class is used with hash-based collections like hashmap and hashtable

The simplified version of General Contract for HashCode method

a) The hashcode must return the same integer value, if called multiple times on the same object, considering that the object properties are not modified.

b) If the equals method returns true for two objects then the hashCode method of the two objects must return the same integer value.

c) If two objects are not equal, there hashcode still can be the same but its a good practice producing distinct integer results for unequal objects becuase it may improve the performance of hashtables.

more to come when ever I got time/chance to write…

 

Einfobuzz Site Map