package cwp.tags; // Package import java.io.*; import java.net.*; import java.util.*; import java.awt.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.TagSupport;// tells the system what to do when it sees the tag. import javax.servlet.http.*; import javax.servlet.*; import cwp.tags.*; public class IncludeTag extends TagSupport { String uriName; // URI name String ttlName; // ttl name int ttlValue; // time to live value protected Writer writer; // OutputStreamWriter protected BufferedReader reader; // InputStreamReader protected String fileHost, fileName; // FileHostName and FileName protected int filePort; // FilePort static int count =0; // Cache Directory String cacheDirector = "C:\\ayeddula\\tomcat\\jakarta-tomcat-4.0.1\\webapps\\examples\\WEB-INF\\classes\\cwp\\tags\\proxy\\"; // The servlet starts to do some processing when sees the tag public int doStartTag() throws JspException { try { //content type to print on the browser String dateString = new Date().toString(); // to print server time JspWriter out = pageContext.getOut(); // JSP writer to the browser // automatic refresh : meta head to refresh 3 seconds // HttpServletRequest request = (HttpServletRequest)pageContext.getRequest(); //getrequest header // out.println("" ); //out.println("

Dynamic Page

\n"); //out.println("
"+"
"); //*out.println("\n"+"Show URI servlet.\n"+"
\n"); //*out.println("Class = "+ getClass().getName() + "
"+"Date = "+dateString+"
"); //**out.print("Print URI = " + uriName +"
"); Process p= null; if(count == 0) { Runtime runtime = java.lang.Runtime.getRuntime(); try { p = runtime.exec("perl c:\\ayeddula\\alternate.pl"); } catch (IOException ex) { System.out.println("Error: "+ex); } //(p.getOutputStream()).close(); //(p.getInputStream()).close(); //(p.getErrorStream()).close(); count++; System.out.println("Count = " + count); } URL url = new URL (uriName); //split the URL fileHost = url.getHost (); filePort = url.getPort (); if (filePort == -1) filePort = 80; // default PORT is 80 fileName = url.getFile (); ttlValue = getTtl(ttlName); // convert string to integer //*out.println("ttl name = " + ttlName + "
"); // get the string value //**out.println("Time To Live Value = " + ttlValue + " Seconds"+"
"); // get the integer value //*out.println("
"+"page begin - -

"); // To check if the page is in cache or not if (checkFile(fileName) == false) { // File is * not * in cache directory // File is not in cache so get from the origin server //*out.println("
File ( "+ fileName +" ) * not * in cache"+"
"); // Establish connection and get the file from the Origin server // also Save a copy in cache grab(); } else { // File is in cache directory // Check the files last modified date in the origin server //**out.println("File ( "+ fileName +" ) * is in * cache"+"
"); if (checkFileStatus(cacheDirector + getFileName(fileName),ttlValue) == 1) { // not modified send the cache copy to the browser //**out.println("File Exists in cache and Time to live * is not expired *

"); BufferedReader in = null; File inFile; String line; in = new BufferedReader(new InputStreamReader(new FileInputStream(new File(cacheDirector+ getFileName(fileName))))); while ((line= in.readLine()) != null) out.println(line); in.close(); } else { // modified on the origin server save a copy in cache // send the cache copy to the browser //**out.println("
File Exists in cache and Time to live is * expired *

"); grab(); } } //out.println("

- - page end"); } catch(Exception ioe) { throw new JspTagException("Error : "); } return SKIP_BODY; } // end do start tag // Check if will expired or not private int checkFileStatus(String filepath, int ttl) throws Exception { JspWriter out = pageContext.getOut(); File fileName = new File(filepath); //**out.println("Timing results : " + (System.currentTimeMillis() - fileName.lastModified())); if ((System.currentTimeMillis() - fileName.lastModified()) <= (ttl * 1000)) return 1; return 0; } // Set URI called when set the Tag attribute public void setUri (String name) { uriName = name; } // Set Ttl called when set the Tag attribute public void setTtl (String name) { ttlName = name; } // Gets File Name from the uri private String getFileName(String uri) { return uri.substring(uri.lastIndexOf('/')+1); } // Check file is in cache or not private boolean checkFile(String s) throws Exception { JspWriter out = pageContext.getOut(); s = cacheDirector+ getFileName(s); //*out.println("file now is "+ s + "
*** And is in Cache now : Gives True if in cache else False" + "
"); File fileName = new File(s); return fileName.isFile(); } // get Time to live value in integer private int getTtl (String ttlName) { int tmp; tmp = Integer.valueOf (ttlName).intValue(); return (tmp); } // does the socket connection and prints the file on the browser public void grab () throws IOException { connect (); try { fetch (); } finally { disconnect (); } } // connect () to the origin server protected void connect () throws IOException { Socket socket = new Socket (fileHost, filePort); OutputStream out = socket.getOutputStream (); writer = new OutputStreamWriter (out, "latin1"); InputStream in = socket.getInputStream (); Reader reader = new InputStreamReader (in, "latin1"); this.reader = new BufferedReader (reader); } // fetch () the web page and also store in cache protected void fetch () throws IOException { PrintWriter outgoing; // Stream for sending a command to the server. PrintWriter cacheOut = null; // open file for writing into the disk cache String tmp; int len=0; // Open Stream for writing into disk cache cacheOut = new PrintWriter(new FileWriter(cacheDirector+ getFileName(fileName))); JspWriter out = pageContext.getOut(); String input; writer.write ("GET " + fileName + " HTTP/1.0\r\n\n"); writer.flush (); // Remove the headers while ((input = reader.readLine ()).length() != 0); // while ((input = reader.readLine ()).length() != 0) out.println(input); // show header // grab the data and display while ((input = reader.readLine ()) != null) { out.println (input); cacheOut.println (input); len = len + input.length(); } //out.println ("file length is "+len); out.flush (); cacheOut.close(); // closing the stream } // disconnect () after work is done protected void disconnect () throws IOException { reader.close (); } }//End IncludeTag class