Search in sources :

Example 41 with JDOMException

use of org.jdom2.JDOMException in project ParadoxosModManager by ThibautSF.

the class MyXML method readFile.

// public MyXML(){}
/**
 * @param file
 * @throws IOException
 * @throws JDOMException
 * @throws Exception
 */
public void readFile(String file) throws JDOMException, IOException {
    SAXBuilder sxb = new SAXBuilder();
    File xml = new File(file);
    if (xml.exists()) {
        document = sxb.build(xml);
        root = document.getRootElement();
    } else {
        root = new Element(USER_LISTS);
        document = new Document(root);
    }
    // Init for export lists
    root_exported = new Element(EXPORTED_LIST);
    root_exported.setAttribute(GAME_ID, ModManager.STEAM_ID.toString());
    document_exported = new Document(root_exported);
    this.file = file;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File)

Example 42 with JDOMException

use of org.jdom2.JDOMException in project xwiki-platform by xwiki.

the class DefaultEntityResourceActionLister method initialize.

@Override
public void initialize() throws InitializationException {
    // Parse the Struts config file (struts-config.xml) to extract all available actions
    List<String> actionNames = new ArrayList<>();
    SAXBuilder builder = new SAXBuilder();
    // Make sure we don't require an Internet Connection to parse the Struts config file!
    builder.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            return new InputSource(new StringReader(""));
        }
    });
    // Step 1: Get a stream on the Struts config file if it exists
    InputStream strutsConfigStream = this.environment.getResourceAsStream(getStrutsConfigResource());
    if (strutsConfigStream != null) {
        // Step 2: Parse the Strust config file, looking for action names
        Document document;
        try {
            document = builder.build(strutsConfigStream);
        } catch (JDOMException | IOException e) {
            throw new InitializationException(String.format("Failed to parse Struts Config file [%s]", getStrutsConfigResource()), e);
        }
        Element mappingElement = document.getRootElement().getChild("action-mappings");
        for (Element element : mappingElement.getChildren("action")) {
            // We extract the action name from the path mapping. Note that we cannot use the "name" attribute since
            // it's not reliable (it's not unique) and for example the sanveandcontinue action uses "save" as its
            // "name" element value.
            actionNames.add(StringUtils.strip(element.getAttributeValue("path"), "/"));
        }
    }
    this.strutsActionNames = actionNames;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) InitializationException(org.xwiki.component.phase.InitializationException) SAXException(org.xml.sax.SAXException) StringReader(java.io.StringReader)

Example 43 with JDOMException

use of org.jdom2.JDOMException in project ldapchai by ldapchai.

the class NmasCrFactory method readNmasAssignedChallengeSetPolicy.

private static ChallengeSet readNmasAssignedChallengeSetPolicy(final ChaiProvider provider, final String challengeSetDN, final Locale locale, final String identifer) throws ChaiUnavailableException, ChaiOperationException, ChaiValidationException {
    if (challengeSetDN == null || challengeSetDN.length() < 1) {
        LOGGER.trace("challengeSetDN is null, return null for readNmasAssignedChallengeSetPolicy()");
        return null;
    }
    final List<Challenge> challenges = new ArrayList<>();
    final ChaiEntry csSetEntry = provider.getEntryFactory().newChaiEntry(challengeSetDN);
    final Map<String, String> allValues = csSetEntry.readStringAttributes(Collections.emptySet());
    final String requiredQuestions = allValues.get("nsimRequiredQuestions");
    final String randomQuestions = allValues.get("nsimRandomQuestions");
    try {
        if (requiredQuestions != null && requiredQuestions.length() > 0) {
            challenges.addAll(NmasResponseSet.parseNmasPolicyXML(requiredQuestions, locale));
        }
        if (randomQuestions != null && randomQuestions.length() > 0) {
            challenges.addAll(NmasResponseSet.parseNmasPolicyXML(randomQuestions, locale));
        }
    } catch (JDOMException e) {
        LOGGER.debug(e);
    } catch (IOException e) {
        LOGGER.debug(e);
    }
    final int minRandQuestions = StringHelper.convertStrToInt(allValues.get("nsimNumberRandomQuestions"), 0);
    return new ChaiChallengeSet(challenges, minRandQuestions, locale, identifer);
}
Also used : ArrayList(java.util.ArrayList) ChaiChallengeSet(com.novell.ldapchai.cr.ChaiChallengeSet) ChaiEntry(com.novell.ldapchai.ChaiEntry) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) Challenge(com.novell.ldapchai.cr.Challenge)

Example 44 with JDOMException

use of org.jdom2.JDOMException in project ldapchai by ldapchai.

the class NmasResponseSet method readNmasUserResponseSet.

static NmasResponseSet readNmasUserResponseSet(final ChaiUser theUser) throws ChaiUnavailableException, ChaiValidationException {
    final GetLoginConfigRequest request = new GetLoginConfigRequest();
    request.setObjectDN(theUser.getEntryDN());
    request.setTag("ChallengeResponseQuestions");
    request.setMethodID(NMASChallengeResponse.METHOD_ID);
    request.setMethodIDLen(NMASChallengeResponse.METHOD_ID.length * 4);
    try {
        final ExtendedResponse response = theUser.getChaiProvider().extendedOperation(request);
        final byte[] responseValue = response.getEncodedValue();
        if (responseValue == null) {
            return null;
        }
        final String xmlString = new String(responseValue, "UTF8");
        LOGGER.trace("[parse v3]: read ChallengeResponseQuestions from server: " + xmlString);
        ChallengeSet cs = null;
        int parseAttempts = 0;
        final StringBuilder parsingErrorMsg = new StringBuilder();
        {
            final int beginIndex = xmlString.indexOf("<");
            if (beginIndex > 0) {
                try {
                    parseAttempts++;
                    final String xmlSubstring = xmlString.substring(beginIndex, xmlString.length());
                    LOGGER.trace("attempting parse of index stripped value: " + xmlSubstring);
                    cs = parseNmasUserResponseXML(xmlSubstring);
                    LOGGER.trace("successfully parsed nmas ChallengeResponseQuestions response after index " + beginIndex);
                } catch (JDOMException e) {
                    if (parsingErrorMsg.length() > 0) {
                        parsingErrorMsg.append(", ");
                    }
                    parsingErrorMsg.append("error parsing index stripped value: ").append(e.getMessage());
                    LOGGER.trace("unable to parse index stripped ChallengeResponseQuestions nmas response; error: " + e.getMessage());
                }
            }
        }
        if (cs == null) {
            if (xmlString.startsWith("<?xml")) {
                try {
                    parseAttempts++;
                    cs = parseNmasUserResponseXML(xmlString);
                } catch (JDOMException e) {
                    parsingErrorMsg.append("error parsing raw value: ").append(e.getMessage());
                    LOGGER.trace("unable to parse raw ChallengeResponseQuestions nmas response; will retry after stripping header; error: " + e.getMessage());
                }
                LOGGER.trace("successfully parsed full nmas ChallengeResponseQuestions response");
            }
        }
        if (cs == null) {
            if (xmlString.length() > 16) {
                // first 16 bytes are non-xml header.
                final String strippedXml = xmlString.substring(16);
                try {
                    parseAttempts++;
                    cs = parseNmasUserResponseXML(strippedXml);
                    LOGGER.trace("successfully parsed full nmas ChallengeResponseQuestions response");
                } catch (JDOMException e) {
                    if (parsingErrorMsg.length() > 0) {
                        parsingErrorMsg.append(", ");
                    }
                    parsingErrorMsg.append("error parsing header stripped value: ").append(e.getMessage());
                    LOGGER.trace("unable to parse stripped ChallengeResponseQuestions nmas response; error: " + e.getMessage());
                }
            }
        }
        if (cs == null) {
            final String logMsg = "unable to parse nmas ChallengeResponseQuestions: " + parsingErrorMsg;
            if (parseAttempts > 0 && xmlString.length() > 16) {
                LOGGER.error(logMsg);
            } else {
                LOGGER.trace(logMsg);
            }
            return null;
        }
        final Map<Challenge, String> crMap = new HashMap<Challenge, String>();
        for (final Challenge loopChallenge : cs.getChallenges()) {
            crMap.put(loopChallenge, null);
        }
        return new NmasResponseSet(crMap, cs.getLocale(), cs.getMinRandomRequired(), AbstractResponseSet.STATE.READ, theUser, cs.getIdentifier());
    } catch (ChaiOperationException e) {
        LOGGER.error("error reading nmas user response for " + theUser.getEntryDN() + ", error: " + e.getMessage());
    } catch (IOException e) {
        LOGGER.error("error reading nmas user response for " + theUser.getEntryDN() + ", error: " + e.getMessage());
    }
    return null;
}
Also used : ChallengeSet(com.novell.ldapchai.cr.ChallengeSet) ChaiChallengeSet(com.novell.ldapchai.cr.ChaiChallengeSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) GetLoginConfigRequest(com.novell.security.nmas.jndi.ldap.ext.GetLoginConfigRequest) Challenge(com.novell.ldapchai.cr.Challenge) ChaiChallenge(com.novell.ldapchai.cr.ChaiChallenge) ExtendedResponse(javax.naming.ldap.ExtendedResponse) ChaiOperationException(com.novell.ldapchai.exception.ChaiOperationException)

Example 45 with JDOMException

use of org.jdom2.JDOMException in project ldapchai by ldapchai.

the class NmasResponseSet method parseNmasPolicyXML.

static List<Challenge> parseNmasPolicyXML(final String str, final Locale locale) throws IOException, JDOMException {
    final List<Challenge> returnList = new ArrayList<Challenge>();
    final Reader xmlreader = new StringReader(str);
    final SAXBuilder builder = new SAXBuilder();
    final Document doc = builder.build(xmlreader);
    final boolean required = doc.getRootElement().getName().equals("RequiredQuestions");
    for (final Iterator questionIterator = doc.getDescendants(new ElementFilter("Question")); questionIterator.hasNext(); ) {
        final Element loopQ = (Element) questionIterator.next();
        final int maxLength = StringHelper.convertStrToInt(loopQ.getAttributeValue("MaxLength"), 255);
        final int minLength = StringHelper.convertStrToInt(loopQ.getAttributeValue("MinLength"), 1);
        final String challengeText = readDisplayString(loopQ, locale);
        final Challenge challenge = new ChaiChallenge(required, challengeText, minLength, maxLength, true, 0, false);
        returnList.add(challenge);
    }
    for (Iterator iter = doc.getDescendants(new ElementFilter("UserDefined")); iter.hasNext(); ) {
        final Element loopQ = (Element) iter.next();
        final int maxLength = StringHelper.convertStrToInt(loopQ.getAttributeValue("MaxLength"), 255);
        final int minLength = StringHelper.convertStrToInt(loopQ.getAttributeValue("MinLength"), 1);
        final Challenge challenge = new ChaiChallenge(required, null, minLength, maxLength, false, 0, false);
        returnList.add(challenge);
    }
    return returnList;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) Document(org.jdom2.Document) Challenge(com.novell.ldapchai.cr.Challenge) ChaiChallenge(com.novell.ldapchai.cr.ChaiChallenge) ElementFilter(org.jdom2.filter.ElementFilter) StringReader(java.io.StringReader) Iterator(java.util.Iterator) ChaiChallenge(com.novell.ldapchai.cr.ChaiChallenge)

Aggregations

Element (org.jdom2.Element)154 Document (org.jdom2.Document)113 JDOMException (org.jdom2.JDOMException)89 IOException (java.io.IOException)75 SAXBuilder (org.jdom2.input.SAXBuilder)67 Test (org.junit.Test)36 File (java.io.File)32 ArrayList (java.util.ArrayList)22 InputStream (java.io.InputStream)17 Attribute (org.jdom2.Attribute)16 StringReader (java.io.StringReader)15 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)14 HashMap (java.util.HashMap)13 XMLOutputter (org.jdom2.output.XMLOutputter)13 SAXException (org.xml.sax.SAXException)13 URL (java.net.URL)12 XmlFile (jmri.jmrit.XmlFile)12 List (java.util.List)11 MCRObject (org.mycore.datamodel.metadata.MCRObject)11 MCRException (org.mycore.common.MCRException)10