Search in sources :

Example 16 with MCRConfigurationException

use of org.mycore.common.config.MCRConfigurationException in project mycore by MyCoRe-Org.

the class MCRSwordUtil method createTempFileFromStream.

/**
 * Stores stream to temp file and checks md5
 *
 * @param inputStream the stream which holds the File
 * @param checkMd5    the md5 to compare with (or null if no md5 check is needed)
 * @return the path to the temp file
 * @throws IOException if md5 does mismatch or if stream could not be read
 */
public static Path createTempFileFromStream(String fileName, InputStream inputStream, String checkMd5) throws IOException {
    MCRSession currentSession = MCRSessionMgr.getCurrentSession();
    if (currentSession.isTransactionActive()) {
        currentSession.commitTransaction();
    }
    final Path zipTempFile = Files.createTempFile("swordv2_", fileName);
    MessageDigest md5Digest = null;
    if (checkMd5 != null) {
        try {
            md5Digest = MessageDigest.getInstance("MD5");
            inputStream = new DigestInputStream(inputStream, md5Digest);
        } catch (NoSuchAlgorithmException e) {
            currentSession.beginTransaction();
            throw new MCRConfigurationException("No MD5 available!", e);
        }
    }
    Files.copy(inputStream, zipTempFile, StandardCopyOption.REPLACE_EXISTING);
    if (checkMd5 != null) {
        final String md5String = MCRUtils.toHexString(md5Digest.digest());
        if (!md5String.equals(checkMd5)) {
            currentSession.beginTransaction();
            throw new IOException("MD5 mismatch, expected " + checkMd5 + " got " + md5String);
        }
    }
    currentSession.beginTransaction();
    return zipTempFile;
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRSession(org.mycore.common.MCRSession) DigestInputStream(java.security.DigestInputStream) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Example 17 with MCRConfigurationException

use of org.mycore.common.config.MCRConfigurationException in project mycore by MyCoRe-Org.

the class MCRSimpleFCTDetector method addRule.

/**
 * Adds a detection rule from the file content type definition XML file. The
 * detector parses the <rules> element provided with each content type
 * in the file content types XML definition.
 *
 * @param type
 *            the file content type the rule is for
 * @param xRules
 *            the rules XML element containing the rules for detecting that
 *            type
 */
public void addRule(MCRFileContentType type, Element xRules) {
    Vector<MCRDetectionRule> rules = new Vector<>();
    rulesTable.put(type, rules);
    typesList.add(type);
    try {
        List extensions = xRules.getChildren("extension");
        for (Object extension : extensions) {
            Element elem = (Element) extension;
            double score = elem.getAttribute("score").getDoubleValue();
            String ext = elem.getTextTrim();
            rules.addElement(new MCRExtensionRule(ext, score));
        }
        List patterns = xRules.getChildren("pattern");
        for (Object pattern1 : patterns) {
            Element elem = (Element) pattern1;
            double score = elem.getAttribute("score").getDoubleValue();
            int offset = elem.getAttribute("offset").getIntValue();
            String format = elem.getAttributeValue("format");
            String pattern = elem.getTextTrim();
            rules.addElement(new MCRPatternRule(pattern, format, offset, score));
        }
        List doctypes = xRules.getChildren("doctype");
        for (Object doctype1 : doctypes) {
            Element elem = (Element) doctype1;
            double score = elem.getAttribute("score").getDoubleValue();
            String doctype = elem.getTextTrim();
            rules.addElement(new MCRDoctypeRule(doctype, score));
        }
        List strings = xRules.getChildren("string");
        for (Object string1 : strings) {
            Element elem = (Element) string1;
            double score = elem.getAttribute("score").getDoubleValue();
            String string = elem.getTextTrim();
            rules.addElement(new MCRStringRule(string, score));
        }
    } catch (Exception exc) {
        String msg = "Error parsing detection rules for file content type " + type.getLabel();
        throw new MCRConfigurationException(msg, exc);
    }
}
Also used : Element(org.jdom2.Element) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) List(java.util.List) Vector(java.util.Vector)

Example 18 with MCRConfigurationException

use of org.mycore.common.config.MCRConfigurationException in project mycore by MyCoRe-Org.

the class MCRContentStoreFactory method initStore.

private static void initStore(String storeID) {
    try {
        String storeClass = CONFIG_PREFIX + storeID + CLASS_SUFFIX;
        LOGGER.debug("getting StoreClass: {}", storeClass);
        MCRContentStore s = MCRConfiguration.instance().getInstanceOf(storeClass);
        s.init(storeID);
        STORES.put(storeID, s);
    } catch (Exception ex) {
        String msg = "Could not load MCRContentStore with store ID = " + storeID;
        throw new MCRConfigurationException(msg, ex);
    }
}
Also used : MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException)

Example 19 with MCRConfigurationException

use of org.mycore.common.config.MCRConfigurationException in project mycore by MyCoRe-Org.

the class MCRLDAPClient method updateUserProperties.

public boolean updateUserProperties(MCRUser user) throws NamingException {
    String userName = user.getUserName();
    boolean userChanged = false;
    if ((defaultGroup != null) && (!user.isUserInRole((defaultGroup.getName())))) {
        LOGGER.info("User {} add to group {}", userName, defaultGroup);
        userChanged = true;
        user.assignRole(defaultGroup.getName());
    }
    // Get user properties from LDAP server
    DirContext ctx = new InitialDirContext(ldapEnv);
    try {
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        NamingEnumeration<SearchResult> results = ctx.search(baseDN, String.format(Locale.ROOT, uidFilter, userName), controls);
        while (results.hasMore()) {
            SearchResult searchResult = results.next();
            Attributes attributes = searchResult.getAttributes();
            for (NamingEnumeration<String> attributeIDs = attributes.getIDs(); attributeIDs.hasMore(); ) {
                String attributeID = attributeIDs.next();
                Attribute attribute = attributes.get(attributeID);
                for (NamingEnumeration<?> values = attribute.getAll(); values.hasMore(); ) {
                    String attributeValue = values.next().toString();
                    LOGGER.debug("{}={}", attributeID, attributeValue);
                    if (attributeID.equals(mapName) && (user.getRealName() == null)) {
                        attributeValue = formatName(attributeValue);
                        LOGGER.info("User {} name = {}", userName, attributeValue);
                        user.setRealName(attributeValue);
                        userChanged = true;
                    }
                    if (attributeID.equals(mapEMail) && (user.getEMailAddress() == null)) {
                        LOGGER.info("User {} e-mail = {}", userName, attributeValue);
                        user.setEMail(attributeValue);
                        userChanged = true;
                    }
                    String groupMapping = "MCR.user2.LDAP.Mapping.Group." + attributeID + "." + attributeValue;
                    String group = MCRConfiguration.instance().getString(groupMapping, null);
                    if ((group != null) && (!user.isUserInRole((group)))) {
                        LOGGER.info("User {} add to group {}", userName, group);
                        user.assignRole(group);
                        userChanged = true;
                    }
                }
            }
        }
    } catch (NameNotFoundException ex) {
        String msg = "LDAP base name not found: " + ex.getMessage() + " " + ex.getExplanation();
        throw new MCRConfigurationException(msg, ex);
    } catch (NamingException ex) {
        String msg = "Exception accessing LDAP server";
        throw new MCRUsageException(msg, ex);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception ignored) {
            }
        }
    }
    return userChanged;
}
Also used : Attribute(javax.naming.directory.Attribute) NameNotFoundException(javax.naming.NameNotFoundException) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) MCRUsageException(org.mycore.common.MCRUsageException) NamingException(javax.naming.NamingException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) NameNotFoundException(javax.naming.NameNotFoundException) MCRUsageException(org.mycore.common.MCRUsageException) SearchControls(javax.naming.directory.SearchControls) NamingException(javax.naming.NamingException)

Example 20 with MCRConfigurationException

use of org.mycore.common.config.MCRConfigurationException in project mycore by MyCoRe-Org.

the class MCRServletJob method isLocal.

/**
 * returns true if the current http request was issued from the local host *
 */
public boolean isLocal() {
    try {
        String serverName = theRequest.getServerName();
        String serverIP = InetAddress.getByName(serverName).getHostAddress();
        String remoteIP = MCRFrontendUtil.getRemoteAddr(theRequest);
        return remoteIP.equals(serverIP) || remoteIP.equals("127.0.0.1");
    } catch (Exception ex) {
        String msg = "Exception while testing if http request was from local host";
        throw new MCRConfigurationException(msg, ex);
    }
}
Also used : MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException)

Aggregations

MCRConfigurationException (org.mycore.common.config.MCRConfigurationException)30 MCRException (org.mycore.common.MCRException)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 IOException (java.io.IOException)5 MCRConfiguration (org.mycore.common.config.MCRConfiguration)5 Properties (java.util.Properties)3 File (java.io.File)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Path (java.nio.file.Path)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Arrays (java.util.Arrays)2 StringTokenizer (java.util.StringTokenizer)2 SAXParser (javax.xml.parsers.SAXParser)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ByteBuffer (java.nio.ByteBuffer)1 NotDirectoryException (java.nio.file.NotDirectoryException)1