Recent Post

Categories

Archives

Cow Computing

10 Jan 22

Simple Mail Server Setup on Ubuntu (Step By Step Guide)

Often you will have to setup a mail server especially when you own a web server or VPS, however, this task is dauntingly hard, since mail is an old product. So, here I would like to jot down a step by step guide to walk through the setup of a simple mail server. By the end of this article, a working IMAP/POP3 server will be up. Nonetheless, if any extra stuff (e.g. the use of spam filter or so) is needed, they can be installed on top later.

First, we have to install “postfix” as the MTA and “mailx” as the mail utility.

sudo apt-get install postfix
sudo apt-get install mailx

Then, we will create a user on ubuntu as the mail user account

sudo useradd -m -s /bin/bash <username>
sudo passwd <username>

Now, the first step is done, and we might have a test on it by issuing

netcat localhost 25

and it should prompt you with this

220 localhost ESMTP Postfix (Ubuntu)

Read More / Comment »

10 Jan 22

Creating Keystore and Self-Signed Certificate with openSSL

It is in fact very easy to generate a self-signed certificate with openSSL.

In order to generate Keystore and Certificate using open SSL, we first need to generate a key

openssl genrsa -out <name of private key file>.key 1024

then we need to generate a Certificate Signing Request by reading the private key we just generated

openssl req -new -key <name of private key file>.key -out <name of csr file>.csr

After that, we could Self-Sign the certificate (note: if you only want the key-cert pair, you could stop after this step, else go to the next step for keystore generation).

openssl x509 -req- days <num of days valid> -in <name of csr file>.csr -signkey <name of private key file>.key -sha1 -out  <name of cert file>.cert

Finally, with the key and certificate, we could combine them into a keystore

openssl pkcs12 -name <key alias> -export -in <name of cert file>.cert -inkey <name of private key file>.key -out <name  of the keystore file>.p12

The key and certificate is ready to be used in various applications (e.g. Dovecot, Apache WebServer…)

10 Jan 16

Some Useful HotKey for Eclipse

If you develop heavily on Java, Eclipse definitely should be your primary programming tool (other than Text Editor).
To be more effective in coding, one must master the tools he/she uses. In terms of Eclipse, hotkey is one of the chapter which should be learnt. After sometimes, the hotkey usage will eventually speed up your coding.

Below are some common ones:

Ctrl + Shift + P // Go to the corresponding ending brace
Ctrl + Q // Go back to last edited location
Alt + Left/Right Arrow // Go to previous or next editor
Ctrl + I // Format code
Ctrl + 1 // Generate try/catch or do/if/while/for
Ctrl + / // Comment/Uncomment
F3 // Find Method
Ctrl + L // Go to line
Ctrl + Space // Toggle code assist
Ctrl + Shift + Space // Toggle arguments hints

10 Jan 11

Using Java Mail API

In order to have the ability of sending email from your java program, you would need Java Mail API. It could be obtained here.

The following code illustrate how to use it. (Comment will guide you thru)

// Obtain the system property and set inside the SMTP server
Properties props = System.getProperties();
props.put("mail.smtp.host", "your isp smtp");

// Obtain the mail session
Session session = Session.getDefaultInstance(props, null);
Message message = new MimeMessage(session);

// Set the address for sender
message.setFrom(new InternetAddress("sender mail address"));

// Set the address for recipient
message.addRecipient(Message.RecipientType.TO, new InternetAddress("recipient mail address"));

// Set the Subject of the mail
message.setSubject("Subject");

// Set the message content of the mail
message.setContent("Message Body", "text/plain");

// Send the mail
Transport.send(message);

10 Jan 5

SLF4J – Simple Logging Facade for Java

The logging tool come with the java.util.logging package is just so horrible to work with, and most people tend to use Log4J, while open source project tend to use common-logging. And here, i would like to introduce a better (IMHO) logging tool — SLF4J. SLF4J is a facade wrapper, while you could choose your own implementation to be run below, and that includes Log4J, Java.util.logging, SimpleLogger and Logback. I personally would recommend the use of Logback as the logging implementation as it’s a better successor of Log4J. So, to use SLF4J, setup the project as follows:

1. Put into your lib folder the following jars:

slf4j-api.jar
logback-core.jar
logback-classic.jar

2. Then create a logback.xml or logback-test.xml in your classpath with the following content:

<?xml version="1.0" encoding="UTF-8" ?>

<configuration>
 <appender name="STDOUT">
   <layout>
     <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n</pattern>
   </layout>
 </appender>
 <root level="debug">
   <appender-ref ref="STDOUT" />
 </root>
</configuration>

3. In your class, make use the logger as simple as follows:

// Simply get a logger from factory and start logging
Logger logger = LoggerFactory.getLogger("LogTest");
logger.debug("DEBUG MSG");

There’s one thing i like SLF4J so much is that, it actually supports parameterized logging:

// Parameterized logging reduce the hassle when constructing long log msg
Logger logger = LoggerFactory.getLogger("LogTest");
logger.debug("Error: {}, Reason: {}", error, reason);

And one more tips, sometimes when you want to override the original logging tool used in certain open source framework or codebase (e.g Spring Framework), you could simply add the jcl-over-slf4j.jar to the lib/, this will automatically hook into the common-logging of spring and replace it with SLF4J.

Hope this is useful! Enjoy!