Sunday, January 17, 2010

Embed fonts for pdf usng itext

Usually, "consola" font family is not used for pdfs, because it is not available in all systems. Using following code sample, you can embed your interested fonts for pdfs.

Registering regular True type "consola" font.
FontFactory.register("D:\\consola.ttf", "Manning");
FontFactory.getFont("Manning", BaseFont.CP1252, BaseFont.EMBEDDED);
Registering bold True type "consola" font.
FontFactory.register("D:\\consolab.ttf","Manning");
FontFactory.getFont("Manning", BaseFont.CP1252, BaseFont.EMBEDDED);

Monday, December 14, 2009

Graphical + Attachment java Email Client

import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailClient {

private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final String SMTP_AUTH_USER =
xxx@gmail.com;
private static final String SMTP_AUTH_PWD = "xxx";


public static void main(String[] args) throws Exception {

new SimpleMailWithAttachment().test();

}

public void test() throws Exception {
Properties props = new Properties();

props.put("mail.transport.protocol", "smtp");
props.put("mail.host", SMTP_HOST_NAME);

Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing javamail with attachment");
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent("<h1>Check attachment</h1>", "text/html");
MimeBodyPart attachFilePart = new MimeBodyPart();

FileDataSource fds =
new FileDataSource("d:\\TwainExample.java");

attachFilePart.setDataHandler(new DataHandler(fds));
attachFilePart.setFileName(fds.getName());
Multipart mp = new MimeMultipart();
mp.addBodyPart(textPart);
mp.addBodyPart(attachFilePart);
message.setContent(mp);
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("
xxx@yahoo.com"));
transport.connect(SMTP_HOST_NAME, SMTP_AUTH_USER,
SMTP_AUTH_PWD);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}

Friday, December 11, 2009

Disable enter key using java script

Use the below code to disable enter key. You can also used this to disable un wanted keys,too. Just add more combination of keys after "evt.keyCode == 13".
<html>
<head>
<script language=javascript type=text/javascript>
<!--
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if (evt.keyCode == 13) {return false;}
}

document.onkeypress = stopRKey;
-->
</script>
</head>

<body>
<textarea rows="5"></textarea>
</body>
</html>

Thursday, December 10, 2009

How to write & read cookie in java?

In this jsp page you can pass the value you want to set as your cookie.

<%@ page language="java" %>
<html>
<head>
<title>Cookie Input Form</title>
</head>
<body>

<form method="post" action="setcookie.jsp">
<p><b>Enter Your Name: </b><input type="text" name="username"><br>
<input type="submit" value="Submit">
</form>

</body>


This page captures the cookie input value and set cookie in client hard disk.

<%@ page language="java" import="java.util.*"%>
<%
String username=request.getParameter("username");
if(username==null) username="";

Date now = new Date();
String timestamp = now.toString();
Cookie cookie = new Cookie("username",username);
cookie.setMaxAge(365 * 24 * 60 * 60);
response.addCookie(cookie);


%>
<html>
<head>
<title>Cookie Saved</title>
</head>
<body>
<p><a href="showcookievalue.jsp">Next Page to view the cookie value</a><p>

</body>

This is a page you can to use to see the cookie value later time.
<%@ page language="java" %>
<%
String cookieName = "username";
Cookie cookies [] = request.getCookies ();
Cookie myCookie = null;
if (cookies != null)
{
for (int i = 0; i < cookies.length; i++)
{
if (cookies [i].getName().equals (cookieName))
{
myCookie = cookies[i];
break;
}
}
}
%>
<html>
<head>
<title>Show Saved Cookie</title>
</head>
<body>
<%
if (myCookie == null) {
%>
No Cookie found with the name <%=cookieName%>
<%
} else {
%>
<p>Welcome: <%=myCookie.getValue()%>.
<%
}
%>
</body>

Wednesday, December 2, 2009

Use keytool to generate keys in java

  1. keytool -genkey -alias weblogic -keyalg RSA -keystore server.keystore
  2. keytool -selfcert -export -alias weblogic -storepass changeit -file server.cer -keystore server.keystore
  3. keytool -genkey -alias client -keyalg RSA -keystore client.keystore
  4. keytool -selfcert -export -alias client -storepass changeit -file client.cer -keystore client.keystore
  5. keytool -import -v -trustcacerts -alias weblogic -file server.cer -keystore client.keystore -keypass changeit -storepass changeit
  6. keytool -keystore \jre\lib\security\cacerts -storepass changeit -file client.cer -import -alias client -trustcacerts
Here, i have created server.cer and client.cer certificates. Password used is "changeit".

Thursday, November 26, 2009

Use AXIS library to create web service and client

Download AXIS binary and extract it to your hard disk.
Set up AXIS2_HOME and path environment veriables.

Create service using WSDL file

1) Open terminal.

2) Go to the directory you want to create service.

3) Run the below command.

wsdl2java -uri "test.wsdl" -ss -sd -d xmlbeans -o service

4) This will create a folder called "service" and created the service using the .wsdl file.

5) Go to the "service" folder and run "ant" command to build the .aar file.

6) Go to AXIS bin and run "axis2server.sh".

7) Put the created .aar file into AXIS repository\services folder.

8) It will auto deployed as a service.

9) You can access the service using "http://127.0.0.1:8080".

Create client using WSDL file

1) Open terminal.

2) Go to the directory you want to create client.

3) Run the below command.

wsdl2java -uri "test.wsdl" -d xmlbeans -o client

4) This will create a folder called "client" and created the client code using the .wsdl file.

Wednesday, November 18, 2009

DNS Mapping in windows

Go to "C:\Windows\System32\drivers\etc".

Edit hosts file in etc folder. Add your ip addres and the mapping name.

Ex :-

192.168.4.7 name.domain.com

Now, you can access your ip address using DNS set above.

Ex :-

http://name.domain.com/App