1 14 package org.compiere.wstore; 15 16 import javax.servlet.*; 17 import javax.servlet.http.*; 18 import java.io.*; 19 import java.util.*; 20 import java.sql.*; 21 22 import org.apache.ecs.*; 23 import org.apache.ecs.xhtml.*; 24 import org.apache.log4j.Logger; 25 26 import org.compiere.util.*; 27 import org.compiere.model.*; 28 import org.compiere.www.*; 29 30 31 37 public class InvoiceServlet extends HttpServlet 38 { 39 40 private Logger log = Logger.getLogger(getClass()); 41 42 static public final String NAME = "invoiceServlet"; 43 44 50 public void init(ServletConfig config) 51 throws ServletException 52 { 53 super.init(config); 54 if (!WEnv.initWeb(config)) 55 throw new ServletException("InvoiceServlet.init"); 56 } 58 62 public String getServletInfo() 63 { 64 return "Compiere Web Invoice Servlet"; 65 } 67 70 public void destroy() 71 { 72 log.debug("destroy"); 73 } 75 76 86 public void doGet(HttpServletRequest request, HttpServletResponse response) 87 throws ServletException, IOException 88 { 89 log.info("doGet from " + request.getRemoteHost() + " - " + request.getRemoteAddr()); 90 91 String url = "invoice.jsp"; 92 HttpSession session = request.getSession(false); 94 session.removeAttribute(JSPEnv.HDR_MESSAGE); 95 if (session == null || session.getAttribute(Info.NAME) == null) 96 url = "login.jsp"; 97 else 98 { 99 Info info = (Info)session.getAttribute(Info.NAME); 100 if (info != null) 101 info.setMessage(""); 102 103 String msg = streamInvoice(request, response); 105 if (msg == null || msg.length() == 0) 106 return; 107 if (info != null) 108 info.setMessage(msg); 109 } 110 111 log.info ("doGet - Forward to " + url); 112 RequestDispatcher dispatcher = getServletContext ().getRequestDispatcher (url); 113 dispatcher.forward (request, response); 114 } 116 124 public void doPost(HttpServletRequest request, HttpServletResponse response) 125 throws ServletException, IOException 126 { 127 log.info("doPost from " + request.getRemoteHost() + " - " + request.getRemoteAddr()); 128 HttpSession session = request.getSession(false); 129 } 131 137 private String streamInvoice (HttpServletRequest request, HttpServletResponse response) 138 { 139 String invoiceString = request.getParameter("Invoice_ID"); 141 if (invoiceString == null || invoiceString.length() == 0) 142 return ""; 143 int C_Invoice_ID = 0; 144 try 145 { 146 C_Invoice_ID = Integer.parseInt (invoiceString); 147 } 148 catch (NumberFormatException ex) 149 { 150 log.debug("streamInvoice - " + ex); 151 } 152 if (C_Invoice_ID == 0) 153 { 154 log.debug("streamInvoice - no ID)"); 155 return "No Invoice ID"; 156 } 157 158 Properties ctx = JSPEnv.getCtx(request); 160 HttpSession session = request.getSession(true); 161 MInvoice invoice = new MInvoice(ctx, C_Invoice_ID); 162 if (invoice.getC_Invoice_ID() != C_Invoice_ID) 163 { 164 log.debug("streamInvoice - Invoice not found - ID=" + C_Invoice_ID); 165 return "Invoice not found"; 166 } 167 WebUser wu = (WebUser)session.getAttribute(WebUser.NAME); 169 if (wu.getC_BPartner_ID() != invoice.getC_BPartner_ID()) 170 { 171 log.warn ("streamInvoice - Invoice from BPartner - C_Invoice_ID=" 172 + C_Invoice_ID + " - BP_Invoice=" + invoice.getC_BPartner_ID() 173 + " = BP_User=" + wu.getC_BPartner_ID()); 174 return "Your invoice not found"; 175 } 176 177 String dirName = ctx.getProperty("documentDir", "."); 179 try 180 { 181 File dir = new File (dirName); 182 if (!dir.exists ()) 183 dir.mkdir (); 184 } 185 catch (Exception ex) 186 { 187 log.error("streamInvoice - Could not create directory " + dirName, ex); 188 return "Streaming error - directory"; 189 } 190 String fileName = invoice.getPDFFileName(dirName); 191 File file = new File(fileName); 192 if (!file.exists()) { 194 file = invoice.getPDF (file); 195 if (file != null) 196 { 197 invoice.setDatePrinted (new Timestamp(System.currentTimeMillis())); 198 invoice.save(); 199 } 200 } 201 if (file == null || !file.exists()) 202 { 203 log.warn("streamInvoice - File does not exist - " + file); 204 return "Streaming error - file"; 205 } 206 207 try 209 { 210 response.setContentType("application/pdf"); 211 int length = 2048; int fileLength = (int)file.length(); 213 response.setBufferSize(length); 214 log.debug("streamInvoice - " + file.getAbsolutePath() + ", length=" + fileLength); 217 long time = System.currentTimeMillis(); 218 FileInputStream in = new FileInputStream (file); 220 ServletOutputStream out = response.getOutputStream (); 221 byte[] buffer = new byte[length]; 222 int totalSize = 0; 223 int count = 0; 224 do 225 { 226 count = in.read(buffer, 0, length); 227 if (count > 0) 228 { 229 totalSize += count; 230 out.write (buffer, 0, count); 231 } 232 } while (count != -1); 233 out.flush(); 234 out.close(); 235 in.close(); 237 time = System.currentTimeMillis() - time; 238 float speed = ((float)totalSize/1024) / ((float)time/1000); 239 log.debug("streamInvoice - " + totalSize + " B - " + time + " ms - " + speed + " kB/sec"); 240 } 241 catch (IOException ex) 242 { 243 log.error("streamInvoice - " + ex); 244 return "Streaming error"; 245 } 246 247 return null; 248 } 250 } | Popular Tags |