Francois Dang Ngoc

Greenhorn
+ Follow
since Sep 02, 2007
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Francois Dang Ngoc

Hi Dave,

Do you mean you want to skip the validation of the document (so don't use the DTD)?
In this case, you can disable the validation as follows:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// disable validation
factory.setValidating(false);

DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("file.xml");

Hope it helps,

Cheers,

Francois
[ May 22, 2008: Message edited by: Francois Dang Ngoc ]
13 years ago
Hi Sujata,

> DateFormat df = new SimpleDateFormat("yyyyMMddHH24MISS");

If we look at the Java API (http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html), we have the following:
- H: hour in day (0-24)
- m: Minute in hour
- s: Second in minute

So the pattern can be defined as: yyyyMMddHHmmss

Hope it helps,

Cheers,

Francois
13 years ago
Hi Omkar,

> 1) How do i forward the actual request and response ?

First you need to extract the hostname to connect to. In the HTTP
request, you have something like:
GET /something HTTP/1.1
Host: www.yahoo.com
...

So we need to parse the line containing Host and extract www.yahoo.com.
and then open a connection and forward everything to the host www.yahoo.com.

2) After getting the response in the inputStream, how do i forward this response back to the browser who made the request ?

you can do a loop like this:

// forward the response from the server to the browser
InputStream sis = hostSocket.getInputStream();
OutputStream bos = clientSocket.getOutputStream();

System.out.println("Forwarding request from server");

do {
n = sis.read(buffer);
if (n > 0) {
bos.write(buffer, 0, n);
}
} while (n > 0);


3) Can the HttpRequest and HttpResponse and Session objects available in java be of any help to us ?

Are you referring to those used in servlets? If you write your program in a servlet, that may be useful.

Below, you find the code of a simple proxy.

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleProxyServer {

public static final int portNumber = 5555;

public static void main(String[] args){
SimpleProxyServer proxyServer = new SimpleProxyServer();
proxyServer.start();

}
public void start(){
System.out.println("Starting the SimpleProxyServer ...");
try {

ServerSocket serverSocket = new ServerSocket(MyProxyServer.portNumber,1);
byte[] buffer = new byte[10000];

while(true){
Socket clientSocket = serverSocket.accept();

InputStream bis = clientSocket.getInputStream();

// reading the request and put it into buffer
int n = bis.read(buffer);
String browserRequest = new String(buffer, 0, n);
System.out.println(browserRequest);

// extract the host to connect to
int start = browserRequest.indexOf("Host: ") + 6;
int end = browserRequest.indexOf('\n', start);
String host = browserRequest.substring(start, end - 1);
System.out.println("Connecting to host " + host);

// forward the response from the proxy to the server
Socket hostSocket = new Socket(host, 80);
OutputStream sos = hostSocket.getOutputStream();
System.out.println("Forwarding request to server");
sos.write(buffer, 0, n);
sos.flush();

// forward the response from the server to the browser
InputStream sis = hostSocket.getInputStream();
OutputStream bos = clientSocket.getOutputStream();

System.out.println("Forwarding request from server");

do {
n = sis.read(buffer);
System.out.println("Receiving " + n + " bytes");
if (n > 0) {
bos.write(buffer, 0, n);
}
} while (n > 0);

bos.flush();
hostSocket.close();
clientSocket.close();
System.out.println("End of communication");

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Hope it helps,

Cheers,

Francois
Hi Omkar,

Yes, by single lines, I mean that there is one meta (Host, User Agent)
per line. One line for the GET followed by another line for Host, another line for User-Agent,...

So when you do:
while((meta = bufferedReader.readLine()).length() > 0){
System.out.println(meta);
}

it reads one line at a time but readLine() returns the line without the EOL (end of line) character, that is why when you print the whole thing using System.out.print, the EOLs are removed and everything is printed in one line.

The way POST works is different from GET in the sense that the
parameters are not passed in the URL as in GET
(e.g., GET http://www.google.com/search?q=test where q is
a parameter).

With POST, parameters are passed
after the meta information (e.g., User-Agent, Host, ...).
The format is as follows:
POST http://www.google.com/ HTTP/1.1
User-Agent: "Web Browser"
...
Content-Type: application/x-www-form-urlencoded
Content-Length: 11
*** HERE WE HAVE AN EMPTY LINE ***
q=foo&x=bar

Where q and x are two inputs entered using POST. The parameters are in the form of key=value separated by &.

In the meta, there is the line "Content-Length: 11" which
specifies the size of all the parameters (here, q=foo&x=bar which is 11 characters).

A small modification to the program should do to extract the string
q=foo&x=bar.

First, we need to get the content length so
we can change a bit the way we read the meta as follows:
String meta = null;
int contentLength = 0;
while((meta = bufferedReader.readLine()).length() > 0){
System.out.println(meta);
if (meta.startsWith("Content-Length:")) {
// get the number
contentLength = Integer.parseInt(meta.substring(meta.indexOf(':') + 2));
}
}

Note that this loop iterates over all the meta lines until it find an empty line. So after the
empty line, we just need to read the parameters (of size contentLength) issued by the post command as follows:
// this comes after the while((meta = ...

if (command.startsWith("POST")) {
// display params
char[] postData = new char[contentLength];
bufferedReader.read(postData);
System.out.println("Params in the POST request: [" + new String(postData) + "]");
}
bufferedReader.close();

The variable postData needs now to be parsed to extract the couples (key= value).

You can take a look at this page: http://www.jmarshall.com/easy/http/#http1.1c1
which concisely describes the HTTP protocol.

Hope it helps,

Cheers,

Francois

[ September 04, 2007: Message edited by: Francois Dang Ngoc ]
[ September 04, 2007: Message edited by: Francois Dang Ngoc ]
Hi Omkar,

The other meta-data are passed as single lines (key: value)
after the GET/POST request. For intance:

GET http://www.google.com/ HTTP/1.1
Host: www.google.com
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20070810 Ubuntu/7.10 (gutsy) Firefox/2.0.0.6
...

That means that the program should iterate over all the lines
following the GET/POST request.

You can do something like that:

while(true) {
System.out.println("Inside while loop ");
Socket clientSocket = serverSocket.accept();
System.out.println("Connection to MyProxyServer is "+clientSocket.isConnected());

InputStreamReader inputStreamReader = new InputStreamReader(clientSocket.getInputStream());

BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String command = bufferedReader.readLine();

System.out.println("Client has asked to ....\n"+command);

if(command.equals("Cancel")){
System.out.println("Shutting down the server ...");
break;

}

// iterate over all the lines following the GET/POST request
String meta = null;
while((meta = bufferedReader.readLine()).length() > 0){
System.out.println(meta);
}

bufferedReader.close();
}

Hope it helps,

Cheers,

Francois
Hi Edward,

You can use the method getServletContext() as follows:

InputStream is = getServletContext().getResourceAsStream ("/WEB-INF/conf/database.properties");
Properties props = new Properties();
props.load(is);
out.println("HOST: " + props.getProperty("host") + " URL: " + props.getProperty("url"));

Hope it helps,

Cheers,

Francois
14 years ago