Search in sources :

Example 31 with StringUtils.isBlank

use of org.apache.commons.lang3.StringUtils.isBlank in project nifi-registry by apache.

the class LdapUserGroupProvider method load.

/**
 * Reloads the tenants.
 */
private void load(final ContextSource contextSource) {
    // create the ldapTemplate based on the context source. use a single source context to use the same connection
    // to support paging when configured
    final SingleContextSource singleContextSource = new SingleContextSource(contextSource.getReadOnlyContext());
    final LdapTemplate ldapTemplate = new LdapTemplate(singleContextSource);
    try {
        final List<User> userList = new ArrayList<>();
        final List<Group> groupList = new ArrayList<>();
        // group dn -> user identifiers lookup
        final Map<String, Set<String>> groupToUserIdentifierMappings = new HashMap<>();
        // user dn -> user lookup
        final Map<String, User> userLookup = new HashMap<>();
        if (performUserSearch) {
            // search controls
            final SearchControls userControls = new SearchControls();
            userControls.setSearchScope(userSearchScope.ordinal());
            // consider paging support for users
            final DirContextProcessor userProcessor;
            if (pageSize == null) {
                userProcessor = new NullDirContextProcessor();
            } else {
                userProcessor = new PagedResultsDirContextProcessor(pageSize);
            }
            // looking for objects matching the user object class
            final AndFilter userFilter = new AndFilter();
            userFilter.and(new EqualsFilter("objectClass", userObjectClass));
            // if a filter has been provided by the user, we add it to the filter
            if (StringUtils.isNotBlank(userSearchFilter)) {
                userFilter.and(new HardcodedFilter(userSearchFilter));
            }
            do {
                userList.addAll(ldapTemplate.search(userSearchBase, userFilter.encode(), userControls, new AbstractContextMapper<User>() {

                    @Override
                    protected User doMapFromContext(DirContextOperations ctx) {
                        // get the user identity
                        final String identity = getUserIdentity(ctx);
                        // build the user
                        final User user = new User.Builder().identifierGenerateFromSeed(identity).identity(identity).build();
                        // store the user for group member later
                        userLookup.put(getReferencedUserValue(ctx), user);
                        if (StringUtils.isNotBlank(userGroupNameAttribute)) {
                            final Attribute attributeGroups = ctx.getAttributes().get(userGroupNameAttribute);
                            if (attributeGroups == null) {
                                logger.warn("User group name attribute [" + userGroupNameAttribute + "] does not exist. Ignoring group membership.");
                            } else {
                                try {
                                    final NamingEnumeration<String> groupValues = (NamingEnumeration<String>) attributeGroups.getAll();
                                    while (groupValues.hasMoreElements()) {
                                        // store the group -> user identifier mapping
                                        groupToUserIdentifierMappings.computeIfAbsent(groupValues.next(), g -> new HashSet<>()).add(user.getIdentifier());
                                    }
                                } catch (NamingException e) {
                                    throw new AuthorizationAccessException("Error while retrieving user group name attribute [" + userIdentityAttribute + "].");
                                }
                            }
                        }
                        return user;
                    }
                }, userProcessor));
            } while (hasMorePages(userProcessor));
        }
        if (performGroupSearch) {
            final SearchControls groupControls = new SearchControls();
            groupControls.setSearchScope(groupSearchScope.ordinal());
            // consider paging support for groups
            final DirContextProcessor groupProcessor;
            if (pageSize == null) {
                groupProcessor = new NullDirContextProcessor();
            } else {
                groupProcessor = new PagedResultsDirContextProcessor(pageSize);
            }
            // looking for objects matching the group object class
            AndFilter groupFilter = new AndFilter();
            groupFilter.and(new EqualsFilter("objectClass", groupObjectClass));
            // if a filter has been provided by the user, we add it to the filter
            if (StringUtils.isNotBlank(groupSearchFilter)) {
                groupFilter.and(new HardcodedFilter(groupSearchFilter));
            }
            do {
                groupList.addAll(ldapTemplate.search(groupSearchBase, groupFilter.encode(), groupControls, new AbstractContextMapper<Group>() {

                    @Override
                    protected Group doMapFromContext(DirContextOperations ctx) {
                        final String dn = ctx.getDn().toString();
                        // get the group identity
                        final String name = getGroupName(ctx);
                        // get the value of this group that may associate it to users
                        final String referencedGroupValue = getReferencedGroupValue(ctx);
                        if (!StringUtils.isBlank(groupMemberAttribute)) {
                            Attribute attributeUsers = ctx.getAttributes().get(groupMemberAttribute);
                            if (attributeUsers == null) {
                                logger.warn("Group member attribute [" + groupMemberAttribute + "] does not exist. Ignoring group membership.");
                            } else {
                                try {
                                    final NamingEnumeration<String> userValues = (NamingEnumeration<String>) attributeUsers.getAll();
                                    while (userValues.hasMoreElements()) {
                                        final String userValue = userValues.next();
                                        if (performUserSearch) {
                                            // find the user by it's referenced attribute and add the identifier to this group
                                            final User user = userLookup.get(userValue);
                                            // ensure the user is known
                                            if (user != null) {
                                                groupToUserIdentifierMappings.computeIfAbsent(referencedGroupValue, g -> new HashSet<>()).add(user.getIdentifier());
                                            } else {
                                                logger.warn(String.format("%s contains member %s but that user was not found while searching users. Ignoring group membership.", name, userValue));
                                            }
                                        } else {
                                            // since performUserSearch is false, then the referenced group attribute must be blank... the user value must be the dn
                                            final String userDn = userValue;
                                            final String userIdentity;
                                            if (useDnForUserIdentity) {
                                                // use the user value to avoid the unnecessary look up
                                                userIdentity = userDn;
                                            } else {
                                                // lookup the user to extract the user identity
                                                userIdentity = getUserIdentity((DirContextAdapter) ldapTemplate.lookup(userDn));
                                            }
                                            // build the user
                                            final User user = new User.Builder().identifierGenerateFromSeed(userIdentity).identity(userIdentity).build();
                                            // add this user
                                            userList.add(user);
                                            groupToUserIdentifierMappings.computeIfAbsent(referencedGroupValue, g -> new HashSet<>()).add(user.getIdentifier());
                                        }
                                    }
                                } catch (NamingException e) {
                                    throw new AuthorizationAccessException("Error while retrieving group name attribute [" + groupNameAttribute + "].");
                                }
                            }
                        }
                        // build this group
                        final Group.Builder groupBuilder = new Group.Builder().identifierGenerateFromSeed(name).name(name);
                        // add all users that were associated with this referenced group attribute
                        if (groupToUserIdentifierMappings.containsKey(referencedGroupValue)) {
                            groupToUserIdentifierMappings.remove(referencedGroupValue).forEach(userIdentifier -> groupBuilder.addUser(userIdentifier));
                        }
                        return groupBuilder.build();
                    }
                }, groupProcessor));
            } while (hasMorePages(groupProcessor));
            // any remaining groupDn's were referenced by a user but not found while searching groups
            groupToUserIdentifierMappings.forEach((referencedGroupValue, userIdentifiers) -> {
                logger.warn(String.format("[%s] are members of %s but that group was not found while searching users. Ignoring group membership.", StringUtils.join(userIdentifiers, ", "), referencedGroupValue));
            });
        } else {
            // since performGroupSearch is false, then the referenced user attribute must be blank... the group value must be the dn
            // groups are not being searched so lookup any groups identified while searching users
            groupToUserIdentifierMappings.forEach((groupDn, userIdentifiers) -> {
                final String groupName;
                if (useDnForGroupName) {
                    // use the dn to avoid the unnecessary look up
                    groupName = groupDn;
                } else {
                    groupName = getGroupName((DirContextAdapter) ldapTemplate.lookup(groupDn));
                }
                // define the group
                final Group.Builder groupBuilder = new Group.Builder().identifierGenerateFromSeed(groupName).name(groupName);
                // add each user
                userIdentifiers.forEach(userIdentifier -> groupBuilder.addUser(userIdentifier));
                // build the group
                groupList.add(groupBuilder.build());
            });
        }
        // record the updated tenants
        tenants.set(new TenantHolder(new HashSet<>(userList), new HashSet<>(groupList)));
    } finally {
        singleContextSource.destroy();
    }
}
Also used : SSLContext(javax.net.ssl.SSLContext) AbstractContextMapper(org.springframework.ldap.core.support.AbstractContextMapper) AndFilter(org.springframework.ldap.filter.AndFilter) ClientAuth(org.apache.nifi.registry.security.util.SslContextFactory.ClientAuth) LoggerFactory(org.slf4j.LoggerFactory) LdapTemplate(org.springframework.ldap.core.LdapTemplate) NamingException(javax.naming.NamingException) KeyStoreException(java.security.KeyStoreException) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(javax.naming.directory.Attribute) Map(java.util.Map) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) ThreadFactory(java.util.concurrent.ThreadFactory) LdapsSocketFactory(org.apache.nifi.registry.security.ldap.LdapsSocketFactory) LdapContextSource(org.springframework.ldap.core.support.LdapContextSource) AuthorizerContext(org.apache.nifi.registry.security.authorization.annotation.AuthorizerContext) UserGroupProvider(org.apache.nifi.registry.security.authorization.UserGroupProvider) Set(java.util.Set) KeyManagementException(java.security.KeyManagementException) HardcodedFilter(org.springframework.ldap.filter.HardcodedFilter) LdapAuthenticationStrategy(org.apache.nifi.registry.security.ldap.LdapAuthenticationStrategy) SslContextFactory(org.apache.nifi.registry.security.util.SslContextFactory) Executors(java.util.concurrent.Executors) List(java.util.List) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NamingEnumeration(javax.naming.NamingEnumeration) SingleContextSource(org.springframework.ldap.core.support.SingleContextSource) IdentityMapping(org.apache.nifi.registry.properties.util.IdentityMapping) DirContextOperations(org.springframework.ldap.core.DirContextOperations) PagedResultsDirContextProcessor(org.springframework.ldap.control.PagedResultsDirContextProcessor) ReferralStrategy(org.apache.nifi.registry.security.ldap.ReferralStrategy) NullDirContextProcessor(org.springframework.ldap.core.LdapTemplate.NullDirContextProcessor) DirContextProcessor(org.springframework.ldap.core.DirContextProcessor) IdentityMappingUtil(org.apache.nifi.registry.properties.util.IdentityMappingUtil) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) SearchControls(javax.naming.directory.SearchControls) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Group(org.apache.nifi.registry.security.authorization.Group) SimpleDirContextAuthenticationStrategy(org.springframework.ldap.core.support.SimpleDirContextAuthenticationStrategy) UserGroupProviderInitializationContext(org.apache.nifi.registry.security.authorization.UserGroupProviderInitializationContext) NiFiRegistryProperties(org.apache.nifi.registry.properties.NiFiRegistryProperties) UserAndGroups(org.apache.nifi.registry.security.authorization.UserAndGroups) UnrecoverableKeyException(java.security.UnrecoverableKeyException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AbstractTlsDirContextAuthenticationStrategy(org.springframework.ldap.core.support.AbstractTlsDirContextAuthenticationStrategy) SecurityProviderDestructionException(org.apache.nifi.registry.security.exception.SecurityProviderDestructionException) DefaultTlsDirContextAuthenticationStrategy(org.springframework.ldap.core.support.DefaultTlsDirContextAuthenticationStrategy) Context(javax.naming.Context) AuthorizationAccessException(org.apache.nifi.registry.security.authorization.exception.AuthorizationAccessException) Logger(org.slf4j.Logger) ContextSource(org.springframework.ldap.core.ContextSource) SecurityProviderCreationException(org.apache.nifi.registry.security.exception.SecurityProviderCreationException) User(org.apache.nifi.registry.security.authorization.User) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TimeUnit(java.util.concurrent.TimeUnit) EqualsFilter(org.springframework.ldap.filter.EqualsFilter) AuthorizerConfigurationContext(org.apache.nifi.registry.security.authorization.AuthorizerConfigurationContext) PropertyValue(org.apache.nifi.registry.util.PropertyValue) Collections(java.util.Collections) FormatUtils(org.apache.nifi.registry.util.FormatUtils) Group(org.apache.nifi.registry.security.authorization.Group) User(org.apache.nifi.registry.security.authorization.User) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) NamingEnumeration(javax.naming.NamingEnumeration) PagedResultsDirContextProcessor(org.springframework.ldap.control.PagedResultsDirContextProcessor) NullDirContextProcessor(org.springframework.ldap.core.LdapTemplate.NullDirContextProcessor) DirContextProcessor(org.springframework.ldap.core.DirContextProcessor) PagedResultsDirContextProcessor(org.springframework.ldap.control.PagedResultsDirContextProcessor) LdapTemplate(org.springframework.ldap.core.LdapTemplate) AuthorizationAccessException(org.apache.nifi.registry.security.authorization.exception.AuthorizationAccessException) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) SearchControls(javax.naming.directory.SearchControls) NamingException(javax.naming.NamingException) EqualsFilter(org.springframework.ldap.filter.EqualsFilter) HashSet(java.util.HashSet) SingleContextSource(org.springframework.ldap.core.support.SingleContextSource) NullDirContextProcessor(org.springframework.ldap.core.LdapTemplate.NullDirContextProcessor) HardcodedFilter(org.springframework.ldap.filter.HardcodedFilter) AndFilter(org.springframework.ldap.filter.AndFilter) AbstractContextMapper(org.springframework.ldap.core.support.AbstractContextMapper) DirContextOperations(org.springframework.ldap.core.DirContextOperations)

Example 32 with StringUtils.isBlank

use of org.apache.commons.lang3.StringUtils.isBlank in project nifi-registry by apache.

the class CryptoKeyLoader method extractKeyFromBootstrapFile.

/**
 * Returns the key (if any) used to encrypt sensitive properties.
 * The key extracted from the bootstrap.conf file at the specified location.
 *
 * @param bootstrapPath the path to the bootstrap file
 * @return the key in hexadecimal format, or {@link CryptoKeyProvider#EMPTY_KEY} if the key is null or empty
 * @throws IOException if the file is not readable
 */
public static String extractKeyFromBootstrapFile(String bootstrapPath) throws IOException {
    File bootstrapFile;
    if (StringUtils.isBlank(bootstrapPath)) {
        logger.error("Cannot read from bootstrap.conf file to extract encryption key; location not specified");
        throw new IOException("Cannot read from bootstrap.conf without file location");
    } else {
        bootstrapFile = new File(bootstrapPath);
    }
    String keyValue;
    if (bootstrapFile.exists() && bootstrapFile.canRead()) {
        try (Stream<String> stream = Files.lines(Paths.get(bootstrapFile.getAbsolutePath()))) {
            Optional<String> keyLine = stream.filter(l -> l.startsWith(BOOTSTRAP_KEY_PREFIX)).findFirst();
            if (keyLine.isPresent()) {
                keyValue = keyLine.get().split("=", 2)[1];
                keyValue = checkHexKey(keyValue);
            } else {
                keyValue = CryptoKeyProvider.EMPTY_KEY;
            }
        } catch (IOException e) {
            logger.error("Cannot read from bootstrap.conf file at {} to extract encryption key", bootstrapFile.getAbsolutePath());
            throw new IOException("Cannot read from bootstrap.conf", e);
        }
    } else {
        logger.error("Cannot read from bootstrap.conf file at {} to extract encryption key -- file is missing or permissions are incorrect", bootstrapFile.getAbsolutePath());
        throw new IOException("Cannot read from bootstrap.conf");
    }
    if (CryptoKeyProvider.EMPTY_KEY.equals(keyValue)) {
        logger.info("No encryption key present in the bootstrap.conf file at {}", bootstrapFile.getAbsolutePath());
    }
    return keyValue;
}
Also used : Logger(org.slf4j.Logger) Stream(java.util.stream.Stream) Files(java.nio.file.Files) Paths(java.nio.file.Paths) LoggerFactory(org.slf4j.LoggerFactory) Optional(java.util.Optional) IOException(java.io.IOException) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) IOException(java.io.IOException) File(java.io.File)

Example 33 with StringUtils.isBlank

use of org.apache.commons.lang3.StringUtils.isBlank in project timbuctoo by HuygensING.

the class TabularUpload method upload.

@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@POST
public Response upload(@FormDataParam("file") final InputStream rdfInputStream, @FormDataParam("file") final FormDataBodyPart body, @FormDataParam("file") final FormDataContentDisposition fileInfo, @FormDataParam("fileMimeTypeOverride") final MediaType mimeTypeOverride, FormDataMultiPart formData, @HeaderParam("authorization") final String authHeader, @PathParam("userId") final String ownerId, @PathParam("dataSetId") final String dataSetId, @QueryParam("forceCreation") boolean forceCreation) throws DataStoreCreationException, FileStorageFailedException, ExecutionException, InterruptedException, LogStorageFailedException {
    final Either<Response, Response> result = authCheck.getOrCreate(authHeader, ownerId, dataSetId, forceCreation).flatMap(userAndDs -> authCheck.hasAdminAccess(userAndDs.getLeft(), userAndDs.getRight())).map(userAndDs -> {
        final MediaType mediaType = mimeTypeOverride == null ? body.getMediaType() : mimeTypeOverride;
        Optional<Loader> loader = LoaderFactory.createFor(mediaType.toString(), formData.getFields().entrySet().stream().filter(entry -> entry.getValue().size() > 0).filter(entry -> entry.getValue().get(0) != null).filter(entry -> MediaTypes.typeEqual(MediaType.TEXT_PLAIN_TYPE, entry.getValue().get(0).getMediaType())).collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().get(0).getValue())));
        if (!loader.isPresent()) {
            return errorResponseHelper.error(400, "We do not support the mediatype '" + mediaType + "'. Make sure to add the correct mediatype to the file " + "parameter. In curl you'd use `-F \"file=@<filename>;type=<mediatype>\"`. In a webbrowser you probably " + "have no way of setting the correct mimetype. So you can use a special parameter to override it: " + "`formData.append(\"fileMimeTypeOverride\", \"<mimetype>\");`");
        }
        final DataSet dataSet = userAndDs.getRight();
        ImportManager importManager = dataSet.getImportManager();
        if (StringUtils.isBlank(fileInfo.getName())) {
            return Response.status(400).entity("filename cannot be empty.").build();
        }
        try {
            String fileToken = importManager.addFile(rdfInputStream, fileInfo.getFileName(), mediaType);
            Future<ImportStatus> promise = importManager.generateLog(dataSet.getMetadata().getBaseUri(), dataSet.getMetadata().getBaseUri(), new TabularRdfCreator(loader.get(), fileToken, fileInfo.getFileName()));
            return handleImportManagerResult(promise);
        } catch (FileStorageFailedException | LogStorageFailedException e) {
            LOG.error("Tabular upload failed", e);
            return Response.serverError().build();
        }
    });
    if (result.isLeft()) {
        return result.getLeft();
    } else {
        return result.get();
    }
}
Also used : PathParam(javax.ws.rs.PathParam) ImportStatus(nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) Produces(javax.ws.rs.Produces) DataSetRepository(nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) StringUtils(org.apache.commons.lang3.StringUtils) MediaType(javax.ws.rs.core.MediaType) Future(java.util.concurrent.Future) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) HeaderParam(javax.ws.rs.HeaderParam) Loader(nl.knaw.huygens.timbuctoo.bulkupload.loaders.Loader) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) AuthCheck(nl.knaw.huygens.timbuctoo.v5.dropwizard.endpoints.auth.AuthCheck) Either(javaslang.control.Either) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) ImportManager(nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager) DataStoreCreationException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.DataStoreCreationException) LoaderFactory(nl.knaw.huygens.timbuctoo.bulkupload.loaders.LoaderFactory) Collectors(java.util.stream.Collectors) ErrorResponseHelper.handleImportManagerResult(nl.knaw.huygens.timbuctoo.v5.dropwizard.endpoints.ErrorResponseHelper.handleImportManagerResult) ExecutionException(java.util.concurrent.ExecutionException) MediaTypes(org.glassfish.jersey.message.internal.MediaTypes) FormDataParam(org.glassfish.jersey.media.multipart.FormDataParam) Response(javax.ws.rs.core.Response) FileStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.FileStorageFailedException) TabularRdfCreator(nl.knaw.huygens.timbuctoo.v5.bulkupload.TabularRdfCreator) Optional(java.util.Optional) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) InputStream(java.io.InputStream) ImportManager(nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) Loader(nl.knaw.huygens.timbuctoo.bulkupload.loaders.Loader) FileStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.FileStorageFailedException) Response(javax.ws.rs.core.Response) ImportStatus(nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus) MediaType(javax.ws.rs.core.MediaType) TabularRdfCreator(nl.knaw.huygens.timbuctoo.v5.bulkupload.TabularRdfCreator) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) POST(javax.ws.rs.POST)

Example 34 with StringUtils.isBlank

use of org.apache.commons.lang3.StringUtils.isBlank in project mule by mulesoft.

the class XmlExtensionLoaderDelegate method declareErrorModels.

private void declareErrorModels(OperationDeclarer operationDeclarer, XmlDslModel xmlDslModel, String operationName, ComponentModel operationModel) {
    Optional<ComponentModel> optionalParametersComponentModel = operationModel.getInnerComponents().stream().filter(child -> child.getIdentifier().equals(OPERATION_ERRORS_IDENTIFIER)).findAny();
    optionalParametersComponentModel.ifPresent(componentModel -> componentModel.getInnerComponents().stream().filter(child -> child.getIdentifier().equals(OPERATION_ERROR_IDENTIFIER)).forEach(param -> {
        final String namespace = xmlDslModel.getPrefix().toUpperCase();
        final String typeName = param.getParameters().get(ERROR_TYPE_ATTRIBUTE);
        if (StringUtils.isBlank(typeName)) {
            throw new IllegalModelDefinitionException(format("The operation [%s] cannot have an <error> with an empty 'type' attribute", operationName));
        }
        if (typeName.contains(NAMESPACE_SEPARATOR)) {
            throw new IllegalModelDefinitionException(format("The operation [%s] cannot have an <error> [%s] that contains a reserved character [%s]", operationName, typeName, NAMESPACE_SEPARATOR));
        }
        operationDeclarer.withErrorModel(ErrorModelBuilder.newError(typeName, namespace).withParent(ErrorModelBuilder.newError(ANY).build()).build());
    }));
}
Also used : PRIMITIVE_TYPES(org.mule.metadata.catalog.api.PrimitiveTypesTypeLoader.PRIMITIVE_TYPES) BEHAVIOUR(org.mule.runtime.api.meta.model.parameter.ParameterRole.BEHAVIOUR) ErrorModelBuilder(org.mule.runtime.api.meta.model.error.ErrorModelBuilder) ParameterRole(org.mule.runtime.api.meta.model.parameter.ParameterRole) Optional.of(java.util.Optional.of) ConnectionProviderDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ConnectionProviderDeclarer) StreamResult(javax.xml.transform.stream.StreamResult) Thread.currentThread(java.lang.Thread.currentThread) StringUtils(org.apache.commons.lang3.StringUtils) ConfigurationDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ConfigurationDeclarer) Collections.singletonList(java.util.Collections.singletonList) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry) Document(org.w3c.dom.Document) Map(java.util.Map) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) NullDslResolvingContext(org.mule.runtime.internal.dsl.NullDslResolvingContext) IllegalParameterModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalParameterModelDefinitionException) ConnectionProvider(org.mule.runtime.api.connection.ConnectionProvider) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Set(java.util.Set) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) TestConnectionGlobalElementModelProperty(org.mule.runtime.config.internal.dsl.model.extension.xml.property.TestConnectionGlobalElementModelProperty) DisplayModel(org.mule.runtime.api.meta.model.display.DisplayModel) IOUtils(org.apache.commons.io.IOUtils) DeclarationOperation(org.mule.runtime.extension.api.loader.xml.declaration.DeclarationOperation) MacroExpansionModulesModel.getUsedNamespaces(org.mule.runtime.config.internal.dsl.model.extension.xml.MacroExpansionModulesModel.getUsedNamespaces) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) XmlModelUtils.createXmlLanguageModel(org.mule.runtime.extension.api.util.XmlModelUtils.createXmlLanguageModel) Category(org.mule.runtime.api.meta.Category) PrivateOperationsModelProperty(org.mule.runtime.config.internal.dsl.model.extension.xml.property.PrivateOperationsModelProperty) LayoutModel(org.mule.runtime.api.meta.model.display.LayoutModel) OperationComponentModelModelProperty(org.mule.runtime.config.internal.dsl.model.extension.xml.property.OperationComponentModelModelProperty) MetadataType(org.mule.metadata.api.model.MetadataType) DslSyntaxResolver(org.mule.runtime.extension.api.dsl.syntax.resolver.DslSyntaxResolver) DirectedGraph(org.jgrapht.DirectedGraph) HasOperationDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.HasOperationDeclarer) Optional.empty(java.util.Optional.empty) TransformerException(javax.xml.transform.TransformerException) ConnectionProviderModel(org.mule.runtime.api.meta.model.connection.ConnectionProviderModel) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) TreeSet(java.util.TreeSet) Placement(org.mule.runtime.extension.api.annotation.param.display.Placement) String.join(java.lang.String.join) LayoutModel.builder(org.mule.runtime.api.meta.model.display.LayoutModel.builder) CycleDetector(org.jgrapht.alg.CycleDetector) DefaultEdge(org.jgrapht.graph.DefaultEdge) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) XmlExtensionModelProperty(org.mule.runtime.extension.api.property.XmlExtensionModelProperty) ANY(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.ANY) GlobalElementComponentModelModelProperty(org.mule.runtime.config.internal.dsl.model.extension.xml.property.GlobalElementComponentModelModelProperty) IOException(java.io.IOException) ConfigurationModel(org.mule.runtime.api.meta.model.config.ConfigurationModel) ConnectionManagementType(org.mule.runtime.api.meta.model.connection.ConnectionManagementType) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) NoReconnectionStrategyModelProperty(org.mule.runtime.extension.internal.property.NoReconnectionStrategyModelProperty) Boolean.parseBoolean(java.lang.Boolean.parseBoolean) ParameterDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ParameterDeclarer) TransformerFactory(javax.xml.transform.TransformerFactory) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) MacroExpansionModulesModel(org.mule.runtime.config.internal.dsl.model.extension.xml.MacroExpansionModulesModel) URL(java.net.URL) NamedObject(org.mule.runtime.api.meta.NamedObject) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) XmlApplicationParser(org.mule.runtime.config.api.dsl.processor.xml.XmlApplicationParser) ByteArrayInputStream(java.io.ByteArrayInputStream) DslResolvingContext(org.mule.runtime.api.dsl.DslResolvingContext) MacroExpansionModuleModel(org.mule.runtime.config.internal.dsl.model.extension.xml.MacroExpansionModuleModel) ComponentModelReader(org.mule.runtime.config.internal.dsl.model.ComponentModelReader) XmlConfigurationDocumentLoader(org.mule.runtime.config.api.XmlConfigurationDocumentLoader) EnvironmentPropertiesConfigurationProvider(org.mule.runtime.config.internal.dsl.model.config.EnvironmentPropertiesConfigurationProvider) ImmutableMap(com.google.common.collect.ImmutableMap) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Sets(com.google.common.collect.Sets) List(java.util.List) XmlDslModel(org.mule.runtime.api.meta.model.XmlDslModel) Optional(java.util.Optional) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) NoOpXmlErrorHandler(org.mule.runtime.config.internal.util.NoOpXmlErrorHandler) IllegalModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalModelDefinitionException) OperationDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclarer) JAVA(org.mule.metadata.api.model.MetadataFormat.JAVA) HashMap(java.util.HashMap) HashSet(java.util.HashSet) TypeResolverException(org.mule.metadata.catalog.api.TypeResolverException) TNS_PREFIX(org.mule.runtime.config.internal.dsl.model.extension.xml.MacroExpansionModuleModel.TNS_PREFIX) ConfigLine(org.mule.runtime.config.api.dsl.processor.ConfigLine) TypeResolver(org.mule.metadata.catalog.api.TypeResolver) DefaultDirectedGraph(org.jgrapht.graph.DefaultDirectedGraph) Optional.ofNullable(java.util.Optional.ofNullable) ParameterizedDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ParameterizedDeclarer) MODULE_CONNECTION_GLOBAL_ELEMENT_NAME(org.mule.runtime.core.internal.processor.chain.ModuleOperationMessageProcessorChainBuilder.MODULE_CONNECTION_GLOBAL_ELEMENT_NAME) ExtensionLoadingContext(org.mule.runtime.extension.api.loader.ExtensionLoadingContext) DefaultConfigurationPropertiesResolver(org.mule.runtime.config.internal.dsl.model.config.DefaultConfigurationPropertiesResolver) XmlConfigurationDocumentLoader.schemaValidatingDocumentLoader(org.mule.runtime.config.api.XmlConfigurationDocumentLoader.schemaValidatingDocumentLoader) XMLNS_ATTRIBUTE(javax.xml.XMLConstants.XMLNS_ATTRIBUTE) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) ComponentModel(org.mule.runtime.config.internal.model.ComponentModel) OutputDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.OutputDeclarer) Collections(java.util.Collections) IllegalModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalModelDefinitionException) ComponentModel(org.mule.runtime.config.internal.model.ComponentModel)

Example 35 with StringUtils.isBlank

use of org.apache.commons.lang3.StringUtils.isBlank in project stanbol by apache.

the class KiWiRepositoryService method activate.

@Activate
protected final void activate(ComponentContext context) throws ConfigurationException, RepositoryException {
    log.info("activate KiWi repository ...");
    if (context == null || context.getProperties() == null) {
        throw new IllegalStateException("No valid" + ComponentContext.class + " parsed in activate!");
    }
    //copy the read-only configuration as we might need to change it before
    //adding it to the registered service
    final Dictionary<String, Object> config = copyConfig(context);
    final BundleContext bc = context.getBundleContext();
    //we want to substitute variables used in the dbURL with configuration, 
    //framework and system properties
    StrSubstitutor strSubstitutor = new StrSubstitutor(new StrLookup<Object>() {

        @Override
        public String lookup(String key) {
            Object val = config.get(key);
            if (val == null) {
                val = bc.getProperty(key);
            }
            return val.toString();
        }
    });
    String name = (String) config.get(REPOSITORY_ID);
    if (StringUtils.isBlank(name)) {
        throw new ConfigurationException(REPOSITORY_ID, "The parsed Repository ID MUST NOT be NULL nor blank!");
    } else {
        log.debug(" - name: {}", name);
    }
    KiWiDialect dialect;
    String db_type;
    if (StringUtils.equalsIgnoreCase("postgres", (String) config.get(DB_DIALECT))) {
        dialect = new PostgreSQLDialect();
        db_type = "postgresql";
    } else if (StringUtils.equalsIgnoreCase("mysql", (String) config.get(DB_DIALECT))) {
        dialect = new MySQLDialect();
        db_type = "mysql";
    } else if (StringUtils.equalsIgnoreCase("h2", (String) config.get(DB_DIALECT))) {
        dialect = new H2Dialect();
        db_type = "h2";
    } else {
        throw new ConfigurationException(DB_DIALECT, "No valid database dialect was given");
    }
    log.debug(" - dialect: {}", dialect);
    String db_url = (String) config.get(DB_URL);
    if (StringUtils.isBlank(db_url)) {
        //build the db url from parameters
        String db_host = (String) config.get(DB_HOST);
        if (StringUtils.isBlank(db_host)) {
            db_host = DEFAULT_DB_HOST;
        }
        log.debug(" - db host: {}", db_host);
        String db_name = (String) config.get(DB_NAME);
        if (StringUtils.isBlank(db_name)) {
            db_name = DEFAULT_DB_NAME;
        }
        log.debug(" - db name:  {}", name);
        int db_port;
        Object value = config.get(DB_PORT);
        if (value instanceof Number) {
            db_port = ((Number) value).intValue();
        } else if (value != null && !StringUtils.isBlank(value.toString())) {
            db_port = Integer.parseInt(value.toString());
        } else {
            db_port = DEFAULT_DB_PORT;
        }
        log.debug(" - db port: {}", db_port);
        String db_opts = (String) config.get(DB_OPTS);
        log.debug(" - db options: {}", db_opts);
        StringBuilder dbUrlBuilder = new StringBuilder("jdbc:").append(db_type);
        if (dialect instanceof H2Dialect) {
            //H2 uses a file path and not a host so we do not need the ://
            dbUrlBuilder.append(':').append(db_host);
        } else {
            dbUrlBuilder.append("://").append(db_host);
        }
        if (db_port > 0) {
            dbUrlBuilder.append(':').append(db_port);
        }
        if (!StringUtils.isBlank(db_name)) {
            dbUrlBuilder.append('/').append(db_name);
        }
        if (!StringUtils.isBlank(db_opts)) {
            dbUrlBuilder.append(db_opts);
        }
        dbUrl = strSubstitutor.replace(dbUrlBuilder);
    } else if (!db_url.startsWith("jdbc:")) {
        throw new ConfigurationException(DB_URL, "Database URLs are expected to start with " + "'jdbc:' (parsed: '" + db_url + "')!");
    } else {
        dbUrl = strSubstitutor.replace(db_url);
    }
    String db_user = (String) config.get(DB_USER);
    if (StringUtils.isBlank(db_user)) {
        db_user = DEFAULT_DB_USER;
    } else {
        db_user = strSubstitutor.replace(db_user);
    }
    log.debug(" - db user: {}", db_user);
    String db_pass = (String) config.get(DB_PASS);
    if (StringUtils.isBlank(db_pass)) {
        log.debug(" - db pwd is set to default");
        db_pass = DEFAULT_DB_PASS;
    } else {
        log.debug(" - db pwd is set to parsed value");
    }
    KiWiConfiguration configuration = new KiWiConfiguration("Marmotta KiWi", dbUrl, db_user, db_pass, dialect);
    //parse cluster options
    String cluster = (String) config.get(CLUSTER);
    if (!StringUtils.isBlank(cluster)) {
        log.debug(" - cluster: {}", cluster);
        configuration.setClustered(true);
        configuration.setClusterName(cluster);
        String clusterAddress = (String) config.get(CLUSTER_ADDRESS);
        if (!StringUtils.isBlank(clusterAddress)) {
            configuration.setClusterAddress(strSubstitutor.replace(clusterAddress));
        }
        log.debug(" - cluster address: {}", configuration.getClusterAddress());
        Object clusterPort = config.get(CLUSTER_PORT);
        if (clusterPort instanceof Number) {
            int port = ((Number) clusterPort).intValue();
            if (port > 0) {
                configuration.setClusterPort(port);
            }
        //else use default
        } else if (clusterPort != null) {
            try {
                int port = Integer.parseInt(strSubstitutor.replace(clusterPort));
                if (port > 0) {
                    configuration.setClusterPort(port);
                }
            } catch (NumberFormatException e) {
                throw new ConfigurationException(CLUSTER_PORT, "Unable to parse " + "Cluster Port from configured value '" + clusterPort + "'!", e);
            }
        }
        log.debug(" - cluster port ({})", configuration.getClusterPort());
        String cachingBackend = (String) config.get(CACHING_BACKEND);
        if (StringUtils.isBlank(cachingBackend)) {
            configuration.setCachingBackend(CachingBackends.valueOf(DEFAULT_CACHING_BACKEND));
        } else {
            try {
                configuration.setCachingBackend(CachingBackends.valueOf(strSubstitutor.replace(cachingBackend)));
            } catch (IllegalArgumentException e) {
                throw new ConfigurationException(CACHING_BACKEND, "Unsupported CachingBackend '" + cachingBackend + "' (supported: " + Arrays.toString(CachingBackends.values()) + ")!", e);
            }
        }
        log.debug(" - caching Backend: {}", configuration.getCachingBackend());
        String cacheMode = (String) config.get(CACHE_MODE);
        if (StringUtils.isBlank(cacheMode)) {
            cacheMode = DEFAULT_CACHE_MODE;
        }
        try {
            configuration.setCacheMode(CacheMode.valueOf(strSubstitutor.replace(cacheMode)));
        } catch (IllegalArgumentException e) {
            throw new ConfigurationException(CACHE_MODE, "Unsupported CacheMode '" + cacheMode + "' (supported: " + Arrays.toString(CacheMode.values()) + ")!");
        }
        log.debug(" - cache mode: {}", configuration.getCacheMode());
    } else {
        // not clustered
        log.debug(" - no cluster configured");
        configuration.setClustered(false);
    }
    log.info(" ... initialise KiWi repository: {}", dbUrl);
    KiWiStore store = new KiWiStore(configuration);
    repository = new SailRepository(new KiWiSparqlSail(store));
    repository.initialize();
    //set the repository type property to KiWiStore
    config.put(SAIL_IMPL, KiWiStore.class.getName());
    repoRegistration = context.getBundleContext().registerService(Repository.class.getName(), repository, config);
    log.info("  - successfully registered KiWi Repository {}", name);
}
Also used : KiWiSparqlSail(org.apache.marmotta.kiwi.sparql.sail.KiWiSparqlSail) H2Dialect(org.apache.marmotta.kiwi.persistence.h2.H2Dialect) SailRepository(org.openrdf.repository.sail.SailRepository) KiWiStore(org.apache.marmotta.kiwi.sail.KiWiStore) KiWiDialect(org.apache.marmotta.kiwi.persistence.KiWiDialect) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) MySQLDialect(org.apache.marmotta.kiwi.persistence.mysql.MySQLDialect) PostgreSQLDialect(org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect) ConfigurationException(org.osgi.service.cm.ConfigurationException) KiWiConfiguration(org.apache.marmotta.kiwi.config.KiWiConfiguration) BundleContext(org.osgi.framework.BundleContext) Activate(org.apache.felix.scr.annotations.Activate)

Aggregations

StringUtils (org.apache.commons.lang3.StringUtils)54 List (java.util.List)33 Collectors (java.util.stream.Collectors)29 Map (java.util.Map)28 Set (java.util.Set)27 ArrayList (java.util.ArrayList)23 Optional (java.util.Optional)22 Collections (java.util.Collections)19 Logger (org.slf4j.Logger)19 LoggerFactory (org.slf4j.LoggerFactory)19 IOException (java.io.IOException)18 HashSet (java.util.HashSet)18 Collection (java.util.Collection)16 HashMap (java.util.HashMap)16 StopWatch (org.apache.commons.lang3.time.StopWatch)13 Autowired (org.springframework.beans.factory.annotation.Autowired)11 Slf4j (lombok.extern.slf4j.Slf4j)10 InputStream (java.io.InputStream)9 Inject (javax.inject.Inject)8 RegisteredTemplate (com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate)7