List of usage examples for org.apache.commons.lang StringUtils substringBeforeLast
public static String substringBeforeLast(String str, String separator)
Gets the substring before the last occurrence of a separator.
From source file:org.jahia.bundles.extender.jahiamodules.BundleHttpResourcesTracker.java
public static void flushJspCache(Bundle bundle, String jspFile) { try {/*from w ww .j a va 2 s.c o m*/ File scratchDirFile = new File(new File(System.getProperty("java.io.tmpdir"), "jahia-jsps"), bundle.getSymbolicName() + "-" + bundle.getBundleId()); if (jspFile != null && jspFile.length() > 0) { JspCompilationContext jspCompilationContext = new JspCompilationContext(jspFile, false, null, JahiaContextLoaderListener.getServletContext(), null, null); String javaPath = jspCompilationContext.getJavaPath(); String classPath = StringUtils.substringBeforeLast(javaPath, ".java") + ".class"; FileUtils.deleteQuietly(new File(scratchDirFile, javaPath)); FileUtils.deleteQuietly(new File(scratchDirFile, classPath)); } } catch (JasperException e) { logger.error(e.getMessage(), e); } }
From source file:org.jahia.bundles.jcrcommands.JCRNodeCompleter.java
@Override public int complete(final Session session, final CommandLine commandLine, final List<String> candidates) { try {//from www. j av a2 s . c o m JCRTemplate.getInstance().doExecuteWithSystemSession(null, getCurrentWorkspace(session), null, new JCRCallback<Object>() { @Override public Object doInJCR(JCRSessionWrapper jcrsession) throws RepositoryException { String arg = commandLine.getCursorArgument(); if (arg == null) { arg = ""; } else { arg = arg.substring(0, commandLine.getArgumentPosition()); } JCRNodeWrapper n = jcrsession.getNode(getCurrentPath(session)); String prefix = ""; if (arg.indexOf('/') > -1) { prefix = StringUtils.substringBeforeLast(arg, "/") + "/"; if (prefix.startsWith("/")) { n = jcrsession.getNode(prefix); } else { n = n.getNode(prefix); } arg = StringUtils.substringAfterLast(arg, "/"); } JCRNodeIteratorWrapper nodes = n.getNodes(); while (nodes.hasNext()) { JCRNodeWrapper next = (JCRNodeWrapper) nodes.nextNode(); if (next.getName().startsWith(arg)) { candidates.add(prefix + next.getName() + "/"); } } return null; } }); } catch (RepositoryException e) { // ignore } return candidates.isEmpty() ? -1 : commandLine.getBufferPosition() - commandLine.getArgumentPosition(); }
From source file:org.jahia.modules.docconverter.rules.DocumentConverterRuleService.java
/** * Converts the specified node using target MIME type. * /*from ww w . ja v a2s. c o m*/ * @param nodeFact the node to be converted * @param targetMimeType the target MIME type * @param overwriteIfExists is set to true, the existing file should be * overwritten if exists; otherwise the new file name will be * generated automatically. * @param drools the rule engine helper class * @throws RepositoryException in case of an error */ public void convert(final AddedNodeFact nodeFact, final String targetMimeType, boolean overwriteIfExists, KnowledgeHelper drools) throws RepositoryException { if (!converterService.isEnabled()) { logger.info("Conversion service is not enabled." + " Skip converting file " + nodeFact.getPath()); return; } if (logger.isDebugEnabled()) { logger.debug( "Converting node " + nodeFact.getPath() + " into target MIME type '" + targetMimeType + "'"); } DocumentFormat format = converterService.getFormatByMimeType(targetMimeType); if (format == null) { logger.warn("Unsupported target MIME type '" + targetMimeType + "'. Skip converting file " + nodeFact.getPath()); return; } InputStream is = null; try { JCRNodeWrapper node = nodeFact.getNode(); if (node.isNodeType("nt:file")) { is = node.getFileContent().downloadFile(); File temp = File.createTempFile("doc-converter-rule", null); FileOutputStream os = new FileOutputStream(temp); try { converterService.convert(is, nodeFact.getMimeType(), os, targetMimeType); os.close(); JCRNodeWrapper folder = node.getParent(); String newName = StringUtils.substringBeforeLast(node.getName(), ".") + "." + format.getExtension(); if (!overwriteIfExists) { newName = JCRContentUtils.findAvailableNodeName(folder, newName); } FileInputStream convertedStream = new FileInputStream(temp); try { folder.uploadFile(newName, convertedStream, format.getMediaType()); logger.info("Converted node " + nodeFact.getPath() + " to type " + format.getMediaType()); } finally { IOUtils.closeQuietly(convertedStream); } } finally { IOUtils.closeQuietly(os); FileUtils.deleteQuietly(temp); } } else { logger.warn("Path should correspond to a file node. Skipping node " + nodeFact.getPath()); } } catch (Exception e) { logger.error(e.getMessage(), e); } finally { IOUtils.closeQuietly(is); } }
From source file:org.jahia.modules.docrules.CreatePDFDocumentRule.java
public void execute(JCRNodeWrapper document) throws OfficeException, IOException, UnsupportedRepositoryOperationException, LockException, RepositoryException { File inFile = null;// w w w.j a v a 2 s. c om File outFile = null; try { inFile = File.createTempFile("doc-rules", null); JCRContentUtils.downloadFileContent(document, inFile); outFile = documentConverterService.convert(inFile, document.getFileContent().getContentType(), "application/pdf"); if (outFile != null) { JCRNodeWrapper folder = getTargetFolder(document); folder.getSession().checkout(folder); String newName = StringUtils.substringBeforeLast(document.getName(), ".") + ".pdf"; if (overwriteIfExists) { if (folder.hasNode(newName)) { folder.getNode(newName).remove(); } } else { newName = JCRContentUtils.findAvailableNodeName(folder, newName); } BufferedInputStream convertedStream = null; try { convertedStream = new BufferedInputStream(new FileInputStream(outFile)); JCRNodeWrapper pdf = folder.uploadFile(newName, convertedStream, "application/pdf"); if (logger.isDebugEnabled()) { logger.debug("Converted document {} to PDF document at {}", document.getPath(), pdf.getPath()); } } finally { IOUtils.closeQuietly(convertedStream); } } } finally { FileUtils.deleteQuietly(inFile); FileUtils.deleteQuietly(outFile); } }
From source file:org.jahia.modules.docviewer.DocumentViewService.java
/** * Converts the specified document file node into SWF file and creates the SWF file node in the same "folder". * /*from ww w . j a va 2s.c o m*/ * @param fileNode * the node to be converted * @param overwriteIfExists * is set to true, the existing file should be overwritten if exists; otherwise the new file name will be generated * automatically. * @throws RepositoryException * in case of an error */ public void convert(JCRNodeWrapper fileNode, boolean overwriteIfExists) throws RepositoryException { if (!isEnabled() || supportedDocumentFormats == null) { logger.info("Conversion service is not enabled." + " Skip converting node {}", fileNode.getPath()); return; } long timer = System.currentTimeMillis(); if (fileNode.isNodeType("nt:file") && isMimeTypeGroup(fileNode.getFileContent().getContentType(), supportedDocumentFormats)) { File inFile = null; File outFile = null; try { inFile = createTempFile(); outFile = convert(JCRContentUtils.downloadFileContent(fileNode, inFile), fileNode.getFileContent().getContentType()); if (outFile != null) { JCRNodeWrapper folder = fileNode.getParent(); String newName = StringUtils.substringBeforeLast(fileNode.getName(), ".") + ".swf"; if (!overwriteIfExists) { newName = JCRContentUtils.findAvailableNodeName(folder, newName); } BufferedInputStream convertedStream = new BufferedInputStream(new FileInputStream(outFile)); JCRNodeWrapper targetNode = null; try { targetNode = folder.uploadFile(newName, convertedStream, "application/x-shockwave-flash"); if (targetNode != null) { targetNode.getSession().save(); } } finally { IOUtils.closeQuietly(convertedStream); } if (logger.isDebugEnabled()) { logger.debug("Converted node {} into {} in {} ms", new Object[] { fileNode.getPath(), targetNode != null ? targetNode.getPath() : null, (System.currentTimeMillis() - timer) }); } } } catch (Exception e) { logger.error(e.getMessage(), e); } finally { FileUtils.deleteQuietly(inFile); FileUtils.deleteQuietly(outFile); } } else { logger.warn( "Path should correspond to a file node with one" + " of the supported formats {}. Skipping node {}", supportedDocumentFormats, fileNode.getPath()); } }
From source file:org.jahia.modules.external.ExtensionItem.java
@Override public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException { if (path.equals("/")) { throw new ItemNotFoundException(); }/*from w ww. jav a 2 s. c om*/ String parentPath = StringUtils.substringBeforeLast(path, "/"); try { controlManager.checkRead(parentPath); } catch (PathNotFoundException e) { throw new AccessDeniedException(parentPath); } try { return session.getNode(parentPath); } catch (PathNotFoundException e) { throw new ItemNotFoundException(parentPath); } }
From source file:org.jahia.modules.external.ExternalContentStoreProvider.java
public PropertyIterator getWeakReferences(JCRNodeWrapper node, String propertyName, Session session) throws RepositoryException { if (dataSource instanceof ExternalDataSource.Referenceable && session instanceof ExternalSessionImpl) { String identifier = node.getIdentifier(); if (this.equals(node.getProvider())) { identifier = ((ExternalNodeImpl) node.getRealNode()).getData().getId(); }/* ww w . ja va 2 s . co m*/ setCurrentSession((ExternalSessionImpl) session); List<String> referringProperties = null; try { referringProperties = ((ExternalDataSource.Referenceable) dataSource) .getReferringProperties(identifier, propertyName); } finally { ExternalContentStoreProvider.removeCurrentSession(); } if (referringProperties == null) { return null; } if (referringProperties.isEmpty()) { return PropertyIteratorAdapter.EMPTY; } List<Property> l = new ArrayList<Property>(); for (String propertyPath : referringProperties) { String nodePath = StringUtils.substringBeforeLast(propertyPath, "/"); if (nodePath.isEmpty()) { nodePath = "/"; } ExternalNodeImpl referringNode = (ExternalNodeImpl) session.getNode(nodePath); if (referringNode != null) { l.add(new ExternalPropertyImpl( new Name(StringUtils.substringAfterLast(propertyPath, "/"), NodeTypeRegistry.getInstance().getNamespaces()), referringNode, (ExternalSessionImpl) session)); } } return new PropertyIteratorAdapter(l); } return null; }
From source file:org.jahia.modules.external.ExternalNodeImpl.java
/** * {@inheritDoc}//from www. j a v a2 s .co m */ @Override public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException { if (data.getPath().equals("/")) { throw new ItemNotFoundException(); } String path = StringUtils.substringBeforeLast(data.getPath(), "/"); try { controlManager.checkRead(path.isEmpty() ? "/" : path); } catch (PathNotFoundException e) { throw new AccessDeniedException(path); } return session.getNode(path.isEmpty() ? "/" : path); }
From source file:org.jahia.modules.external.ExternalNodeImpl.java
public List<String> getExternalChildren() throws RepositoryException { if (externalChildren == null) { if (isNew) { externalChildren = new ArrayList<String>(); } else {// ww w . ja v a 2 s . c om ExternalContentStoreProvider.setCurrentSession(session); try { final ExternalDataSource dataSource = session.getRepository().getDataSource(); if (dataSource instanceof ExternalDataSource.CanLoadChildrenInBatch) { ExternalDataSource.CanLoadChildrenInBatch childrenLoader = (ExternalDataSource.CanLoadChildrenInBatch) dataSource; final List<ExternalData> childrenNodes = childrenLoader.getChildrenNodes(getPath()); if (externalChildren == null) { externalChildren = new ArrayList<String>(childrenNodes.size()); } for (ExternalData child : childrenNodes) { String parentPath = StringUtils.substringBeforeLast(child.getPath(), "/"); if (parentPath.equals("")) { parentPath = "/"; } if (parentPath.equals(getPath())) { externalChildren.add(child.getName()); } final ExternalNodeImpl node = new ExternalNodeImpl(child, session); session.registerNode(node); } } else { externalChildren = new ArrayList<String>(dataSource.getChildren(getPath())); } } finally { ExternalContentStoreProvider.removeCurrentSession(); } } } return externalChildren; }
From source file:org.jahia.modules.external.ExternalNodeImpl.java
public Node getExtensionNode(boolean create) throws RepositoryException { Session extensionSession = getSession().getExtensionSession(); if (extensionSession == null) { return null; }/*w w w.j ava 2 s .com*/ List<String> extensionAllowedTypes = getSession().getExtensionAllowedTypes(); boolean allowed = false; if (extensionAllowedTypes != null) { for (String type : extensionAllowedTypes) { if (isNodeType(type, false)) { allowed = true; break; } } } if (!allowed) { return null; } String path = getPath(); boolean isRoot = path.equals("/"); String mountPoint = getStoreProvider().getMountPoint(); String globalPath = mountPoint + (isRoot ? "" : path); if (!extensionSession.itemExists(globalPath)) { if (!create) { return null; } else { // create extension nodes if needed String[] splittedPath = StringUtils.split(path, "/"); StringBuilder currentExtensionPath = new StringBuilder(mountPoint); StringBuilder currentExternalPath = new StringBuilder(); // create extension node on the mountpoint if needed if (!extensionSession.nodeExists(mountPoint)) { String parent = StringUtils.substringBeforeLast(mountPoint, "/"); if (parent.equals("")) { parent = "/"; } final Node extParent = extensionSession.getNode(parent); takeLockToken(extParent); extParent.addMixin("jmix:hasExternalProviderExtension"); Node n = extParent.addNode(StringUtils.substringAfterLast(mountPoint, "/"), "jnt:externalProviderExtension"); n.addMixin("jmix:externalProviderExtension"); n.setProperty("j:isExternalProviderRoot", true); Node externalNode = (Node) session.getItemWithNoCheck("/"); n.setProperty("j:externalNodeIdentifier", externalNode.getIdentifier()); List<Value> values = ExtensionNode.createNodeTypeValues(session.getValueFactory(), externalNode.getPrimaryNodeType().getName()); n.setProperty("j:extendedType", values.toArray(new Value[values.size()])); } for (String p : splittedPath) { currentExtensionPath.append("/").append(p); currentExternalPath.append("/").append(p); if (!extensionSession.nodeExists(currentExtensionPath.toString())) { final Node extParent = extensionSession .getNode(StringUtils.substringBeforeLast(currentExtensionPath.toString(), "/")); takeLockToken(extParent); Node n = extParent.addNode(p, "jnt:externalProviderExtension"); Node externalNode = (Node) session.getItemWithNoCheck(currentExternalPath.toString()); List<Value> values = ExtensionNode.createNodeTypeValues(session.getValueFactory(), externalNode.getPrimaryNodeType().getName()); n.setProperty("j:extendedType", values.toArray(new Value[values.size()])); n.addMixin("jmix:externalProviderExtension"); n.setProperty("j:isExternalProviderRoot", false); n.setProperty("j:externalNodeIdentifier", externalNode.getIdentifier()); } } } } Node node = extensionSession.getNode(globalPath); if (create && isRoot && !node.isNodeType("jmix:hasExternalProviderExtension")) { node.addMixin("jmix:hasExternalProviderExtension"); } if (!node.isNodeType("jmix:externalProviderExtension")) { node.addMixin("jmix:externalProviderExtension"); } return node; }