public static Map<String, String> createParameterMap(String[] params) { Map<String, String> map = new HashMap<>(); int subtract = params.length % 2; if (subtract != 0) { throw new ConfigurationException( "'params' is a string array " + "and they must be in a key value pair configuration. It looks like you" + " have specified an odd number of parameters and there should only be an even number." + " (e.g. params = {\"key\", \"value\"})"); } for (int i = 0; i < params.length; i = i + 2) { String key = params[i]; String value = params[i + 1]; map.put(key, value); } return map; } }
private void loadDocuments(String configFileName) { try { loadedFileUrls.clear(); documents = loadConfigurationFiles(configFileName, null); } catch (ConfigurationException e) { throw e; } catch (Exception e) { throw new ConfigurationException("Error loading configuration file " + configFileName, e); } }
private Set<Class<?>> parseExcludedClasses(String commaDelimitedClasses) { Set<String> classNames = TextParseUtil.commaDelimitedStringToSet(commaDelimitedClasses); Set<Class<?>> classes = new HashSet<Class<?>>(); for (String className : classNames) { try { classes.add(Class.forName(className)); } catch (ClassNotFoundException e) { throw new ConfigurationException("Cannot load excluded class: " + className, e); } } return classes; }
public StrutsWildcardServletApplicationContext(ServletContext context) { super(context); Set<URL> urls = new HashSet<>(); for (Object path : context.getResourcePaths("/")) { try { String realPath = context.getRealPath(String.valueOf(path)); if (realPath != null) { URL url = new File(realPath).toURI().toURL(); urls.add(url); } } catch (MalformedURLException e) { throw new ConfigurationException(e); } } try { Enumeration<URL> resources = getClass().getClassLoader().getResources("/"); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); urls.add(resource); } } catch (IOException e) { throw new ConfigurationException(e); } finder = new ResourceFinder(urls.toArray(new URL[urls.size()])); }
/** * Depending how Tiles definition was defined, request can an instance of JspRequest (for JSPs) * or a ServletRequest (FreeMarker) */ protected ActionContext readActionContext(Request request) { LOG.debug("Obtaining HttpServletRequest based on [{}]", request.getClass().getName()); HttpServletRequest httpRequest = ServletUtil.getServletRequest(request).getRequest(); ActionContext ctx = ServletActionContext.getActionContext(httpRequest); if (ctx == null) { LOG.error("Cannot obtain HttpServletRequest from [{}]", request.getClass().getName()); throw new ConfigurationException("There is no ActionContext for current request!"); } return ctx; }
String determineType(String location, PackageConfig packageConfig, Map<String, ResultTypeConfig> resultsByExtension) { int indexOfDot = location.lastIndexOf("."); if (indexOfDot > 0) { String extension = location.substring(indexOfDot + 1); ResultTypeConfig resultTypeConfig = resultsByExtension.get(extension); if (resultTypeConfig != null) { return resultTypeConfig.getName(); } else throw new ConfigurationException("Unable to find a result type for extension [" + extension + "] " + "in location attribute [" + location + "]."); } else { return packageConfig.getFullDefaultResultType(); } } }
@Inject public void setContainer(Container container) { this.container = container; try { build(); } catch (Exception e) { throw new ConfigurationException(e); } }
public void parseValidatorDefinitions(Map<String, String> validators, InputStream is, String resourceName) { InputSource in = new InputSource(is); in.setSystemId(resourceName); Map<String, String> dtdMappings = new HashMap<String, String>(); dtdMappings.put("-//Apache Struts//XWork Validator Config 1.0//EN", "xwork-validator-config-1.0.dtd"); dtdMappings.put("-//Apache Struts//XWork Validator Definition 1.0//EN", "xwork-validator-definition-1.0.dtd"); Document doc = DomHelper.parse(in, dtdMappings); if (doc != null) { NodeList nodes = doc.getElementsByTagName("validator"); for (int i = 0; i < nodes.getLength(); i++) { Element validatorElement = (Element) nodes.item(i); String name = validatorElement.getAttribute("name"); String className = validatorElement.getAttribute("class"); try { // catch any problems here objectFactory.buildValidator(className, new HashMap<String, Object>(), ActionContext.getContext().getContextMap()); validators.put(name, className); } catch (Exception e) { throw new ConfigurationException("Unable to load validator class " + className, e, validatorElement); } } } }
/** * Builds a list of actions from an @Actions annotation, and check that they are not all empty * @param actionsAnnotation Actions annotation * @return a list of Actions */ protected List<Action> checkActionsAnnotation(Actions actionsAnnotation) { Action[] actionArray = actionsAnnotation.value(); boolean valuelessSeen = false; List<Action> actions = new ArrayList<>(); for (Action ann : actionArray) { if (ann.value().equals(Action.DEFAULT_VALUE) && !valuelessSeen) { valuelessSeen = true; } else if (ann.value().equals(Action.DEFAULT_VALUE)) { throw new ConfigurationException("You may only add a single Action annotation that has no value parameter."); } actions.add(ann); } return actions; }
@Override public Locale resolveLocale(Request request) { HttpServletRequest httpRequest = ServletUtil.getServletRequest(request).getRequest(); ActionContext ctx = ServletActionContext.getActionContext(httpRequest); if (ctx == null) { LOG.error("Cannot obtain HttpServletRequest from [{}]", request.getClass().getName()); throw new ConfigurationException("There is no ActionContext for current request!"); } LocaleProviderFactory localeProviderFactory = ctx.getInstance(LocaleProviderFactory.class); return localeProviderFactory.createLocaleProvider().getLocale(); }
/** * {@inheritDoc} */ public String determineResultPath(Class<?> actionClass) { String localResultPath = resultPath; ResultPath resultPathAnnotation = AnnotationUtils.findAnnotation(actionClass, ResultPath.class); if (resultPathAnnotation != null) { if (resultPathAnnotation.value().equals("") && resultPathAnnotation.property().equals("")) { throw new ConfigurationException("The ResultPath annotation must have either" + " a value or property specified."); } String property = resultPathAnnotation.property(); if (property.equals("")) { localResultPath = resultPathAnnotation.value(); } else { try { ResourceBundle strutsBundle = ResourceBundle.getBundle("struts"); localResultPath = strutsBundle.getString(property); } catch (Exception e) { throw new ConfigurationException("The action class [" + actionClass + "] defines" + " a @ResultPath annotation and a property definition however the" + " struts.properties could not be found in the classpath using ResourceBundle" + " OR the bundle exists but the property [" + property + "] is not defined" + " in the file.", e); } } } return localResultPath; }
/** * Save the result to be used later. * @param actionConfig current ActionConfig * @param methodResult the result of the action. * @return the result code to process. * * @throws ConfigurationException If it is an incorrect result. */ @Override protected String saveResult(ActionConfig actionConfig, Object methodResult) { if (methodResult instanceof Result) { explicitResult = (Result) methodResult; // Wire the result automatically container.inject(explicitResult); } else if (methodResult instanceof HttpHeaders) { httpHeaders = (HttpHeaders) methodResult; resultCode = httpHeaders.getResultCode(); } else if (methodResult instanceof String) { resultCode = (String) methodResult; } else if (methodResult != null) { throw new ConfigurationException("The result type " + methodResult.getClass() + " is not allowed. Use the type String, HttpHeaders or Result."); } return resultCode; }
@Override public Object evaluate(String expression, Request request) { Object result = expression; HttpServletRequest httpRequest = ServletUtil.getServletRequest(request).getRequest(); ActionContext ctx = ServletActionContext.getActionContext(httpRequest); if (ctx == null) { LOG.error("Cannot obtain HttpServletRequest from [{}]", request.getClass().getName()); throw new ConfigurationException("There is no ActionContext for current request!"); } TextProviderFactory tpf = ctx.getContainer().getInstance(TextProviderFactory.class); TextProvider textProvider = tpf.createInstance(ctx.getActionInvocation().getAction().getClass()); if (textProvider != null) { LOG.debug("Trying find text [{}] using TextProvider {}", expression, textProvider); result = textProvider.getText(expression); } return result; }
@Override public Object evaluate(String expression, Request request) { try { HttpServletRequest httpRequest = ServletUtil.getServletRequest(request).getRequest(); ActionContext ctx = ServletActionContext.getActionContext(httpRequest); if (ctx == null) { LOG.error("Cannot obtain HttpServletRequest from [{}]", request.getClass().getName()); throw new ConfigurationException("There is no ActionContext for current request!"); } OgnlUtil ognlUtil = ctx.getContainer().getInstance(OgnlUtil.class); LOG.debug("Trying evaluate expression [{}] using OgnlUtil's getValue", expression); Object result = ognlUtil.getValue(expression, ctx.getContextMap(), ctx.getValueStack().getRoot()); LOG.debug("Final result of evaluating expression [{}] is: {}", expression, result); return result; } catch (OgnlException e) { throw new EvaluationException(e); } }
public ResultInfo(String name, Result result, PackageConfig packageConfig, String resultPath, Class<?> actionClass, Map<String, ResultTypeConfig> resultsByExtension) { this.name = name; if (StringUtils.isNotBlank(result.type())) { this.type = result.type(); } else if (StringUtils.isNotBlank(result.location())) { this.type = determineType(result.location(), packageConfig, resultsByExtension); } else { throw new ConfigurationException("The action class [" + actionClass + "] contains a " + "result annotation that has no type parameter and no location parameter. One of " + "these must be defined."); } // See if we can handle relative locations or not if (StringUtils.isNotBlank(result.location())) { if (relativeResultTypes.contains(this.type) && !result.location().startsWith("/")) { location = resultPath + result.location(); } else { location = result.location(); } } else { this.location = null; } }
public void addPackageConfig(String name, PackageConfig packageContext) { PackageConfig check = packageContexts.get(name); if (check != null) { if (check.getLocation() != null && packageContext.getLocation() != null && check.getLocation().equals(packageContext.getLocation())) { if (LOG.isDebugEnabled()) { LOG.debug("The package name '" + name + "' is already been loaded by the same location and could be removed: " + packageContext.getLocation()); } } else { throw new ConfigurationException("The package name '" + name + "' at location "+packageContext.getLocation() + " is already been used by another package at location " + check.getLocation(), packageContext); } } packageContexts.put(name, packageContext); }
/** * Uses getResult to get the final Result and executes it * * @throws ConfigurationException If not result can be found with the returned code */ private void executeResult() throws Exception { result = createResult(); String timerKey = "executeResult: " + getResultCode(); try { UtilTimerStack.push(timerKey); if (result != null) { result.execute(this); } else if (resultCode != null && !Action.NONE.equals(resultCode)) { throw new ConfigurationException("No result defined for action " + getAction().getClass().getName() + " and result " + getResultCode(), proxy.getConfig()); } else { if (LOG.isDebugEnabled()) { LOG.debug("No result returned for action " + getAction().getClass().getName() + " at " + proxy.getConfig().getLocation()); } } } finally { UtilTimerStack.pop(timerKey); } }
protected void prepare() { String profileKey = "create DefaultActionProxy: "; try { UtilTimerStack.push(profileKey); config = configuration.getRuntimeConfiguration().getActionConfig(namespace, actionName); if (config == null && unknownHandlerManager.hasUnknownHandlers()) { config = unknownHandlerManager.handleUnknownAction(namespace, actionName); } if (config == null) { throw new ConfigurationException(getErrorMessage()); } resolveMethod(); if (!config.isAllowedMethod(method)) { throw new ConfigurationException("Invalid method: " + method + " for action " + actionName); } invocation.init(this); } finally { UtilTimerStack.pop(profileKey); } }
/** * Get the current XWork configuration object. By default an instance of DefaultConfiguration will be returned * * @see com.opensymphony.xwork2.config.impl.DefaultConfiguration */ public synchronized Configuration getConfiguration() { if (configuration == null) { setConfiguration(createConfiguration(defaultFrameworkBeanName)); try { configuration.reloadContainer(getContainerProviders()); } catch (ConfigurationException e) { setConfiguration(null); throw new ConfigurationException("Unable to load configuration.", e); } } else { conditionalReload(); } return configuration; }
public void renderTemplate(TemplateRenderingContext templateContext) throws Exception { Template t = templateContext.getTemplate(); Theme theme = themes.get(t.getTheme()); if (theme == null) { // Theme not supported, so do what struts would have done if we were not here. LOG.debug("Theme not found [{}] trying default template engine using template type [{}]", t.getTheme(), defaultTemplateType); final TemplateEngine engine = templateEngineManager.getTemplateEngine(templateContext.getTemplate(), defaultTemplateType); if (engine == null) { // May be the default template has changed? throw new ConfigurationException("Unable to find a TemplateEngine for template type '" + defaultTemplateType + "' whilst trying to render template " + templateContext.getTemplate()); } else { try { // Retry render engine.renderTemplate(templateContext); } catch (Exception e) { // Give up and throw a new StrutsException(e); throw new StrutsException("Cannot render tag [" + t.getName() + "] because theme [" + t.getTheme() + "] was not found.", e); } } } else { // Render our template theme.renderTag(t.getName(), templateContext); } }