Friday 26 July 2013

Automatic page redirect on session time out java

0 comments
 <meta http-equiv="refresh" content="<%=session.getMaxInactiveInterval()%>;url=logout" />

Wednesday 17 July 2013

Basic Spring Example With ClassPathXmlApplicationContext & AnnotationConfigApplicationContext

0 comments
Spring Framework Jars -


Project Structure -





User.java
package com.beans;

public class User {

 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
}

Order.java
package com.beans;

public class Order {

 private String orderName;
 private Integer price;
 public String getOrderName() {
  return orderName;
 }
 public void setOrderName(String orderName) {
  this.orderName = orderName;
 }
 public Integer getPrice() {
  return price;
 }
 public void setPrice(Integer price) {
  this.price = price;
 }
 
 
}

JavaConfigBean.java
package com.beans;

public class JavaConfigBean {

 private String version;

 public String getVersion() {
  return "Harit";
 }

 public void setVersion(String version) {
  this.version = version;
 }
 
}

Configure java beans using xml configuration file SpringBeans.xml

 
 
  
 
    
    

Order.xml

 
 
  
  
 
 

Java Class based bean configuration AppConfig.java
package com.javaconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.beans.JavaConfigBean;

@Configuration
public class AppConfig {

 @Bean(name="javaconfigbean")
 public JavaConfigBean getJavaConfigBean()
 {
  return new JavaConfigBean();
 }
}

Test the app Test.java
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.beans.JavaConfigBean;
import com.beans.Order;
import com.beans.User;
import com.javaconfig.AppConfig;

public class Test {

 public static void main(String[] args) {
  
  //For Class Path Xml Application Context
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("SpringBeans.xml");  
  User user = (User) applicationContext.getBean("userbean");
  System.out.println(user.getName());
  
  
  //Import Order.xml's Bean
  Order order = (Order) applicationContext.getBean("orderbean");
  System.out.println(order.getOrderName());
  
  
  //For Annotation Based ApplicationContext
  ApplicationContext applicationContext2 = new AnnotationConfigApplicationContext(AppConfig.class);
  JavaConfigBean javaConfigBean = (JavaConfigBean) applicationContext2.getBean("javaconfigbean");
  System.out.println(javaConfigBean.getVersion());
 }
}

Output:
Harit Kumar
Bread
Harit

Tuesday 16 July 2013

Implement Urban Airship For Push Notification In Java

1 comments
MyAuthenticator.java
import java.net.Authenticator;
import java.net.PasswordAuthentication;

/**
* MyAuthenticator is the class to authenticate the user using userName and password
* @author Harit Kumar
*/
public class MyAuthenticator extends Authenticator{
 
 private String user;
    private String passwd;

    public MyAuthenticator(String user, String passwd)
    {
        this.user = user;
        this.passwd = passwd;
    }

    public PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication(user, passwd.toCharArray());
    }

}
PushUtil.java

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.net.Authenticator;
import java.net.URL;
import java.net.URLConnection;

import javax.net.ssl.HttpsURLConnection;

import org.apache.log4j.Logger;

public class PushUtil {

 static Logger logger = Logger.getLogger(PushUtil.class);
 
 public static boolean sendPushMessage(String message,String deviceToken){
    boolean result=false;

    try {
     
     Authenticator.setDefault(new MyAuthenticator("App Key","App Master Secret"));
     URL url = new URL("https://go.urbanairship.com/api/push/");
     URLConnection urlConnection = url.openConnection();
     HttpsURLConnection httpConn = (HttpsURLConnection)urlConnection;
     ByteArrayOutputStream bout = new ByteArrayOutputStream();
     String postdata = "{\"aps\": {\"alert\": \""+message+"\"}, \"device_tokens\": [\""+deviceToken+"\"]}";
     
     logger.info("postdata : "+postdata);
     
     byte[] buffer = new byte[postdata.length()];
     buffer = postdata.getBytes("UTF8");
     bout.write(buffer);
     byte[] b = bout.toByteArray();
     httpConn.setRequestProperty("Content-Length",String.valueOf(b.length));
     httpConn.setRequestProperty("Content-Type","application/json");
     httpConn.setRequestMethod("POST");
     httpConn.setDoOutput(true);
     httpConn.setDoInput(true);
     OutputStream out = httpConn.getOutputStream();
     out.write(b);
     out.close();
     logger.info("Push ResponseCode : "+httpConn.getResponseCode());
     if(httpConn.getResponseCode()==200)
     {
        result=true;
     }
    }
    catch (Exception e) {
     logger.error("Error : "+e);
    }
    return result;
   }
 
 
 public static void main(String[] args) {
  PushUtil.sendPushMessage("Hi Demo","98b79b2f 74f4457d 55966598 03be5fa3 36693326 37700848 33454309 c408f253");
 }
}


Tuesday 2 July 2013

Spring Security

0 comments

Resize , Merge Images Using Java

1 comments
package custom;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class MergeImages {
 private static final int COMBINE_IMG_WIDTH = 180;
 private static final int COMBINE_IMG_HEIGHT = 90;
 
 private static final int POTRAIT_IMG_WIDTH = 70;
 private static final int POTRAIT_IMG_HEIGHT = 90;
 
 
 
public static void main(String[] args) throws IOException {
 
 
    File basePath = new File("f:/");
 // load source images
 BufferedImage background = ImageIO.read(new File(basePath, "back.png"));
 BufferedImage overlay = ImageIO.read(new File(basePath, "land.png"));

 int imageHeight = overlay.getHeight();
 int imageWidth =  overlay.getWidth();
 System.out.println("height: "+imageHeight+" : "+"width: "+imageWidth);
 if(imageHeight > imageWidth)
 {
  System.out.println("Potrate Thumnail");
  BufferedImage resizeImage = resizePotraitImage(overlay, BufferedImage.TYPE_INT_ARGB);
  
  // create the new image, canvas size is the max. of both image sizes
     BufferedImage combined = new BufferedImage(COMBINE_IMG_WIDTH, COMBINE_IMG_HEIGHT, BufferedImage.TYPE_INT_ARGB);

  // paint both images, preserving the alpha channels
  Graphics g = combined.getGraphics();
  g.drawImage(background, 0, 0, null);
  g.drawImage(resizeImage, 55, 0, null);

  // Save as new image
  ImageIO.write(combined, "PNG", new File(basePath, "combined.png"));

 }
 else
 {
  System.out.println("Landescape Thumnail");
  
  BufferedImage resizeImage = resizeLandscapeImage(overlay, BufferedImage.TYPE_INT_ARGB);
  // paint both images, preserving the alpha channels
  Graphics g = resizeImage.getGraphics();
  g.drawImage(resizeImage, 0, 0, null);
  
  // Save as new image
  ImageIO.write(resizeImage, "PNG", new File(basePath, "combined.png"));
 }
 
}



 private static BufferedImage resizePotraitImage(BufferedImage originalImage, int type){
 BufferedImage resizedImage = new BufferedImage(POTRAIT_IMG_WIDTH, POTRAIT_IMG_HEIGHT, BufferedImage.TYPE_INT_ARGB);
 Graphics2D g = resizedImage.createGraphics();
 g.drawImage(originalImage, 0, 0, POTRAIT_IMG_WIDTH, POTRAIT_IMG_HEIGHT, null);
 g.dispose();
 
 return resizedImage;
    }
 
 
 
 private static BufferedImage resizeLandscapeImage(BufferedImage originalImage, int type){
  BufferedImage resizedImage = new BufferedImage(COMBINE_IMG_WIDTH, COMBINE_IMG_HEIGHT, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g = resizedImage.createGraphics();
  g.drawImage(originalImage, 0, 0, COMBINE_IMG_WIDTH, COMBINE_IMG_HEIGHT, null);
  g.dispose();
  
  return resizedImage;
 }  
 
 
}


Related Posts Plugin for WordPress, Blogger...