Search in sources :

Example 46 with DocumentException

use of org.dom4j.DocumentException in project zm-mailbox by Zimbra.

the class LocalConfigCLI method exec.

private void exec(String[] args) {
    CommandLine cl = null;
    CommandLineParser parser = new GnuParser();
    try {
        cl = parser.parse(mOptions, args);
    } catch (ParseException pe) {
        Logging.error("Failed to parse command line: " + pe);
        System.exit(1);
    }
    if (cl.hasOption("q")) {
        Logging.setQuietMode(true);
    }
    if (cl.hasOption("h")) {
        usage();
    }
    // Load known keys from BackupLC if available
    loadExtensionLC("com.zimbra.cs.backup.BackupLC");
    // Load known keys from ZimbraOpenOfficeExt if available
    loadExtensionLC("com.zimbra.openoffice.config.OpenOfficeLC");
    // Load known keys from ZimbraVoice if available
    loadExtensionLC("com.zimbra.cs.voice.VoiceLC");
    // info/docs for supported keys
    if (cl.hasOption("i")) {
        checkCompatibleOptions("i", "q", cl);
        LocalConfig.printDoc(System.out, cl.getArgs(), false);
        return;
    }
    // info/docs for all keys (hidden option)
    if (cl.hasOption("all")) {
        checkCompatibleOptions("all", "q", cl);
        LocalConfig.printDoc(System.out, cl.getArgs(), true);
        return;
    }
    LocalConfig lc = null;
    try {
        lc = new LocalConfig(cl.getOptionValue("c"));
    } catch (DocumentException de) {
        error("failed when reading config file", de);
    } catch (ConfigException ce) {
        error("failed with error in config file", ce);
    }
    // edit
    if (cl.hasOption("e")) {
        checkNotRoot("-e");
        checkCompatibleOptions("e", "qfrc", cl);
        String[] av = cl.getArgs();
        if (av == null || av.length == 0) {
            error("insufficient arguments", null);
        }
        for (int i = 0; i < av.length; i++) {
            String key = null;
            String value = null;
            if (cl.hasOption("r")) {
                key = av[i];
                value = RandomPassword.generate();
            } else {
                int eqidx = av[i].indexOf("=");
                if (eqidx <= 0) {
                    // <= 0 also catches first char being =, ie no key specified
                    error("argument '" + av[i] + "' not in key=value form", null);
                }
                key = av[i].substring(0, eqidx);
                value = av[i].substring(eqidx + 1, av[i].length());
            }
            if (KnownKey.needForceToEdit(key) && !cl.hasOption("f")) {
                error("can not edit key " + key, null);
            }
            lc.set(key, value);
        }
        try {
            lc.save();
        } catch (Exception e) {
            error("save to " + lc.getConfigFile() + " failed", e);
        }
        return;
    }
    // unset
    if (cl.hasOption("u")) {
        checkNotRoot("-u");
        checkCompatibleOptions("u", "qfc", cl);
        String[] av = cl.getArgs();
        if (av == null || av.length == 0) {
            error("insufficient arguments", null);
        }
        for (int i = 0; i < av.length; i++) {
            String key = av[i];
            if (!lc.isSet(key)) {
                error("key " + key + " is not set", null);
            }
            lc.remove(key);
        }
        try {
            lc.save();
        } catch (Exception e) {
            error("save to " + lc.getConfigFile() + " failed", e);
        }
        return;
    }
    // show path
    if (cl.hasOption("p")) {
        checkCompatibleOptions("p", "qc", cl);
        System.out.println(lc.getConfigFile());
        return;
    }
    if (cl.hasOption("l")) {
        // reset logging and run native lib load
        CliUtil.toolSetup("WARN");
        try {
            reload();
        } catch (ServiceException e) {
            if (e.getCause() instanceof ConnectException) {
                error("'" + Provisioning.SERVICE_MAILBOX + "' service is not running", null);
            } else {
                error(e.getMessage(), e);
            }
        }
        return;
    }
    // print values
    String format = cl.getOptionValue("m");
    ConfigWriter cwriter = null;
    try {
        cwriter = ConfigWriter.getInstance(format, cl.hasOption("x"), !cl.hasOption("s"));
    } catch (ConfigException iae) {
        error("failed to create writer " + format, iae);
    }
    try {
        // changed
        if (cl.hasOption("n")) {
            checkCompatibleOptions("n", "qscmx", cl);
            lc.printChanged(System.out, cwriter, cl.getArgs());
            return;
        }
        // default
        if (cl.hasOption("d")) {
            checkCompatibleOptions("d", "qscmx", cl);
            lc.printDefaults(System.out, cwriter, cl.getArgs());
            return;
        }
        // current
        checkCompatibleOptions("", "qscmx", cl);
        lc.print(System.out, cwriter, cl.getArgs());
    } catch (Exception e) {
        error("exception occurred when printing", e);
    }
}
Also used : LocalConfig(com.zimbra.common.localconfig.LocalConfig) GnuParser(org.apache.commons.cli.GnuParser) ConfigException(com.zimbra.common.localconfig.ConfigException) ServiceException(com.zimbra.common.service.ServiceException) DocumentException(org.dom4j.DocumentException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) ConfigException(com.zimbra.common.localconfig.ConfigException) ZClientException(com.zimbra.common.zclient.ZClientException) ConfigWriter(com.zimbra.common.localconfig.ConfigWriter) CommandLine(org.apache.commons.cli.CommandLine) ServiceException(com.zimbra.common.service.ServiceException) DocumentException(org.dom4j.DocumentException) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) ConnectException(java.net.ConnectException)

Example 47 with DocumentException

use of org.dom4j.DocumentException in project zm-mailbox by Zimbra.

the class MailPort method postModify.

@Override
public void postModify(CallbackContext context, String attrName, Entry entry) {
    super.postModify(context, attrName, entry);
    if (entry instanceof Server) {
        Server localServer = null;
        try {
            localServer = Provisioning.getInstance().getLocalServer();
            if (entry == localServer) {
                String port = localServer.getAttr(attrName);
                LocalConfig lc = new LocalConfig(null);
                lc.set(LC.zimbra_mail_service_port.key(), port);
                lc.save();
            }
        } catch (ServiceException | DocumentException | ConfigException | NumberFormatException | IOException e) {
            ZimbraLog.misc.warn("Unable to update LC.zimbra_mail_port due to Exception", e);
        }
    }
}
Also used : Server(com.zimbra.cs.account.Server) ServiceException(com.zimbra.common.service.ServiceException) LocalConfig(com.zimbra.common.localconfig.LocalConfig) DocumentException(org.dom4j.DocumentException) ConfigException(com.zimbra.common.localconfig.ConfigException) IOException(java.io.IOException)

Example 48 with DocumentException

use of org.dom4j.DocumentException in project Openfire by igniterealtime.

the class HttpSessionManager method createSession.

/**
 * Creates an HTTP binding session which will allow a user to exchange packets with Openfire.
 *
 * @param body the body element that was sent containing the request for a new session.
 * @param connection the HTTP connection object which abstracts the individual connections to
 * Openfire over the HTTP binding protocol. The initial session creation response is returned to
 * this connection.
 * @return the created HTTP session.
 *
 * @throws UnauthorizedException if the Openfire server is currently in an uninitialized state.
 * Either shutting down or starting up.
 * @throws HttpBindException when there is an internal server error related to the creation of
 * the initial session creation response.
 * @throws UnknownHostException if no IP address for the peer could be found
 */
public HttpSession createSession(HttpBindBody body, HttpConnection connection) throws UnauthorizedException, HttpBindException, UnknownHostException {
    // TODO Check if IP address is allowed to connect to the server
    final Duration wait = body.getWait().compareTo(MAX_WAIT.getValue()) > 0 ? MAX_WAIT.getValue() : body.getWait();
    final Duration defaultInactivityTimeout;
    if (wait.isZero() || body.getHold() == 0) {
        // Session will be polling.
        defaultInactivityTimeout = POLLING_INACTIVITY_TIMEOUT.getValue();
    } else {
        defaultInactivityTimeout = INACTIVITY_TIMEOUT.getValue();
    }
    HttpSession session = createSession(connection, Locale.forLanguageTag(body.getLanguage()), wait, body.getHold(), connection.isSecure(), POLLING_INTERVAL.getValue(), MAX_REQUESTS.getValue(), MAX_PAUSE.getValue(), defaultInactivityTimeout, body.getMajorVersion(), body.getMinorVersion());
    session.resetInactivityTimeout();
    connection.setSession(session);
    try {
        connection.deliverBody(createSessionCreationResponse(session), true);
    } catch (HttpConnectionClosedException | DocumentException | IOException e) {
        Log.error("Error creating session.", e);
        throw new HttpBindException("Internal server error", BoshBindingError.internalServerError);
    }
    return session;
}
Also used : DocumentException(org.dom4j.DocumentException) Duration(java.time.Duration) IOException(java.io.IOException)

Example 49 with DocumentException

use of org.dom4j.DocumentException in project wechat by dllwh.

the class XmlUtils method readXML.

/**
 * @方法名称: readXML
 * @方法描述: 从文件获得DOM对象
 *
 * @param xmlFile
 *            XML文件对象
 * @return DOM对象
 * @throws DocumentException
 */
public static Document readXML(File xmlFile) throws DocumentException {
    if (xmlFile == null) {
        throw new NullPointerException("Xml file is null !");
    }
    if (xmlFile.exists() == false) {
        throw new RuntimeExceptionHelper("File [" + xmlFile.getAbsolutePath() + "] not a exist!");
    }
    if (xmlFile.isFile() == false) {
        throw new RuntimeExceptionHelper("[" + xmlFile.getAbsolutePath() + "] not a file!");
    }
    try {
        xmlFile = xmlFile.getCanonicalFile();
        final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        final DocumentBuilder builder = dbf.newDocumentBuilder();
        return builder.parse(xmlFile);
    } catch (Exception e) {
        throw new RuntimeExceptionHelper("Parse xml file [" + xmlFile.getAbsolutePath() + "] error!", e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) RuntimeExceptionHelper(com.cdeledu.common.exception.RuntimeExceptionHelper) DocumentException(org.dom4j.DocumentException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 50 with DocumentException

use of org.dom4j.DocumentException in project wechat by dllwh.

the class XmlUtils method parseDoc.

/*--------------------------私有方法 end-------------------------------*/
/*--------------------------公有方法 start-------------------------------*/
/**
 * @方法:将String类型的XML转换为XML文档
 * @创建人:独泪了无痕
 * @param xmlStr
 *            XML字符串
 * @return XML文档
 * @throws DocumentException
 */
public static Document parseDoc(String xmlStr) {
    if (StringUtils.isBlank(xmlStr)) {
        throw new RuntimeExceptionHelper("Xml content string is empty !");
    }
    xmlStr = cleanInvalid(xmlStr);
    final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        final DocumentBuilder builder = dbf.newDocumentBuilder();
        return builder.parse(new InputSource(StringUtilHelper.getReader(xmlStr)));
    } catch (Exception e) {
        throw new RuntimeExceptionHelper("Parse xml file [" + xmlStr + "] error!", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) RuntimeExceptionHelper(com.cdeledu.common.exception.RuntimeExceptionHelper) DocumentException(org.dom4j.DocumentException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

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