Example usage for org.dom4j Document addElement

List of usage examples for org.dom4j Document addElement

Introduction

In this page you can find the example usage for org.dom4j Document addElement.

Prototype

Element addElement(String name);

Source Link

Document

Adds a new Element node with the given name to this branch and returns a reference to the new node.

Usage

From source file:itensil.repository.web.ShellServlet.java

License:Open Source License

/**
 *  /xslt//from   w  w w. j  av  a  2  s.c om
 *
 *    XSL Transform
 * 
 *      srcUri - input xml file
 *      dstUri - output html/xml/text file
 *      xslUri - style document
 */
@ContentType("text/xml")
public void webXslt(HttpServletRequest request, HttpServletResponse response) throws Exception {

    Map<String, String> params = RequestUtil.readParameters(request,
            new String[] { "srcUri", "dstUri", "xslUri" });

    String xslUri = params.get("xslUri");
    String srcUri = params.get("srcUri");
    String dstUri = params.get("dstUri");
    if (!(Check.isEmpty(srcUri) || srcUri.length() < 2 || Check.isEmpty(dstUri) || dstUri.length() < 2
            || Check.isEmpty(xslUri) || xslUri.length() < 2)) {

        xslUri = RepositoryHelper.resolveUri(xslUri);
        srcUri = RepositoryHelper.resolveUri(srcUri);
        dstUri = RepositoryHelper.resolveUri(dstUri);

        RepositoryHelper.beginTransaction();
        Repository repository = RepositoryHelper.getRepository(dstUri);
        MutableRepositoryNode node;
        NodeProperties props;
        NodeVersion version = new DefaultNodeVersion();
        try {
            node = repository.getNodeByUri(dstUri, true);
            if (node.isCollection())
                throw new AccessDeniedException(dstUri, "collection");
            props = node.getProperties(version);
            if (props != null) {
                version = RepositoryHelper.nextVersion(node, props.getVersion(), true);
                props = new DefaultNodeProperties(version, props.getPropertyMap());
            } else {
                props = new DefaultNodeProperties(version);
            }
        } catch (NotFoundException nfe) {
            node = repository.createNode(dstUri, false, (User) request.getUserPrincipal());
            props = new DefaultNodeProperties(version);
        }

        ByteArrayOutputStream ostream = new ByteArrayOutputStream(1024 * 16);

        StreamResult result = new StreamResult(ostream);

        ExslTransform.transformXML(result, new StreamSource(RepositoryHelper.loadContent(srcUri)),
                new StreamSource(RepositoryHelper.loadContent(xslUri)), UriHelper.getParent(srcUri), null);

        NodeContent content = new DefaultNodeContent(ostream.toByteArray(), version);

        // properties
        String mime = getServletContext().getMimeType(dstUri);
        if (mime == null)
            mime = "application/octet-stream";
        PropertyHelper.setStandardProperties(props, dstUri, mime, content.getLength());
        node.setProperties(props);

        node.setContent(content);

        RepositoryHelper.commitTransaction();

        Document doc = DocumentHelper.createDocument();
        doc.addElement("ok").addAttribute("result", node.getUri());

        ServletUtil.setExpired(response);
        doc.write(response.getWriter());
    }
}

From source file:itensil.repository.web.ShellServlet.java

License:Open Source License

/**
 *  /copy/*ww  w. ja v a2  s .c om*/
 *
 * Copy a node
 *
 */
@ContentType("text/xml")
public void webCopy(HttpServletRequest request, HttpServletResponse response) throws Exception {

    Map<String, String> params = RequestUtil.readParameters(request,
            new String[] { "srcUri", "dstUri", "description" });

    String description = params.get("description");
    String srcUri = params.get("srcUri");
    String dstUri = params.get("dstUri");
    if (!(Check.isEmpty(srcUri) || srcUri.length() < 2 || Check.isEmpty(dstUri) || dstUri.length() < 2)) {
        dstUri = filterDestUri(dstUri);
        RepositoryHelper.beginTransaction();
        RepositoryNode resNode = RepositoryHelper.copyAndUpdate(dstUri, srcUri,
                (User) request.getUserPrincipal(), description);

        RepositoryHelper.commitTransaction();

        // expire since probably get
        ServletUtil.setExpired(response);
        Document doc = DocumentHelper.createDocument();
        doc.addElement("ok").addAttribute("result", resNode.getUri());
        doc.write(response.getWriter());

    } else {
        throw new NotFoundException("[blank]");
    }
}

From source file:itensil.repository.web.ShellServlet.java

License:Open Source License

/**
 *  /move/*ww  w  . j a  v a 2 s. c  o m*/
 *
 * Mode a node
 *
 */
@ContentType("text/xml")
public void webMove(HttpServletRequest request, HttpServletResponse response) throws Exception {

    String srcUri = request.getParameter("srcUri");
    String dstUri = request.getParameter("dstUri");
    if (!(Check.isEmpty(srcUri) || srcUri.length() < 2 || Check.isEmpty(dstUri) || dstUri.length() < 2)) {
        dstUri = filterDestUri(dstUri);
        RepositoryHelper.beginTransaction();
        dstUri = RepositoryHelper.resolveUri(dstUri);
        srcUri = RepositoryHelper.resolveUri(srcUri);
        MutableRepositoryNode node = getNode(srcUri);
        node.move(dstUri);
        RepositoryHelper.commitTransaction();

        // expire since probably get
        ServletUtil.setExpired(response);
        Document doc = DocumentHelper.createDocument();
        doc.addElement("ok").addAttribute("result", node.getUri());
        doc.write(response.getWriter());

    } else {
        throw new NotFoundException("[blank]");
    }
}

From source file:itensil.security.web.CommunityAdmin.java

License:Open Source License

/**
 *  /getConfigProp/* w w  w.  j a  va  2  s  .c om*/
 *
 *
 */
@ContentType("text/xml")
public void webGetConfigProp(HttpServletRequest request, HttpServletResponse response) throws Exception {
    HibernateUtil.beginTransaction();

    HibernateUtil.readOnlySession();

    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("config-property");
    Property prop = ConfigManager.getProperty(request.getParameter("component"));
    root.addElement("component").setText(prop.getComponent());
    root.addElement("properties").setText(prop.getProperties());

    HibernateUtil.commitTransaction();

    ServletUtil.noCache(response);
    doc.write(response.getWriter());
}

From source file:itensil.security.web.CommunityAdmin.java

License:Open Source License

/**
 *  /getInfo/*from   w w w . ja v a  2 s  . c  o  m*/
 *
 *  Community Info
 *
 */
public void webGetInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {

    HibernateUtil.beginTransaction();
    HibernateUtil.readOnlySession();

    UserEntity self = (UserEntity) request.getUserPrincipal();
    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("community");
    String uspaceId = request.getParameter("uspaceId");
    if (SysAdmin.isSysAdmin(self) && !Check.isEmpty(uspaceId)) {
        UserSpaceEntity uspace = (UserSpaceEntity) HibernateUtil.getSession().get(UserSpaceEntity.class,
                uspaceId);
        if (uspace != null) {
            root.addElement("uspaceId").setText(uspace.getUserSpaceId());
            root.addElement("name").setText(Check.emptyIfNull(uspace.getName()));
            root.addElement("baseUrl").setText(Check.emptyIfNull(uspace.getBaseUrl()));
            root.addElement("brand").setText(Check.emptyIfNull(uspace.getBrand()));
            root.addElement("features").setText(Check.emptyIfNull(uspace.getFeaturesStr()));
        }
    }
    HibernateUtil.commitTransaction();

    ServletUtil.noCache(response);
    doc.write(response.getWriter());
}

From source file:itensil.security.web.UserSpaceServlet.java

License:Open Source License

/**
 *  /list//from   w w w  .  java  2  s .  c o  m
 *
 *  Output:
 *      User List XML
 *
 */
@ContentType("text/xml")
public void webList(HttpServletRequest request, HttpServletResponse response) throws Exception {
    UserEntity self = (UserEntity) request.getUserPrincipal();

    HibernateUtil.beginTransaction();
    HibernateUtil.readOnlySession();
    UserSpace uspace = self.getUserSpace();

    HibernateUtil.getSession().refresh(uspace);

    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("list");
    root.addAttribute("name", uspace.getName());
    root.addAttribute("self", self.getUserId());
    root.addAttribute("self-roles", self.getUSpaceUser().getRoleStr());
    for (User user : uspace.getUsers()) {
        Element uElem = root.addElement("user");
        uElem.addAttribute("id", user.getUserId());

        // email source may change here....
        uElem.addAttribute("email", user.getName());
        uElem.addAttribute("name", user.getSimpleName());
    }

    for (Group grp : uspace.getGroups()) {
        Element gElem = root.addElement("group");
        gElem.addAttribute("id", grp.getGroupId());
        gElem.addAttribute("name", grp.getSimpleName());
    }

    // include everyone for lookup
    Everyone evo = new Everyone();
    Element gElem = root.addElement("group");
    gElem.addAttribute("id", evo.getGroupId());
    gElem.addAttribute("name", evo.getSimpleName());
    gElem.addAttribute("everyone", "1");

    HibernateUtil.commitTransaction();

    // let the list live a bit
    ServletUtil.cacheTimeout(response, 12);
    doc.write(response.getWriter());
}

From source file:itensil.security.web.UserSpaceServlet.java

License:Open Source License

/**
 *  /userManList//from w  w  w  . j a v a 2 s.  co  m
 *
 *  Output:
 *      User/Groups List XML
 *
 */
@ContentType("text/xml")
public void webUserManList(HttpServletRequest request, HttpServletResponse response) throws Exception {
    AuthenticatedUser self = (AuthenticatedUser) request.getUserPrincipal();
    HibernateUtil.beginTransaction();
    HibernateUtil.readOnlySession();

    if (!self.getRoles().contains("admin"))
        throw new UserSpaceException("Admin role required");

    UserSpaceEntity uspace = (UserSpaceEntity) self.getUserSpace();

    HibernateUtil.getSession().refresh(uspace);

    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("list");

    root.addAttribute("name", uspace.getName());
    root.addAttribute("self", self.getUserId());
    for (USpaceUserEntity uus : uspace.getUserSpaceUsers().values()) {
        UserEntity user = uus.getUserEntity();

        Element uElem = root.addElement("user");
        uElem.addAttribute("id", user.getUserId());

        // email source may change here....
        uElem.addAttribute("email", user.getName());
        uElem.addAttribute("name", user.getSimpleName());
        uElem.addAttribute("roles", uus.getRoleStr());

        for (Group grp : uspace.getGroupsForUser(user)) {
            Element gElem = uElem.addElement("group");
            gElem.addAttribute("id", grp.getGroupId());
            gElem.addAttribute("name", grp.getSimpleName());
        }
    }
    HibernateUtil.commitTransaction();
    ServletUtil.noCache(response);
    doc.write(response.getWriter());
}

From source file:itensil.security.web.UserSpaceServlet.java

License:Open Source License

/**
 *  /groupManList// w  w w .  ja v a 2  s  .  c  om
 *
 *  Output:
 *      Group/User List XML
 *
 */
@SuppressWarnings("unchecked")
@ContentType("text/xml")
public void webGroupManList(HttpServletRequest request, HttpServletResponse response) throws Exception {
    AuthenticatedUser self = (AuthenticatedUser) request.getUserPrincipal();
    HibernateUtil.beginTransaction();
    HibernateUtil.readOnlySession();

    if (!self.getRoles().contains("admin"))
        throw new UserSpaceException("Admin role required");

    UserSpaceEntity uspace = (UserSpaceEntity) self.getUserSpace();

    //HibernateUtil.getSession().refresh(uspace);

    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("list");
    Query qry = HibernateUtil.getSession().getNamedQuery("USpace.groupsByType");
    qry.setInteger("gtype", 0);
    qry.setEntity("uspace", uspace);

    for (GroupEntity grp : (List<GroupEntity>) qry.list()) {
        Element gElem = root.addElement("group");
        gElem.addAttribute("id", grp.getGroupId());
        gElem.addAttribute("name", grp.getSimpleName());
        gElem.addAttribute("parentId", Check.emptyIfNull(grp.getParentGroupId()));
        for (User user : grp.getUserEntities()) {
            Element uElem = gElem.addElement("user");
            uElem.addAttribute("id", user.getUserId());
            uElem.addAttribute("name", user.getSimpleName());
            uElem.addAttribute("email", user.getName());
        }
    }
    HibernateUtil.commitTransaction();

    ServletUtil.noCache(response);
    doc.write(response.getWriter());
}

From source file:itensil.security.web.UserSpaceServlet.java

License:Open Source License

/**
 *  /orgList/*w ww .ja  v  a 2  s . co  m*/
 *
 *  Output:
 *      Group/User List XML
 *
 */
@SuppressWarnings("unchecked")
@ContentType("text/xml")
public void webOrgList(HttpServletRequest request, HttpServletResponse response) throws Exception {
    AuthenticatedUser self = (AuthenticatedUser) request.getUserPrincipal();
    HibernateUtil.beginTransaction();
    HibernateUtil.readOnlySession();

    UserSpaceEntity uspace = (UserSpaceEntity) self.getUserSpace();

    if (!uspace.getFeatures().contains("orgs"))
        throw new UserSpaceException("Org feature required");

    //HibernateUtil.getSession().refresh(uspace);

    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("list");
    Query qry = HibernateUtil.getSession().getNamedQuery("USpace.groupsByType");
    qry.setInteger("gtype", 1);
    qry.setEntity("uspace", uspace);

    for (GroupEntity grp : (List<GroupEntity>) qry.list()) {
        Element gElem = root.addElement("group");
        gElem.addAttribute("id", grp.getGroupId());
        gElem.addAttribute("name", grp.getSimpleName());
        gElem.addAttribute("fullName", grp.getGroupName());
        gElem.addAttribute("remoteKey", grp.getRemoteKey());
        gElem.addAttribute("parentId", grp.getParentGroupId());
    }

    HibernateUtil.commitTransaction();

    ServletUtil.noCache(response);
    doc.write(response.getWriter());
}

From source file:itensil.security.web.UserSpaceServlet.java

License:Open Source License

/**
 *  /addOrg//  w w  w  .  j  av a 2  s  . c  o m
 *
 *  Input:
 *   name
 *   fullName
 *   remoteKey
 *   parentId or parentRemoteKey
 *   
 */
@SuppressWarnings("unchecked")
@ContentType("text/xml")
public void webAddOrg(HttpServletRequest request, HttpServletResponse response) throws Exception {

    Map<String, String> params = RequestUtil.readParameters(request,
            new String[] { "name", "fullName", "remoteKey", "parentId", "parentRemoteKey" });

    HibernateUtil.beginTransaction();
    AuthenticatedUser self = (AuthenticatedUser) request.getUserPrincipal();

    UserSpaceEntity uspace = (UserSpaceEntity) self.getUserSpace();

    if (!uspace.getFeatures().contains("orgs"))
        throw new UserSpaceException("Org feature required");

    HibernateUtil.getSession().refresh(uspace);
    GroupEntity grp = new GroupEntity();
    grp.initNew();
    grp.setUserSpaceEntity(uspace);
    grp.setGroupType(1);
    grp.setSimpleName(params.get("name"));
    grp.setGroupName(params.get("fullName"));
    if (Check.isEmpty(grp.getGroupName()))
        grp.setGroupName(grp.getSimpleName());
    grp.setRemoteKey(params.get("remoteKey"));
    GroupEntity parGrp = null;
    if (!Check.isEmpty(params.get("parentId"))) {
        parGrp = (GroupEntity) uspace.resolve(new DefaultGroup(params.get("parentId")));
    } else if (!Check.isEmpty(params.get("parentRemoteKey"))) {
        Query qry = HibernateUtil.getSession().getNamedQuery("USpace.groupByRemote");
        qry.setString("rkey", params.get("parentRemoteKey"));
        qry.setEntity("uspace", uspace);
        parGrp = (GroupEntity) qry.uniqueResult();
    }
    if (parGrp != null) {
        grp.setParentGroupId(parGrp.getGroupId());
    }
    HibernateUtil.getSession().save(grp);

    HibernateUtil.commitTransaction();

    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("ok");
    root.addAttribute("id", grp.getGroupId());

    ServletUtil.noCache(response);
    doc.write(response.getWriter());
}