use of org.mycore.common.config.MCRConfiguration2 in project mycore by MyCoRe-Org.
the class MCRThumbnailResource method getThumbnail.
private Response getThumbnail(String documentId, int size, String ext) {
List<MCRObjectID> derivateIds = MCRMetadataManager.getDerivateIds(MCRJerseyUtil.getID(documentId), 1, TimeUnit.MINUTES);
for (MCRObjectID derivateId : derivateIds) {
if (MCRAccessManager.checkPermissionForReadingDerivate(derivateId.toString())) {
String nameOfMainFile = MCRMetadataManager.retrieveMCRDerivate(derivateId).getDerivate().getInternals().getMainDoc();
if (nameOfMainFile != null && !nameOfMainFile.equals("")) {
MCRPath mainFile = MCRPath.getPath(derivateId.toString(), '/' + nameOfMainFile);
try {
FileTime lastModified = Files.getLastModifiedTime(mainFile);
Date lastModifiedDate = new Date(lastModified.toMillis());
Response.ResponseBuilder resp = request.evaluatePreconditions(lastModifiedDate);
if (resp != null) {
return resp.build();
}
String mimeType = Files.probeContentType(mainFile);
List<MCRThumbnailGenerator> generators = MCRConfiguration2.getOrThrow("MCR.Media.Thumbnail.Generators", MCRConfiguration2::splitValue).map(MCRConfiguration2::<MCRThumbnailGenerator>instantiateClass).filter(thumbnailGenerator -> thumbnailGenerator.matchesFileType(mimeType, mainFile)).collect(Collectors.toList());
final Optional<BufferedImage> thumbnail = generators.stream().map(thumbnailGenerator -> {
try {
return thumbnailGenerator.getThumbnail(mainFile, size);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}).filter(Optional::isPresent).map(Optional::get).findFirst();
if (thumbnail.isPresent()) {
CacheControl cc = new CacheControl();
cc.setMaxAge((int) TimeUnit.DAYS.toSeconds(1));
String type = "image/png";
if ("jpg".equals(ext) || "jpeg".equals(ext)) {
type = "image/jpeg";
}
return Response.ok(thumbnail.get()).cacheControl(cc).lastModified(lastModifiedDate).type(type).build();
}
} catch (IOException | RuntimeException e) {
throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
}
}
return Response.status(Response.Status.NOT_FOUND).build();
}
use of org.mycore.common.config.MCRConfiguration2 in project mycore by MyCoRe-Org.
the class MCREditorOutValidator method getValidatorMap.
private static Map<String, MCREditorMetadataValidator> getValidatorMap() {
Map<String, MCREditorMetadataValidator> map = new HashMap<>();
map.put(MCRMetaBoolean.class.getSimpleName(), getObjectCheckInstance(MCRMetaBoolean.class));
map.put(MCRMetaPersonName.class.getSimpleName(), getObjectCheckWithLangInstance(MCRMetaPersonName.class));
map.put(MCRMetaInstitutionName.class.getSimpleName(), getObjectCheckWithLangInstance(MCRMetaInstitutionName.class));
map.put(MCRMetaAddress.class.getSimpleName(), new MCRMetaAdressCheck());
map.put(MCRMetaNumber.class.getSimpleName(), getObjectCheckWithLangNotEmptyInstance(MCRMetaNumber.class));
map.put(MCRMetaLinkID.class.getSimpleName(), getObjectCheckWithLinksInstance(MCRMetaLinkID.class));
map.put(MCRMetaEnrichedLinkID.class.getSimpleName(), getObjectCheckWithLinksInstance(MCRMetaEnrichedLinkID.class));
map.put(MCRMetaDerivateLink.class.getSimpleName(), getObjectCheckWithLinksInstance(MCRMetaDerivateLink.class));
map.put(MCRMetaLink.class.getSimpleName(), getObjectCheckWithLinksInstance(MCRMetaLink.class));
map.put(MCRMetaISO8601Date.class.getSimpleName(), getObjectCheckWithLangNotEmptyInstance(MCRMetaISO8601Date.class));
map.put(MCRMetaLangText.class.getSimpleName(), getObjectCheckWithLangNotEmptyInstance(MCRMetaLangText.class));
map.put(MCRMetaAccessRule.class.getSimpleName(), getObjectCheckInstance(MCRMetaAccessRule.class));
map.put(MCRMetaClassification.class.getSimpleName(), new MCRMetaClassificationCheck());
map.put(MCRMetaHistoryDate.class.getSimpleName(), new MCRMetaHistoryDateCheck());
Map<String, String> props = MCRConfiguration2.getPropertiesMap().entrySet().stream().filter(p -> p.getKey().startsWith(CONFIG_PREFIX + "class.")).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
for (Entry<String, String> entry : props.entrySet()) {
try {
String className = entry.getKey();
className = className.substring(className.lastIndexOf('.') + 1);
LOGGER.info("Adding Validator {} for class {}", entry.getValue(), className);
@SuppressWarnings("unchecked") Class<? extends MCREditorMetadataValidator> cl = (Class<? extends MCREditorMetadataValidator>) Class.forName(entry.getValue());
map.put(className, cl.getDeclaredConstructor().newInstance());
} catch (Exception e) {
final String msg = "Cannot instantiate " + entry.getValue() + " as validator for class " + entry.getKey();
LOGGER.error(msg);
throw new MCRException(msg, e);
}
}
return map;
}
use of org.mycore.common.config.MCRConfiguration2 in project mycore by MyCoRe-Org.
the class MCRJWTUtil method getJWTBuilder.
public static JWTCreator.Builder getJWTBuilder(MCRSession mcrSession, String[] userAttributes, String[] sessionAttributes) {
MCRUserInformation userInformation = mcrSession.getUserInformation();
String[] roles = MCRConfiguration2.getOrThrow(ROLES_PROPERTY, MCRConfiguration2::splitValue).filter(userInformation::isUserInRole).toArray(String[]::new);
String subject = userInformation.getUserID();
String email = userInformation.getUserAttribute(MCRUserInformation.ATT_EMAIL);
String name = userInformation.getUserAttribute(MCRUserInformation.ATT_REAL_NAME);
JWTCreator.Builder builder = JWT.create().withIssuedAt(new Date()).withSubject(subject).withArrayClaim("mcr:roles", roles).withClaim("email", email).withClaim("name", name);
if (userAttributes != null) {
for (String userAttribute : userAttributes) {
String value = userInformation.getUserAttribute(userAttribute);
if (value != null) {
builder.withClaim(JWT_USER_ATTRIBUTE_PREFIX + userAttribute, value);
}
}
}
if (sessionAttributes != null) {
for (String sessionAttribute : sessionAttributes) {
Object object = mcrSession.get(sessionAttribute);
Optional.ofNullable(object).map(Object::toString).ifPresent((value) -> {
builder.withClaim(JWT_SESSION_ATTRIBUTE_PREFIX + sessionAttribute, value);
});
}
}
return builder;
}
use of org.mycore.common.config.MCRConfiguration2 in project mycore by MyCoRe-Org.
the class MCRSword method initWorkspaceCollection.
private static void initWorkspaceCollection(String workspace, String collection) {
LOGGER.info("Found collection: {} in workspace {}", collection, workspace);
String name = MCRSwordConstants.MCR_SWORD_COLLECTION_PREFIX + workspace + "." + collection;
LOGGER.info("Try to init : {}", name);
MCRSwordCollectionProvider collectionProvider = MCRConfiguration2.getOrThrow(name, MCRConfiguration2::instantiateClass);
collections.put(collection, collectionProvider);
final MCRSwordLifecycleConfiguration lifecycleConfiguration = new MCRSwordLifecycleConfiguration(collection);
collectionProvider.init(lifecycleConfiguration);
List<String> collectionsOfWorkspace;
if (workspaceCollectionTable.containsKey(workspace)) {
collectionsOfWorkspace = workspaceCollectionTable.get(workspace);
} else {
collectionsOfWorkspace = new ArrayList<>();
workspaceCollectionTable.put(workspace, collectionsOfWorkspace);
}
collectionsOfWorkspace.add(collection);
}
use of org.mycore.common.config.MCRConfiguration2 in project mycore by MyCoRe-Org.
the class MCRDOICommands method synchronizeDatabase.
@MCRCommand(syntax = "repair registered dois {0}", help = "Contacts the Registration Service and inserts all registered DOIs to the Database. " + "It also updates all media files. The Service ID{0} is the id from the configuration.", order = 10)
public static void synchronizeDatabase(String serviceID) {
MCRDOIService registrationService = (MCRDOIService) MCRConfiguration2.getInstanceOf(MCRPIServiceManager.REGISTRATION_SERVICE_CONFIG_PREFIX + serviceID).get();
try {
MCRDataciteClient dataciteClient = registrationService.getDataciteClient();
List<MCRDigitalObjectIdentifier> doiList = dataciteClient.getDOIList();
doiList.stream().filter(doi -> {
boolean isTestDOI = doi.getPrefix().equals(MCRDigitalObjectIdentifier.TEST_DOI_PREFIX);
return !isTestDOI;
}).forEach(doi -> {
try {
URI uri = dataciteClient.resolveDOI(doi);
if (uri.toString().startsWith(registrationService.getRegisterURL())) {
LOGGER.info("Checking DOI: {}", doi.asString());
MCRObjectID objectID = getObjectID(uri);
if (MCRMetadataManager.exists(objectID)) {
if (!registrationService.isRegistered(objectID, "")) {
LOGGER.info("DOI is not registered in MyCoRe. Add to Database: {}", doi.asString());
MCRPI databaseEntry = new MCRPI(doi.asString(), registrationService.getType(), objectID.toString(), "", serviceID, new Date());
MCREntityManagerProvider.getCurrentEntityManager().persist(databaseEntry);
}
// Update main files
MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
List<Map.Entry<String, URI>> entryList = registrationService.getMediaList(obj);
dataciteClient.setMediaList(doi, entryList);
} else {
LOGGER.info("Could not find Object : {}", objectID);
}
} else {
LOGGER.info("DOI/URL is not from this application: {}/{}", doi.asString(), uri);
}
} catch (MCRPersistentIdentifierException e) {
LOGGER.error("Error occurred for DOI: {}", doi, e);
}
});
} catch (MCRPersistentIdentifierException e) {
LOGGER.error("Error while receiving DOI list from Registration-Service!", e);
}
}
Aggregations