@Before public void onSetUp() { smtpPort = smtpPort + (int) (Math.random() * 100); LocalizedTextUtil.addDefaultResourceBundle(Constants.BUNDLE_KEY); // Initialize ActionContext ConfigurationManager configurationManager = new ConfigurationManager(); configurationManager.addContainerProvider(new XWorkConfigurationProvider()); Configuration config = configurationManager.getConfiguration(); Container container = config.getContainer(); ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack(); stack.getContext().put(ActionContext.CONTAINER, container); ActionContext.setContext(new ActionContext(stack.getContext())); ActionContext.getContext().setSession(new HashMap<String, Object>()); // change the port on the mailSender so it doesn't conflict with an // existing SMTP server on localhost JavaMailSenderImpl mailSender = (JavaMailSenderImpl) applicationContext.getBean("mailSender"); mailSender.setPort(getSmtpPort()); mailSender.setHost("localhost"); // populate the request so getRequest().getSession() doesn't fail in BaseAction.java ServletActionContext.setRequest(new MockHttpServletRequest()); }
/** * Load configurations, including both XML and zero-configuration strategies, and update optional * settings, including whether to reload configurations and resource files. */ public void init() { if (configurationManager == null) { configurationManager = createConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME); } try { init_FileManager(); init_DefaultProperties(); // [1] init_TraditionalXmlConfigurations(); // [2] init_LegacyStrutsProperties(); // [3] init_CustomConfigurationProviders(); // [5] init_FilterInitParameters(); // [6] init_AliasStandardObjects(); // [7] Container container = init_PreloadConfiguration(); container.inject(this); init_CheckWebLogicWorkaround(container); if (!dispatcherListeners.isEmpty()) { for (DispatcherListener l : dispatcherListeners) { l.dispatcherInitialized(this); } } } catch (Exception ex) { if (LOG.isErrorEnabled()) LOG.error("Dispatcher initialization failed", ex); throw new StrutsException(ex); } }
@Inject public ClasspathPackageProvider(Container container) { this.actionConfigBuilder = container.getInstance( ActionConfigBuilder.class, container.getInstance( String.class, ConventionConstants.CONVENTION_ACTION_CONFIG_BUILDER)); }
private Container init_PreloadConfiguration() { Configuration config = configurationManager.getConfiguration(); Container container = config.getContainer(); boolean reloadi18n = Boolean.valueOf(container.getInstance(String.class, StrutsConstants.STRUTS_I18N_RELOAD)); LocalizedTextUtil.setReloadBundles(reloadi18n); return container; }
/** * Constructs the SimpleResultMapBuilder using the given result location. * * @param servletContext The ServletContext for finding the resources of the web application. * @param container The Xwork container * @param relativeResultTypes The list of result types that can have locations that are relative * and the result location (which is the resultPath plus the namespace) prepended to them. */ @Inject public StuResultMapBuilder( ServletContext servletContext, Container container, @Inject("struts.convention.relative.result.types") String relativeResultTypes) { this.servletContext = servletContext; this.relativeResultTypes = new HashSet<String>(Arrays.asList(relativeResultTypes.split("\\s*[,]\\s*"))); this.conventionsService = container.getInstance( ConventionsService.class, container.getInstance( String.class, ConventionConstants.CONVENTION_CONVENTIONS_SERVICE)); }
private void init_CheckConfigurationReloading(Container container) { FileManager.setReloadingConfigs( "true" .equals( container.getInstance( String.class, StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD))); }
private TextProvider getTextProvider(Object action) { TextProviderFactory tpf = new TextProviderFactory(); if (container != null) { container.inject(tpf); } LocaleProvider localeProvider = getLocaleProvider(action); return tpf.createInstance(action.getClass(), localeProvider); }
private LocaleProvider getLocaleProvider(Object action) { LocaleProvider localeProvider; if (action instanceof LocaleProvider) { localeProvider = (LocaleProvider) action; } else { localeProvider = container.getInstance(LocaleProvider.class); } return localeProvider; }
private TextProvider getTextProvider() { if (textProvider == null) { TextProviderFactory tpf = new TextProviderFactory(); if (container != null) { container.inject(tpf); } textProvider = tpf.createInstance(getClass(), this); } return textProvider; }
@Before public void onSetUp() { smtpPort = (new Random().nextInt(9999 - 1000) + 1000); log.debug("SMTP Port set to: " + smtpPort); LocalizedTextUtil.addDefaultResourceBundle(Constants.BUNDLE_KEY); // Initialize ActionContext ConfigurationManager configurationManager = new ConfigurationManager(); configurationManager.addContainerProvider(new XWorkConfigurationProvider()); Configuration config = configurationManager.getConfiguration(); Container container = config.getContainer(); ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack(); stack.getContext().put(ActionContext.CONTAINER, container); ActionContext.setContext(new ActionContext(stack.getContext())); ActionContext.getContext().setSession(new HashMap<String, Object>()); // populate the request so getRequest().getProject() doesn't fail in BaseAction.java ServletActionContext.setRequest(new MockHttpServletRequest()); }
private void init_CheckWebLogicWorkaround(Container container) { // test whether param-access workaround needs to be enabled if (servletContext != null && servletContext.getServerInfo() != null && servletContext.getServerInfo().indexOf("WebLogic") >= 0) { LOG.info("WebLogic server detected. Enabling Struts parameter access work-around."); paramsWorkaroundEnabled = true; } else { paramsWorkaroundEnabled = "true" .equals( container.getInstance( String.class, StrutsConstants.STRUTS_DISPATCHER_PARAMETERSWORKAROUND)); } }
@Override protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { if (LOG.isDebugEnabled()) { LOG.debug("In doExecute. finalLocation: " + finalLocation + ", renderer: " + renderer); } OutputStream os = null; try { final ActionContext actionContext = invocation.getInvocationContext(); final HttpServletRequest request = (HttpServletRequest) actionContext.get(HTTP_REQUEST); final HttpServletResponse response = (HttpServletResponse) actionContext.get(HTTP_RESPONSE); final SimpleServletResponseWrapper responseWrapper = new SimpleServletResponseWrapper(response); final ServletContext servletContext = (ServletContext) actionContext.get(SERVLET_CONTEXT); ViewRenderer viewRenderer; if (renderer == null) { viewRenderer = container.getInstance(ViewRenderer.class); } else { viewRenderer = container.getInstance(ViewRenderer.class, renderer); } if (viewRenderer == null) { final String err = "Cannot get an instance of ViewRenderer with the name '" + renderer + "'."; LOG.error(err); throw new AssertionError(err); } // render view viewRenderer.render( finalLocation, request, responseWrapper, servletContext, actionContext.getLocale(), invocation.getStack(), invocation.getAction()); // Set the content type response.setContentType(PDF_MIME_TYPE); // Set the content-disposition if (contentDisposition != null) { response.addHeader("Content-Disposition", conditionalParse(contentDisposition, invocation)); } // Set the cache control headers if necessary if (!allowCaching) { response.addHeader("Pragma", "no-cache"); response.addHeader("Cache-Control", "no-cache"); } if (LOG.isTraceEnabled()) { LOG.trace("Content before parsing:\n" + responseWrapper.toString()); } // parse response wrapper final Document document = parseContent(responseWrapper.toString()); final Element head = document.head(); // add CSS from cssPathsSet parameter if (cssPathsSet != null && !cssPathsSet.isEmpty()) { for (String css : cssPathsSet) { // remove leading slash if (css.startsWith("\\")) { css = css.substring(1); } head.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + css + "\" />"); } } // add style for font family that supports unicode head.append(FONT_STYLE_TAG); final String content = document.html(); if (LOG.isTraceEnabled()) { LOG.trace("Content after parsing:\n" + content); } // put pdf stream into response createPdfStream(content, findBaseUrl(request), response.getOutputStream()); } finally { if (os != null) { os.close(); } } }