Search in sources :

Example 1 with JahiaException

use of org.jahia.exceptions.JahiaException in project jahia by Jahia.

the class ContentManagerHelper method copy.

public List<GWTJahiaNode> copy(final List<String> pathsToCopy, final String destinationPath, final String newName, final boolean moveOnTop, final boolean cut, final boolean reference, final List<String> childNodeTypesToSkip, boolean allLanguages, JCRSessionWrapper currentUserSession, final Locale uiLocale) throws GWTJahiaServiceException {
    final List<String> missedPaths = new ArrayList<String>();
    final List<GWTJahiaNode> res = new ArrayList<GWTJahiaNode>();
    // perform a check to prevent pasting content to itself or its children
    for (Iterator<String> iterator = pathsToCopy.iterator(); iterator.hasNext(); ) {
        String toCopy = iterator.next();
        if (destinationPath.equals(toCopy) || destinationPath.startsWith(toCopy + "/")) {
            missedPaths.add(Messages.getInternalWithArguments("failure.paste.cannot.paste", "Content {0} cannot be pasted into {1}", uiLocale, toCopy, destinationPath));
            iterator.remove();
        }
    }
    if (!missedPaths.isEmpty() && pathsToCopy.isEmpty()) {
        throw new GWTJahiaServiceException(StringUtils.join(missedPaths, "\n"));
    }
    try {
        JCRCallback<List<String>> callback = new JCRCallback<List<String>>() {

            @Override
            public List<String> doInJCR(JCRSessionWrapper session) throws RepositoryException {
                List<String> res = new ArrayList<String>();
                final JCRNodeWrapper targetParent;
                JCRNodeWrapper targetNode;
                targetNode = session.getNode(destinationPath);
                if (moveOnTop) {
                    targetParent = targetNode.getParent();
                } else {
                    targetParent = targetNode;
                }
                for (String aNode : pathsToCopy) {
                    JCRNodeWrapper node = session.getNode(aNode);
                    String name = newName != null ? newName : node.getName();
                    try {
                        name = findAvailableName(targetParent, name);
                        boolean canPaste = (!targetParent.isLocked() || Optional.ofNullable(targetParent.getLockInfos().get(null)).orElse(Collections.emptyList()).stream().allMatch(s -> s.endsWith(JCRNodeLockType.ALLOWS_ADD_SUFFIX)));
                        if (targetParent.hasPermission("jcr:addChildNodes") && canPaste) {
                            final JCRNodeWrapper copy = doPaste(targetParent, node, name, cut, reference, childNodeTypesToSkip);
                            if (moveOnTop && targetParent.getPrimaryNodeType().hasOrderableChildNodes()) {
                                targetParent.orderBefore(name, targetNode.getName());
                            }
                            session.save();
                            res.add(copy.getIdentifier());
                        } else {
                            missedPaths.add("File " + name + " could not be referenced in " + targetParent.getPath());
                        }
                    } catch (RepositoryException e) {
                        logger.error("Exception", e);
                        if (cut) {
                            missedPaths.add(Messages.getInternalWithArguments("failure.cut.cannot.cut", uiLocale, name, targetParent.getPath(), node.getPath(), session.getUser().getName()));
                        } else {
                            missedPaths.add("File " + name + " could not be referenced in " + targetParent.getPath());
                        }
                    } catch (JahiaException e) {
                        logger.error("Exception", e);
                        missedPaths.add("File " + name + " could not be referenced in " + targetParent.getPath());
                    }
                }
                return res;
            }
        };
        List<String> uuids;
        if (allLanguages) {
            uuids = JCRTemplate.getInstance().doExecute(currentUserSession.getUser(), currentUserSession.getWorkspace().getName(), null, callback);
        } else {
            uuids = callback.doInJCR(currentUserSession);
        }
        if (missedPaths.size() > 0) {
            StringBuilder errors = new StringBuilder();
            if (cut) {
                for (String err : missedPaths) {
                    errors.append("\n").append(err);
                }
            } else {
                errors.append("The following files could not have their reference pasted:");
                for (String err : missedPaths) {
                    errors.append("\n").append(err);
                }
            }
            throw new GWTJahiaServiceException(errors.toString());
        }
        for (String uuid : uuids) {
            res.add(navigation.getGWTJahiaNode(currentUserSession.getNodeByUUID(uuid), COPIED_NODE_FIELDS));
        }
    } catch (RepositoryException e) {
        throw new GWTJahiaServiceException(e);
    }
    return res;
}
Also used : GWTJahiaNode(org.jahia.ajax.gwt.client.data.node.GWTJahiaNode) GWTJahiaServiceException(org.jahia.ajax.gwt.client.service.GWTJahiaServiceException) JahiaException(org.jahia.exceptions.JahiaException)

Example 2 with JahiaException

use of org.jahia.exceptions.JahiaException in project jahia by Jahia.

the class PropertiesHelper method getCategoryPathValues.

public List<Value> getCategoryPathValues(String value) {
    if (value == null || value.length() == 0) {
        return Collections.emptyList();
    }
    List<Value> values = new LinkedList<Value>();
    String[] categories = StringUtils.split(value, ",");
    for (String categoryKey : categories) {
        try {
            values.add(new StringValue(Category.getCategoryPath(categoryKey.trim())));
        } catch (JahiaException e) {
            logger.warn("Unable to retrieve category path for category key '" + categoryKey + "'. Cause: " + e.getMessage(), e);
        }
    }
    return values;
}
Also used : JahiaException(org.jahia.exceptions.JahiaException) GWTJahiaNodePropertyValue(org.jahia.ajax.gwt.client.data.definition.GWTJahiaNodePropertyValue) StringValue(org.apache.jackrabbit.value.StringValue) StringValue(org.apache.jackrabbit.value.StringValue)

Example 3 with JahiaException

use of org.jahia.exceptions.JahiaException in project jahia by Jahia.

the class JahiaContentManagementServiceImpl method drawPortletInstanceOutput.

@Override
public GWTJahiaPortletOutputBean drawPortletInstanceOutput(String windowID, String entryPointIDStr, String pathInfo, String queryString) {
    GWTJahiaPortletOutputBean result = new GWTJahiaPortletOutputBean();
    try {
        int fieldId = Integer.parseInt(windowID);
        String portletOutput = ServicesRegistry.getInstance().getApplicationsDispatchService().getAppOutput(fieldId, entryPointIDStr, getRemoteJahiaUser(), getRequest(), getResponse(), getServletContext(), getWorkspace());
        try {
            JCRNodeWrapper node = JCRSessionFactory.getInstance().getCurrentUserSession().getNodeByUUID(entryPointIDStr);
            String nodeTypeName = node.getPrimaryNodeTypeName();
            /**
             * todo cleanup the hardcoded value here
             */
            if ("jnt:htmlPortlet".equals(nodeTypeName)) {
                result.setInIFrame(false);
            }
            if ("jnt:contentPortlet".equals(nodeTypeName) || "jnt:rssPortlet".equals(nodeTypeName)) {
                result.setInContentPortlet(true);
            }
        } catch (RepositoryException e) {
            logger.error(e.getMessage(), e);
        }
        result.setHtmlOutput(portletOutput);
        // what we need to do now is to do special processing for <script> tags, and on the client side we will
        // create them dynamically.
        Source source = new Source(portletOutput);
        source = new Source((new SourceFormatter(source)).toString());
        List<StartTag> scriptTags = source.getAllStartTags(HTMLElementName.SCRIPT);
        for (StartTag curScriptTag : scriptTags) {
            if ((curScriptTag.getAttributeValue("src") != null) && (!curScriptTag.getAttributeValue("src").equals(""))) {
                result.getScriptsWithSrc().add(curScriptTag.getAttributeValue("src"));
            } else {
                result.getScriptsWithCode().add(curScriptTag.getElement().getContent().toString());
            }
        }
    } catch (JahiaException e) {
        logger.error(e.getMessage(), e);
    }
    return result;
}
Also used : JahiaException(org.jahia.exceptions.JahiaException) SourceFormatter(net.htmlparser.jericho.SourceFormatter) Source(net.htmlparser.jericho.Source) StartTag(net.htmlparser.jericho.StartTag)

Example 4 with JahiaException

use of org.jahia.exceptions.JahiaException in project jahia by Jahia.

the class JCRCategoryProvider method removeCategory.

/**
 * Remove the category from the repository
 * @param categoryBean
 * @throws JahiaException
 */
public void removeCategory(CategoryBean categoryBean) throws JahiaException {
    try {
        JCRSessionWrapper jcrSessionWrapper = sessionFactory.getCurrentUserSession();
        Node node = null;
        if (categoryBean.getId() == null) {
            List<Category> categories = findCategoryByKey(categoryBean.getKey());
            if (categories.size() == 1) {
                node = jcrSessionWrapper.getNodeByUUID(categories.get(0).getID());
            } else {
                String msg = "Category " + categoryBean.getKey() + (categories.size() == 0 ? "not found" : " exists multiple times and could not be deleted");
                throw new JahiaException(msg, msg, JahiaException.DATA_ERROR, JahiaException.ERROR_SEVERITY);
            }
        } else {
            node = jcrSessionWrapper.getNodeByUUID(categoryBean.getId());
        }
        if (node != null) {
            if (!node.getParent().isCheckedOut()) {
                jcrSessionWrapper.checkout(node.getParent());
            }
            node.remove();
            jcrSessionWrapper.save();
        }
    } catch (RepositoryException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : Category(org.jahia.services.categories.Category) JahiaException(org.jahia.exceptions.JahiaException) JCRSessionWrapper(org.jahia.services.content.JCRSessionWrapper)

Example 5 with JahiaException

use of org.jahia.exceptions.JahiaException in project jahia by Jahia.

the class ServletContextManager method getApplicationContext.

// --------------------------------------------------------------------------
/**
 * Get an ApplicationContext for a given context
 *
 * @return the application context , null if not found
 * @param appBean the application bean against which we want the wepapp context
 * @throws org.jahia.exceptions.JahiaException on error
 */
public WebAppContext getApplicationContext(ApplicationBean appBean) throws JahiaException {
    if (appBean == null) {
        String errMsg = "Error getting application bean for application";
        logger.debug(errMsg);
        throw new JahiaException(errMsg, errMsg, JahiaException.ERROR_SEVERITY, JahiaException.APPLICATION_ERROR);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Requested for context : " + appBean.getContext());
    }
    if (appBean.getContext() == null) {
        return null;
    }
    WebAppContext appContext = mRegistry.get(appBean.getContext());
    if (appContext == null) {
        // try to load from disk
        appContext = loadContextInfoFromDisk(appBean.getID(), appBean.getContext());
        if (appContext == null) {
            // create a fake Application Context to avoid loading from disk the next time.
            appContext = new WebAppContext(appBean.getContext());
        }
        mRegistry.put(appBean.getContext(), appContext);
    }
    return appContext;
}
Also used : WebAppContext(org.jahia.data.applications.WebAppContext) JahiaException(org.jahia.exceptions.JahiaException)

Aggregations

JahiaException (org.jahia.exceptions.JahiaException)44 IOException (java.io.IOException)10 RepositoryException (javax.jcr.RepositoryException)9 ApplicationBean (org.jahia.data.applications.ApplicationBean)6 Category (org.jahia.services.categories.Category)6 JahiaInitializationException (org.jahia.exceptions.JahiaInitializationException)5 JahiaSite (org.jahia.services.sites.JahiaSite)5 JCRSessionWrapper (org.jahia.services.content.JCRSessionWrapper)4 Map (java.util.Map)3 JarFile (java.util.jar.JarFile)3 ZipEntry (java.util.zip.ZipEntry)3 ZipFile (java.util.zip.ZipFile)3 ZipInputStream (java.util.zip.ZipInputStream)3 PathNotFoundException (javax.jcr.PathNotFoundException)3 ServletException (javax.servlet.ServletException)3 File (java.io.File)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 AccessDeniedException (javax.jcr.AccessDeniedException)2 PortletWindow (org.apache.pluto.container.PortletWindow)2