Search in sources :

Example 11 with ActionInvocation

use of com.opensymphony.xwork2.ActionInvocation in project bamboobsc by billchen198318.

the class ActionInfoSupportInterceptor method intercept.

@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
    /*
		ActionInvocation ai=(ActionInvocation)ActionContext.getContext().get(ActionContext.ACTION_INVOCATION); 
		String action=ai.getProxy().getActionName(); 
		String namespace=ai.getProxy().getNamespace();
		*/
    HttpServletRequest request = ServletActionContext.getRequest();
    ActionContext context = actionInvocation.getInvocationContext();
    String action = actionInvocation.getProxy().getActionName();
    String namespace = actionInvocation.getProxy().getNamespace();
    String remoteAddr = request.getRemoteAddr();
    String referer = request.getHeader("referer");
    context.getSession().put(Constants.SESS_PAGE_INFO_ACTION_ByInterceptor, action);
    context.getSession().put(Constants.SESS_PAGE_INFO_NAMESPACE_ByInterceptor, namespace);
    context.getSession().put(Constants.SESS_PAGE_INFO_RemoteAddr_ByInterceptor, remoteAddr);
    context.getSession().put(Constants.SESS_PAGE_INFO_Referer_ByInterceptor, referer);
    return actionInvocation.invoke();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext)

Example 12 with ActionInvocation

use of com.opensymphony.xwork2.ActionInvocation in project bamboobsc by billchen198318.

the class JsonOutermostBracketsInterceptor method intercept.

@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
    ActionContext context = actionInvocation.getInvocationContext();
    HttpServletResponse response = (HttpServletResponse) context.get(StrutsStatics.HTTP_RESPONSE);
    response.setCharacterEncoding("utf8");
    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();
    writer.print("[");
    writer.flush();
    String forward = actionInvocation.invoke();
    writer.print("]");
    writer.flush();
    return forward;
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) ActionContext(com.opensymphony.xwork2.ActionContext) PrintWriter(java.io.PrintWriter)

Example 13 with ActionInvocation

use of com.opensymphony.xwork2.ActionInvocation in project bamboobsc by billchen198318.

the class JasperReportsResult method doExecute.

protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
    // Will throw a runtime exception if no "datasource" property. TODO Best place for that is...?
    initializeProperties(invocation);
    LOG.debug("Creating JasperReport for dataSource = {}, format = {}", dataSource, format);
    HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
    HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(ServletActionContext.HTTP_RESPONSE);
    // TODO Set content type to config settings?
    if ("contype".equals(request.getHeader("User-Agent"))) {
        try (OutputStream outputStream = response.getOutputStream()) {
            response.setContentType("application/pdf");
            response.setContentLength(0);
        } catch (IOException e) {
            LOG.error("Error writing report output", e);
            throw new ServletException(e.getMessage(), e);
        }
        return;
    }
    // Construct the data source for the report.
    ValueStack stack = invocation.getStack();
    ValueStackDataSource stackDataSource = null;
    Connection conn = (Connection) stack.findValue(connection);
    if (conn == null)
        stackDataSource = new ValueStackDataSource(stack, dataSource, wrapField);
    if ("https".equalsIgnoreCase(request.getScheme())) {
        // set the the HTTP Header to work around IE SSL weirdness
        response.setHeader("CACHE-CONTROL", "PRIVATE");
        response.setHeader("Cache-Control", "maxage=3600");
        response.setHeader("Pragma", "public");
        response.setHeader("Accept-Ranges", "none");
    }
    // Determine the directory that the report file is in and set the reportDirectory parameter
    // For WW 2.1.7:
    //  ServletContext servletContext = ((ServletConfig) invocation.getInvocationContext().get(ServletActionContext.SERVLET_CONFIG)).getServletContext();
    ServletContext servletContext = (ServletContext) invocation.getInvocationContext().get(ServletActionContext.SERVLET_CONTEXT);
    String systemId = servletContext.getRealPath(finalLocation);
    // TODO 更改 systemId 的位址
    if (Constants.JASPER_REPORTS_RESULT_LOCATION_REPLACE_MODE) {
        systemId = finalLocation;
    }
    Map parameters = new ValueStackShadowMap(stack);
    File directory = new File(systemId.substring(0, systemId.lastIndexOf(File.separator)));
    parameters.put("reportDirectory", directory);
    parameters.put(JRParameter.REPORT_LOCALE, invocation.getInvocationContext().getLocale());
    // put timezone in jasper report parameter
    if (timeZone != null) {
        timeZone = conditionalParse(timeZone, invocation);
        final TimeZone tz = TimeZone.getTimeZone(timeZone);
        if (tz != null) {
            // put the report time zone
            parameters.put(JRParameter.REPORT_TIME_ZONE, tz);
        }
    }
    // Add any report parameters from action to param map.
    Map reportParams = (Map) stack.findValue(reportParameters);
    if (reportParams != null) {
        LOG.debug("Found report parameters; adding to parameters...");
        parameters.putAll(reportParams);
    }
    ByteArrayOutputStream output;
    JasperPrint jasperPrint;
    // Fill the report and produce a print object
    try {
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new File(systemId));
        if (conn == null) {
            jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, stackDataSource);
        } else {
            jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
        }
    } catch (JRException e) {
        LOG.error("Error building report for uri {}", systemId, e);
        throw new ServletException(e.getMessage(), e);
    }
    // Export the print object to the desired output format
    try {
        if (contentDisposition != null || documentName != null) {
            final StringBuffer tmp = new StringBuffer();
            tmp.append((contentDisposition == null) ? "inline" : contentDisposition);
            if (documentName != null) {
                tmp.append("; filename=");
                tmp.append(documentName);
                tmp.append(".");
                tmp.append(format.toLowerCase());
            }
            response.setHeader("Content-disposition", tmp.toString());
        }
        JRExporter exporter;
        if (format.equals(FORMAT_PDF)) {
            response.setContentType("application/pdf");
            exporter = new JRPdfExporter();
        } else if (format.equals(FORMAT_CSV)) {
            response.setContentType("text/csv");
            exporter = new JRCsvExporter();
        } else if (format.equals(FORMAT_HTML)) {
            response.setContentType("text/html");
            // IMAGES_MAPS seems to be only supported as "backward compatible" from JasperReports 1.1.0
            Map imagesMap = new HashMap();
            request.getSession(true).setAttribute("IMAGES_MAP", imagesMap);
            exporter = new JRHtmlExporter();
            exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
            exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imageServletUrl);
            // Needed to support chart images:
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print", jasperPrint);
        } else if (format.equals(FORMAT_XLS)) {
            response.setContentType("application/vnd.ms-excel");
            exporter = new JRXlsExporter();
        } else if (format.equals(FORMAT_XML)) {
            response.setContentType("text/xml");
            exporter = new JRXmlExporter();
        } else if (format.equals(FORMAT_RTF)) {
            response.setContentType("application/rtf");
            exporter = new JRRtfExporter();
        } else {
            throw new ServletException("Unknown report format: " + format);
        }
        Map exportParams = (Map) stack.findValue(exportParameters);
        if (exportParams != null) {
            LOG.debug("Found export parameters; adding to exporter parameters...");
            exporter.getParameters().putAll(exportParams);
        }
        output = exportReportToBytes(jasperPrint, exporter);
    } catch (JRException e) {
        LOG.error("Error producing {} report for uri {}", format, systemId, e);
        throw new ServletException(e.getMessage(), e);
    } finally {
        try {
            conn.close();
        } catch (Exception e) {
            LOG.warn("Could not close db connection properly", e);
        }
    }
    response.setContentLength(output.size());
    // Will throw ServletException on IOException.
    writeReport(response, output);
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletContext(javax.servlet.ServletContext) Connection(java.sql.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) TimeZone(java.util.TimeZone) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 14 with ActionInvocation

use of com.opensymphony.xwork2.ActionInvocation in project dhis2-core by dhis2.

the class XWorkSecurityInterceptor method intercept.

@Override
public String intercept(ActionInvocation invocation) throws Exception {
    ActionConfig actionConfig = invocation.getProxy().getConfig();
    definitionSourceTag.set(requiredAuthoritiesProvider.createSecurityMetadataSource(actionConfig));
    InterceptorStatusToken token = beforeInvocation(actionConfig);
    addActionAccessResolver(invocation);
    Object result = null;
    try {
        result = invocation.invoke();
    } finally {
        result = afterInvocation(token, result);
        definitionSourceTag.remove();
    }
    if (result != null) {
        return result.toString();
    }
    return null;
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) InterceptorStatusToken(org.springframework.security.access.intercept.InterceptorStatusToken)

Example 15 with ActionInvocation

use of com.opensymphony.xwork2.ActionInvocation in project dhis2-core by dhis2.

the class PlainTextErrorResult method execute.

// -------------------------------------------------------------------------
// Result implementation
// -------------------------------------------------------------------------
@Override
public void execute(ActionInvocation invocation) throws Exception {
    HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);
    response.setContentType("text/plain; charset=UTF-8");
    response.setHeader("Content-Disposition", "inline");
    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    ValueStack stack = ActionContext.getContext().getValueStack();
    String finalMessage = parse ? TextParseUtil.translateVariables(message, stack) : message;
    // ---------------------------------------------------------------------
    // Write final message
    // ---------------------------------------------------------------------
    PrintWriter writer = null;
    try {
        writer = response.getWriter();
        writer.print(finalMessage);
        writer.flush();
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter)

Aggregations

ActionContext (com.opensymphony.xwork2.ActionContext)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 HashMap (java.util.HashMap)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 ValueStack (com.opensymphony.xwork2.util.ValueStack)3 Map (java.util.Map)3 Action (com.opensymphony.xwork2.Action)2 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)2 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)2 PrintWriter (java.io.PrintWriter)2 ServletActionContext (org.apache.struts2.ServletActionContext)2 GreenStepBaseUsernamePasswordToken (com.netsteadfast.greenstep.sys.GreenStepBaseUsernamePasswordToken)1 AccountVO (com.netsteadfast.greenstep.vo.AccountVO)1 ValidationAware (com.opensymphony.xwork2.ValidationAware)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Method (java.lang.reflect.Method)1 Connection (java.sql.Connection)1