Posts

Showing posts from 2012

Showing tool-tip on a component in Wicket

Few days back I was working on an application which was using Apache Wicket and there was a requirement to show tool-tip on a component. After googling it for hours I found this really easy solution for my requirement. 

The following code snippet shows how you can add a tool tip on a Label component.

...
Label lbl = new Label("myLable", "My Label Value");
lbl.add(new AttributeModifier("title", true, new Model<String>("My Tool-Tip"))); 
...

Enabling HTTPS request on Tomcat Server

Enabling HTTPS Request on TomcatIf you are developing a web application which is handling some sensitive data then you may want to use a secure medium for transferring the sensitive data between the web browser and your web server. SSL or Secure Socket Layer is an easy way to offer security to your users. SSL allows web browsers and web servers to encrypt all information and communicate it over a secured connection. 

Hypertext Transfer Protocol Secure (HTTPS) is a widely used communication protocol for secure communication over the Internet. It is simply layering of HTTP over SSL protocol. When we say we are using HTTPS for secure connection, we mean to say that the information (request) is first encrypted using SSL and transmitted using HTTP protocol and then the receiver decrypts it using SSL.

Apache Tomcat fully supports the SSL protocol and provides document on how to configure Tomcat to use SSL , but somehow I found it a little confusing for first time users. So here I am writing i…

An introduction to XML

What is XML?XML, or eXtensible Markup Language was created by the World Wide Web Consortium (W3C) to overcome the limitations of HTML. While the HTML tags tell a browser how to display this information, the tags don't tell the browser what the information is. With XML, you can assign some meaning to the tags in the document, which can be processed by the machine.


A simple XML File
<?xmlversion="1.0"encoding="UTF-8"?> <blog> <postid="1"value="post1"> <title>Serialization in Java</title> <author>Dev</author> <date>05/06/2012</date> <url>http://techiesinsight.blogspot.in/2012/06/serialization-in-java.html</url> </post> <post id="2"value="post2"> <title>serialVersionUId in Java Object Serialization</title> <author>Dev</author> <date>07/06/2012</date> <url>http://techiesinsight.blogspot.in/2012/06…

Annotations in Java

Annotations are the tags that can be inserted into a Java programs so that they can be processed by the tools. In the Java programming language, an annotation is used like a modifier, and it is placed before the annotated item, without a semicolon. The name of each annotation is preceded by an @ symbol. 
For Example:
@Override
public boolean equals(Object obj){
 ... 
Annotation SyntaxAn annotation is defined by an annotation interface: 

public @interfaceAnnotationName
 //element declarations 
typeelementName(); 
typeelementName()defaultvalue; 
 . . . 
}  One of the followingtypecan be used for annotation fields:
All primitives (boolean, char, short, int, long, double)StringClassEnumArray of any of above typesMeta-AnnotationsMeta annotations are annotations that are used to annotate annotations. There are four meta-annotation types that come standard with Java 5:  @Documented:is a marker annotation. Annotation declaration (of the @documented annotation type) will be included in the documentati…

Serializable vs Externizable

Serialization vs Externalization

SerializableExternizableSerializable is a marker interface (an interface with no methods)Unlike Serializable, Externizable is a standard interface with two methods defined to be implemented by the implementing class.Serailization is a recursive process, all non-transient variables and super classes in the object hierarchy will be serialized causing an unnecessary overheadUser defines what should be serialized and what should not. Hence it is more optimized. Should be preferred for "Fat Objects" Serialization uses reflection mechanism for marshalling and un marshalling the objects. Marshalling/Unmarshalling process is user defined.During de-serialization no constructor is called, hence initialization done in constructor will be skipped. During de-serialization default constructor is invokedA default construtor definition is not mandatory when parameterized constructor(s) are definedAn explicit default constructor definition is mandatory, w…

The Externalizable Interface

Before you start exploring Externalization in Java, I would recommend you to gather some knowledge of Serialization in Java because Externalizable is an alternative to Serializable interface.


Externalizable interface allows you to customize how serialization is done. By implementing Externalizable you are controlling what gets serialized and what does not get serialized. Externalizable interface  extends Serializable interface and defines two methods to be overridden by the implementing class.  public void readExternal(ObjectInput in) throws IOException, ClassNotFoundExceptionpublic void writeExternal(ObjectOutput out) throws IOExceptionJava class implementing Externalizable interface

package learn.java.serialization;


import java.io.Externalizable;
importjava.io.IOException;
importjava.io.ObjectInput;
importjava.io.ObjectOutput;
importjava.util.Date;


public class ExternalizableObject implements Externalizable{
   private String name;
   private Date dob;
   private int age;

public String getName()…

How to scale image using Java

In this post we will see how can we scale an image to a desired dimension. 


In the following code snippet the scaleImage method is responsible for scaling the source image to the given height & width. Instead of overwriting the original file the code creates a new file for the scaled image version.


public static String  scaleImage(String imgSrc, int width, int height, String dest){
try{
File f = new File(imgSrc);

//Reads the Image from the given URL
BufferedImage img = ImageIO.read(f);

//Scales the BufferedImage to the desired dimensions
   Image scaledImg = img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);

//'coz the Image object cannot be written to disk directly 
   //We have to recreate a new BufferedImage instance from the scaled Image instance
   BufferedImage biScaledImg = toBufferedImage(scaledImg, BufferedImage.TYPE_INT_RGB);

//Writes the file on to the disk at the given destination
   ImageIO.write(biScaledImg, "jpeg", new File(dest));
}
catch(IOException …

How to delete Solr Index

To delete all documents from my Solr index, we have to issue an update request with the  stream.body parameter set to <delete><query>*:*</query></delete>, as with the following URL: 


http://<host>:<port>/solr/update?stream.body=<delete><query>*:*</query></delete>&commit=true


Just replace the placeholder with the correct details. If executed successfully the it will return a response with status = 0 and query execution time.
<response>

<lstname="responseHeader"> <intname="status">0</int> <intname="QTime">3693</int> </lst> </response>

If you want to remove indexes of a particular field then specify the field name in the delete query
<delete><query>desc:*</query></delete>


You can also delete a wrongly indexed value from a particular field, just by adding the value in the delete query
<delete><query>desc:wrold</query>&l…

Using Solr Spellchecker from Java

Continuing from my prevoius post Implementing Spellchecker in Solr, we'll now see how to use SorlJ API to fetch spellcheck suggestions through a Java program.


public static Map<String, List<String>> getSuggestions(String queryStr){
if (queryStr != null &&  !queryStr.isEmpty()) {

try {

SolrServer server = new HttpSolrServer("http://localhost:8983/solr");


   ModifiableSolrParams params = new ModifiableSolrParams();
   params.set("qt", "/spell");
   params.set("q", queryStr);
   params.set("spellcheck", "true");
   params.set("spellcheck.collate", "true");


   QueryResponse response = server.query(params);
   System.out.println("response = " + response);

   if(response != null){
SpellCheckResponse scr = response.getSpellCheckResponse();
if(scr != null){
List<Suggestion> suggLst = scr.getSuggestions();
for(Suggestion sugg : suggLst){
System.out.println("Suggestion fo…