Some good tips on using spring for testing your code.
InfoQ: Software Testing With Spring Framework
Article: Software Testing With Spring Framework
Good Read: Twelve Best Practices For Spring XML Configurations
Great Must Read for all Developers using spring.
Twelve Best Practices For Spring XML Configurations
Eclipse is running in a JRE, but a JDK is required - FIX
I installed m2eclipse on a new Windows Vista Laptop, and now everytime I open eclipse, the console will show:
11/27/08 12:07:25 PM PST: Eclipse is running in a JRE, but a JDK is required
Some Maven plugins may not work when importing projects or updating source folders.
The fix for this is to add the following lines
C:\Java\jdk1.5.0_16\bin\javaw.exe
to the C:\Java\Apps\eclipse\eclipse.ini file in the eclipse app dir.
GoDaddy MySQL Hosting Database are not accessable outside GoDaddy's Server Farm
I been moving my websites to GoDaddy Hosting and I wanted to start using MySQL Database for one of my sites (thepharaohs.net).
I was able to create a database using the Hosting Contral Center, and Able to login to the DB using the MySQL Web interface. But i'm not able to connect to the database from my Computer...
After calling Custmer Support i was told that I can't... i would have to create my DB on my Computer then import it using the tools but i can't just connect to the MySQL Servers... BOOOOOOOO
Oh well.... i guess i'll have to install MySQL on my computer...
Helpful Java Tag to display request and session Attributes on JSP Page
As a java developer who works on JSP pages once a while, i find it very hard to see what is in the request. I used to work on Coldfusion where the server was able to display debug info about the page and objects on the page but that does not exist in java.
Over the years i keep moving a jsp file that i included when i needed to display dbug info... Today i moved that jsp file into a JavaTag ...
Check it out, it might be useful for you also...
1. Create the following Java Class
package com.mycompany.ui.tag;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
/**
* This tag will spit the request data to the page
*
* @author gdawoud
*
*/
public class RequestDebugTag extends TagSupport {
@Override
public int doEndTag() throws JspException {
try {
pageContext.getOut().print("<hr />");
displayAttributeList();
pageContext.getOut().print("<hr />");
displaySessionList();
} catch (IOException e) {
e.printStackTrace();
}
return super.doEndTag();
}
private void displayAttributeList() throws IOException {
StringBuilder sb = new StringBuilder("Attribute Request List");
sb.append("<table class='debugTable'>");
ServletRequest request = pageContext.getRequest();
Enumeration attributes = request.getAttributeNames();
while (attributes.hasMoreElements()) {
String name = attributes.nextElement();
Object value = request.getAttribute(name);
sb.append("<tr>");
sb.append("<td>").append(name).append("</td>");
sb.append("<td>").append(value).append("</td>");
sb.append("</tr>");
}
sb.append("</table>");
pageContext.getOut().print(sb.toString());
}
private void displaySessionList() throws IOException {
StringBuilder sb = new StringBuilder("Session Attribute List");
sb.append("<table class='debugTable'>");
Enumeration attributes = pageContext.getSession().getAttributeNames();
while (attributes.hasMoreElements()) {
String name = attributes.nextElement();
Object value = pageContext.getSession().getAttribute(name);
sb.append("<tr>");
sb.append("<td>").append(name).append("</td>");
sb.append("<td>").append(value).append("</td>");
sb.append("</tr>");
}
sb.append("</table>");
pageContext.getOut().print(sb.toString());
}
}
2. add to tag to your tld
<tag>
<name>requestDebug</name>
<tag-class>com.mycompany.ui.tag.RequestDebugTag</tag-class>
<body-content>empty</body-content>
<description>add debug data from request</description>
</tag>
3. add to your JSP page
<XXX:requestDebug/>
at Wednesday, November 26, 2008 0 comments Links to this post
Labels: Development, Java, JSP, Software, Web
The Java Tag Lifecycle and Data Persistence Bug
If you create your own Java Tag by extending Interface Tag or any of the classes that implent it. Be aware of data persistence between tag usages... The Java VM tend to release the same Tag objects over and over.
Check this out from the Tag JavaDoc
Once all invocations on the tag handler are completed, the release method is invoked on it. Once a release method is invoked all properties, including parent and pageContext, are assumed to have been reset to an unspecified value. The page compiler guarantees that release() will be invoked on the Tag handler before the handler is released to the GC.
so if you declare private Objects in your Tag class like the following
public class MyPageTag extends BodyTagSupport {
private String pageName;
make sure you call release() as follows
@Override
public void release() {
super.release();
pageName = null;
}
Missing indirectly referenced artifact javax.transaction:jta:jar:1.0.1B:compile
Today I was trying to start playing with Hibernate Annotation.
I added the following to my pom.xml
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.4.0.GA</version> </dependency>
I was presented with the following error message from maven
Missing indirectly referenced artifact javax.transaction:jta:jar:1.0.1B:compile
This is due to licensing reason, jta cannot be downloaded from the central repositories.
You will need to download/install the jar into your own repository manually.
Check out the Java.net maven 2 repo.
http://download.java.net/maven/2/javax/transaction/jta/1.0.1B/
Download the .jar, .jar.md5, and .jar.sha1 into you [.m2/javax/transaction/jta/1.0.1B/] using the following:
mvn install:install-file -DgroupId=javax.transaction -DartifactId=jta -Dversion=1.0.1B -Dpackaging=jar -Dfile=jta-1.0.1B.jar
Maven 2 - maven-changes-plugin - changes.xml XSD
I Been using the maven-changes-plugin for almost 2 years now but was never able to find the XSD for the changes.xml anywhere... will i found it today and it works great...
If you have not checked out my maven-chnages-plugin posting give it a read 1st.
Here is an example changes.xml with the XSD for validation.
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns="http://maven.apache.org/changes/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/xsd/changes-1.0.0.xsd">
<properties>
<title>Change Log for My App</title>
</properties>
<body>
<release version="1.0.1" description="1st release" date="11/11/08">
<action dev="gdawoud" issue="12344" type="fix">Add a fix for bug</action>
</release>
<release version="1.0" description="1st release" date="11/11/08">
<action dev="gdawoud" issue="12344" type="fix">Add a fix for bug</action>
</release>
</body>
</document>
Maven 2 - maven-changes-plugin - with email notification and changes.xml validation
One very usefull feature of Maven is that when you release your project a notification can be sent out to the Dev/QA teams and a changes.html page can be created in the maven site.
The following is how to do that
1. Add the following plugin to pom.xml
1.1 Setup from and to emails
<properties> <releaseEmail.from.name>Release Mailler</releaseEmail.from.name> <releaseEmail.from.email>app-release@company.com</releaseEmail.from.email> <releaseEmail.to.email1>dev-team@company.com</releaseEmail.to.email1> <releaseEmail.to.email2>qa-team@company.com </releaseEmail.to.email2></properties>
1.2 Add the plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-changes-plugin</artifactId>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>announcement-generate</goal>
</goals>
<id>announcement-generate</id>
</execution>
<execution>
<phase>deploy</phase>
<goals>
<goal>announcement-mail</goal>
</goals>
<id>announcement-mail</id>
</execution>
</executions>
<configuration>
<smtpHost>mailone.real.com</smtpHost>
<smtpPort implementation="java.lang.Integer">25</smtpPort>
<mailSender>
<name>${releaseEmail.from.name}</name>
<email>${releaseEmail.from.email}</email>
</mailSender>
<toAddresses>
<toAddress implementation="java.lang.String">${releaseEmail.to.email1}</toAddress>
<toAddress implementation="java.lang.String">${releaseEmail.to.email2}</toAddress>
</toAddresses>
<developmentTeam>MyCompany.com Team</developmentTeam>
<urlDownload>http://mavenrepo.company.com/repository/com/copmany/${project.artifactId}/${project.version}/</urlDownload>
<introduction> This is a notification email that the ${project.version} build of ${project.name} has been code reviewed and deployed to Repository. Change list can be viewed on ${project.url}/changes-report.html</introduction>
</configuration>
</plugin>
2. Create a changes.xml in /src/changes/changes.xml
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns="http://maven.apache.org/changes/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/xsd/changes-1.0.0.xsd">
<properties>
<title>Change Log for My App</title>
</properties>
<body>
<release version="1.0.1" description="1st release" date="11/11/08">
<action dev="gdawoud" issue="12344" type="fix">Add a fix for bug</action>
</release>
<release version="1.0" description="1st release" date="11/11/08">
<action dev="gdawoud" issue="12344" type="fix">Add a fix for bug</action>
</release>
</body>
</document>
And that is it.
Check out the follow up blog
at Tuesday, November 25, 2008 4 comments Links to this post
Labels: Development, Maven, Plugin, Software, Web
Tomcat 5.5.x cookie parsing changed breaking cookies
It appears that Tomcat parses cookie values differently between 5.5.23 and 5.5.26 (latest and easiest to find in the 5.5.x line).
We use cookies where the value of the cookie is in name value format ie the cookie value is something like "has-player=false&player-version=&has-flash=true&flash-version=9.0" (without the quotes). In Tomcat 5.5.26 returns the cookie value as "has-player" (again, no quotes), truncating it at the first '='.
The fix for this is to URL encode the cookie value.
How to set a cookie in spring WebFlow action
So in spring webflow 1.x there is lots of people asking how to set a cookie or read cookies in a WebFlow Action.
We have created the following utility that does that... check out
public static ServletExternalContext getServletExternalContext(RequestContext context) {
ExternalContext externalCntxt = context.getExternalContext();
if (externalCntxt instanceof ServletExternalContext) {
ServletExternalContext servletExtContext = (ServletExternalContext) externalCntxt;
return servletExtContext;
}
return null;
}
at Wednesday, November 19, 2008 0 comments Links to this post
Labels: Development, Java, Spring, Web
spring-ws does not send carriage return to server
So while trying to use spring-ws to send a String to the back end server where the string contains a carriage return \t \r or \n we were not able to do so.
The reason is not spring-ws but the XML parser
xsd:token — Whitespace is swallowed or replaced and collapsed strings
check out RELAX NG for more info."The lexical and value spaces of xsd:token are the sets of all strings after whitespace replacement; i.e., after any occurrence of #x9 (tab), #xA (linefeed), or #xD (carriage return).These are replaced by an occurrence of #x20 (space) and collapsing. Collapsing is when contiguous occurrences of spaces are replaced by a single space, and leading and trailing spaces are removed."
Tomcat Servlet 2.4 simple 404 Page
So i moved one of my sites from ASP.net to Servlet/JSP pages and had a need to make sure all my request for my old pages do not get lost.
Here is my simple 404 salution.
create a web.xml (if you don't have one) in WEB-INF.
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<error-page>
<error-code>404</error-code>
<location>/</location>
</error-page>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
Beginners Guide to GnuPG
To download GnuPG check out the download links for your OS. The following is the most complete doc on the web for GnuPG. Here is the link to the full thread. By Dr Small Note: the following posting/how to assumes you are on ubuntu.
░▒▓»§«▓▒░
Introduction
I am going to give a basic run down of how to use GnuPG to encrypt files, sign your messages, read encrypted messages from your friends and whatnot, along with some of useful commands and applications you can use in aiding you along the way
First off, incase you don't understand completely (which is absolutely fine, as we are not expected to know everything), GPG is a key-based encryption method. You will be given a public key and a private key. The private key, as indicated, should remain private as to keep the entire idea of encryption secure.
A person who holds your public key and wishes to send you an encrypted message, would encrypt the message with your public key. They can not decrypt their own message after they encrypt it. Only you, who holds the private key can decrypt the message.
Applications
There are two different GUI based applications which can assist you in setting up a GPG key. These two are GPA, which is a very simple application that does everything you need, as far as key managing, deleteing, adding, signing and adding a level of trust to another person's public key.
The second one is seahorse. It is built for Gnome, and uses the gnome-keyring (if I am not mistaken) so it is a little bit more heavy than GPA, which is my favorite. (Note to the reader, I am not a KDE user, but I am sure there is a KDE GnuPG application suited for them. I am in no way discriminating them from this guide for any reason).
Let's begin by installing GPA and seahorse (or you can choose one of the two if you wish).
From the Terminal (Applications > Accessories > Terminal), run the following command:
sudo apt-get install gpa seahorse
gpa
seahorse
Another application worth mentioning would be FireGPG for Firefox. It can encrypt / decrypt / sign / verify / import and export with GPG. To install it for Firefox, please visit their website:
http://firegpg.tuxfamily.org/?page=install&lang=en
Key Generating
As an alternative, you could create a GPG key from the command line of the terminal.
To do so, launch your terminal (Applications > Accessories > Terminal) and run the following command, to get started:
gpg --gen-key
Please select what kind of key you want: (1) DSA and Elgamal (default) (2) DSA (sign only) (5) RSA (sign only)Your selection?
You then will be prompted with the following:
DSA keypair will have 1024 bits.ELG-E keys may be between 1024 and 4096 bits long.What keysize do you want? (2048)
If you don't want your key to expire (for the next prompt, select 0).
Answer yes if the information is correct, when prompted, and then enter your Real Name, Email address, and a comment (which is optional). If everything is correct, press "o" (for Ok) and then enter.
You will then be asked to enter a passphrase. This process will be repeated. As always, make a strong password which would be difficult to crack. Do not enter a name / address / birthdate or word from a dictionary as your password. Take the usual precautions, and make it random and difficult to crack.
After entering your passphrase, follow the instructions in the terminal:
We need to generate a lot of random bytes. It is a good idea to performsome other action (type on the keyboard, move the mouse, utilize thedisks) during the prime generation; this gives the random numbergenerator a better chance to gain enough entropy.
When you have successfully finished generating your key, you will see a message similar to the following:
| gpg: key 069C39A4 marked as ultimately trusted public and secret key created and signed. gpg: checking the trustdb gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model gpg: depth: 0 valid: 2 signed: 1 trust: 0-, 0q, 0n, 0m, 0f, 2u gpg: depth: 1 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 1f, 0u pub 1024D/069C39A4 2008-01-28 Key fingerprint = 516E E759 94BA 0DC1 37BE 1074 E46C B27D 069C 39A4 uid samplekey <samplekey@linux.org> sub 2048g/BC9EC4CB 2008-01-28 |
The Key fingerprint is also listed there.
Key Servers
Key servers are used to distribute your public key to other key servers and so other users can easily look your name (or email up) in the database and find your public key to send encrypted messages to you. This eliminates the process of physically or unsecurely giving your friend your public key, and allows others to be able to find you on an online database.
To upload your public key to the Ubuntu keyserver, there are 2 ways to do this.
- By pasting your ASCII Armored Public Key into the submission box at the Ubuntu Keyserver
- By using the terminal and gnupg to send your public key to the Ubuntu Keyserver.
To accomplish method 1, you will need to open seahorse, select your key under "My Personal Keys" and click the "Export Public Key" in the toolbar. You can optionally choose the name and location of which it will be saved to. Proceed to open up:
http://keyserver.ubuntu.com:11371
while opening your newly exported public key with a text editor (gedit).
Select and copy the entire contents of your public key file, and paste it into the "Submit a Key" text area on the Ubuntu Keyserver (link provided above). Submit it, and it should then proceed to submit the key to the keyserver.
You should then be able to search for your email or name in the search string box on the Ubuntu Keyserver page, to find your public key on the internet. This is undoubtedly the Graphical way of doing it, but it can be somewhat longer.
To do it by the means of method 2, you would first need to open up a terminal (Applications > Accessories > Terminal) and paste the following into it:
gpg --send-keys --keyserver keyserver.ubuntu.com <KEY-ID>
If you forget what your keyid is, just run:
gpg --list-keys <EMAIL>
Importing Keys
There are four different methods to importing a key, as stated below:
- FireGPG
- GPA
- Seahorse
- Terminal
All are quite simple to do, but FireGPG is the easiest of all if you are importing a public key from a keyserver with Firefox. I will briefly explain all four.
FireGPG
If you have somebody's public key on a webpage while in Firefox and have installed FireGPG (as mentioned above under Applications), then simply highlight the Public key from beginning PGP comment to ending PGP comment, right click on it, select FireGPG and click the Import button. It's that simple!
For your information, to solve confusion, the beginning and ending PGP comment tags look like the following:
-----BEGIN PGP PUBLIC KEY BLOCK----------END PGP PUBLIC KEY BLOCK-----
GPA
If someone has given you their public key as a file, simply launch GPA and select "Import" from the toolbar.
Seahorse
If someone has given you their public key as a file, you can do one of two things. First, you can open up Nautilus and double click this file, and it should automatically import the public key into your GnuPG, or open Seahorse and select "Key" from the menu and click "Import".
Terminal
Open up the terminal (Applications > Accessories > Terminal) and type:
gpg --import KEYFILE
(If it is not in your home folder, please cd to the proper directory first, and then run the above command.)
Tips and Tricks
Here is some more valuable information that can be useful when encrypting / decrypting files with GPG from the terminal.
List Keys
If you wish to see all of the keys you have imported into GnuPG, you can issue the following command:
gpg --list-keys
Encrypt a File
If you wish to encrypt a file for your friend with his Public Key, run the command in the following format:
gpg -o encrypted_file.gpg --encrypt -r <KEY-ID> original.file
-o encrypted_file.gpg = Output to the following filename.
--encrypt = Duh, that's the encrypting part
-r <KEY-ID> = Recipient. KEY-ID would be your friends KEY-ID here.
original.file = The original file that you will be encrypting.
Decrypt a File
If someone has sent you a file that has been encrypted with your public key, you can decrypt it by the following:
gpg --decrypt filename.gpg
Clearsign a Document
Clearsigning is very similar to adding your signature to the bottom of a letter or important paper. It signifies that it actually came from you. By clearsigning, it generates a SHA1 hash of the entire file's contents and add's the SHA1 sum to the bottom of the signature. If the file has been tampered with, the signature verification will fail, which can be used to spot forgery.
If the user has edited the file after it has been signed, the verification of the signature will also fail, because the SHA1 sum will not match that of the actual content.
To clearsign a document / file, run the following:
gpg --clearsign filename.txt
Exporting your Public Key
To export your public key in ASCII Armored fashion, run the following command:
gpg --export -a <KEY-ID> > publickey.asc
Symmetric Encryption
GPG can also do a symmetric encrytion where you can encrypt a file with a passphrase (this is not keybased encryption). To encrypt a file with a passphrase, use this:
gpg -c filename.txt
gpg filename.txt
Additional Links:
GnuPG.org
GnuPrivacyGuardHowto
The GNU Privacy Handbook
Wikipedia: GNU Privacy Guard
Related Guides
How To: Use GnuPG along with WHIRLPOOL Hash to Encrypt an Individual File
Advanced GnuPG Concepts - Advanced Key Generation
How To: Install a Port Knocker - FWKNOP
Using Log4J category to create specialized log files
I like most use it everyday to output messages from my application to log files on disk. One thing i been doing a lot is taken these log lines and outputting them into a specialized log file.
An example of that is cerating a WebTrackingLogger calls where i have a log method as follows
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class WebTrackingLogger {
private static final Log log = LogFactory.getLog(WebTrackingLogger.class);
public static void log(Sting ip, String url, String.... otherValues) {
log.info(IP + " " + URL);
....
log.warn(IP + " " + URL, e);
}
Now for me to isolate the logs from this Logger class into a new file i add the following to my log4j.properties
# Daily Rolling File appender for WebTrackingLogger log4j.category.com.company.WebTrackingLogger.INFO, PL log4j.appender.PL=org.apache.log4j.DailyRollingFileAppender log4j.appender.PL.layout=org.apache.log4j.PatternLayout log4j.appender.PL.layout.ConversionPattern=%d %x - %m%n log4j.appender.PL.File=/var/log/app/web-tracking-log log4j.appender.PL.DatePattern='.'yyyy-MM-dd
and that is it...
Convert Raw HTML or Code to Escaped HTML
I been posting a few blog posting with code in the posting, i been trying to find some good software to make that task easy.... so far nothing the following steps looks some what promising
- Copy your Code into the following tool Convert Raw HTML to Escaped HTML
- Copy the Generated code and surround it with a div with a class of code and pre tags
- Add the following CSS to your page
<div class="code"> <pre> ...code ... ...code... </pre> </div>
pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
.code{
background-color: #FFFFFF;
width: 98%;
border: 1px ridge #999999;
text-align: left;
padding-left: 10px;
padding-top: 15px;
}
let me know what you think... or if you think there is a better way...
NOTE: Some of my old posts that have color code on a while background were done using a Mac OSX Software called Code (http://www.panic.com/coda/) which you can copy any text as XHTML.





