Search in sources :

Example 31 with MCRException

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

the class MCRSimpleFCTDetector method parseDocumentType.

/**
 * Copy from MCRLayoutServlet, messages changed from MCRLayoutServlet to
 * MCRSimpleFCTDetector Try to detect doctype of xml data
 *
 * @param in
 *            xml data
 *
 * @return detected doctype
 */
protected String parseDocumentType(InputStream in) {
    SAXParser parser = null;
    try {
        parser = SAXParserFactory.newInstance().newSAXParser();
    } catch (Exception ex) {
        String msg = "Could not build a SAX Parser for processing XML input";
        throw new MCRConfigurationException(msg, ex);
    }
    final Properties detected = new Properties();
    final String forcedInterrupt = "mcr.forced.interrupt";
    DefaultHandler handler = new DefaultHandler() {

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) {
            logger.debug("MCRSimpleFCTDetector detected root element = {}", qName);
            detected.setProperty("docType", qName);
            throw new MCRException(forcedInterrupt);
        }
    };
    try {
        parser.parse(new InputSource(in), handler);
    } catch (Exception ex) {
        if (!forcedInterrupt.equals(ex.getMessage())) {
            String msg = "Error while detecting XML document type from input source";
            throw new MCRException(msg, ex);
        }
    }
    return detected.getProperty("docType");
}
Also used : MCRException(org.mycore.common.MCRException) InputSource(org.xml.sax.InputSource) Attributes(org.xml.sax.Attributes) SAXParser(javax.xml.parsers.SAXParser) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) Properties(java.util.Properties) MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 32 with MCRException

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

the class MCRCStoreVFS method init.

@Override
public void init(String storeId) {
    super.init(storeId);
    uri = MCRConfiguration.instance().getString(storeConfigPrefix + "URI");
    String check = MCRConfiguration.instance().getString(storeConfigPrefix + "StrictHostKeyChecking", "no");
    try {
        fsManager = VFS.getManager();
        opts = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, check);
        FileObject baseDir = getBase();
        // Create a folder, if it does not exist or throw an
        // exception, if baseDir is not a folder
        baseDir.createFolder();
        if (!baseDir.isWriteable()) {
            String msg = "Content store base directory is not writeable: " + uri;
            throw new MCRConfigurationException(msg);
        }
    } catch (FileSystemException ex) {
        throw new MCRException(ex.getCode(), ex);
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) FileObject(org.apache.commons.vfs2.FileObject) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions)

Example 33 with MCRException

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

the class MCRContentStoreFactory method buildExtender.

/**
 * If the MCRContentStore of the MCRFile given provides an
 * MCRAudioVideoExtender implementation, this method creates and initializes
 * the MCRAudioVideoExtender instance for the MCRFile. The instance that is
 * returned is configured by the property
 * <tt>MCR.IFS.AVExtender.&lt;StoreID&gt;.Class</tt> in mycore.properties.
 *
 * @param file
 *            the non-null MCRFile that should get an MCRAudioVideoExtender
 * @return the MCRAudioVideoExtender for the MCRFile given, or null
 * @throws MCRConfigurationException
 *             if the MCRAudioVideoExtender implementation class could not
 *             be loaded
 */
static MCRAudioVideoExtender buildExtender(MCRFile file) {
    if (file == null || !providesAudioVideoExtender(file.getStoreID())) {
        return null;
    }
    Class<? extends MCRAudioVideoExtender> cl = getExtenderClass(file.getStoreID());
    try {
        MCRAudioVideoExtender ext = cl.getDeclaredConstructor().newInstance();
        ext.init(file);
        return ext;
    } catch (Exception exc) {
        if (exc instanceof MCRException) {
            throw (MCRException) exc;
        }
        String msg = "Could not build MCRAudioVideoExtender instance";
        throw new MCRConfigurationException(msg, exc);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException)

Example 34 with MCRException

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

the class MCRURNOAIRegistrationService method isRegistered.

@Override
public boolean isRegistered(MCRObjectID id, String additional) {
    boolean registered = super.isRegistered(id, additional);
    if (registered)
        return true;
    if (!isCreated(id, additional)) {
        return false;
    }
    // URN is created. Now we need to check if it is resolvable
    MCRPI mcrpi = getTableEntry(id, additional);
    MCRDNBURN dnburn = new MCRDNBURNParser().parse(mcrpi.getIdentifier()).orElseThrow(() -> new MCRException("Cannot parse Identifier from table: " + mcrpi.getIdentifier()));
    try {
        // Find register date in dnb rest
        Date dnbRegisteredDate = MCRURNUtils.getDNBRegisterDate(dnburn);
        if (dnbRegisteredDate == null) {
            return false;
        }
        mcrpi.setRegistered(dnbRegisteredDate);
        updateFlag(id, additional, mcrpi);
        return true;
    } catch (ParseException e) {
        LOGGER.error("Could not parse Date from PIDEF ! URN wont be marked as registered because of this! ", e);
        return false;
    } catch (MCRIdentifierUnresolvableException e) {
        return false;
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) MCRPI(org.mycore.pi.backend.MCRPI) ParseException(java.text.ParseException) Date(java.util.Date)

Example 35 with MCRException

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

the class MCRPIRegistrationService method getGenerator.

private MCRPersistentIdentifierGenerator<T> getGenerator() {
    Supplier<? extends RuntimeException> generatorPropertiesNotSetError = () -> new MCRConfigurationException("Configuration property " + REGISTRATION_CONFIG_PREFIX + registrationServiceID + ".Generator is not set");
    String generatorName = Optional.ofNullable(getProperties().get("Generator")).orElseThrow(generatorPropertiesNotSetError);
    String inscriberPropertyKey = GENERATOR_CONFIG_PREFIX + generatorName;
    String className = MCRConfiguration.instance().getString(inscriberPropertyKey);
    try {
        @SuppressWarnings("unchecked") Class<MCRPersistentIdentifierGenerator<T>> classObject = (Class<MCRPersistentIdentifierGenerator<T>>) Class.forName(className);
        Constructor<MCRPersistentIdentifierGenerator<T>> constructor = classObject.getConstructor(String.class);
        return constructor.newInstance(generatorName);
    } catch (ClassNotFoundException e) {
        throw new MCRConfigurationException("Configurated class (" + inscriberPropertyKey + ") not found: " + className, e);
    } catch (NoSuchMethodException e) {
        throw new MCRConfigurationException("Configurated class (" + inscriberPropertyKey + ") needs a string constructor: " + className);
    } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
        throw new MCRException(e);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

MCRException (org.mycore.common.MCRException)131 IOException (java.io.IOException)39 Element (org.jdom2.Element)26 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)19 Document (org.jdom2.Document)18 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)18 File (java.io.File)15 MCRConfigurationException (org.mycore.common.config.MCRConfigurationException)12 MCRObject (org.mycore.datamodel.metadata.MCRObject)12 ArrayList (java.util.ArrayList)11 JDOMException (org.jdom2.JDOMException)11 MCRAccessException (org.mycore.access.MCRAccessException)11 MCRPath (org.mycore.datamodel.niofs.MCRPath)10 SAXException (org.xml.sax.SAXException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 List (java.util.List)7 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)7 SAXParseException (org.xml.sax.SAXParseException)7 URI (java.net.URI)6 Path (java.nio.file.Path)6