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;
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;
try {
URL url = new URL(UrlString);
InputStream is = url.openStream();
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];
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) {};
}
}