Monday 24 February 2014

Internet Protocols (SMTP, POP3, IMAP, MIME)

0 comments
SMTP
Simple Mail Transfer Protocol (SMTP) is an Internet standard for electronic mail (e-mail) transmission.
First defined by RFC 821 in 1982, it was last updated in 2008 with the Extended SMTP additions by RFC 5321 -which is the protocol in widespread use today.

SMTP by default uses TCP port 25. The protocol for mail submission is the same, but using port 587,
and SMTP connections secured by SSL, known as SMTPS, default to port 465.

While electronic mail servers and other mail transfer agents use SMTP to send and receive mail messages,
user-level client mail applications typically use SMTP only for sending messages to a mail server for relaying.

For receiving messages, client applications usually use either the POP3 or the IMAP.

POP3/IMAP
Post Office Protocol (POP) is an application-layer Internet standard protocol used by local e-mail clients to retrieve e-mail from a remote server over a TCP/IP connection. POP has been developed through several versions, with version 3 (POP3) being the current standard.
Virtually all modern e-mail clients and servers support POP3, and it along with IMAP (Internet Message Access Protocol) are the two most prevalent Internet standard protocols for e-mail retrieval,with many webmail service providers such as Google Mail, Microsoft Mail and Yahoo!

Mail also providing support for either IMAP or POP3 to allow mail to be downloaded.

IMAP is short for Internet Message Access Protocol, while POP translates to Post Office Protocol. In other words, both are email protocols. They allow you to read emails locally using a third party application. Examples of such applications are Outlook, Thunderbird, Eudora, GNUMail, or (Mac) Mail.

POP Workflow:

  • Connect to server
  • Retrieve all mail
  • Store locally as new mail
  • Delete mail from server*
  • Disconnect
*The default behavior of POP is to delete mail from the server. However, most POP clients also provide an option to leave a copy of downloaded mail on the server.

IMAP Workflow:

  • Connect to server
  • Fetch user requested content and cache it locally, e.g. list of new mail, message summaries, or content of explicitly selected emails
  • Process user edits, e.g. marking email as read, deleting email etc.
  • Disconnect
As you can see, the IMAP workflow is a little more complex than POP. Essentially, folder structures and emails are stored on the server and only copies are kept locally. Typically, these local copies are stored temporarily. However, you can also store them permanently.

What Are The Advantages Of POP?
Being the original protocol, POP follows the simplistic idea that only one client requires access to mail on the server and that mails are best stored locally. This leads to the following advantages:

Mail stored locally, i.e. always accessible, even without internet connection
Internet connection needed only for sending and receiving mail
Saves server storage space
Option to leave copy of mail on server
Consolidate multiple email accounts and servers into one inbox

What Are The Advantages Of IMAP?
As mentioned in the introduction, IMAP was created to allow remote access to emails stored on a remote server. The idea was to allow multiple clients or users to manage the same inbox. So whether you log in from your home or your work computer, you will always see the same emails and folder structure since they are stored on the server and all changes you make to local copies are immediately synced to the server.

As a result, IMAP has the following advantages:

Mail stored on remote server, i.e. accessible from multiple different locations
Internet connection needed to access mail
Faster overview as only headers are downloaded until content is explicitly requested
Mail is automatically backed up if server is managed properly
Saves local storage space
Option to store mail locally

MIME
Multipurpose Internet Mail Extensions (MIME) is an Internet standard that extends the format of email to support:

Text in character sets other than ASCII
Non-text attachments
Message bodies with multiple parts
Header information in non-ASCII character sets

Although MIME was designed mainly for SMTP protocol, its use today has grown beyond describing the content of email and now often includes describe content type.

Friday 21 February 2014

List all Files from a Directory (or Subdirectory)

0 comments
import java.io.File;

public class ListFilesUtil {
 
    /**
     * List all the files and folders from a directory
     * @param directoryName to be listed
     */
    public void listFilesAndFolders(String directoryName){
 
        File directory = new File(directoryName);
 
        //get all the files from a directory
        File[] fList = directory.listFiles();
 
        for (File file : fList){
            System.out.println(file.getName());
        }
    }
 
    /**
     * List all the files under a directory
     * @param directoryName to be listed
     */
    public void listFiles(String directoryName){
 
        File directory = new File(directoryName);
 
        //get all the files from a directory
        File[] fList = directory.listFiles();
 
        for (File file : fList){
            if (file.isFile()){
                System.out.println(file.getName());
            }
        }
    }
 
    /**
     * List all the folder under a directory
     * @param directoryName to be listed
     */
    public void listFolders(String directoryName){
 
        File directory = new File(directoryName);
 
        //get all the files from a directory
        File[] fList = directory.listFiles();
 
        for (File file : fList){
            if (file.isDirectory()){
                System.out.println(file.getName());
            }
        }
    }
 
    /**
     * List all files from a directory and its subdirectories
     * @param directoryName to be listed
     */
    public void listFilesAndFilesSubDirectories(String directoryName){
 
        File directory = new File(directoryName);
 
        //get all the files from a directory
        File[] fList = directory.listFiles();
 
        for (File file : fList){
            if (file.isFile()){
                System.out.println(file.getAbsolutePath());
            } else if (file.isDirectory()){
                listFilesAndFilesSubDirectories(file.getAbsolutePath());
            }
        }
    }
 
    public static void main (String[] args){
 
        ListFilesUtil listFilesUtil = new ListFilesUtil();
 
        final String directoryLinuxMac ="/Users/loiane/test";
 
        //Windows directory example
        final String directoryWindows ="C://test";
 
        listFilesUtil.listFiles(directoryLinuxMac);
    }
}

Saturday 15 February 2014

Transient, Persistence and Detached Objects in Hibernate

0 comments

While an entity class is created to create a database table or an entity, it is done by making an instance of the entity class and by assigning this instance to the session instance, database table schema is added or updated into the database and a table entry is made. Once the session transaction is saved, it is required to release the entity class instance. These three states of object are classified into their specific categories as Transient, Persistence and Detached object. This article will dictate these all three states of an entity class and will go with an example on this.

Transient, Persistence and Detached Objects in Detail:
Once an entity class has been created to create a database entity or table, it is required to connection with the hibernate and database using SessionFactory and its set of classes. Once the connection configuration is done, we are required to create an instance of the entity class. This state of instance is known as Transient Object.To save the entity class instance into the database, we are required to create a Session class instance that will use the method save() to save the entity class instance into the database. This configuration of the entity class instance is known as Persistence Object.It is necessary to close the session instance after performing the database task. Once a session instance is closed, the state of entity class instance is set to Detached Object.

Let’s go with an example that will dictate the use of transient, persistence and detached object as started with Listing 1:

Listing 1: UserDetails.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
public class UserDetails {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private intuserId;
    private String userName;
    public intgetUserId() {
        return userId;
    }
    public void setUserId(intuserId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
}
Listing 2: AddRecords.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class AddRecords {
    public static void main(String[]args){
        SessionFactorysessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
         
        //To create data
        for(int i = 0; i < 10; i++){
            UserDetails user = new UserDetails();//Creating Transient Object
            user.setUserName("user" + i); //Setting up values to the transient object
            session.save(user);//Updating the transient object to persistnence object
        }
         
        session.getTransaction().commit();
        session.close(); //Session is closed
    }
}
A new instance of a a persistent class which is not associated with a Session, has no representation in the database and no identifier value is considered transient by Hibernate:

Person person = new Person();
person.setName("Foobar");

// person is in a transient state
A persistent instance has a representation in the database, an identifier value and is associated with a Session. You can make a transient instance persistent by associating it with a Session:

Long id = (Long) session.save(person);
// person is now in a persistent state
Now, if we close the Hibernate Session, the persistent instance will become a detached instance: it isn't attached to a Session anymore (but can still be modified and reattached to a new Session later though).

Tuesday 11 February 2014

How to get system path of web application java

0 comments
public String getPath() throws UnsupportedEncodingException {
  String path = this.getClass().getClassLoader().getResource("").getPath();
  String fullPath = URLDecoder.decode(path, "UTF-8");
  String pathArr[] = fullPath.split("/WEB-INF/classes/");
  System.out.println(fullPath);
  System.out.println(pathArr[0]);
  fullPath = pathArr[0];
  
  String reponsePath = "";
// to read a file from webcontent
  reponsePath = new File(fullPath).getPath() + File.separatorChar + "newfile.txt";
  return reponsePath;
 }

Related Posts Plugin for WordPress, Blogger...