Search in sources :

Example 31 with StringUtils.join

use of org.apache.commons.lang3.StringUtils.join in project nifi 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) LoggerFactory(org.slf4j.LoggerFactory) LdapTemplate(org.springframework.ldap.core.LdapTemplate) NamingException(javax.naming.NamingException) KeyStoreException(java.security.KeyStoreException) StringUtils(org.apache.commons.lang3.StringUtils) PropertyValue(org.apache.nifi.components.PropertyValue) UserGroupProvider(org.apache.nifi.authorization.UserGroupProvider) Attribute(javax.naming.directory.Attribute) Map(java.util.Map) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) ThreadFactory(java.util.concurrent.ThreadFactory) LdapContextSource(org.springframework.ldap.core.support.LdapContextSource) Set(java.util.Set) AuthorizerConfigurationContext(org.apache.nifi.authorization.AuthorizerConfigurationContext) KeyManagementException(java.security.KeyManagementException) HardcodedFilter(org.springframework.ldap.filter.HardcodedFilter) Executors(java.util.concurrent.Executors) User(org.apache.nifi.authorization.User) UserAndGroups(org.apache.nifi.authorization.UserAndGroups) List(java.util.List) ClientAuth(org.apache.nifi.security.util.SslContextFactory.ClientAuth) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NamingEnumeration(javax.naming.NamingEnumeration) SingleContextSource(org.springframework.ldap.core.support.SingleContextSource) UserGroupProviderInitializationContext(org.apache.nifi.authorization.UserGroupProviderInitializationContext) DirContextOperations(org.springframework.ldap.core.DirContextOperations) LdapsSocketFactory(org.apache.nifi.ldap.LdapsSocketFactory) PagedResultsDirContextProcessor(org.springframework.ldap.control.PagedResultsDirContextProcessor) NullDirContextProcessor(org.springframework.ldap.core.LdapTemplate.NullDirContextProcessor) DirContextProcessor(org.springframework.ldap.core.DirContextProcessor) HashMap(java.util.HashMap) Group(org.apache.nifi.authorization.Group) AtomicReference(java.util.concurrent.atomic.AtomicReference) SearchControls(javax.naming.directory.SearchControls) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SimpleDirContextAuthenticationStrategy(org.springframework.ldap.core.support.SimpleDirContextAuthenticationStrategy) AuthorizerContext(org.apache.nifi.authorization.annotation.AuthorizerContext) UnrecoverableKeyException(java.security.UnrecoverableKeyException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AbstractTlsDirContextAuthenticationStrategy(org.springframework.ldap.core.support.AbstractTlsDirContextAuthenticationStrategy) DefaultTlsDirContextAuthenticationStrategy(org.springframework.ldap.core.support.DefaultTlsDirContextAuthenticationStrategy) Context(javax.naming.Context) IdentityMapping(org.apache.nifi.authorization.util.IdentityMapping) ProviderDestructionException(org.apache.nifi.authentication.exception.ProviderDestructionException) IdentityMappingUtil(org.apache.nifi.authorization.util.IdentityMappingUtil) Logger(org.slf4j.Logger) ContextSource(org.springframework.ldap.core.ContextSource) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TimeUnit(java.util.concurrent.TimeUnit) EqualsFilter(org.springframework.ldap.filter.EqualsFilter) ReferralStrategy(org.apache.nifi.ldap.ReferralStrategy) FormatUtils(org.apache.nifi.util.FormatUtils) NiFiProperties(org.apache.nifi.util.NiFiProperties) SslContextFactory(org.apache.nifi.security.util.SslContextFactory) AuthorizationAccessException(org.apache.nifi.authorization.exception.AuthorizationAccessException) LdapAuthenticationStrategy(org.apache.nifi.ldap.LdapAuthenticationStrategy) Collections(java.util.Collections) Group(org.apache.nifi.authorization.Group) User(org.apache.nifi.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.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.join

use of org.apache.commons.lang3.StringUtils.join in project nifi by apache.

the class FlowResource method scheduleComponents.

/**
 * Updates the specified process group.
 *
 * @param httpServletRequest       request
 * @param id                       The id of the process group.
 * @param requestScheduleComponentsEntity A scheduleComponentsEntity.
 * @return A processGroupEntity.
 */
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("process-groups/{id}")
@ApiOperation(value = "Schedule or unschedule components in the specified Process Group.", response = ScheduleComponentsEntity.class, authorizations = { @Authorization(value = "Read - /flow"), @Authorization(value = "Write - /{component-type}/{uuid} - For every component being scheduled/unscheduled") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response scheduleComponents(@Context HttpServletRequest httpServletRequest, @ApiParam(value = "The process group id.", required = true) @PathParam("id") String id, @ApiParam(value = "The request to schedule or unschedule. If the comopnents in the request are not specified, all authorized components will be considered.", required = true) final ScheduleComponentsEntity requestScheduleComponentsEntity) {
    // ensure the same id is being used
    if (!id.equals(requestScheduleComponentsEntity.getId())) {
        throw new IllegalArgumentException(String.format("The process group id (%s) in the request body does " + "not equal the process group id of the requested resource (%s).", requestScheduleComponentsEntity.getId(), id));
    }
    final ScheduledState state;
    if (requestScheduleComponentsEntity.getState() == null) {
        throw new IllegalArgumentException("The scheduled state must be specified.");
    } else {
        try {
            state = ScheduledState.valueOf(requestScheduleComponentsEntity.getState());
        } catch (final IllegalArgumentException iae) {
            throw new IllegalArgumentException(String.format("The scheduled must be one of [%s].", StringUtils.join(EnumSet.of(ScheduledState.RUNNING, ScheduledState.STOPPED), ", ")));
        }
    }
    // ensure its a supported scheduled state
    if (ScheduledState.DISABLED.equals(state) || ScheduledState.STARTING.equals(state) || ScheduledState.STOPPING.equals(state)) {
        throw new IllegalArgumentException(String.format("The scheduled must be one of [%s].", StringUtils.join(EnumSet.of(ScheduledState.RUNNING, ScheduledState.STOPPED), ", ")));
    }
    // if the components are not specified, gather all components and their current revision
    if (requestScheduleComponentsEntity.getComponents() == null) {
        // get the current revisions for the components being updated
        final Set<Revision> revisions = serviceFacade.getRevisionsFromGroup(id, group -> {
            final Set<String> componentIds = new HashSet<>();
            // ensure authorized for each processor we will attempt to schedule
            group.findAllProcessors().stream().filter(ScheduledState.RUNNING.equals(state) ? ProcessGroup.SCHEDULABLE_PROCESSORS : ProcessGroup.UNSCHEDULABLE_PROCESSORS).filter(processor -> processor.isAuthorized(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser())).forEach(processor -> {
                componentIds.add(processor.getIdentifier());
            });
            // ensure authorized for each input port we will attempt to schedule
            group.findAllInputPorts().stream().filter(ScheduledState.RUNNING.equals(state) ? ProcessGroup.SCHEDULABLE_PORTS : ProcessGroup.UNSCHEDULABLE_PORTS).filter(inputPort -> inputPort.isAuthorized(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser())).forEach(inputPort -> {
                componentIds.add(inputPort.getIdentifier());
            });
            // ensure authorized for each output port we will attempt to schedule
            group.findAllOutputPorts().stream().filter(ScheduledState.RUNNING.equals(state) ? ProcessGroup.SCHEDULABLE_PORTS : ProcessGroup.UNSCHEDULABLE_PORTS).filter(outputPort -> outputPort.isAuthorized(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser())).forEach(outputPort -> {
                componentIds.add(outputPort.getIdentifier());
            });
            return componentIds;
        });
        // build the component mapping
        final Map<String, RevisionDTO> componentsToSchedule = new HashMap<>();
        revisions.forEach(revision -> {
            final RevisionDTO dto = new RevisionDTO();
            dto.setClientId(revision.getClientId());
            dto.setVersion(revision.getVersion());
            componentsToSchedule.put(revision.getComponentId(), dto);
        });
        // set the components and their current revision
        requestScheduleComponentsEntity.setComponents(componentsToSchedule);
    }
    if (isReplicateRequest()) {
        return replicate(HttpMethod.PUT, requestScheduleComponentsEntity);
    }
    final Map<String, RevisionDTO> requestComponentsToSchedule = requestScheduleComponentsEntity.getComponents();
    final Map<String, Revision> requestComponentRevisions = requestComponentsToSchedule.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> getRevision(e.getValue(), e.getKey())));
    final Set<Revision> requestRevisions = new HashSet<>(requestComponentRevisions.values());
    return withWriteLock(serviceFacade, requestScheduleComponentsEntity, requestRevisions, lookup -> {
        // ensure access to the flow
        authorizeFlow();
        // ensure access to every component being scheduled
        requestComponentsToSchedule.keySet().forEach(componentId -> {
            final Authorizable connectable = lookup.getLocalConnectable(componentId);
            connectable.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
        });
    }, () -> serviceFacade.verifyScheduleComponents(id, state, requestComponentRevisions.keySet()), (revisions, scheduleComponentsEntity) -> {
        final ScheduledState scheduledState = ScheduledState.valueOf(scheduleComponentsEntity.getState());
        final Map<String, RevisionDTO> componentsToSchedule = scheduleComponentsEntity.getComponents();
        final Map<String, Revision> componentRevisions = componentsToSchedule.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> getRevision(e.getValue(), e.getKey())));
        // update the process group
        final ScheduleComponentsEntity entity = serviceFacade.scheduleComponents(id, scheduledState, componentRevisions);
        return generateOkResponse(entity).build();
    });
}
Also used : Bundle(org.apache.nifi.bundle.Bundle) DateTimeParameter(org.apache.nifi.web.api.request.DateTimeParameter) StatusHistoryEntity(org.apache.nifi.web.api.entity.StatusHistoryEntity) Produces(javax.ws.rs.Produces) BulletinBoardPatternParameter(org.apache.nifi.web.api.request.BulletinBoardPatternParameter) ApiParam(io.swagger.annotations.ApiParam) StringUtils(org.apache.commons.lang3.StringUtils) BucketsEntity(org.apache.nifi.web.api.entity.BucketsEntity) MediaType(javax.ws.rs.core.MediaType) ProcessGroupDTO(org.apache.nifi.web.api.dto.ProcessGroupDTO) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException) AboutDTO(org.apache.nifi.web.api.dto.AboutDTO) Map(java.util.Map) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException) RegistriesEntity(org.apache.nifi.web.api.entity.RegistriesEntity) CurrentUserEntity(org.apache.nifi.web.api.entity.CurrentUserEntity) ProcessGroupFlowEntity(org.apache.nifi.web.api.entity.ProcessGroupFlowEntity) EnumSet(java.util.EnumSet) HistoryQueryDTO(org.apache.nifi.web.api.dto.action.HistoryQueryDTO) NarClassLoaders(org.apache.nifi.nar.NarClassLoaders) ControllerServicesEntity(org.apache.nifi.web.api.entity.ControllerServicesEntity) Set(java.util.Set) BulletinBoardDTO(org.apache.nifi.web.api.dto.BulletinBoardDTO) ScheduledState(org.apache.nifi.controller.ScheduledState) WebApplicationException(javax.ws.rs.WebApplicationException) ActionEntity(org.apache.nifi.web.api.entity.ActionEntity) ControllerBulletinsEntity(org.apache.nifi.web.api.entity.ControllerBulletinsEntity) RemoteProcessGroupStatusEntity(org.apache.nifi.web.api.entity.RemoteProcessGroupStatusEntity) GET(javax.ws.rs.GET) ControllerServiceEntity(org.apache.nifi.web.api.entity.ControllerServiceEntity) TemplateEntity(org.apache.nifi.web.api.entity.TemplateEntity) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) BulletinBoardEntity(org.apache.nifi.web.api.entity.BulletinBoardEntity) HttpMethod(javax.ws.rs.HttpMethod) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ReportingTaskTypesEntity(org.apache.nifi.web.api.entity.ReportingTaskTypesEntity) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) NodeConnectionState(org.apache.nifi.cluster.coordination.node.NodeConnectionState) Api(io.swagger.annotations.Api) ProcessGroupFlowDTO(org.apache.nifi.web.api.dto.flow.ProcessGroupFlowDTO) FlowDTO(org.apache.nifi.web.api.dto.flow.FlowDTO) FlowConfigurationEntity(org.apache.nifi.web.api.entity.FlowConfigurationEntity) NiFiServiceFacade(org.apache.nifi.web.NiFiServiceFacade) RequestAction(org.apache.nifi.authorization.RequestAction) BannerEntity(org.apache.nifi.web.api.entity.BannerEntity) HistoryDTO(org.apache.nifi.web.api.dto.action.HistoryDTO) ClusteSummaryEntity(org.apache.nifi.web.api.entity.ClusteSummaryEntity) Authorizer(org.apache.nifi.authorization.Authorizer) NiFiProperties(org.apache.nifi.util.NiFiProperties) VersionedFlowSnapshotMetadataEntity(org.apache.nifi.web.api.entity.VersionedFlowSnapshotMetadataEntity) VersionedFlowSnapshotMetadataSetEntity(org.apache.nifi.web.api.entity.VersionedFlowSnapshotMetadataSetEntity) ProcessorStatusEntity(org.apache.nifi.web.api.entity.ProcessorStatusEntity) ApiResponse(io.swagger.annotations.ApiResponse) BucketEntity(org.apache.nifi.web.api.entity.BucketEntity) ScheduleComponentsEntity(org.apache.nifi.web.api.entity.ScheduleComponentsEntity) VersionedFlowEntity(org.apache.nifi.web.api.entity.VersionedFlowEntity) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ProcessGroup(org.apache.nifi.groups.ProcessGroup) BulletinQueryDTO(org.apache.nifi.web.api.dto.BulletinQueryDTO) Date(java.util.Date) Path(javax.ws.rs.Path) ClusterSummaryDTO(org.apache.nifi.web.api.dto.ClusterSummaryDTO) ProcessGroupStatusEntity(org.apache.nifi.web.api.entity.ProcessGroupStatusEntity) ApiOperation(io.swagger.annotations.ApiOperation) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) ControllerStatusDTO(org.apache.nifi.web.api.dto.status.ControllerStatusDTO) ActivateControllerServicesEntity(org.apache.nifi.web.api.entity.ActivateControllerServicesEntity) ConnectionStatusEntity(org.apache.nifi.web.api.entity.ConnectionStatusEntity) ControllerStatusEntity(org.apache.nifi.web.api.entity.ControllerStatusEntity) DefaultValue(javax.ws.rs.DefaultValue) IntegerParameter(org.apache.nifi.web.api.request.IntegerParameter) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) HistoryEntity(org.apache.nifi.web.api.entity.HistoryEntity) Context(javax.ws.rs.core.Context) Authorizable(org.apache.nifi.authorization.resource.Authorizable) ReportingTaskEntity(org.apache.nifi.web.api.entity.ReportingTaskEntity) Predicate(java.util.function.Predicate) NodeSearchResultDTO(org.apache.nifi.web.api.dto.search.NodeSearchResultDTO) ClusterSearchResultsEntity(org.apache.nifi.web.api.entity.ClusterSearchResultsEntity) LongParameter(org.apache.nifi.web.api.request.LongParameter) Collectors(java.util.stream.Collectors) List(java.util.List) Response(javax.ws.rs.core.Response) BannerDTO(org.apache.nifi.web.api.dto.BannerDTO) ProcessGroupEntity(org.apache.nifi.web.api.entity.ProcessGroupEntity) ControllerServiceState(org.apache.nifi.controller.service.ControllerServiceState) SearchResultsEntity(org.apache.nifi.web.api.entity.SearchResultsEntity) PathParam(javax.ws.rs.PathParam) Revision(org.apache.nifi.web.Revision) TemplatesEntity(org.apache.nifi.web.api.entity.TemplatesEntity) ClusterDTO(org.apache.nifi.web.api.dto.ClusterDTO) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) HashMap(java.util.HashMap) ApiResponses(io.swagger.annotations.ApiResponses) BundleDetails(org.apache.nifi.bundle.BundleDetails) HashSet(java.util.HashSet) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) ControllerServiceTypesEntity(org.apache.nifi.web.api.entity.ControllerServiceTypesEntity) PrioritizerTypesEntity(org.apache.nifi.web.api.entity.PrioritizerTypesEntity) ProcessorTypesEntity(org.apache.nifi.web.api.entity.ProcessorTypesEntity) RegistryClientsEntity(org.apache.nifi.web.api.entity.RegistryClientsEntity) PortStatusEntity(org.apache.nifi.web.api.entity.PortStatusEntity) ComponentHistoryEntity(org.apache.nifi.web.api.entity.ComponentHistoryEntity) NiFiUserUtils(org.apache.nifi.authorization.user.NiFiUserUtils) SearchResultsDTO(org.apache.nifi.web.api.dto.search.SearchResultsDTO) AboutEntity(org.apache.nifi.web.api.entity.AboutEntity) RegistryEntity(org.apache.nifi.web.api.entity.RegistryEntity) NodeDTO(org.apache.nifi.web.api.dto.NodeDTO) PUT(javax.ws.rs.PUT) IllegalClusterResourceRequestException(org.apache.nifi.web.IllegalClusterResourceRequestException) Authorization(io.swagger.annotations.Authorization) VersionedFlowsEntity(org.apache.nifi.web.api.entity.VersionedFlowsEntity) ReportingTasksEntity(org.apache.nifi.web.api.entity.ReportingTasksEntity) HashMap(java.util.HashMap) ScheduleComponentsEntity(org.apache.nifi.web.api.entity.ScheduleComponentsEntity) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) Revision(org.apache.nifi.web.Revision) ScheduledState(org.apache.nifi.controller.ScheduledState) Authorizable(org.apache.nifi.authorization.resource.Authorizable) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 33 with StringUtils.join

use of org.apache.commons.lang3.StringUtils.join in project nifi by apache.

the class ControllerFacade method setComponentDetails.

private void setComponentDetails(final ProvenanceEventDTO dto) {
    final ProcessGroup root = flowController.getGroup(flowController.getRootGroupId());
    final Connectable connectable = root.findLocalConnectable(dto.getComponentId());
    if (connectable != null) {
        dto.setGroupId(connectable.getProcessGroup().getIdentifier());
        dto.setComponentName(connectable.getName());
        return;
    }
    final RemoteGroupPort remoteGroupPort = root.findRemoteGroupPort(dto.getComponentId());
    if (remoteGroupPort != null) {
        dto.setGroupId(remoteGroupPort.getProcessGroupIdentifier());
        dto.setComponentName(remoteGroupPort.getName());
        return;
    }
    final Connection connection = root.findConnection(dto.getComponentId());
    if (connection != null) {
        dto.setGroupId(connection.getProcessGroup().getIdentifier());
        String name = connection.getName();
        final Collection<Relationship> relationships = connection.getRelationships();
        if (StringUtils.isBlank(name) && CollectionUtils.isNotEmpty(relationships)) {
            name = StringUtils.join(relationships.stream().map(relationship -> relationship.getName()).collect(Collectors.toSet()), ", ");
        }
        dto.setComponentName(name);
        return;
    }
}
Also used : Bundle(org.apache.nifi.bundle.Bundle) Arrays(java.util.Arrays) SearchableFields(org.apache.nifi.provenance.SearchableFields) StringUtils(org.apache.commons.lang3.StringUtils) QueueSize(org.apache.nifi.controller.queue.QueueSize) ClassUtils(org.apache.commons.lang3.ClassUtils) ROOT_GROUP_ID_ALIAS(org.apache.nifi.controller.FlowController.ROOT_GROUP_ID_ALIAS) AuthorizationResult(org.apache.nifi.authorization.AuthorizationResult) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) Map(java.util.Map) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException) ProvenanceRepository(org.apache.nifi.provenance.ProvenanceRepository) RootGroupPort(org.apache.nifi.remote.RootGroupPort) Connectable(org.apache.nifi.connectable.Connectable) Connection(org.apache.nifi.connectable.Connection) ProcessorStatus(org.apache.nifi.controller.status.ProcessorStatus) FlowFilePrioritizer(org.apache.nifi.flowfile.FlowFilePrioritizer) ProcessGroupStatus(org.apache.nifi.controller.status.ProcessGroupStatus) Set(java.util.Set) FlowController(org.apache.nifi.controller.FlowController) VariableRegistry(org.apache.nifi.registry.VariableRegistry) AttributeDTO(org.apache.nifi.web.api.dto.provenance.AttributeDTO) ControllerService(org.apache.nifi.controller.ControllerService) RemoteProcessGroup(org.apache.nifi.groups.RemoteProcessGroup) WebApplicationException(javax.ws.rs.WebApplicationException) ExtensionManager(org.apache.nifi.nar.ExtensionManager) ConnectionStatus(org.apache.nifi.controller.status.ConnectionStatus) ComponentStatusRepository(org.apache.nifi.controller.status.history.ComponentStatusRepository) LineageRequestDTO(org.apache.nifi.web.api.dto.provenance.lineage.LineageRequestDTO) ProvenanceRequestDTO(org.apache.nifi.web.api.dto.provenance.ProvenanceRequestDTO) DtoFactory(org.apache.nifi.web.api.dto.DtoFactory) Resource(org.apache.nifi.authorization.Resource) Counter(org.apache.nifi.controller.Counter) ProvenanceEventDTO(org.apache.nifi.web.api.dto.provenance.ProvenanceEventDTO) AccessDeniedException(org.apache.nifi.authorization.AccessDeniedException) ControllerServiceEntity(org.apache.nifi.web.api.entity.ControllerServiceEntity) ConfigurableComponent(org.apache.nifi.components.ConfigurableComponent) CollectionUtils(org.apache.commons.collections4.CollectionUtils) TreeSet(java.util.TreeSet) LineageDTO(org.apache.nifi.web.api.dto.provenance.lineage.LineageDTO) ArrayList(java.util.ArrayList) ContentAvailability(org.apache.nifi.controller.ContentAvailability) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) Relationship(org.apache.nifi.processor.Relationship) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) ControllerServiceProvider(org.apache.nifi.controller.service.ControllerServiceProvider) ProcessGroupCounts(org.apache.nifi.groups.ProcessGroupCounts) ProvenanceDTO(org.apache.nifi.web.api.dto.provenance.ProvenanceDTO) Collator(java.text.Collator) DocumentedTypeDTO(org.apache.nifi.web.api.dto.DocumentedTypeDTO) ContentDirection(org.apache.nifi.controller.repository.claim.ContentDirection) RequestAction(org.apache.nifi.authorization.RequestAction) ProvenanceOptionsDTO(org.apache.nifi.web.api.dto.provenance.ProvenanceOptionsDTO) IOException(java.io.IOException) Authorizer(org.apache.nifi.authorization.Authorizer) NiFiProperties(org.apache.nifi.util.NiFiProperties) ReportingTask(org.apache.nifi.reporting.ReportingTask) CoreAttributes(org.apache.nifi.flowfile.attributes.CoreAttributes) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ProcessGroup(org.apache.nifi.groups.ProcessGroup) SortedSet(java.util.SortedSet) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) Query(org.apache.nifi.provenance.search.Query) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) Port(org.apache.nifi.connectable.Port) FlowService(org.apache.nifi.services.FlowService) BundleDTO(org.apache.nifi.web.api.dto.BundleDTO) ControllerStatusDTO(org.apache.nifi.web.api.dto.status.ControllerStatusDTO) Locale(java.util.Locale) SearchableField(org.apache.nifi.provenance.search.SearchableField) Template(org.apache.nifi.controller.Template) Label(org.apache.nifi.controller.label.Label) ProvenanceSearchableFieldDTO(org.apache.nifi.web.api.dto.provenance.ProvenanceSearchableFieldDTO) Authorizable(org.apache.nifi.authorization.resource.Authorizable) RequiredPermission(org.apache.nifi.components.RequiredPermission) TimeZone(java.util.TimeZone) Collection(java.util.Collection) BundleUtils(org.apache.nifi.util.BundleUtils) Collectors(java.util.stream.Collectors) ResourceFactory(org.apache.nifi.authorization.resource.ResourceFactory) Processor(org.apache.nifi.processor.Processor) SystemDiagnostics(org.apache.nifi.diagnostics.SystemDiagnostics) List(java.util.List) Result(org.apache.nifi.authorization.AuthorizationResult.Result) StatusHistoryDTO(org.apache.nifi.web.api.dto.status.StatusHistoryDTO) ProvenanceResultsDTO(org.apache.nifi.web.api.dto.provenance.ProvenanceResultsDTO) SearchTerms(org.apache.nifi.provenance.search.SearchTerms) ProcessorNode(org.apache.nifi.controller.ProcessorNode) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) HashMap(java.util.HashMap) Function(java.util.function.Function) HashSet(java.util.HashSet) QueryResult(org.apache.nifi.provenance.search.QueryResult) ReportingTaskNode(org.apache.nifi.controller.ReportingTaskNode) SearchTerm(org.apache.nifi.provenance.search.SearchTerm) QuerySubmission(org.apache.nifi.provenance.search.QuerySubmission) ContentNotFoundException(org.apache.nifi.controller.repository.ContentNotFoundException) Logger(org.slf4j.Logger) RemoteGroupPort(org.apache.nifi.remote.RemoteGroupPort) ProcessorDiagnosticsDTO(org.apache.nifi.web.api.dto.diagnostics.ProcessorDiagnosticsDTO) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) RemoteProcessGroupStatus(org.apache.nifi.controller.status.RemoteProcessGroupStatus) NiFiCoreException(org.apache.nifi.web.NiFiCoreException) ComputeLineageSubmission(org.apache.nifi.provenance.lineage.ComputeLineageSubmission) FormatUtils(org.apache.nifi.util.FormatUtils) NiFiUserUtils(org.apache.nifi.authorization.user.NiFiUserUtils) PortStatus(org.apache.nifi.controller.status.PortStatus) SearchResultsDTO(org.apache.nifi.web.api.dto.search.SearchResultsDTO) BulletinRepository(org.apache.nifi.reporting.BulletinRepository) DownloadableContent(org.apache.nifi.web.DownloadableContent) LineageRequestType(org.apache.nifi.web.api.dto.provenance.lineage.LineageRequestDTO.LineageRequestType) Comparator(java.util.Comparator) Collections(java.util.Collections) InputStream(java.io.InputStream) Connectable(org.apache.nifi.connectable.Connectable) RemoteGroupPort(org.apache.nifi.remote.RemoteGroupPort) Relationship(org.apache.nifi.processor.Relationship) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) RemoteProcessGroup(org.apache.nifi.groups.RemoteProcessGroup) ProcessGroup(org.apache.nifi.groups.ProcessGroup) Connection(org.apache.nifi.connectable.Connection)

Example 34 with StringUtils.join

use of org.apache.commons.lang3.StringUtils.join in project swift by luastar.

the class ExcelUtils method readXlsxSheet.

/**
 * 从sheet中读取数据
 *
 * @param workbook
 * @param sheetConfig
 * @throws Exception
 */
private static void readXlsxSheet(XSSFWorkbook workbook, ImportSheet sheetConfig) throws Exception {
    if (workbook == null || sheetConfig == null || sheetConfig.getDataClass() == null || sheetConfig.getColumnList() == null) {
        throw new IllegalArgumentException("excel导入参数错误!");
    }
    // 公式执行器
    CreationHelper createHelper = workbook.getCreationHelper();
    FormulaEvaluator formulaEvaluator = createHelper.createFormulaEvaluator();
    int sheetNum = workbook.getNumberOfSheets();
    if (sheetConfig.getIndex() >= sheetNum) {
        String msg = StrUtils.formatString("sheet【{0}】不存在", sheetConfig.getIndex() + 1);
        throw new RuntimeException(msg);
    }
    XSSFSheet sheet = workbook.getSheetAt(sheetConfig.getIndex());
    int firstRowNum = sheet.getFirstRowNum();
    int lastRowNum = sheet.getLastRowNum();
    if (lastRowNum < 1) {
        String msg = StrUtils.formatString("sheet【{0}】数据为空", sheet.getSheetName());
        throw new RuntimeException(msg);
    }
    // 通过标题找对应的列
    List<String> columnNotFound = Lists.newArrayList();
    List<ImportColumn> columnList = sheetConfig.getColumnList();
    int columnNum = columnList.size();
    XSSFRow titleRow = sheet.getRow(firstRowNum);
    for (int i = 0; i < columnNum; i++) {
        ImportColumn column = columnList.get(i);
        for (int j = 0; j < columnNum; j++) {
            XSSFCell cell = titleRow.getCell(j);
            if (cell != null && column.getTitle().equals(cell.getStringCellValue())) {
                column.setColumnIndex(j);
            }
        }
        if (column.getColumnIndex() == null) {
            columnNotFound.add(column.getTitle());
        }
    }
    // 找不到对应的列
    if (columnNotFound.size() > 0) {
        String msg = StrUtils.formatString("列【{0}】不存在", StringUtils.join(columnNotFound, ","));
        throw new RuntimeException(msg);
    }
    // 获取数据
    List<ExcelData> dataList = Lists.newArrayList();
    for (int i = firstRowNum + 1; i <= lastRowNum; i++) {
        XSSFRow row = sheet.getRow(i);
        Object data = sheetConfig.getDataClass().newInstance();
        if (row == null) {
            ExcelData excelData = new ExcelData(i, data);
            excelData.setCheckMsg("获取行数据为空");
            dataList.add(excelData);
            continue;
        }
        // 行不为空
        List<String> setPropList = Lists.newArrayList();
        for (int j = 0; j < columnNum; j++) {
            ImportColumn column = columnList.get(j);
            XSSFCell cell = row.getCell(column.getColumnIndex());
            setPropList.add(ExcelUtils.setProperty(column, cell, data, formulaEvaluator));
        }
        ExcelData excelData = new ExcelData(i, data);
        // 赋值失败的列
        List setErrList = setPropList.stream().filter(rs -> rs != null).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(setErrList)) {
            String msg = StrUtils.formatString("获取以下属性的值失败:{0}", StringUtils.join(setErrList, ","));
            excelData.setCheckMsg(msg);
        }
        dataList.add(excelData);
    }
    sheetConfig.setDataList(dataList);
}
Also used : CollectionUtils(com.luastar.swift.base.utils.CollectionUtils) PropertyUtils(org.apache.commons.beanutils.PropertyUtils) Logger(org.slf4j.Logger) org.apache.poi.hssf.usermodel(org.apache.poi.hssf.usermodel) ImmutableMap(com.google.common.collect.ImmutableMap) ObjUtils(com.luastar.swift.base.utils.ObjUtils) Date(java.util.Date) StrUtils(com.luastar.swift.base.utils.StrUtils) LoggerFactory(org.slf4j.LoggerFactory) MethodUtils(org.apache.commons.lang3.reflect.MethodUtils) ArrayUtils(org.apache.commons.lang3.ArrayUtils) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) org.apache.poi.ss.usermodel(org.apache.poi.ss.usermodel) org.apache.poi.xssf.usermodel(org.apache.poi.xssf.usermodel) BigDecimal(java.math.BigDecimal) List(java.util.List) JSON(com.alibaba.fastjson.JSON) Lists(com.google.common.collect.Lists) java.io(java.io) DateUtils(com.luastar.swift.base.utils.DateUtils) Map(java.util.Map) CellRangeAddressList(org.apache.poi.ss.util.CellRangeAddressList) List(java.util.List) CellRangeAddressList(org.apache.poi.ss.util.CellRangeAddressList)

Example 35 with StringUtils.join

use of org.apache.commons.lang3.StringUtils.join in project fess by codelibs.

the class AbstractFessFileTransformer method generateData.

protected Map<String, Object> generateData(final ResponseData responseData) {
    final Extractor extractor = getExtractor(responseData);
    final Map<String, String> params = new HashMap<>();
    params.put(TikaMetadataKeys.RESOURCE_NAME_KEY, getResourceName(responseData));
    final String mimeType = responseData.getMimeType();
    params.put(HttpHeaders.CONTENT_TYPE, mimeType);
    params.put(HttpHeaders.CONTENT_ENCODING, responseData.getCharSet());
    final StringBuilder contentMetaBuf = new StringBuilder(1000);
    final Map<String, Object> dataMap = new HashMap<>();
    final Map<String, Object> metaDataMap = new HashMap<>();
    String content;
    try (final InputStream in = responseData.getResponseBody()) {
        final ExtractData extractData = getExtractData(extractor, in, params);
        content = extractData.getContent();
        if (fessConfig.isCrawlerDocumentFileIgnoreEmptyContent() && StringUtil.isBlank(content)) {
            return null;
        }
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("ExtractData: " + extractData);
        }
        // meta
        //
        extractData.getKeySet().stream().filter(//
        k -> extractData.getValues(k) != null).forEach(key -> {
            final String[] values = extractData.getValues(key);
            metaDataMap.put(key, values);
            if (fessConfig.isCrawlerMetadataContentIncluded(key)) {
                final String joinedValue = StringUtils.join(values, ' ');
                if (StringUtil.isNotBlank(joinedValue)) {
                    if (contentMetaBuf.length() > 0) {
                        contentMetaBuf.append(' ');
                    }
                    contentMetaBuf.append(joinedValue.trim());
                }
            }
            final Pair<String, String> mapping = fessConfig.getCrawlerMetadataNameMapping(key);
            if (mapping != null) {
                if (Constants.MAPPING_TYPE_ARRAY.equalsIgnoreCase(mapping.getSecond())) {
                    dataMap.put(mapping.getFirst(), values);
                } else if (Constants.MAPPING_TYPE_STRING.equalsIgnoreCase(mapping.getSecond())) {
                    final String joinedValue = StringUtils.join(values, ' ');
                    dataMap.put(mapping.getFirst(), joinedValue.trim());
                } else if (values.length == 1) {
                    try {
                        if (Constants.MAPPING_TYPE_LONG.equalsIgnoreCase(mapping.getSecond())) {
                            dataMap.put(mapping.getFirst(), Long.parseLong(values[0]));
                        } else if (Constants.MAPPING_TYPE_DOUBLE.equalsIgnoreCase(mapping.getSecond())) {
                            dataMap.put(mapping.getFirst(), Double.parseDouble(values[0]));
                        } else {
                            logger.warn("Unknown mapping type: {}={}", key, mapping);
                        }
                    } catch (final NumberFormatException e) {
                        logger.warn("Failed to parse " + values[0], e);
                    }
                }
            }
        });
    } catch (final Exception e) {
        final CrawlingAccessException rcae = new CrawlingAccessException("Could not get a text from " + responseData.getUrl(), e);
        rcae.setLogLevel(CrawlingAccessException.WARN);
        throw rcae;
    }
    if (content == null) {
        content = StringUtil.EMPTY;
    }
    final String contentMeta = contentMetaBuf.toString().trim();
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final CrawlingInfoHelper crawlingInfoHelper = ComponentUtil.getCrawlingInfoHelper();
    final String sessionId = crawlingInfoHelper.getCanonicalSessionId(responseData.getSessionId());
    final PathMappingHelper pathMappingHelper = ComponentUtil.getPathMappingHelper();
    final CrawlingConfigHelper crawlingConfigHelper = ComponentUtil.getCrawlingConfigHelper();
    final CrawlingConfig crawlingConfig = crawlingConfigHelper.get(responseData.getSessionId());
    final Date documentExpires = crawlingInfoHelper.getDocumentExpires(crawlingConfig);
    final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
    final FileTypeHelper fileTypeHelper = ComponentUtil.getFileTypeHelper();
    final DocumentHelper documentHelper = ComponentUtil.getDocumentHelper();
    String url = responseData.getUrl();
    final String indexingTarget = crawlingConfig.getIndexingTarget(url);
    url = pathMappingHelper.replaceUrl(sessionId, url);
    final Map<String, String> fieldConfigMap = crawlingConfig.getConfigParameterMap(ConfigName.FIELD);
    String urlEncoding;
    final UrlQueue<?> urlQueue = CrawlingParameterUtil.getUrlQueue();
    if (urlQueue != null && urlQueue.getEncoding() != null) {
        urlEncoding = urlQueue.getEncoding();
    } else {
        urlEncoding = responseData.getCharSet();
    }
    // cid
    final String configId = crawlingConfig.getConfigId();
    if (configId != null) {
        putResultDataBody(dataMap, fessConfig.getIndexFieldConfigId(), configId);
    }
    //  expires
    if (documentExpires != null) {
        putResultDataBody(dataMap, fessConfig.getIndexFieldExpires(), documentExpires);
    }
    // segment
    putResultDataBody(dataMap, fessConfig.getIndexFieldSegment(), sessionId);
    // content
    final StringBuilder buf = new StringBuilder(content.length() + 1000);
    if (fessConfig.isCrawlerDocumentFileAppendBodyContent()) {
        buf.append(content);
    }
    if (fessConfig.isCrawlerDocumentFileAppendMetaContent()) {
        if (buf.length() > 0) {
            buf.append(' ');
        }
        buf.append(contentMeta);
    }
    final String bodyBase = buf.toString().trim();
    final String body = documentHelper.getContent(responseData, bodyBase, dataMap);
    putResultDataBody(dataMap, fessConfig.getIndexFieldContent(), body);
    if ((Constants.TRUE.equalsIgnoreCase(fieldConfigMap.get(fessConfig.getIndexFieldCache())) || fessConfig.isCrawlerDocumentCacheEnabled()) && fessConfig.isSupportedDocumentCacheMimetypes(mimeType)) {
        if (responseData.getContentLength() > 0 && responseData.getContentLength() <= fessConfig.getCrawlerDocumentCacheMaxSizeAsInteger().longValue()) {
            final String cache = content.trim().replaceAll("[ \\t\\x0B\\f]+", " ");
            // text cache
            putResultDataBody(dataMap, fessConfig.getIndexFieldCache(), cache);
            putResultDataBody(dataMap, fessConfig.getIndexFieldHasCache(), Constants.TRUE);
        }
    }
    // digest
    putResultDataBody(dataMap, fessConfig.getIndexFieldDigest(), documentHelper.getDigest(responseData, bodyBase, dataMap, fessConfig.getCrawlerDocumentFileMaxDigestLengthAsInteger()));
    // title
    final String fileName = getFileName(url, urlEncoding);
    if (!dataMap.containsKey(fessConfig.getIndexFieldTitle())) {
        if (url.endsWith("/")) {
            if (StringUtil.isNotBlank(content)) {
                putResultDataBody(dataMap, fessConfig.getIndexFieldTitle(), documentHelper.getDigest(responseData, body, dataMap, fessConfig.getCrawlerDocumentFileMaxTitleLengthAsInteger()));
            } else {
                putResultDataBody(dataMap, fessConfig.getIndexFieldTitle(), fessConfig.getCrawlerDocumentFileNoTitleLabel());
            }
        } else {
            if (StringUtil.isBlank(fileName)) {
                putResultDataBody(dataMap, fessConfig.getIndexFieldTitle(), decodeUrlAsName(url, url.startsWith("file:")));
            } else {
                putResultDataBody(dataMap, fessConfig.getIndexFieldTitle(), fileName);
            }
        }
    }
    // host
    putResultDataBody(dataMap, fessConfig.getIndexFieldHost(), getHostOnFile(url));
    // site
    putResultDataBody(dataMap, fessConfig.getIndexFieldSite(), getSiteOnFile(url, urlEncoding));
    // filename
    if (StringUtil.isNotBlank(fileName)) {
        putResultDataBody(dataMap, fessConfig.getIndexFieldFilename(), fileName);
    }
    // url
    putResultDataBody(dataMap, fessConfig.getIndexFieldUrl(), url);
    // created
    final Date now = systemHelper.getCurrentTime();
    putResultDataBody(dataMap, fessConfig.getIndexFieldCreated(), now);
    // TODO anchor
    putResultDataBody(dataMap, fessConfig.getIndexFieldAnchor(), StringUtil.EMPTY);
    // mimetype
    putResultDataBody(dataMap, fessConfig.getIndexFieldMimetype(), mimeType);
    if (fileTypeHelper != null) {
        // filetype
        putResultDataBody(dataMap, fessConfig.getIndexFieldFiletype(), fileTypeHelper.get(mimeType));
    }
    // content_length
    putResultDataBody(dataMap, fessConfig.getIndexFieldContentLength(), Long.toString(responseData.getContentLength()));
    // last_modified
    final Date lastModified = responseData.getLastModified();
    if (lastModified != null) {
        putResultDataBody(dataMap, fessConfig.getIndexFieldLastModified(), lastModified);
        // timestamp
        putResultDataBody(dataMap, fessConfig.getIndexFieldTimestamp(), lastModified);
    } else {
        // timestamp
        putResultDataBody(dataMap, fessConfig.getIndexFieldTimestamp(), now);
    }
    // indexingTarget
    putResultDataBody(dataMap, Constants.INDEXING_TARGET, indexingTarget);
    //  boost
    putResultDataBody(dataMap, fessConfig.getIndexFieldBoost(), crawlingConfig.getDocumentBoost());
    // label: labelType
    final Set<String> labelTypeSet = new HashSet<>();
    for (final String labelType : crawlingConfig.getLabelTypeValues()) {
        labelTypeSet.add(labelType);
    }
    final LabelTypeHelper labelTypeHelper = ComponentUtil.getLabelTypeHelper();
    labelTypeSet.addAll(labelTypeHelper.getMatchedLabelValueSet(url));
    putResultDataBody(dataMap, fessConfig.getIndexFieldLabel(), labelTypeSet);
    // role: roleType
    final List<String> roleTypeList = getRoleTypes(responseData);
    stream(crawlingConfig.getPermissions()).of(stream -> stream.forEach(p -> roleTypeList.add(p)));
    putResultDataBody(dataMap, fessConfig.getIndexFieldRole(), roleTypeList);
    // lang
    if (StringUtil.isNotBlank(fessConfig.getCrawlerDocumentFileDefaultLang())) {
        putResultDataBody(dataMap, fessConfig.getIndexFieldLang(), fessConfig.getCrawlerDocumentFileDefaultLang());
    }
    // id
    putResultDataBody(dataMap, fessConfig.getIndexFieldId(), crawlingInfoHelper.generateId(dataMap));
    // parentId
    String parentUrl = responseData.getParentUrl();
    if (StringUtil.isNotBlank(parentUrl)) {
        parentUrl = pathMappingHelper.replaceUrl(sessionId, parentUrl);
        putResultDataBody(dataMap, fessConfig.getIndexFieldUrl(), parentUrl);
        putResultDataBody(dataMap, fessConfig.getIndexFieldParentId(), crawlingInfoHelper.generateId(dataMap));
        // set again
        putResultDataBody(dataMap, fessConfig.getIndexFieldUrl(), url);
    }
    // from config
    final Map<String, String> scriptConfigMap = crawlingConfig.getConfigParameterMap(ConfigName.SCRIPT);
    final Map<String, String> metaConfigMap = crawlingConfig.getConfigParameterMap(ConfigName.META);
    for (final Map.Entry<String, String> entry : metaConfigMap.entrySet()) {
        final String key = entry.getKey();
        final String[] values = entry.getValue().split(",");
        for (final String value : values) {
            putResultDataWithTemplate(dataMap, key, metaDataMap.get(value), scriptConfigMap.get(key));
        }
    }
    final Map<String, String> valueConfigMap = crawlingConfig.getConfigParameterMap(ConfigName.VALUE);
    for (final Map.Entry<String, String> entry : valueConfigMap.entrySet()) {
        final String key = entry.getKey();
        putResultDataWithTemplate(dataMap, key, entry.getValue(), scriptConfigMap.get(key));
    }
    return dataMap;
}
Also used : CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) Constants(org.codelibs.fess.Constants) CrawlingConfigHelper(org.codelibs.fess.helper.CrawlingConfigHelper) URLDecoder(java.net.URLDecoder) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) Pair(org.codelibs.core.misc.Pair) HashMap(java.util.HashMap) TikaMetadataKeys(org.apache.tika.metadata.TikaMetadataKeys) SerializeUtil(org.codelibs.core.io.SerializeUtil) CrawlingParameterUtil(org.codelibs.fess.crawler.util.CrawlingParameterUtil) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) StringUtils(org.apache.commons.lang3.StringUtils) CrawlingConfig(org.codelibs.fess.es.config.exentity.CrawlingConfig) ConfigName(org.codelibs.fess.es.config.exentity.CrawlingConfig.ConfigName) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) ExtractData(org.codelibs.fess.crawler.entity.ExtractData) Map(java.util.Map) AccessResultData(org.codelibs.fess.crawler.entity.AccessResultData) AbstractTransformer(org.codelibs.fess.crawler.transformer.impl.AbstractTransformer) SambaHelper(org.codelibs.fess.helper.SambaHelper) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) Logger(org.slf4j.Logger) ResultData(org.codelibs.fess.crawler.entity.ResultData) Extractor(org.codelibs.fess.crawler.extractor.Extractor) StringUtil(org.codelibs.core.lang.StringUtil) SID(jcifs.smb.SID) Set(java.util.Set) List(java.util.List) ACE(jcifs.smb.ACE) FileTypeHelper(org.codelibs.fess.helper.FileTypeHelper) ComponentUtil(org.codelibs.fess.util.ComponentUtil) CrawlingInfoHelper(org.codelibs.fess.helper.CrawlingInfoHelper) SystemHelper(org.codelibs.fess.helper.SystemHelper) LabelTypeHelper(org.codelibs.fess.helper.LabelTypeHelper) PathMappingHelper(org.codelibs.fess.helper.PathMappingHelper) DocumentHelper(org.codelibs.fess.helper.DocumentHelper) HttpHeaders(org.apache.tika.metadata.HttpHeaders) SmbClient(org.codelibs.fess.crawler.client.smb.SmbClient) UrlQueue(org.codelibs.fess.crawler.entity.UrlQueue) InputStream(java.io.InputStream) ResponseData(org.codelibs.fess.crawler.entity.ResponseData) HashMap(java.util.HashMap) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) CrawlingConfig(org.codelibs.fess.es.config.exentity.CrawlingConfig) PathMappingHelper(org.codelibs.fess.helper.PathMappingHelper) CrawlingInfoHelper(org.codelibs.fess.helper.CrawlingInfoHelper) Extractor(org.codelibs.fess.crawler.extractor.Extractor) HashSet(java.util.HashSet) ExtractData(org.codelibs.fess.crawler.entity.ExtractData) DocumentHelper(org.codelibs.fess.helper.DocumentHelper) InputStream(java.io.InputStream) LabelTypeHelper(org.codelibs.fess.helper.LabelTypeHelper) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) Date(java.util.Date) CrawlingConfigHelper(org.codelibs.fess.helper.CrawlingConfigHelper) SystemHelper(org.codelibs.fess.helper.SystemHelper) FileTypeHelper(org.codelibs.fess.helper.FileTypeHelper) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

StringUtils (org.apache.commons.lang3.StringUtils)34 List (java.util.List)30 Collectors (java.util.stream.Collectors)23 ArrayList (java.util.ArrayList)21 Map (java.util.Map)17 HashMap (java.util.HashMap)15 Set (java.util.Set)14 Logger (org.slf4j.Logger)14 LoggerFactory (org.slf4j.LoggerFactory)14 IOException (java.io.IOException)13 HashSet (java.util.HashSet)11 Arrays (java.util.Arrays)10 Collections (java.util.Collections)10 Date (java.util.Date)9 File (java.io.File)6 StopWatch (org.apache.commons.lang3.time.StopWatch)6 InputStream (java.io.InputStream)5 java.util (java.util)5 Pair (org.apache.commons.lang3.tuple.Pair)5 Path (java.nio.file.Path)4