Last active
August 29, 2015 14:01
-
-
Save mrg/b814a42d86597440c9d9 to your computer and use it in GitHub Desktop.
Tapestry 5 service to determine User Agent of common browsers. Can easily be expanded upon to include mobile browsers, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a Tapestry 5 service to query a browser's User Agent. | |
Inject it into your page when you need to do browser-specific | |
customizations. Make it a property and you can query via TML. | |
Add to AppModule.java to make the service available: | |
public static void bind(ServiceBinder binder) | |
{ | |
... | |
binder.bind(UserAgent.class, UserAgentImpl.class); | |
... | |
} | |
Then in a Java component/page: | |
@Inject | |
@Property | |
private UserAgent userAgent; | |
And in the corresponding TML component/page: | |
<t:if t:test="userAgent.IE6"> | |
Do something IE6-specific here. | |
</t:if> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Put this in your Tapestry src/main/java/.../services folder: | |
public interface UserAgent | |
{ | |
String getUserAgent(); | |
boolean isIE(); // Any version of IE. | |
boolean isIE6(); // Any version of IE 6. | |
boolean isIE7(); // Any version of IE 7. | |
boolean isIE8(); // Any version of IE 8. | |
boolean isIE9(); // Any version of IE 9. | |
boolean isBadIE(); // Any version of IE 6, 7, or 8. | |
boolean isChrome(); // Any version of Chrome. | |
boolean isFirefox(); // Any version of Firefox. | |
boolean isSafari(); // Any version of Safari. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Put this in your Tapestry src/main/java/.../services folder (or sub-folder): | |
import ....services.UserAgent; | |
import org.apache.tapestry5.ioc.annotations.Inject; | |
import org.apache.tapestry5.services.Request; | |
// Modified from https://gist.github.com/bryanjswift/318237 which contains many more options. | |
public class UserAgentImpl implements UserAgent | |
{ | |
// Standard desktop browser version strings. | |
public static final String IE = "MSIE"; | |
public static final String IE60 = "MSIE 6.0"; | |
public static final String IE61 = "MSIE 6.1"; | |
public static final String IE7 = "MSIE 7.0"; | |
public static final String IE8 = "MSIE 8.0"; | |
public static final String IE9 = "MSIE 9.0"; | |
public static final String CHROME = "Chrome"; | |
public static final String FIREFOX = "Firefox"; | |
public static final String SAFARI = "Safari"; | |
@Inject | |
private Request request; | |
public String getUserAgent() | |
{ | |
return request.getHeader("User-Agent"); | |
} | |
public boolean isIE() | |
{ | |
return getUserAgent().indexOf(IE) != -1; | |
} | |
public boolean isIE6() | |
{ | |
return getUserAgent().indexOf(IE60) != -1 && getUserAgent().indexOf(IE61) != -1; | |
} | |
public boolean isIE7() | |
{ | |
return getUserAgent().indexOf(IE7) != -1; | |
} | |
public boolean isIE8() | |
{ | |
return getUserAgent().indexOf(IE8) != -1; | |
} | |
public boolean isIE9() | |
{ | |
return getUserAgent().indexOf(IE9) != -1; | |
} | |
public boolean isBadIE() | |
{ | |
return isIE6() || isIE7() || isIE8(); | |
} | |
public boolean isChrome() | |
{ | |
// Chrome contains CHROME and SAFARI, so just test for CHROME. | |
return getUserAgent().indexOf(CHROME) != -1; | |
} | |
public boolean isFirefox() | |
{ | |
// Firefox contains FIREFOX. | |
return getUserAgent().indexOf(FIREFOX) != -1; | |
} | |
public boolean isSafari() | |
{ | |
// Safari contains SAFARI, but not CHROME, so test for SAFARI with a missing CHROME. | |
return getUserAgent().indexOf(SAFARI) != -1 && getUserAgent().indexOf(CHROME) == -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment