How can an Java applet access files inside the jar file the applet is stored in?

Often time, we bundle our config file as text file or XML file and store it together with Applet jar file, and deploy it together to client. For instance, an jtest.jar file , include a Java Applet class (jtest.java) ann a XML config file.

jtest.jar
  |--jtest.java
  |--config.xml

How can we access the “config.xml” XML file in “jtest.java” class? The simplest solution is using “getResourceAsStream” to get the config.xml file content in InputStream format.

InputStream is = jtest.class.getResourceAsStream("/config.xml");

Done. :)

Please refer to the following website to understand more about the Java’s resource concept.
http://java.sun.com/j2se/1.4.2/docs/guide/resources/resources.html

Here is a simple example to demonstrate how to generate a file checksum value in Java (SHA-1).

import java.io.FileInputStream;
import java.security.MessageDigest;
 
public class TestCheckSum {
 
  public static void main(String args[]) throws Exception {
 
    String datafile = "c:\\INSTLOG.TXT";
 
    MessageDigest md = MessageDigest.getInstance("SHA1");
    FileInputStream fis = new FileInputStream(datafile);
    byte[] dataBytes = new byte[1024];
 
    int nread = 0; 
 
    while ((nread = fis.read(dataBytes)) != -1) {
      md.update(dataBytes, 0, nread);
    };
    byte[] mdbytes = md.digest();
 
    //convert the byte to hex format
    StringBuffer sb = new StringBuffer("");
    for (int i = 0; i < mdbytes.length; i++) {
    	sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
    }
 
    System.out.println("Digest(in hex format):: " + sb.toString());
 
  }
}

Result

Digest(in hex format):: bf35fa420d3e0f669e27b337062bf19f510480d4

The “INSTLOG.TXT” file has a “bf35fa420d3e0f669e27b337062bf19f510480d4″ SHA-1 checksum value. If we want to generate a Checksum value in MD5 format , we can change the MessageDigest as following

MessageDigest md = MessageDigest.getInstance("MD5");

More detail about Message Digest Algorithms, please check following website
http://java.sun.com/j2se/1.4.2/docs/guide/security/CryptoSpec.html#AppA

Reference

1) http://www.itl.nist.gov/fipspubs/fip180-1.htm
2) http://java.sun.com/j2se/1.4.2/docs/api/java/security/MessageDigest.html
3) http://java.sun.com/j2se/1.4.2/docs/guide/security/CryptoSpec.html

Often time, while we downloading files or software via some secure website, “Checksum” values in MD5 or SHA1 format are provided to protect users from downloading corrupted files or trojan infected files. Unfortunately, many users has no idea how to verify the downloaded file with “Checksum” value, end up in downloading virus or trojan infected files.

Marxio File Checksum Verifier

Software Name : Marxio File Checksum Verifier
Website : http://www.marxio-tools.net/en/marxio-fcv.php
Version : 1.3.4

The Marxio File Checksum Verifier is a handy freeware tool which can help us to calculate and verify Checksum value in following major checksum types.

- CRC32,MD4,MD5,SHA1,SHA-256,SHA-384,SHA-512,RIPEMD-128,RIPEMD-160,HAVAL 256,TIGER 192.

how-to-verify-file-checksum

Jacksum

Software Name : Jacksum
Website : http://www.jonelo.de/java/jacksum/index.html
Version : 1.7.0

The Jacksum is a freeware tool and suitable for more advanced users verify checksum values. It is platform independent software (support Windows and Unix) and supports 58 popular standard algorithms (Adler32, BSD sum, Bzip2’s CRC-32, POSIX cksum, CRC-8, CRC-16, CRC-24, CRC-32 (FCS-32), CRC-64, ELF-32, eMule/eDonkey, FCS-16, GOST R 34.11-94, HAS-160, HAVAL (3/4/5 passes, 128/160/192/224/256 bits), MD2, MD4, MD5, MPEG-2’s CRC-32, RIPEMD-128, RIPEMD-160, RIPEMD-256, RIPEMD-320, SHA-0, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, Tiger-128, Tiger-160, Tiger, Tiger2, Tiger Tree Hash, Tiger2 Tree Hash, Unix System V sum, sum8, sum16, sum24, sum32, Whirlpool-0, Whirlpool-1, Whirlpool and xor8).

The Jacksum is written in Java and open source, we can download the source code and try to understand how Jacksum implement the above algorithm.

Conclusion

Please do the “Checksum” value checking if “Checksum” value is provided from the downloading website, this is very good mechanism to ensure we are downloading what we expected to download. :)

AjaxLazyLoadPanel

A panel where you can lazy load another panel. This can be used if you have a panel/component that is pretty heavy in creation and you first want to show the user the page and the replace the panel when it is ready.

I love this feature , the effect is really awesome and impressive. Here i show how do convert a normal wicket panel to a AjaxLazyLoadPanel.

Original Panel

HTML file

<span wicket:id="price"><span>

Java file

add(new PricePanel("price"));

Lazy load Panel

HTML file (no change)

<span wicket:id="price"><span>

Java file

add(new AjaxLazyLoadPanel("price")
{
  @Override
  public Component getLazyLoadComponent(String id)
  {
       return PricePanel(id);
  }
});

Done, now the PricePanel has the lazy loading effect. Nice

However one of the drawback of this AjaxLazyLoadPanel is Wicket does not has AjaxLazyLoadPanel fallback version. When browser’s javascript is disabled, the image will keep loading forever.

Here is a trick that play around with it.

1) Put following code in Wicket’s application class

protected void init() {		
		getRequestCycleSettings().setGatherExtendedBrowserInfo(true);
}

2) Conditional it

WebClientInfo clientInfo = (WebClientInfo)WebRequestCycle.get().getClientInfo();
if(clientInfo.getProperties().isJavaEnabled()){
add(new AjaxLazyLoadPanel("price")
{
  @Override
  public Component getLazyLoadComponent(String id)
  {
       return PricePanel("price");
  }
});
}else{
  add(new PricePanel("price"));
}

Above function will run the AjaxLazyLoadPanel function if browser’s Javscript or Ajax is enable , else it will delegate to normal request.

Do share with me, if you have any alternative ways to implement the AjaxLazyLoadPanel fallback version :)

Reference

How do detect browser javascript or ajax disabled in Wicket?

Wicket build-in a lot of Ajax fallback components that degrade to a normal request if ajax is not available or Javascript is disabled. Some good examples are AjaxFallbackButton, AjaxFallbackLink… However there are some components that didn’t implement fallback behavior, for instance AjaxLazyLoadPanel. The AjaxLazyLoadPanel will keep display the Wicket’s default image forever if Javascript is disabled.

Is there a function in Wicket to detect the browser’s Javascript is disabled? Yes, it is. Wicket has a build-in mechanism to detect the browser capabilities. However , Wicket turn this function off by default for a reason, i will come back to it later.

Here i demonstrate how to detect browser’s Javascript is disabled in Wicket

1) Put the following code in Wicket application class. It will tell Wicket to gather extra information from browser’s.

protected void init() {		
		getRequestCycleSettings().setGatherExtendedBrowserInfo(true);
}

2) Put the following code in where we want to assert whether browser’s Javascript is disabled.

WebClientInfo clientInfo = (WebClientInfo)WebRequestCycle.get().getClientInfo();
if(clientInfo.getProperties().isJavaEnabled()){
  //Javascript is enable
}else{
 //Javascript is enable
}

3) Done.

Explanation

When user request a page, Wicket will execute a simple Javascript redirect testing in client browser to assert whether browser’s Javascript is disabled, and save it into user’s session. After executed the redirect testing, Wicket will redirect to the original requested page. The whole process is very fast , but we will see a flash of blank page if our connection is slow. This is why Wicket turn it off by default. The flash of black page really annoying, when we seeing this kind of flash of page, we may assume this site is a phishing site.

Wicket redirect testing page is generic and suitable in all of the cases, however the flash of a blank page really unacceptable , at least for me. Does anyone has any better idea?