Search in sources :

Example 21 with WebUser

use of org.compiere.util.WebUser in project adempiere by adempiere.

the class InvoiceServlet method streamInvoice.

//	doPost
/**
	 * 	Stream invoice
	 * 	@param request request
	 * 	@param response response
	 * 	@return "" or error message
	 */
private String streamInvoice(HttpServletRequest request, HttpServletResponse response) {
    //	if not created size is 1015
    int MIN_SIZE = 2000;
    //	Get Invoice ID
    int C_Invoice_ID = WebUtil.getParameterAsInt(request, "Invoice_ID");
    if (C_Invoice_ID == 0) {
        log.fine("No ID)");
        return "No Invoice ID";
    }
    //	Get Invoice
    Properties ctx = JSPEnv.getCtx(request);
    MInvoice invoice = new MInvoice(ctx, C_Invoice_ID, null);
    if (invoice.getC_Invoice_ID() != C_Invoice_ID) {
        log.fine("Invoice not found - ID=" + C_Invoice_ID);
        return "Invoice not found";
    }
    //	Get WebUser & Compare with invoice
    HttpSession session = request.getSession(true);
    WebUser wu = (WebUser) session.getAttribute(WebUser.NAME);
    if (wu.getC_BPartner_ID() != invoice.getC_BPartner_ID()) {
        log.warning("Invoice from BPartner - C_Invoice_ID=" + C_Invoice_ID + " - BP_Invoice=" + invoice.getC_BPartner_ID() + " = BP_Web=" + wu.getC_BPartner_ID());
        return "Your invoice not found";
    }
    //	Check Directory
    String dirName = ctx.getProperty("documentDir", ".");
    try {
        File dir = new File(dirName);
        if (!dir.exists())
            dir.mkdir();
    } catch (Exception ex) {
        log.log(Level.SEVERE, "Could not create directory " + dirName, ex);
        return "Streaming error - directory";
    }
    //	Check if Invoice already created
    String fileName = invoice.getPDFFileName(dirName);
    File file = new File(fileName);
    if (file.exists() && file.isFile() && file.length() > MIN_SIZE)
        log.info("Existing: " + file + " - " + new Timestamp(file.lastModified()));
    else {
        log.info("New: " + fileName);
        file = invoice.createPDF(file);
        if (file != null) {
            invoice.setDatePrinted(new Timestamp(System.currentTimeMillis()));
            invoice.saveEx();
        }
    }
    //	Issue Error
    if (file == null || !file.exists() || file.length() < MIN_SIZE) {
        log.warning("File does not exist - " + file);
        return "Streaming error - file";
    }
    //	Send PDF
    try {
        //	2k Buffer
        int bufferSize = 2048;
        int fileLength = (int) file.length();
        //
        response.setContentType("application/pdf");
        response.setBufferSize(bufferSize);
        response.setContentLength(fileLength);
        //
        log.fine(file.getAbsolutePath() + ", length=" + fileLength);
        //	timer start
        long time = System.currentTimeMillis();
        //
        FileInputStream in = new FileInputStream(file);
        ServletOutputStream out = response.getOutputStream();
        byte[] buffer = new byte[bufferSize];
        double totalSize = 0;
        int count = 0;
        do {
            count = in.read(buffer, 0, bufferSize);
            if (count > 0) {
                totalSize += count;
                out.write(buffer, 0, count);
            }
        } while (count != -1);
        out.flush();
        out.close();
        //
        in.close();
        time = System.currentTimeMillis() - time;
        double speed = (totalSize / 1024) / ((double) time / 1000);
        log.fine("Length=" + totalSize + " - " + time + " ms - " + speed + " kB/sec");
    } catch (IOException ex) {
        log.log(Level.SEVERE, ex.toString());
        return "Streaming error";
    }
    return null;
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) HttpSession(javax.servlet.http.HttpSession) MInvoice(org.compiere.model.MInvoice) IOException(java.io.IOException) Properties(java.util.Properties) Timestamp(java.sql.Timestamp) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) WebUser(org.compiere.util.WebUser) File(java.io.File)

Example 22 with WebUser

use of org.compiere.util.WebUser in project adempiere by adempiere.

the class CheckOutServlet method doGet.

//  destroy
/**
	 *  Process the HTTP Get request.
	 * 	(logout, deleteCookie)
	 *  Sends Web Request Page
	 *
	 *  @param request request
	 *  @param response response
	 *  @throws ServletException
	 *  @throws IOException
	 */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.info("Get from " + request.getRemoteHost() + " - " + request.getRemoteAddr());
    HttpSession session = request.getSession(true);
    session.removeAttribute(WebSessionCtx.HDR_MESSAGE);
    //	Web User/Basket
    WebUser wu = (WebUser) session.getAttribute(WebUser.NAME);
    WebBasket wb = (WebBasket) session.getAttribute(WebBasket.NAME);
    String url = "/login.jsp";
    //	Nothing in basket
    if (wb == null || wb.getLineCount() == 0)
        url = "/basket.jsp";
    else {
        //	indicate checkout
        session.setAttribute(ATTR_CHECKOUT, "Y");
        if (wu != null && wu.isLoggedIn())
            url = "/addressInfo.jsp";
    }
    //	if (request.isSecure())
    //	{
    log.info("Forward to " + url);
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
    dispatcher.forward(request, response);
//	}
//	else
//	Switch to secure
//	{
//		url = "https://" + request.getServerName() + request.getContextPath() + "/" + url;
//		log.info ("doGet - Secure Forward to " + url);
//		WUtil.createForwardPage(response, "Secure Access", url);
//	}
}
Also used : HttpSession(javax.servlet.http.HttpSession) WebUser(org.compiere.util.WebUser) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 23 with WebUser

use of org.compiere.util.WebUser in project adempiere by adempiere.

the class WorkflowServlet method streamAttachment.

//  doGet
/**
	 * 	Stream Attachment
	 * 	@param request request
	 * 	@param response response
	 * 	@return "" or error message
	 */
private String streamAttachment(HttpServletRequest request, HttpServletResponse response) {
    //	Get Activity ID
    int AD_WF_Activity_ID = WebUtil.getParameterAsInt(request, P_WF_Activity_ID);
    if (AD_WF_Activity_ID == 0) {
        log.fine("streamAttachment - no AD_WF_Activity_ID)");
        return "No Activity ID";
    }
    int attachmentIndex = WebUtil.getParameterAsInt(request, P_ATTACHMENT_INDEX);
    if (attachmentIndex == 0) {
        log.fine("streamAttachment - no index)");
        return "No Request Attachment index";
    }
    log.info("streamAttachment - AD_WF_Activity_ID=" + AD_WF_Activity_ID + " / " + attachmentIndex);
    //	Get Note
    Properties ctx = JSPEnv.getCtx(request);
    MWFActivity doc = new MWFActivity(ctx, AD_WF_Activity_ID, null);
    if (doc.get_ID() != AD_WF_Activity_ID) {
        log.fine("streamAttachment - Activity not found - ID=" + AD_WF_Activity_ID);
        return "Activity not found";
    }
    MAttachment attachment = doc.getAttachment(false);
    if (attachment == null) {
        log.fine("streamAttachment - No Attachment for AD_WF_Activity_ID=" + AD_WF_Activity_ID);
        return "Notice Attachment not found";
    }
    //	Get WebUser & Compare with invoice
    HttpSession session = request.getSession(true);
    WebUser wu = (WebUser) session.getAttribute(WebUser.NAME);
    if (wu.getAD_User_ID() != doc.getAD_User_ID()) {
        log.warning("streamAttachment - AD_WF_Activity_ID=" + AD_WF_Activity_ID + " - User_Activity=" + doc.getAD_User_ID() + " = Web_User=" + wu.getAD_User_ID());
        return "Your Activity not found";
    }
    //	Stream it
    return WebUtil.streamAttachment(response, attachment, attachmentIndex);
}
Also used : MWFActivity(org.compiere.wf.MWFActivity) MAttachment(org.compiere.model.MAttachment) HttpSession(javax.servlet.http.HttpSession) WebUser(org.compiere.util.WebUser) Properties(java.util.Properties)

Example 24 with WebUser

use of org.compiere.util.WebUser in project adempiere by adempiere.

the class RequestServlet method streamAttachment.

//  doGet
/**
	 * 	Stream Attachment
	 * 	@param request request
	 * 	@param response response
	 * 	@return null or error message
	 */
private String streamAttachment(HttpServletRequest request, HttpServletResponse response) {
    //	Get Request ID
    int R_Request_ID = WebUtil.getParameterAsInt(request, P_REQUEST_ID);
    if (R_Request_ID == 0) {
        log.fine("No R_Request_ID)");
        return "No Request ID";
    }
    int attachmentIndex = WebUtil.getParameterAsInt(request, P_ATTACHMENT_INDEX);
    if (attachmentIndex == 0) {
        log.fine("No index)");
        return "No Request Attachment index";
    }
    log.info("R_Request_ID=" + R_Request_ID + " / " + attachmentIndex);
    //	Get Request
    Properties ctx = JSPEnv.getCtx(request);
    MRequest doc = new MRequest(ctx, R_Request_ID, null);
    if (doc.getR_Request_ID() != R_Request_ID) {
        log.fine("Request not found - R_Request_ID=" + R_Request_ID);
        return "Request not found";
    }
    MAttachment attachment = doc.getAttachment(false);
    if (attachment == null) {
        log.fine("No Attachment for R_Request_ID=" + R_Request_ID);
        return "Request Attachment not found";
    }
    //	Get WebUser & Compare with invoice
    HttpSession session = request.getSession(true);
    WebUser wu = (WebUser) session.getAttribute(WebUser.NAME);
    if (wu.getAD_User_ID() == doc.getAD_User_ID() || wu.getAD_User_ID() == doc.getSalesRep_ID())
        ;
    else {
        log.warning("R_Request_ID=" + R_Request_ID + " Web_User=" + wu.getAD_User_ID() + " <> AD_User_ID=" + doc.getAD_User_ID() + " | SalesRep_ID=" + doc.getSalesRep_ID());
        return "Your Request not found";
    }
    //	Stream it
    return WebUtil.streamAttachment(response, attachment, attachmentIndex);
}
Also used : MRequest(org.compiere.model.MRequest) MAttachment(org.compiere.model.MAttachment) HttpSession(javax.servlet.http.HttpSession) WebUser(org.compiere.util.WebUser) Properties(java.util.Properties)

Example 25 with WebUser

use of org.compiere.util.WebUser in project adempiere by adempiere.

the class RfQServlet method doPost.

//	streamAttachment
/**************************************************************************
	 *  Process the HTTP Post request
	 *
	 *  @param request request
	 *  @param response response
	 *  @throws ServletException
	 *  @throws IOException
	 */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.info("From " + request.getRemoteHost() + " - " + request.getRemoteAddr());
    //	Log.setTraceLevel(9);
    //	WebEnv.dump(request);
    //	WebEnv.dump(request.getSession());
    //  Get Session attributes
    HttpSession session = request.getSession(true);
    session.removeAttribute(WebSessionCtx.HDR_MESSAGE);
    //
    Properties ctx = JSPEnv.getCtx(request);
    WebUser wu = (WebUser) session.getAttribute(WebUser.NAME);
    if (wu == null) {
        log.warning("doPost - no web user");
        if (!response.isCommitted())
            //	entry
            response.sendRedirect("loginServlet?ForwardTo=note.jsp");
        return;
    }
    int C_RfQResponse_ID = WebUtil.getParameterAsInt(request, P_RfQResponse_ID);
    int C_RfQ_ID = WebUtil.getParameterAsInt(request, "C_RfQ_ID");
    MRfQResponse rfqResponse = new MRfQResponse(ctx, C_RfQResponse_ID, null);
    if (C_RfQResponse_ID == 0 || rfqResponse == null || rfqResponse.get_ID() != C_RfQResponse_ID) {
        WebUtil.createForwardPage(response, "RfQ Response not found", "rfqs.jsp", 5);
        return;
    }
    if (wu.getC_BPartner_ID() != rfqResponse.getC_BPartner_ID()) {
        WebUtil.createForwardPage(response, "Your RfQ Response not found", "rfqs.jsp", 5);
        return;
    }
    //	Update Data
    String msg = updateResponse(request, rfqResponse);
    session.setAttribute(WebSessionCtx.HDR_MESSAGE, msg);
    String url = "/rfqDetails.jsp?C_RfQ_ID=" + C_RfQ_ID;
    //
    log.info("doGet - Forward to " + url);
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
    dispatcher.forward(request, response);
}
Also used : HttpSession(javax.servlet.http.HttpSession) MRfQResponse(org.compiere.model.MRfQResponse) WebUser(org.compiere.util.WebUser) Properties(java.util.Properties) RequestDispatcher(javax.servlet.RequestDispatcher)

Aggregations

WebUser (org.compiere.util.WebUser)25 HttpSession (javax.servlet.http.HttpSession)23 Properties (java.util.Properties)22 RequestDispatcher (javax.servlet.RequestDispatcher)9 IOException (java.io.IOException)4 ServletException (javax.servlet.ServletException)4 MAttachment (org.compiere.model.MAttachment)3 MPayment (org.compiere.model.MPayment)3 Timestamp (java.sql.Timestamp)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 JspWriter (javax.servlet.jsp.JspWriter)2 MNote (org.compiere.model.MNote)2 MRequest (org.compiere.model.MRequest)2 HtmlCode (org.compiere.util.HtmlCode)2 WebSessionCtx (org.compiere.util.WebSessionCtx)2 MWFActivity (org.compiere.wf.MWFActivity)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 BigDecimal (java.math.BigDecimal)1