import java.io.*;
import java.util.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class mm extends HttpServlet {
static int indentLevel = -1;
public void service(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Enumeration flds = req.getParameterNames();
String Coden = new String();
String HEAD="/mm/head";
String EXTFILE="/apps/multimedia/ext_includes";
String TAIL="/mm/tail";
String ROOTDIR="/ftp/public/epaps";
String PapId = req.getParameter("ID");
PapId = ((PapId==null) ? "" : PapId );
String IdType = req.getParameter("IDTYPE");
IdType = ((IdType==null) ? "" : IdType );
String DoiCoden = req.getParameter("CODEN");
DoiCoden = ((DoiCoden==null) ? "" : DoiCoden );
String FileName = req.getParameter("FILE");
FileName = ((FileName==null) ? "" : FileName );
if ( PapId.equals("") )
this.error(req,res,out,"Missing papId");
if ( !IdType.equals("") || IdType.indexOf("doi") >= 0 ){
if( !DoiCoden.equals("")) this.error(req,res,out,"Missing DoiCoden");
Coden = ( DoiCoden);
String Ptr = PapId.substring(8,PapId.length()-1);
PapId += Ptr;
//out.println("DEBUG0 : " +Coden);
}
else {
char currentChar;
if ( PapId.indexOf("E-") < 0 ) this.error(req,res,out,"Missing E-");
currentChar = PapId.charAt(1);
if ( currentChar != '-' ) this.error(req,res,out,"Missing E-");
Coden = PapId.substring(2,8);
//out.println("DEBUG1 : " +Coden);
}
String SubDir = GetDirectory( Coden);
String Directory = ( ROOTDIR +"/" +SubDir +"/" +PapId);
if( !FileName.equals("") ) {
String Location = ("ftp://ftp.aip.org/" +Directory.substring(11,Directory.length()-1) +"/" +FileName);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(Location);
try {
dispatcher.forward(req, res);
}
catch (ServletException e) {
out.print("");
}
}
this.loadFile (req, res, out, HEAD);
//out.println("DEBUG2 : " +Directory);
PrintTitle( Coden , out);
//out.println("DEBUG3 : " +Directory);
ScanFiles( Directory, out);
this.loadFile (req, res, out, TAIL);
}
public void PrintTitle ( String Coden, PrintWriter out ){
String text = null;
String retIt = new String();
File input = new File("/apps/multimedia/title_lookup");
int fieldNumber = 0;
String fields[] = new String[2];
try {
BufferedReader myStream =
new BufferedReader(new InputStreamReader(
new FileInputStream(input)));
while ((text = myStream.readLine()) != null){
char[] possArray = text.toCharArray();
if (possArray[0] == '#' ) continue;
else if ( text.indexOf(Coden) >= 0){
if ( text.length() > 6 )
out.println(text.substring(6, text.length() -1));
}
}
}
catch(IOException e) {
System.out.println("Exception in mm : "+e.toString() );
}
}
public void ScanFiles ( String Directory, PrintWriter out){
File path = new File(Directory);
File files[];
indentLevel++;
int Count = 0;
String Store[] = new String[1000];
files = path.listFiles();
Arrays.sort(files);
for (int i = 0, n = files.length; i < n; i++) {
for (int indent = 0; indent < indentLevel; indent++) {
System.out.print(" ");
}
if (!files[i].isDirectory()){
String pname = files[i].toString();
if( pname.indexOf("index.html") >= 0) {
out.println( "
The author has provided an ");
out.println( "HTML index page to facilitate the access of these files.
");
}
else if( pname.indexOf("index.htm") >= 0) {
out.println( "
The author has provided an ");
out.println("HTML index page to facilitate the access of these files.
");
}
else Store[Count++] = pname;
// if the file is a diectory, call listPath recursevly
//if (files[i].isDirectory()) {
// listPath(files[i]);
// }
}
indentLevel--;
}
for( int i=0; i < Count; i++) {
if ( Store[i].length() > 12 ){
StringTokenizer z = new StringTokenizer(Store[i], "/");
int n = z.countTokens();
String t = null;
while (z.hasMoreTokens())
t = z.nextToken();
out.println("" +t +"");
if ( Store[i].indexOf("README.TXT") >= 0)
out.println(" files accompany each deposit and contain a description of the materials and instructions for use.
");
else {
// Get the number of bytes in the file
File file = new File(Store[i]);
float length = file.length() / (float) 1024;
out.println(" (" +length +" kB)
");
}
}
}
}
public static String GetDirectory ( String Coden ){
String text = null;
String retIt = new String();
File input = new File("/apps/multimedia/coden_lookup");
int fieldNumber = 0;
String fields[] = new String[2];
try {
BufferedReader myStream =
new BufferedReader(new InputStreamReader(
new FileInputStream(input)));
while ((text = myStream.readLine()) != null){
char[] possArray = text.toCharArray();
if (possArray[0] == '#' ) continue;
else if ( (text.indexOf(Coden) ) >= 0){
StringTokenizer st = new StringTokenizer(text," ");
while (st.hasMoreTokens())
fields[fieldNumber++] = st.nextToken();
}
retIt = fields[1];
} // end while
}
catch(IOException e) {
System.out.println("Exception in mm : "+e.toString() );
}
return retIt;
}
public void error(HttpServletRequest req, HttpServletResponse res,
PrintWriter out, String code) throws IOException {
req.setAttribute ("myError" , code);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/mm/error.jsp");
try {
dispatcher.forward(req, res);
}
catch (ServletException e) {
out.print("