Showing posts with label url. Show all posts
Showing posts with label url. Show all posts

Wednesday, January 6, 2010

Part 2: NoRouteToHostException

Part1 : Part2

If following code run without proxy details, then java.net.NoRouteToHostException will be thrown in windows/linux platform


import java.net.Socket;
public class SocketPrg {
public static void main(String[] args) throws Exception{
Socket sock=new Socket("abc.javafundu.com",8001);
}
}


UnknownHostException

Assume, I have given proper proxy details if internet connection established through proxy server. If we refer wrong host name which does not exsit then we do get java.net.UnknownHostException.

Additionally, we may refer right host name then also we do get this exception. In this case we have to check hosts file and correct the entry.

windows: /windows/system32/drivers/hosts
Linux : /etc/hosts

Make following entry in hosts file
127.87.6.207 abc.javafunud.com javafundu.com
Exception will be thrown as below


Exception in thread "main" java.net.UnknownHostException: abc.javafundu.com
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at SocketPrg.main(SocketPrg.java:7)


ConnectException

If host name is correct and hosts points right IP address with wrong port number then ConnectException will be thrown with Connection timed out for remote host most of the time( with default timeout configuration). For localhost or same network host, we do get Connection refused.

Socket sock=new Socket("localhost",8001);


Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at SocketPrg.main(SocketPrg.java:7)

Socket sock=new Socket("javafundu.com",8001);


Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at SocketPrg.main(SocketPrg.java:9)

Tuesday, July 8, 2008

URLHandler for ZIP stream

Accessing file from ZIP using URL

In our usual life, we may need to access particular content from ZIP file often and often. We may not be interested in retrieving complete content from ZIP. It would be good idea to have a URL Handler, which will help us to access only the content of ZIP directly and/or seamlessly.

We have a built-in facility of accessing content from JAR file directly. Why should not we form the similar approach to access the content from ZIP ?.

Aim of this post is not only to explain the code which will help us to retrieve the particular content, we do get to know the way how URL handler is going to work.


Agenda:

Name the protocol
Name the Java package
Creating Java files required to enable URLHandler for ZIP protocol
Notifying to JVM by setting in environment
Notifying to JVM by setting as part of Main method

1. Name the Protocol

We have to name the protocol i.e we have to prefix a string which will help us to say what we are trying to do. Here, we will create our own protocol and we will name it as “zip”. Hence the URL format will looks like below
zip:http://www.javafundu.com/baz.zip!/afolder/a.txt
where
!/ - Separator
http://www.javafundu.com/baz.zip - points actual zip file
afolder/a.txt – is the content available in baz.zip

2. Name the Java Package

Java files has to be placed in a package and the naming package should looks like below

<anypackagename>.protocol.<nameoftheprotocol>
Example: com.javafundu.protocol.zip

3. Create Java files URLStreamHandler and URLConnection

Handler, which will instruct JVM to call this Class to open the connection when URL.openConnection().

Handler.java

package com.javafundu.protocol;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

public class Handler extends URLStreamHandler {

@Override
protected URLConnection openConnection(URL url) throws IOException {
// create new instance of URLConnection
return new ZIPURLConnection(url);
}
}

URLConnection does the actual job of unzipping and returning the subelement. URL argument in openConnection method has the URL http://… , when actual url is “zip:http://…”

ZIPURLConnection.java:

package com.javafundu.protocol;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZIPURLConnection extends URLConnection {

protected ZIPURLConnection(URL url)throws MalformedURLException {
super(url);
// TODO Auto-generated constructor stub
}

@Override
public void connect() throws IOException {
// TODO Auto-generated method stub

}

/* (non-Javadoc)
* @see java.net.URLConnection#getInputStream()
*/
public InputStream getInputStream() throws IOException {

InputStream result= null;
URL url = this.url;

String spec = url.getFile();

int separator = spec.indexOf('!');
/*
* REMIND: we don't handle nested JAR URLs
*/
if (separator == -1) {
throw new MalformedURLException("no ! found in url spec:" + spec);
}

long startTime = System.currentTimeMillis();

URL zipFileURL = new URL(spec.substring(0, separator++));

String entryName =spec.substring(separator+1);



ZipInputStream zis = new ZipInputStream(zipFileURL.openStream());
ZipEntry entry = null;

// check inputStream is ZIP or not
if ((entry = zis.getNextEntry()) != null) {
do {
// System.out.println(entry.getName());
if(entryName.equals( entry.getName()))
{
result= zis;
break;
}
} while ((entry = zis.getNextEntry()) != null);
} else {
System.out.println("Given path is not referring ZIP file");
}
System.out.println( "Looking for " +entryName + " " +(System.currentTimeMillis() - startTime) + "ms");
return result;
}
}

Now Java implementation is ready for compilation. Bundle your application with these two class files. Assume bundled as jf.jar.

4.Notifying to JVM by setting in environment

Set the environment variable, which specifies the information of Handlers available location.

Java -Djava.protocol.handler.pkgs=”com.javafundu.protocol”

Now, your JVM will understand “zip:” as a URL authority and start works. If you create a URL with zip: , then Java will call com.javafundu.protocol.Handler to parse this URL.

URL url=new URL(“zip:http://www.javafundu.com/baz.zip!/afolder/a.txt ”)

5.Notifying to JVM by setting as part of Main method

Sometime, this is very difficult to set this value in environment variable due to some restriction. Hence, we have alternate approach, which will help us to set and make use of this facility.

System.setProperty("java.protocol.handler.pkgs","com.javafundu.protocol");

We have to set this property at the earliest possible to before creating URL with zip:
If we wants to specify more than one protocol then we have to delimit using symbol. For example

System.setProperty("java.protocol.handler.pkgs","com.javafundu.msaccess.protocolcom.javafundu.protocol");


Resource:
URL (Java 2 Platform SE v1.4.2) |
JarURLConnection (Java 2 Platform SE v1.4.2) |
RFC 2373: IP Version 6 Addressing Architecture

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com