Search in sources :

Example 91 with DocumentException

use of org.dom4j.DocumentException in project cuba by cuba-platform.

the class UserSetHelper method removeEntities.

public static String removeEntities(String filterXml, Collection ids) {
    Document document;
    try {
        document = DocumentHelper.parseText(filterXml);
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    }
    Element param = document.getRootElement().element("and").element("c").element("param");
    String currentIds = param.getTextTrim();
    Set<String> set = parseSet(currentIds);
    String listOfIds = removeIds(set, ids);
    param.setText(listOfIds);
    return document.asXML();
}
Also used : DocumentException(org.dom4j.DocumentException) Element(org.dom4j.Element) Document(org.dom4j.Document)

Example 92 with DocumentException

use of org.dom4j.DocumentException in project cuba by cuba-platform.

the class AbstractViewRepository method deployViews.

public void deployViews(Reader xml) {
    lock.readLock().lock();
    try {
        checkInitialized();
    } finally {
        lock.readLock().unlock();
    }
    SAXReader reader = new SAXReader();
    Document doc;
    try {
        doc = reader.read(xml);
    } catch (DocumentException e) {
        throw new RuntimeException("Unable to read views xml", e);
    }
    Element rootElem = doc.getRootElement();
    for (Element includeElem : Dom4j.elements(rootElem, "include")) {
        String file = includeElem.attributeValue("file");
        if (!StringUtils.isBlank(file))
            deployViews(file);
    }
    for (Element viewElem : Dom4j.elements(rootElem, "view")) {
        deployView(rootElem, viewElem);
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) Element(org.dom4j.Element) Document(org.dom4j.Document)

Example 93 with DocumentException

use of org.dom4j.DocumentException in project tbd-studio-se by Talend.

the class HadoopConfsResolveUtils method readConfFile.

public static Document readConfFile(File file) {
    Document document = null;
    try {
        SAXReader reader = new SAXReader();
        document = reader.read(file);
    } catch (DocumentException e) {
        ExceptionHandler.process(e);
    }
    return document;
}
Also used : SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) Document(org.dom4j.Document)

Example 94 with DocumentException

use of org.dom4j.DocumentException in project hudson-2.x by hudson.

the class ManagedWindowsServiceLauncher method launch.

@Override
public void launch(final SlaveComputer computer, final TaskListener listener) throws IOException, InterruptedException {
    try {
        final PrintStream logger = listener.getLogger();
        final String name = determineHost(computer);
        logger.println(Messages.ManagedWindowsServiceLauncher_ConnectingTo(name));
        InetAddress host = InetAddress.getByName(name);
        try {
            Socket s = new Socket();
            s.connect(new InetSocketAddress(host, 135), 5000);
            s.close();
        } catch (IOException e) {
            logger.println("Failed to connect to port 135 of " + name + ". Is Windows firewall blocking this port? Or did you disable DCOM service?");
        // again, let it continue.
        }
        JIDefaultAuthInfoImpl auth = createAuth();
        JISession session = JISession.createSession(auth);
        session.setGlobalSocketTimeout(60000);
        SWbemServices services = WMI.connect(session, name);
        String path = computer.getNode().getRemoteFS();
        if (path.indexOf(':') == -1)
            throw new IOException("Remote file system root path of the slave needs to be absolute: " + path);
        SmbFile remoteRoot = new SmbFile("smb://" + name + "/" + path.replace('\\', '/').replace(':', '$') + "/", createSmbAuth());
        if (!remoteRoot.exists())
            remoteRoot.mkdirs();
        try {
            // does Java exist?
            logger.println("Checking if Java exists");
            WindowsRemoteProcessLauncher wrpl = new WindowsRemoteProcessLauncher(name, auth);
            Process proc = wrpl.launch("java -fullversion", "c:\\");
            proc.getOutputStream().close();
            IOUtils.copy(proc.getInputStream(), logger);
            proc.getInputStream().close();
            int exitCode = proc.waitFor();
            if (exitCode == 1) {
                // we'll get this error code if Java is not found
                // TODO enable me when JDK installer based on REST API will be ready
                logger.println("No JDK found on slave node. Please install JDK");
                throw new InterruptedException("No JDK found on slave node. Please install JDK");
            // logger.println("No Java found. Downloading JDK");
            // JDKInstaller jdki = new JDKInstaller("jdk-6u16-oth-JPR@CDS-CDS_Developer",true);
            // URL jdk = jdki.locate(listener, Platform.WINDOWS, CPU.i386);
            // 
            // listener.getLogger().println("Installing JDK");
            // copyStreamAndClose(jdk.openStream(), new SmbFile(remoteRoot, "jdk.exe").getOutputStream());
            // 
            // String javaDir = path + "\\jdk"; // this is where we install Java to
            // 
            // WindowsRemoteFileSystem fs = new WindowsRemoteFileSystem(name, createSmbAuth());
            // fs.mkdirs(javaDir);
            // 
            // jdki.install(new WindowsRemoteLauncher(listener,wrpl), Platform.WINDOWS,
            // fs, listener, javaDir ,path+"\\jdk.exe");
            }
        } catch (Exception e) {
            e.printStackTrace(listener.error("Failed to prepare Java"));
        }
        String id = WindowsSlaveInstaller.generateServiceId(path);
        Win32Service slaveService = services.getService(id);
        if (slaveService == null) {
            logger.println(Messages.ManagedWindowsServiceLauncher_InstallingSlaveService());
            if (!DotNet.isInstalled(2, 0, name, auth)) {
                // abort the launch
                logger.println(Messages.ManagedWindowsServiceLauncher_DotNetRequired());
                return;
            }
            // copy exe
            logger.println(Messages.ManagedWindowsServiceLauncher_CopyingSlaveExe());
            copyStreamAndClose(getClass().getResource("/windows-service/hudson.exe").openStream(), new SmbFile(remoteRoot, "hudson-slave.exe").getOutputStream());
            copySlaveJar(logger, remoteRoot);
            // copy hudson-slave.xml
            logger.println(Messages.ManagedWindowsServiceLauncher_CopyingSlaveXml());
            String xml = WindowsSlaveInstaller.generateSlaveXml(id, "javaw.exe", "-tcp %BASE%\\port.txt");
            copyStreamAndClose(new ByteArrayInputStream(xml.getBytes("UTF-8")), new SmbFile(remoteRoot, "hudson-slave.xml").getOutputStream());
            // install it as a service
            logger.println(Messages.ManagedWindowsServiceLauncher_RegisteringService());
            Document dom = new SAXReader().read(new StringReader(xml));
            Win32Service svc = services.Get("Win32_Service").cast(Win32Service.class);
            int r = svc.Create(id, dom.selectSingleNode("/service/name").getText() + " at " + path, path + "\\hudson-slave.exe", Win32OwnProcess, 0, "Manual", true);
            if (r != 0) {
                listener.error("Failed to create a service: " + svc.getErrorMessage(r));
                return;
            }
            slaveService = services.getService(id);
        } else {
            copySlaveJar(logger, remoteRoot);
        }
        logger.println(Messages.ManagedWindowsServiceLauncher_StartingService());
        slaveService.start();
        // wait until we see the port.txt, but don't do so forever
        logger.println(Messages.ManagedWindowsServiceLauncher_WaitingForService());
        SmbFile portFile = new SmbFile(remoteRoot, "port.txt");
        for (int i = 0; !portFile.exists(); i++) {
            if (i >= 30) {
                listener.error(Messages.ManagedWindowsServiceLauncher_ServiceDidntRespond());
                return;
            }
            Thread.sleep(1000);
        }
        int p = readSmbFile(portFile);
        // connect
        logger.println(Messages.ManagedWindowsServiceLauncher_ConnectingToPort(p));
        final Socket s = new Socket(name, p);
        // ready
        computer.setChannel(new BufferedInputStream(new SocketInputStream(s)), new BufferedOutputStream(new SocketOutputStream(s)), listener.getLogger(), new Listener() {

            @Override
            public void onClosed(Channel channel, IOException cause) {
                afterDisconnect(computer, listener);
            }
        });
    } catch (SmbException e) {
        e.printStackTrace(listener.error(e.getMessage()));
    } catch (JIException e) {
        if (e.getErrorCode() == 5)
            // access denied error
            e.printStackTrace(listener.error(Messages.ManagedWindowsServiceLauncher_AccessDenied()));
        else
            e.printStackTrace(listener.error(e.getMessage()));
    } catch (DocumentException e) {
        e.printStackTrace(listener.error(e.getMessage()));
    }
}
Also used : Listener(hudson.remoting.Channel.Listener) TaskListener(hudson.model.TaskListener) InetSocketAddress(java.net.InetSocketAddress) SAXReader(org.dom4j.io.SAXReader) Win32OwnProcess(org.jvnet.hudson.wmi.Win32Service.Win32OwnProcess) Document(org.dom4j.Document) Win32Service(org.jvnet.hudson.wmi.Win32Service) SmbException(jcifs.smb.SmbException) JISession(org.jinterop.dcom.core.JISession) BufferedInputStream(java.io.BufferedInputStream) DocumentException(org.dom4j.DocumentException) StringReader(java.io.StringReader) BufferedOutputStream(java.io.BufferedOutputStream) JIException(org.jinterop.dcom.common.JIException) PrintStream(java.io.PrintStream) SocketOutputStream(hudson.remoting.SocketOutputStream) JIDefaultAuthInfoImpl(org.jinterop.dcom.common.JIDefaultAuthInfoImpl) Channel(hudson.remoting.Channel) IOException(java.io.IOException) SocketInputStream(hudson.remoting.SocketInputStream) WindowsRemoteProcessLauncher(org.jvnet.hudson.remcom.WindowsRemoteProcessLauncher) SmbException(jcifs.smb.SmbException) DocumentException(org.dom4j.DocumentException) JIException(org.jinterop.dcom.common.JIException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SmbFile(jcifs.smb.SmbFile) ByteArrayInputStream(java.io.ByteArrayInputStream) SWbemServices(org.jvnet.hudson.wmi.SWbemServices) InetAddress(java.net.InetAddress) Socket(java.net.Socket)

Example 95 with DocumentException

use of org.dom4j.DocumentException in project hudson-2.x by hudson.

the class Api method doXml.

/**
 * Exposes the bean as XML.
 */
public void doXml(StaplerRequest req, StaplerResponse rsp, @QueryParameter String xpath, @QueryParameter String wrapper, @QueryParameter int depth) throws IOException, ServletException {
    String[] excludes = req.getParameterValues("exclude");
    if (xpath == null && excludes == null) {
        // serve the whole thing
        rsp.serveExposedBean(req, bean, Flavor.XML);
        return;
    }
    StringWriter sw = new StringWriter();
    // first write to String
    Model p = MODEL_BUILDER.get(bean.getClass());
    p.writeTo(bean, depth, Flavor.XML.createDataWriter(bean, sw));
    // apply XPath
    Object result;
    try {
        Document dom = new SAXReader().read(new StringReader(sw.toString()));
        // apply exclusions
        if (excludes != null) {
            for (String exclude : excludes) {
                List<org.dom4j.Node> list = (List<org.dom4j.Node>) dom.selectNodes(exclude);
                for (org.dom4j.Node n : list) {
                    Element parent = n.getParent();
                    if (parent != null)
                        parent.remove(n);
                }
            }
        }
        if (xpath == null) {
            result = dom;
        } else {
            List list = dom.selectNodes(xpath);
            if (wrapper != null) {
                Element root = DocumentFactory.getInstance().createElement(wrapper);
                for (Object o : list) {
                    if (o instanceof String) {
                        root.addText(o.toString());
                    } else {
                        root.add(((org.dom4j.Node) o).detach());
                    }
                }
                result = root;
            } else if (list.isEmpty()) {
                rsp.setStatus(HttpServletResponse.SC_NOT_FOUND);
                rsp.getWriter().print(Messages.Api_NoXPathMatch(xpath));
                return;
            } else if (list.size() > 1) {
                rsp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                rsp.getWriter().print(Messages.Api_MultipleMatch(xpath, list.size()));
                return;
            } else {
                result = list.get(0);
            }
        }
    } catch (DocumentException e) {
        throw new IOException2(e);
    }
    OutputStream o = rsp.getCompressedOutputStream(req);
    try {
        if (result instanceof CharacterData) {
            rsp.setContentType("text/plain;charset=UTF-8");
            o.write(((CharacterData) result).getText().getBytes("UTF-8"));
            return;
        }
        if (result instanceof String || result instanceof Number || result instanceof Boolean) {
            rsp.setContentType("text/plain;charset=UTF-8");
            o.write(result.toString().getBytes("UTF-8"));
            return;
        }
        // otherwise XML
        rsp.setContentType("application/xml;charset=UTF-8");
        new XMLWriter(o).write(result);
    } finally {
        o.close();
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) OutputStream(java.io.OutputStream) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) StringWriter(java.io.StringWriter) CharacterData(org.dom4j.CharacterData) DocumentException(org.dom4j.DocumentException) StringReader(java.io.StringReader) List(java.util.List) IOException2(hudson.util.IOException2)

Aggregations

DocumentException (org.dom4j.DocumentException)123 Document (org.dom4j.Document)80 SAXReader (org.dom4j.io.SAXReader)73 Element (org.dom4j.Element)51 IOException (java.io.IOException)45 File (java.io.File)27 InputStream (java.io.InputStream)21 StringReader (java.io.StringReader)15 InputStreamReader (java.io.InputStreamReader)11 ArrayList (java.util.ArrayList)10 XMLWriter (org.dom4j.io.XMLWriter)9 InputSource (org.xml.sax.InputSource)9 FileInputStream (java.io.FileInputStream)7 URL (java.net.URL)7 List (java.util.List)7 Node (org.dom4j.Node)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 FileNotFoundException (java.io.FileNotFoundException)5 Reader (java.io.Reader)5