List of usage examples for org.apache.commons.lang StringUtils substringBetween
public static String substringBetween(String str, String open, String close)
Gets the String that is nested in between two Strings.
From source file:org.jahia.services.render.URLResolver.java
protected boolean resolveUrlMapping(String serverName, HttpServletRequest request) { boolean mappingResolved = false; try {//from w w w .j a v a 2 s . c om siteKeyByServerName = ServicesRegistry.getInstance().getJahiaSitesService() .getSitenameByServerName(serverName); } catch (JahiaException e) { logger.warn("Error finding site via servername: " + serverName, e); } if (getSiteKey() == null) { String siteKeyInPath = StringUtils.substringBetween(getPath(), "/sites/", "/"); if (!StringUtils.isEmpty(siteKeyInPath)) { setSiteKey(siteKeyInPath); } else if (!Url.isLocalhost(serverName)) { if (siteKeyByServerName != null) { setSiteKey(siteKeyByServerName); } } } if (isServletAllowingUrlMapping() && !Url.isLocalhost(serverName)) { String tempPath = null; try { String tempWorkspace = verifyWorkspace(StringUtils.substringBefore(getPath(), "/")); tempPath = StringUtils.substringAfter(getPath(), "/"); VanityUrl resolvedVanityUrl = null; if (logger.isDebugEnabled()) { logger.debug("Trying to resolve vanity url for tempPath = " + tempPath); } boolean doNotMatchesCrossSitesPattern = !crossSitesURLPattern.matcher(tempPath).matches(); if (!StringUtils.isEmpty(getSiteKey()) && doNotMatchesCrossSitesPattern) { List<VanityUrl> vanityUrls = getVanityUrlService().findExistingVanityUrls("/" + tempPath, getSiteKey(), tempWorkspace); for (VanityUrl vanityUrl : vanityUrls) { if (vanityUrl.isActive()) { resolvedVanityUrl = vanityUrl; break; } } } else if (doNotMatchesCrossSitesPattern) { List<VanityUrl> vanityUrls = getVanityUrlService().findExistingVanityUrls("/" + tempPath, StringUtils.EMPTY, tempWorkspace); for (VanityUrl vanityUrl : vanityUrls) { if (vanityUrl.isActive() && (StringUtils.isEmpty(getSiteKey()) || getSiteKey().equals(vanityUrl.getSite()))) { resolvedVanityUrl = vanityUrl; break; } } } if (resolvedVanityUrl != null) { workspace = tempWorkspace; locale = StringUtils.isEmpty(resolvedVanityUrl.getLanguage()) ? DEFAULT_LOCALE : LanguageCodeConverters.languageCodeToLocale(resolvedVanityUrl.getLanguage()); path = StringUtils.substringBefore(resolvedVanityUrl.getPath(), VANITY_URL_NODE_PATH_SEGMENT) + ".html"; setVanityUrl(resolvedVanityUrl.getUrl()); if (SettingsBean.getInstance().isPermanentMoveForVanityURL() && !resolvedVanityUrl.isDefaultMapping()) { VanityUrl defaultVanityUrl = getVanityUrlService() .getVanityUrlForWorkspaceAndLocale(getNode(), workspace, locale, siteKey); if (defaultVanityUrl != null && defaultVanityUrl.isActive() && !resolvedVanityUrl.equals(defaultVanityUrl)) { redirect(request, defaultVanityUrl); } } mappingResolved = true; } } catch (RepositoryException e) { logger.warn("Error when trying to resolve URL mapping: " + tempPath, e); } } return mappingResolved; }
From source file:org.jahia.services.render.URLResolver.java
/** * Creates a node from the specified path. * <p/>//from www . j a va 2 s. c om * The path should looks like : [nodepath][.templatename].[templatetype] or [nodepath].[templatetype] * * @param workspace * The workspace where to get the node * @param locale * current locale * @param path * The path of the node, in the specified workspace * @return The node, if found * @throws PathNotFoundException * if the resource cannot be resolved * @throws RepositoryException */ protected JCRNodeWrapper resolveNode(final String workspace, final Locale locale, final String path) throws RepositoryException { if (logger.isDebugEnabled()) { logger.debug("Resolving node for workspace '" + workspace + "' locale '" + locale + "' and path '" + path + "'"); } final String cacheKey = getCacheKey(workspace, locale, path); if (resolvedNodes.containsKey(cacheKey)) { return resolvedNodes.get(cacheKey); } JCRNodeWrapper node = null; Element element = nodePathCache.get(cacheKey); String nodePath = null; if (element != null) { nodePath = (String) element.getObjectValue(); } element = siteInfoCache.get(cacheKey); if (element != null) { siteInfo = (SiteInfo) element.getObjectValue(); } if (nodePath == null || siteInfo == null) { nodePath = JCRTemplate.getInstance().doExecuteWithSystemSessionAsUser(null, workspace, locale, new JCRCallback<String>() { public String doInJCR(JCRSessionWrapper session) throws RepositoryException { String nodePath = JCRContentUtils.escapeNodePath( path.endsWith("/*") ? path.substring(0, path.lastIndexOf("/*")) : path); String siteName = StringUtils.substringBetween(nodePath, "/sites/", "/"); if (siteName != null && session.itemExists("/sites/" + siteName)) { siteInfo = new SiteInfo((JCRSiteNode) session.getNode("/sites/" + siteName)); if (siteInfo.isMixLanguagesActive() && siteInfo.getDefaultLanguage() != null) { session.setFallbackLocale(LanguageCodeConverters .getLocaleFromCode(siteInfo.getDefaultLanguage())); } } if (logger.isDebugEnabled()) { logger.debug(cacheKey + " has not been found in the cache, still looking for node " + nodePath); } JCRNodeWrapper node = null; while (true) { try { node = session.getNode(nodePath); break; } catch (PathNotFoundException ex) { if (nodePath.lastIndexOf("/") < nodePath.lastIndexOf(".")) { nodePath = nodePath.substring(0, nodePath.lastIndexOf(".")); } else { throw new PathNotFoundException("'" + nodePath + "'not found"); } } } nodePathCache.put(new Element(cacheKey, nodePath)); // the next condition is false e.g. when nodePath is "/" and session's locale is not in systemsite's locales JCRSiteNode resolveSite = node.getResolveSite(); if (resolveSite != null) { siteInfo = new SiteInfo(resolveSite); siteInfoCache.put(new Element(cacheKey, siteInfo)); } return nodePath; } }); } if (siteInfo == null) { siteInfoCache.remove(cacheKey); throw new RepositoryException( "could not resolve site for " + path + " in workspace " + workspace + " in language " + locale); } if (siteInfo.isMixLanguagesActive() && siteInfo.getDefaultLanguage() != null) { JCRSessionFactory.getInstance() .setFallbackLocale(LanguageCodeConverters.getLocaleFromCode(siteInfo.getDefaultLanguage())); } JCRSessionWrapper userSession = JCRSessionFactory.getInstance().getCurrentUserSession(workspace, locale); if (userSession.getVersionDate() == null && versionDate != null) userSession.setVersionDate(versionDate); if (userSession.getVersionLabel() == null && versionLabel != null) userSession.setVersionLabel(versionLabel); try { node = userSession.getNode(nodePath); } catch (PathNotFoundException e) { throw new AccessDeniedException(path); } resolvedNodes.put(cacheKey, node); return node; }
From source file:org.jahia.services.seo.urlrewrite.ServerNameToSiteMapper.java
public void getLinkLocale(HttpServletRequest request, String ctx, String path) { String linkLocale = request.getAttribute("currentLocale").toString(); if (path.startsWith("/sites/")) { String siteKey = StringUtils.substringBetween(path, "/sites/", "/"); try {// w ww .j a va2s . c o m JahiaSite site = JahiaSitesService.getInstance().getSiteByKey(siteKey); if (site != null && !site.getLanguages().contains(linkLocale)) { linkLocale = site.getDefaultLanguage(); } } catch (JahiaException e) { // cannot get site, don't change locale } } request.setAttribute("currentLinkLocale", linkLocale); }
From source file:org.jahia.services.templates.ModuleBuildHelper.java
private void checkMavenExecutable() { if (settingsBean.isDevelopmentMode()) { settingsBean.setMavenExecutableSet(false); String mavenExecutable = this.mavenExecutable; StringBuilder resultOut = new StringBuilder(); try {/*from w w w . ja v a 2s. c o m*/ String[] args = new String[] { "-version" }; int res = 0; if (System.getProperty("os.name").toLowerCase().startsWith("windows") && !mavenExecutable.endsWith(".bat") && !mavenExecutable.endsWith(".cmd")) { // check for Maven 3.3.x try { res = ProcessHelper.execute(mavenExecutable + ".cmd", args, null, null, resultOut, null); if (res == 0) { // we are dealing with Maven 3.3.x mavenExecutable = mavenExecutable + ".cmd"; } } catch (JahiaRuntimeException e) { // assume Maven < 3.3.x mavenExecutable = mavenExecutable + ".bat"; } } if (res > 0 || resultOut.length() == 0) { res = ProcessHelper.execute(mavenExecutable, args, null, null, resultOut, null); } if (res > 0) { toolbarWarningsService.addMessage("warning.maven.missing"); logger.error("Cannot set maven executable to " + mavenExecutable + ", please check your configuration"); return; } String mvnVersionString = StringUtils.substringBefore( StringUtils.substringBetween(resultOut.toString(), "Apache Maven ", "\n"), " "); String[] mvnVersion = StringUtils.split(mvnVersionString, "."); String[] requiredVersion = StringUtils.split(mavenMinRequiredVersion, "."); boolean isValid = true; for (int i = 0; i < mvnVersion.length; i++) { isValid = Integer.parseInt(mvnVersion[i]) >= Integer.parseInt(requiredVersion[i]); if (!isValid || i == requiredVersion.length - 1) { break; } } if (isValid) { this.mavenExecutable = mavenExecutable; settingsBean.setMavenExecutableSet(true); if (new Version(mvnVersionString).compareTo(new Version(mavenWarnIfVersionIsOlderThan)) < 0) { warnOldMavenVersion(mvnVersionString); } } else { toolbarWarningsService.addMessage("warning.maven.wrong.version"); logger.error("Detected Maven Version: " + StringUtils.join(mvnVersion, ".") + " do not match the minimum required version " + mavenMinRequiredVersion); } } catch (Exception e) { toolbarWarningsService.addMessage("warning.maven.missing"); logger.error( "Cannot set maven executable to " + mavenExecutable + ", please check your configuration", e); } if (!settingsBean.isMavenExecutableSet()) { logger.error("Until maven executable is correctly set, the studio will not be available"); } } }
From source file:org.jahia.services.templates.SvnSourceControlManagement.java
@Override public String getURI() throws IOException { ExecutionResult result = executeCommand(executable, new String[] { "info", "--xml" }); String url = StringUtils.substringBetween(result.out, "<url>", "</url>").trim(); return "scm:svn:" + url; }
From source file:org.jahia.services.templates.TemplatePackageDeployer.java
private void resetModuleAttributes(JCRSessionWrapper session, JahiaTemplatesPackage pack) throws RepositoryException { JCRNodeWrapper modules = session.getNode("/modules"); JCRNodeWrapper m = modules.getNode(pack.getIdWithVersion()); m.setProperty("j:title", pack.getName()); if (pack.getModuleType() != null) { m.setProperty("j:moduleType", pack.getModuleType()); }// w w w . j a v a 2 s. c o m m.setProperty("j:modulePriority", pack.getModulePriority()); List<Value> l = new ArrayList<Value>(); for (String d : pack.getDepends()) { String v = templatePackageRegistry.getModuleId(d); if (v != null) { l.add(session.getValueFactory().createValue(v)); } else { logger.warn("Cannot find dependency {} for package '{}'", d, pack.getName()); } } Value[] values = new Value[pack.getDepends().size()]; m.setProperty("j:dependencies", l.toArray(values)); if (pack.getModuleType() == null) { String moduleType = guessModuleType(session, pack); pack.setModuleType(moduleType); } if (!m.hasNode("templates")) { m.addNode("templates", "jnt:templatesFolder"); } if (!m.hasNode("permissions")) { m.addNode("permissions", "jnt:permission"); } JCRNodeWrapper perms = m.getNode("permissions"); if (!perms.hasNode("components")) { perms.addNode("components", "jnt:permission"); } if (!perms.hasNode("templates")) { perms.addNode("templates", "jnt:permission"); } JCRNodeWrapper tpls = m.getNode("templates"); if (!tpls.hasProperty("j:rootTemplatePath") && JahiaTemplateManagerService.MODULE_TYPE_MODULE.equals(pack.getModuleType())) { tpls.setProperty("j:rootTemplatePath", "/base"); } if (pack.getResourceBundleName() != null) { List<String> langs = new ArrayList<String>(); Resource[] resources = pack.getResources("resources"); for (Resource resource : resources) { try { String key = resource.getURI().getPath().substring(1).replace("/", "."); if (key.startsWith(pack.getResourceBundleName())) { String langCode = StringUtils.substringBetween(key, pack.getResourceBundleName() + "_", ".properties"); if (langCode != null) { langs.add(langCode); } } } catch (IOException e) { logger.error("Cannot get resources", e); } } JCRNodeWrapper moduleNode = m.getParent(); if (moduleNode.hasProperty("j:languages")) { Value[] oldValues = m.getParent().getProperty("j:languages").getValues(); for (Value value : oldValues) { if (!langs.contains(value.getString())) { langs.add(value.getString()); } } } moduleNode.setProperty("j:languages", langs.toArray(new String[langs.size()])); } }
From source file:org.jahia.services.usermanager.GroupCacheHelper.java
private static GroupPathByGroupNameCacheKey getPathCacheKey(String groupPath) { return new GroupPathByGroupNameCacheKey( groupPath.startsWith("/sites/") ? StringUtils.substringBetween(groupPath, "/sites/", "/") : null, StringUtils.substringAfterLast(groupPath, "/")); }
From source file:org.jahia.services.usermanager.ldap.JahiaLDAPConfig.java
private void buildConfig(Properties properties, AbstractConfig config, String key, Object value, boolean isUser) { if (key.contains(".attribute.map")) { config.getAttributesMapper().put(StringUtils .substringBetween(key, isUser ? "user." : "group.", ".attribute.map").replace("_", ":"), (String) value);/*from w ww .jav a 2 s.c o m*/ } else if (key.contains("search.wildcards.attributes")) { if (StringUtils.isNotEmpty((String) value)) { for (String wildcardAttr : ((String) value).split(",")) { config.getSearchWildcardsAttributes().add(wildcardAttr.trim()); } } } else { properties.put(transformPropKeyToBeanAttr(key.substring(isUser ? 5 : 6)), value); } }
From source file:org.jahia.services.usermanager.mongo.JahiaMongoConfig.java
/** * * @param properties/* w w w.j a v a 2s . c o m*/ * @param config * @param key * @param value * @param isUser */ private void buildConfig(final Properties properties, final AbstractConfig config, final String key, final Object value, final boolean isUser) { if (key.contains(".attribute.map")) { config.getAttributesMapper().put( StringUtils.substringBetween(key, "user.", ".attribute.map").replace("_", ":"), (String) value); } else if (key.contains("search.wildcards.attributes")) { if (StringUtils.isNotEmpty((String) value)) { for (String wildcardAttr : ((String) value).split(",")) { config.getSearchWildcardsAttributes().add(wildcardAttr.trim()); } } } else { properties.put(transformPropKeyToBeanAttr(key.substring(isUser ? 5 : 6)), value); } }
From source file:org.jahia.services.usermanager.UserCacheHelper.java
private String getSiteKey(String userPath) { return userPath.startsWith("/sites/") ? StringUtils.substringBetween(userPath, "/sites/", "/") : null; }