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;
}
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);
}
}
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);
}
}
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;
}
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);
}
}
Aggregations