Archive for the ‘Programming’ Category

RIA’s war

Thursday, August 2nd, 2007

RIA warIsn’t this me-too RIA buzz a little bit naive? A long time ago Java “invented” applets - a great idea for RIA that has gracefully failed. Flash has taken the seat. MS’s silverlight is not getting developers anywhere for too many reasons. JavaFX requires the latest Java JVM installed on the user’s computer. I believe in the power of the Sun, but the UI is not their strongest side. Flex is the only way to go if you want to have a good-looking RIA application. Almost everybody has it installed and it is a really quick download if you don’t have it. Great look, nice cooperation with J2EE technology.

Fake it!

Friday, July 13th, 2007

Sometimes you need test data to... test some class or jsp page. Here is the java class that can be of help to someone.



JAVA:

  1. import org.apache.commons.lang.RandomStringUtils;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.apache.commons.lang.math.RandomUtils;
  4.  
  5. public class Fake {
  6.  
  7.     public static String getAddress() {
  8.         String str = RandomStringUtils.randomNumeric(2) + "/"
  9.                 + RandomStringUtils.randomNumeric(3);
  10.         String streetName = get(7) + 8;
  11.         streetName = StringUtils.capitalise(streetName);
  12.         str += " ";
  13.         str += streetName;
  14.         return str;
  15.     }
  16.  
  17.     public static String getWord() {
  18.         String streetName = RandomStringUtils.randomAlphabetic(7).toLowerCase();
  19.         streetName = StringUtils.capitalise(streetName);
  20.         return streetName;
  21.     }
  22.  
  23.     public static String get(int maxLength) {
  24.         int l = RandomUtils.nextInt(maxLength);
  25.         return RandomStringUtils.randomAlphabetic(maxLength + 3).toLowerCase();
  26.     }
  27.    
  28.     public static String getSentence(int numberOfWords) {
  29.         StringBuffer sb = new StringBuffer();
  30.         for (int i = 0; i <numberOfWords; i++) {
  31.             sb.append(get(16));
  32.             sb.append(" ");
  33.         }
  34.         return StringUtils.capitalise(sb.toString());
  35.     }
  36.  
  37.     public static String getEmail() {
  38.         StringBuilder sb = new StringBuilder(get(4));
  39.         sb.append("@");
  40.         sb.append(get(4));
  41.         sb.append(".");
  42.         sb.append(RandomStringUtils.randomAlphabetic(3).toLowerCase());
  43.         return sb.toString();
  44.  
  45.     }
  46.  
  47.     public static int getId() {
  48.         return RandomUtils.nextInt(99999);
  49.     }
  50.  
  51.     public static String getPhoneNumber() {
  52.         return RandomStringUtils.randomNumeric(12);
  53.     }
  54.  
  55.     public static String getPostalCode() {
  56.         return RandomStringUtils.randomNumeric(7);
  57.     }
  58.  
  59.     public static String getState() {
  60.         return RandomStringUtils.randomAlphabetic(3).toUpperCase();
  61.     }
  62. }

Java 3D Application in JavaScript (Irrlicht & Jirr)

Tuesday, April 24th, 2007

With the new features of Java 1.6 it is incredibly easy to add scripting capabilities to your programs. By using JavaScript you do not have to recompile your application every time you change something, also JavaScript is much easier especially for those new to Java.

Check out the way we implement an Irrlicht application in JavaScript. Of course, you have to download Jirr & have JDK 1.6 installed.

This is the Java program to load and execute the JavaScript code:

JAVA:

  1. package com.javazing.gameengine;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.Reader;
  9.  
  10. import javax.script.ScriptEngine;
  11. import javax.script.ScriptEngineManager;
  12.  
  13. /**
  14. *
  15. * This program loads and executes the JavaScript program
  16. *
  17. * @author Sergey Nechaev (snechaev@javazing.com)
  18. *
  19. */
  20. public class GameEngine {
  21.  
  22.     private static final String SCRIPT_ENGINE_NAME = "js";
  23.  
  24.     private static final String SCRIPT_FILE_NAME = "script/js.js";
  25.  
  26.     private ScriptEngine jsEngine;
  27.  
  28.     private ScriptEngineManager mgr;
  29.  
  30.     static {
  31.         System.loadLibrary("irrlicht_wrap");
  32.     }
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.  
  36.         new GameEngine();
  37.  
  38.     }
  39.  
  40.     public GameEngine() throws Exception {
  41.  
  42.         mgr = new ScriptEngineManager();
  43.         jsEngine = mgr.getEngineByExtension(SCRIPT_ENGINE_NAME);
  44.  
  45.         // InputStream is = this.getClass().getResourceAsStream("js.js");
  46.         File file = new File(SCRIPT_FILE_NAME);
  47.         InputStream is = new BufferedInputStream(new FileInputStream(file));
  48.  
  49.         Reader reader = new InputStreamReader(is);
  50.         jsEngine.eval(reader);
  51.  
  52.     }
  53.  
  54. }

And the JavaScript with the 'HelloWorld' example from the Irrlicht distribution:

JavaScript:

  1. importPackage(net.sf.jirr);
  2. importPackage(java.lang);
  3.  
  4.  
  5. var device = Jirr.createDevice(E_DRIVER_TYPE.EDT_OPENGL,
  6. new dimension2di(640, 480), 16, false, false, false, null);
  7.  
  8. print("idevice = " + device);
  9.  
  10. var windowCaption = "Java@Hello World! - Irrlicht Engine Demo";
  11. device.setWindowCaption(windowCaption);
  12.  
  13. var driver = device.getVideoDriver();
  14. var smgr = device.getSceneManager();
  15. var guienv = device.getGUIEnvironment();
  16.  
  17. var text = "Hello World! This is the Irrlicht Software engine for Java!";
  18. var rect = new recti(10, 10, 280, 30);
  19. var staticText = guienv.addStaticText(text, rect, true, true, null, -1);
  20.  
  21. smgr.addCameraSceneNode(null, new vector3df(0,10,-60),
  22. new vector3df(0,0,0), -1);
  23.  
  24. var step = 100;
  25. var counter = 0;
  26. var diff /*= 0*/;
  27. var timer1 = System.currentTimeMillis();
  28. var timer2 /*= 0*/;
  29. while(device.run())
  30. {
  31.  
  32.  
  33.     var a = 0;
  34.     var r = 100;
  35.     var g = 100;
  36.     var b = 100;
  37.  
  38.     driver.beginScene(true, true, new SColor(a, r, g, b));
  39.  
  40.     smgr.drawAll();
  41.     guienv.drawAll();
  42.  
  43.     driver.endScene();
  44.  
  45.     counter++;
  46.     if (counter>= step)
  47.     {
  48.         timer2 = System.currentTimeMillis();
  49.         diff = 1000.0 / ((timer2 - timer1) / step);
  50.         counter = 0;
  51.         timer1 = System.currentTimeMillis();
  52.  
  53.         //text = Jirr.createWchar("This is the Irrlicht Software
  54.         //engine for Java! FPS: " + diff);
  55.         text = "This is the Irrlicht Software engine for Java! FPS: " + diff;
  56.         staticText.setText(text);
  57.     }
  58. }
  59.  
  60. device.closeDevice();

Protect Your Contact Form against Spammers

Friday, April 6th, 2007

‘Contact Us’ page

How a user can contact the administration of a web site? Usually every web site has a ‘contact us’ page with the contact details of people in charge. Moreover, this is not really convenient. A user has to start his mail client, to make sure the address of the recipient is correct, to make sure the mail account (if he/she has many) is correct, and to add the subject and so on.

Those people who have no mail client program installed (probably, millions including my mother and sister) have to log in to their HOTMAIL or YAHOO accounts and compose a letter. In addition, the spammers have those programs that scan the Internet to collect e-mail addresses. Very soon, the owners of the web site start receiving hundreds and thousands of spam letters. Moreover, the important letter from the customer can be deleted accidentally by the person in charge or by the sophisticated ‘anti-spam’ program.

As you can see a regular ‘contact us’ page with an e-mail specified is pain in the arse for everyone involved. What is the proper way to go? A custom form made specifically to serve the guests and the administration of a web site will handle that simple but important task for you.

All the user has to do is to write (or select) a proper subject, his name and e-mail, write his message and hit the ‘SEND’ button. Fast and furious.

Though the form on the screenshot requires user’s e-mail address and name it is sometimes better not to require them. Instead of performing unnecessary validations and getting fake names and emails anyway, you may want to let it go.

Stop Spammers

The form is simple for a web developer, what I would like to emphasize is the potential vulnerability of the form. The spammers may use your form to perform mass mailing campaigns on behalf of your server. The rule number one here is to make sure the user information is never set to the headers of the generated e-mail address. Your code MUST have hardcoded FROM, TO, SUBJ, CC fields. All the information from the contact form MUST be written to the body of the message. Additionally you may want to filter it against HTML and JavaScript tags, though some may see this is paranoid.

Stop Spam

Additionally you may want to stop people from spamming the mailbox that receives the contact form messages. Some web developers add CAPTCHA (a picture of random numbers) to the contact form. Personally I hate CAPTCHAs, it is such a pain to ‘guess’ what is written there. In addition, any advanced spammer will get around it: they use XXX sites with photos and your CAPTCHA in a frame. Moreover, in order to see the next photo, the XXX maniac deciphers your CAPTCHA. Nice approach.

I prefer filtering the user’s input on the server side against a list of well-known words such as ‘stock’ or ‘Viagra’ words. Just make sure your users do not need to submit this information for your site.

Conclusion

And there is a rule number two for every web developer when defending your web site: be quiet. It is so tempting sometimes to give an ironic feedback – do not do that. Silence is the secret weapon here. Let your code do nothing or react with a regular response.

Good luck!