import javax.servlet.*; import javax.servlet.http.*; import java.io.RandomAccessFile; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; import java.net.URLDecoder; /* * [todo 2006.06.06] integrate exception message into response instead * of to System.err. */ public class ModuleAccessHandlerServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { String requestText = request.getQueryString(); if (requestText != null) requestText = URLDecoder.decode(requestText, "UTF-8"); doEither(request, response, requestText); }//end (doGet) public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { BufferedReader in = request.getReader(); String line, requestText = new String(); while ((line = in.readLine()) != null) { requestText += line; } doEither(request, response, requestText); }//end (doPost) public void doEither(HttpServletRequest request, HttpServletResponse response, String requestText) throws IOException { /* * The driverName can be specified in the URL as follows: * http://142.88.2.43/mah?driver=aDriverName& -1) requestText = requestText.substring(requestStart + 1); try { /* * This way is 2-3 times faster than first opening read-only to * throw an exception if the file does not exist ... */ java.io.File checkExists = new java.io.File(driverName); if (!checkExists.exists()) throw new IOException ("The driver does not exist on this server"); RandomAccessFile driver = new RandomAccessFile(driverName, "rwd"); FileOutputStream writer = new FileOutputStream(driver.getFD()); FileReader reader = new FileReader(driver.getFD()); PrintWriter mahOut = new PrintWriter(writer); mahOut.println(requestText); mahOut.flush(); BufferedReader mahIn = new BufferedReader(reader); driver.seek(0); PrintWriter responseMessage = response.getWriter(); responseMessage = response.getWriter(); //response.setHeader("Content-Type", "text/xml"); String line; while((line = mahIn.readLine()) != null) { responseMessage.println(line); } driver.close(); } catch (java.io.UnsupportedEncodingException uee) { try { response.sendError (500, "ModuleAccessHandlerServlet." + "UnsupportedEncodingException: The content-type " + "'charset' property was set to a value not " + "supported by the server:\n" + uee.getMessage()); } catch (IOException ioe) { response.setStatus(500); } catch (IllegalStateException ise) { response.setStatus(500); } } // if a Writer already exists on the response catch (IllegalStateException ise) { response.setStatus(500); } catch (IOException e) { try { response.sendError (500, "ModuleAccessHandlerServlet.IOException " + "while attempting to communicate with CMIB " + "driver software: '" + driverName + "':\n" + e.getMessage()); } catch (IOException ioe) { response.setStatus(500); } catch (IllegalStateException ise) { response.setStatus(500); } } }// end (doEither) } //end (ModuleAccessHandlerServlet.java)