Spartan Java

Tips

Protecting web requests

by on Jul.15, 2011, under JavaScript, Security, Tips, Web related

Afraid of malicious injections in your web app requests, heres a simple way to improve your application security. Push every request parameter through a filtering function before it’s feeded to your application code.

Such a function can be as simple as:

1
2
3
4
5
6
7
8
9
10
private String cleanParameter(String value) {
   if (value != null) {
      value = value.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
      value = value.replaceAll("\\(", "&#40;").replaceAll("\\)", "&#41;");
      value = value.replaceAll("'", "&#39;");
      value = value.replaceAll("eval\\((.*)\\)", "");
      value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
   }
   return value;
}

This will escape/remove potentially dangerous Javascript code and HTML/XML tags.

You can implement this on a web filter or a struts interceptor or a DWR filter depending on the technology you use for you app.

Leave a Comment :, , , , , , , more...

View Androids emulator log from Eclipse

by on Nov.19, 2009, under Android, Tips

Sounds like it should be quite straightforward, right? Well, it is, but for some reason it took me more than a few minutes (maybe I need to get more sleep :P). Anyway, to view Androids emulator log from Eclipse go to Window / Show View / Other… / Android / LogCat

1 Comment :, , , , more...

Get a web page programatically from Android

by on Nov.18, 2009, under Android, Tips

The Google folks were kind enough to include a version of the Apache HTTP Client in the Android SDK, ergo loading a web resource/page from our Android apps is really simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.spartanjava.com");
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
 
BufferedReader reader = new BufferedReader(
    new InputStreamReader(
      response.getEntity().getContent()
    )
  );
 
String line = null;
while ((line = reader.readLine()) != null){
  result += line + "\n";
}
 
// Now you have the whole HTML loaded on the result variable
8 Comments :, , , more...

Obovweb source code – a Google Appengine sample

by on Oct.20, 2009, under Security, Tips, Web related

Someone asked me about the implementation details of the Google Appengine sample I published a few weeks ago (http://obovweb.appspot.com/). Well, you can get a hold of the source code here.

There’s not much to tell, apart from the specific HMAC-SHA1 implementation (which you can check out in Obovs source code) it’s a very simple JSP/DWR application.

Leave a Comment :, , , , , , , , , more...

Enabling “Enter” in a form without a submit button

by on Oct.13, 2009, under JavaScript, Tips

I find it quite annoying when I can’t post/confirm a web form using the Enter key.

Here is a nifty little javascript trick to do it when you do not need/want a submit input in your html.
(continue reading…)

Leave a Comment :, , , , more...

Java application as a MS Windows service

by on Jul.29, 2009, under Tips

It’s really annoying to run something interactively on MS Windows when you know deep inside it should be an OS Service. Doing this on a *nix system is quite simple (most of the times you just need to write a short shell script), but on MS Windows it’s not so easy.
(continue reading…)

Leave a Comment :, , , , , , , more...

Reload resource bundles on the fly, finally!

by on May.13, 2009, under Tips

Ever since the very first time I used a properties file in Java I’ve wanted to be able to reload it upon a sysadmin request or some other circumstance without having to reload the whole application, reset the application server or use some dark proprietary Sun APIs. Well, finally in Java 1.6 we can do it!!
(continue reading…)

2 Comments :, , , more...

Fast collection look-ups

by on Apr.30, 2009, under Performance, Tips

I recently had to load a bunch of objects into memory and then perform thousands of look-ups over that collection. Using the good old java.util.ArrayList just didn’t cut it, the contains() function is extremely slow (as you would guess of course since this implementation stores elements as they are inserted and without any aditional indexing structure).
(continue reading…)

6 Comments :, , more...

Automatically serialize POJOs to and from JSON

by on Apr.22, 2009, under JavaScript, Tips, Web related

The best tool I’ve found so far to serialize POJOs to JSON (and back again) is XStream, it’s automatic, simple and elegant, check it out.

2 Comments :, , , more...

PHP & Java interoperable encryption

by on Jan.16, 2009, under Articles, Security, Tips

I recently faced the problem of encrypting something in PHP and decrypting it using Java, this proved to be a little more of a challenge than what it initially seemed like. (continue reading…)

3 Comments :, , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...