Search in sources :

Example 1 with CommandException

use of org.apache.jackrabbit.standalone.cli.CommandException in project jackrabbit by apache.

the class Info method execute.

public boolean execute(Context ctx) throws Exception {
    PrintWriter out = CommandHelper.getOutput(ctx);
    out.println();
    try {
        CommandHelper.getRepository(ctx);
    } catch (CommandException e) {
        out.println("No connection to a repository.");
        return false;
    }
    out.println("Repository: " + CommandHelper.getRepositoryAddress(ctx));
    Session session;
    String currentPath;
    try {
        session = CommandHelper.getSession(ctx);
        currentPath = CommandHelper.getCurrentNode(ctx).getPath();
    } catch (CommandException e) {
        out.println("Not logged in / no session.");
        return false;
    }
    out.println("User      : " + session.getUserID());
    out.println("Workspace : " + session.getWorkspace().getName());
    out.println("Node      : " + currentPath);
    out.println();
    if (session.isLive()) {
        out.println("Session is live.");
    } else {
        out.println("Session is not live.");
    }
    if (session.hasPendingChanges()) {
        out.println("Session has pending changes.");
    } else {
        out.println("Session has no changes.");
    }
    return false;
}
Also used : CommandException(org.apache.jackrabbit.standalone.cli.CommandException) PrintWriter(java.io.PrintWriter) Session(javax.jcr.Session)

Example 2 with CommandException

use of org.apache.jackrabbit.standalone.cli.CommandException in project jackrabbit by apache.

the class ExportPropertyToFile method exportValue.

/**
 * Export th given value to a File
 * @param ctx
 *        the <code>Context</code>
 * @param value
 *        the <code>Value</code>
 * @param to
 *        the target file system path
 * @throws CommandException
 *         if the <code>File</code> already exists
 * @throws IOException
 *         if an <code>IOException</code> occurs
 * @throws RepositoryException
 *         if the current working <code>Repository</code> throws an
 *         <code>Exception</code>
 */
private void exportValue(Context ctx, Value value, String to) throws CommandException, IOException, RepositoryException {
    boolean overwrite = Boolean.valueOf((String) ctx.get(this.overwriteKey)).booleanValue();
    File file = new File(to);
    // Check if there's a file at the given target path
    if (file.exists() && !overwrite) {
        throw new CommandException("exception.file.exists", new String[] { to });
    }
    // If it doesn't exists create the file
    if (!file.exists()) {
        file.createNewFile();
    }
    if (value.getType() == PropertyType.BINARY) {
        InputStream in = value.getStream();
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        int c;
        while ((c = in.read()) != -1) {
            out.write(c);
        }
        in.close();
        out.flush();
        out.close();
    } else {
        Reader in = new StringReader(value.getString());
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        int c;
        while ((c = in.read()) != -1) {
            out.write(c);
        }
        in.close();
        out.flush();
        out.close();
    }
}
Also used : InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) FileWriter(java.io.FileWriter) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) CommandException(org.apache.jackrabbit.standalone.cli.CommandException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) BufferedWriter(java.io.BufferedWriter)

Example 3 with CommandException

use of org.apache.jackrabbit.standalone.cli.CommandException in project jackrabbit by apache.

the class ExportFileSystem method execute.

/**
 * {@inheritDoc}
 */
public boolean execute(Context ctx) throws Exception {
    String from = (String) ctx.get(this.srcJcrPathKey);
    String to = (String) ctx.get(this.destFsPathKey);
    boolean overwrite = Boolean.valueOf((String) ctx.get(this.overwriteKey)).booleanValue();
    if (log.isDebugEnabled()) {
        log.debug("exporting node at " + from + " to the filesystem (" + to + ") overwrite=" + overwrite);
    }
    Node node = CommandHelper.getNode(ctx, from);
    File f = new File(to);
    // check if the file exists
    if (f.exists() && !overwrite) {
        throw new CommandException("exception.file.exists", new String[] { to });
    }
    // export either a file or a folder
    if (node.isNodeType("nt:file")) {
        this.createFile(node, f);
    } else if (node.isNodeType("nt:folder")) {
        this.addFolder(node, f);
    } else {
        throw new CommandException("exception.not.file.or.folder", new String[] { node.getPrimaryNodeType().getName() });
    }
    return false;
}
Also used : Node(javax.jcr.Node) CommandException(org.apache.jackrabbit.standalone.cli.CommandException) File(java.io.File)

Example 4 with CommandException

use of org.apache.jackrabbit.standalone.cli.CommandException in project jackrabbit by apache.

the class ExportFileSystem method createFile.

/**
 * Exports an nt:file to the file system
 * @param node
 *        the <code>Node</code>
 * @param file
 *        the <code>File</code>
 * @throws IOException
 *         if an IOException occurs
 * @throws CommandException
 *         if the <code>File</code> can't be created
 * @throws ValueFormatException
 *         if a <code>Value</code> can't be retrieved
 * @throws PathNotFoundException
 *         if the <code>Node</code> can't be found
 * @throws RepositoryException
 *         if the current working <code>Repository</code> throws an
 *         <code>Exception</code>
 */
private void createFile(Node node, File file) throws IOException, CommandException, ValueFormatException, PathNotFoundException, RepositoryException {
    boolean created = file.createNewFile();
    if (!created) {
        throw new CommandException("exception.file.not.created", new String[] { file.getPath() });
    }
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    InputStream in = node.getNode("jcr:content").getProperty("jcr:data").getStream();
    int c;
    while ((c = in.read()) != -1) {
        out.write(c);
    }
    in.close();
    out.flush();
    out.close();
}
Also used : InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) CommandException(org.apache.jackrabbit.standalone.cli.CommandException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 5 with CommandException

use of org.apache.jackrabbit.standalone.cli.CommandException in project jackrabbit by apache.

the class ExportFileSystem method addFolder.

/**
 * Exports a nt:folder and all its children to the file system
 * @param node
 *        the <code>Node</code>
 * @param file
 *        <code>File</code>
 * @throws CommandException
 *         if the <code>File</code> can't be created
 * @throws RepositoryException
 *         if the current working <code>Repository</code> throws an
 *         <code>Exception</code>
 * @throws IOException
 *         if an IOException occurs
 */
private void addFolder(Node node, File file) throws CommandException, RepositoryException, IOException {
    boolean created = file.mkdir();
    if (!created) {
        throw new CommandException("exception.folder.not.created", new String[] { file.getPath() });
    }
    NodeIterator iter = node.getNodes();
    while (iter.hasNext()) {
        Node child = iter.nextNode();
        // File
        if (child.isNodeType("nt:file")) {
            File childFile = new File(file, child.getName());
            createFile(child, childFile);
        } else if (child.isNodeType("nt:folder")) {
            File childFolder = new File(file, child.getName());
            addFolder(child, childFolder);
        }
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) CommandException(org.apache.jackrabbit.standalone.cli.CommandException) File(java.io.File)

Aggregations

CommandException (org.apache.jackrabbit.standalone.cli.CommandException)11 File (java.io.File)7 Node (javax.jcr.Node)4 BufferedOutputStream (java.io.BufferedOutputStream)3 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)2 PrintWriter (java.io.PrintWriter)2 Session (javax.jcr.Session)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 InvalidItemStateException (javax.jcr.InvalidItemStateException)1 NodeIterator (javax.jcr.NodeIterator)1 Repository (javax.jcr.Repository)1