package WebFlow.Charon;
import WebFlow.*;
import WebFlow.event.*;
import WebFlow.Charon.CharonServerPackage.*;
import org.omg.CORBA.*;
import java.util.*;
import java.net.*;
import java.io.*;
public class CharonServerImp extends WebFlow.BeanContextChildSupport
implements CharonServerOperations {
String msg_;
org.omg.CORBA.Object peer;
BufferedInputStream bif;
int blksize;
static String myID;
int testint;
boolean headerflag;
URLConnection conn;
public CharonServerImp(org.omg.CORBA.Object peer, String msg)
throws WebFlow.NullPointerException{
super(peer);
this.peer = peer;
String msg_ = msg;
blksize=1024;
}
public void test(){
System.out.println("Charon Server is up!");
}
public boolean connect(String UrlString) {
boolean status=true;
headerflag=true;
try {
URL url = new URL(UrlString);
// InputStream is = url.openStream();
//I added the stuff below--Marlon.
conn=url.openConnection();
conn.setDoOutput(true);
conn.setDefaultUseCaches(false);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.connect();
// System.out.println("Test header: "+conn.getHeaderField("Set-Cookie"));
InputStream is=conn.getInputStream();
bif = new BufferedInputStream(is);
} catch (Exception e) {
System.out.println("cannot connect to "+UrlString);
System.out.println(e.toString());
status=false;
}
return status;
}
public void closeConnection() {
System.out.println("close connection");
try {
if(bif != null) bif.close();
else System.out.println("bif is already closed");
} catch (Exception e) {
System.out.println("cannot close input stream");
System.out.println(e.toString());
}
bif=null;
}
public void setBlockSize(int size) {
blksize=size;
}
public String getBlock() {
String response="";
byte[] a = null;
byte[] b = new byte[blksize];
try {
if(bif==null) System.out.println("bif is null");
int icount = bif.read(b);
System.out.println("icount="+icount);
if(icount == -1) {
response="EOF";
}
else {
if(icount == blksize) a=b; //buffer filled
else {
a = new byte[icount];
for(int i =0; i < icount; i++) a[i]=b[i];
}
response=new String(a);
}
}
catch(Exception e) {
System.out.println("IO Exception in getBlock");
System.out.println(e);
}
/* you may want to modify a
response=checkString(response,TargetHost);
*/
if(response==null) response="null";
return response;
}
public byte[] getBinaryBlock() {
byte[] a = null;
byte[] b = new byte[blksize];
//Return header information first
System.out.println("In binary block");
if(headerflag) {
String holder="HTTP/1.0 200 OK \n";
String key;
int n=1;
while((key=conn.getHeaderFieldKey(n)) !=null){
holder=holder+
conn.getHeaderFieldKey(n)+": "+
conn.getHeaderField(conn.getHeaderFieldKey(n)) +"\n";
n++;
}
holder+="\n";
a=holder.getBytes();
headerflag=false;
System.out.println("header info sent");
}
else {
try {
if(bif==null) System.out.println("bif is null");
int icount = bif.read(b);
System.out.println("icount="+icount);
if(icount == -1) {
a = new byte[0];
}
else {
if(icount == blksize) a=b; //buffer filled
else {
a = new byte[icount];
for(int i =0; i < icount; i++) a[i]=b[i];
}
}
}
catch(Exception e) {
System.out.println("IO Exception in getBlock");
System.out.println(e);
}
}
return a;
}
private String checkString(String string, String TargetServer) {
/* replaces relative references to absolute ones
for example:
to
*/
String newString = removeAbsolute(string,"HREF",TargetServer);
newString = removeAbsolute(newString,"href",TargetServer);
newString = removeAbsolute(newString,"SRC",TargetServer);
newString = removeAbsolute(newString,"src",TargetServer);
return newString;
}
private String removeAbsolute(String string, String Tag,
String TargetServer){
String newstring = null;
String temp,temp1,temp2;
temp1="";
int idx=string.indexOf(Tag);
if(idx<0) return string;
while(idx>-1) {
newstring=string.substring(0,idx);
temp=string.substring(idx);
/* now temp starts with Tag: move Tag to newstring */
int idx2 = temp.indexOf("=")+1;
newstring += temp.substring(0,idx2);
temp = temp.substring(idx2);
/* now temp starts after = */
if(temp.startsWith("\"/") ||
temp.startsWith(" \"/") ||
temp.startsWith("/") ||
temp.startsWith(" /")) {
temp = "http://"+TargetServer+temp;
}
newstring += temp;
temp1 += newstring;
string = temp;
string=string.substring(Tag.length());
idx=string.indexOf(Tag);
}
return temp1;
}
public synchronized void waitfordata() {
try {
System.out.println("wait");
wait(100);
} catch(Exception e) {};
}
// public void setKerberosID(int anint) {
// this.testint=anint;
// }
// public int getKerberosID() {
// return this.testint;
// }
public void setKerberosID(String astring) {
this.myID=astring;
}
public String getKerberosID() {
//Returns the id as a string.
return this.myID;
}
// public void registerObserver(CharonServer charonserv){
// this.charonserv=charonserv;
// }
// public void notifyObserver(String UserName){
// charonserv.setKerberosID(UserName);
// }
}