วันพุธที่ 5 กุมภาพันธ์ พ.ศ. 2557

ส่งเมล์กับ Apache Commons Mail แบบง่ายๅ

แบบที่ 1

public static void send(String to, String subject, String message, String from) throws EmailException {
        HtmlEmail email = new HtmlEmail();
        email.setHostName("smtp.gmail.com");
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator("user@gmail.com", "password"));
        email.setSSLOnConnect(true);

        email.setFrom(from);
        email.setSubject(subject);
        email.setHtmlMsg(message);
        email.setCharset("utf-8");

        email.addTo(to);
        email.send();
}

ตรวจสอบว่า Client ที่เรียกเว็ปเราใช้ Browser อะไร Device อะไร

1. ตรวจสอบ Browser

public static String isBrowser(HttpServletRequest request) {
        String deviceType = request.getHeader("User-Agent");

        if (deviceType.indexOf("MSIE") != -1) {
            return "Internet Explorer";
        } else if (deviceType.indexOf("Firefox") != -1) {
            return "Firefox";
        } else if (deviceType.indexOf("Chrome") != -1) {
            return "Chrome";
        } else if (deviceType.indexOf("Safari") != -1) {
            return "Safari";
        } else if (deviceType.indexOf("Presto") != -1) {
            return "Opera";
        } else {
            return null;
        }
}

2. ตรวจสอบ Device

public static String isDevice(HttpServletRequest request) {
        String deviceType = request.getHeader("User-Agent");

        if (deviceType.indexOf("Mobile") != -1 || deviceType.indexOf("Android") != -1) {
            return "Mobile";
        } else {
            return "Desktop";
        }

}

Java Reflect

ปกติแล้วเราจะเรียกใช้งาน method ใน class โดยการ สร้าง object class นั้น ๆ แล้วเรียกใช้งาน method ใน class

เช่น 

A obj = new A();
obj.method();

แต่ยังมีอีกวิธีนึง คือการเรียกใช้งาน แบบ reflect ตัวอย่างเช่น

แบบที่ 1 (ธรรมดา)

1. Class Service มี method hello() ไว้ให้เรียกใช้งาน

package demo.reflect;

public class Services {

    public String hello() throws InterruptedException {
        return "Hi method hello()";

    }
}


2. Class Test เป็น class ที่เรียกใช้งาน method hello ของ class Service โดยวิธีการ reflect

package demo.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test {

    public static void main(String[] args) {
        try {
            Services clazz = new Services();
            Method method = clazz.getClass().getMethod("hello", new Class[]{});
            Object ret = method.invoke(clazz, new Object[]{});
            System.out.println("result : " + ret);
        } catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

แบบที่ 2 (ส่ง Parameter ไปด้วย)


1. Class Service มี method hello() ไว้ให้เรียกใช้งาน

package demo.reflect;

public class Services {

    public String hello(String name) throws InterruptedException {
       return "Hi " + name;
    }
}


2. Class Test เป็น class ที่เรียกใช้งาน method hello ของ class Service โดยวิธีการ reflect

package demo.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test {

    public static void main(String[] args) {
        try {
            Services clazz = new Services();
            Method method = clazz.getClass().getMethod("hello", new Class[]{String.class});
            Object ret = method.invoke(clazz, new Object[]{"aekasit"});
            System.out.println("result : " + ret);
        } catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

วันเสาร์ที่ 1 กุมภาพันธ์ พ.ศ. 2557

LDAP บน Redhat 5.4

1. Install (RPM)

 - compat-openldap-2.3.43_2.2.29-3.el5.i386.rpm
 - libtool-ltdl-1.5.26-4.fc10.i386.rpm
 - openldap-servers-2.3.43-3.el5.i386.rpm
 - openldap-clients-2.3.43-3.el5.i386.rpm

2. Config
/etc/Openldap/slapd.conf

3. Start Server


4. Create Structure (Apache Directory Studio)


5. Create Group and User    ou = Users, ou = Pumbaa