Search in sources :

Example 41 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project adeptj-runtime by AdeptJ.

the class Server method populateCredentialsStore.

private void populateCredentialsStore(Config undertowConf) {
    try (MVStore store = MVStore.open(MV_CREDENTIALS_STORE)) {
        MVMap<String, String> credentials = store.openMap(H2_MAP_ADMIN_CREDENTIALS);
        // put the default password only when it is not set from web console.
        undertowConf.getObject(KEY_USER_CREDENTIAL_MAPPING).entrySet().stream().filter(entry -> StringUtils.isEmpty(credentials.get(entry.getKey()))).forEach(entry -> credentials.put(entry.getKey(), ((String) entry.getValue().unwrapped()).substring(PWD_START_INDEX)));
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) KEY_HTTPS(com.adeptj.runtime.server.ServerConstants.KEY_HTTPS) RequestLimit(io.undertow.server.handlers.RequestLimit) SSLContext(javax.net.ssl.SSLContext) KEY_HTTP_ONLY(com.adeptj.runtime.server.ServerConstants.KEY_HTTP_ONLY) LogbackManagerHolder(com.adeptj.runtime.common.LogbackManagerHolder) StringUtils(org.apache.commons.lang3.StringUtils) HttpString(io.undertow.util.HttpString) KEY_LOGBACK_STATUS_SERVLET_PATH(com.adeptj.runtime.common.Constants.KEY_LOGBACK_STATUS_SERVLET_PATH) Environment(com.adeptj.runtime.common.Environment) GeneralSecurityException(java.security.GeneralSecurityException) REALM(com.adeptj.runtime.server.ServerConstants.REALM) KEY_ERROR_HANDLER_CODES(com.adeptj.runtime.common.Constants.KEY_ERROR_HANDLER_CODES) Path(java.nio.file.Path) ATTRIBUTE_BUNDLE_CONTEXT(com.adeptj.runtime.common.Constants.ATTRIBUTE_BUNDLE_CONTEXT) WORKER_TASK_CORE_THREADS(org.xnio.Options.WORKER_TASK_CORE_THREADS) MV_CREDENTIALS_STORE(com.adeptj.runtime.common.Constants.MV_CREDENTIALS_STORE) KEY_ERROR_HANDLER_PATH(com.adeptj.runtime.common.Constants.KEY_ERROR_HANDLER_PATH) AllowedMethodsHandler(io.undertow.server.handlers.AllowedMethodsHandler) Set(java.util.Set) KEY_WORKER_OPTIONS(com.adeptj.runtime.server.ServerConstants.KEY_WORKER_OPTIONS) LOGBACK_STATUS_SERVLET_NAME(com.adeptj.runtime.server.ServerConstants.LOGBACK_STATUS_SERVLET_NAME) KEY_PORT(com.adeptj.runtime.common.Constants.KEY_PORT) SYS_PROP_SERVER_HTTPS_PORT(com.adeptj.runtime.server.ServerConstants.SYS_PROP_SERVER_HTTPS_PORT) ContextPathPredicate(com.adeptj.runtime.predicate.ContextPathPredicate) KEY_HTTP(com.adeptj.runtime.common.Constants.KEY_HTTP) SYS_PROP_REQ_BUFF_MAX_BUFFERS(com.adeptj.runtime.server.ServerConstants.SYS_PROP_REQ_BUFF_MAX_BUFFERS) WORKER_TASK_MAX_THREADS(org.xnio.Options.WORKER_TASK_MAX_THREADS) KEY_HEALTH_CHECK_HANDLER_PATH(com.adeptj.runtime.server.ServerConstants.KEY_HEALTH_CHECK_HANDLER_PATH) KEY_WORKER_TASK_CORE_THREADS(com.adeptj.runtime.server.ServerConstants.KEY_WORKER_TASK_CORE_THREADS) ArrayList(java.util.ArrayList) RedirectHandler(io.undertow.server.handlers.RedirectHandler) Servlets(io.undertow.servlet.Servlets) KEY_AUTH_ROLES(com.adeptj.runtime.server.ServerConstants.KEY_AUTH_ROLES) MVStore(org.h2.mvstore.MVStore) ServletSessionConfig(io.undertow.servlet.api.ServletSessionConfig) SYS_PROP_SESSION_TIMEOUT(com.adeptj.runtime.server.ServerConstants.SYS_PROP_SESSION_TIMEOUT) KEY_INVALIDATE_SESSION_ON_LOGOUT(com.adeptj.runtime.server.ServerConstants.KEY_INVALIDATE_SESSION_ON_LOGOUT) KEY_MULTIPART_MAX_REQUEST_SIZE(com.adeptj.runtime.server.ServerConstants.KEY_MULTIPART_MAX_REQUEST_SIZE) KEY_WORKER_TASK_MAX_THREADS(com.adeptj.runtime.server.ServerConstants.KEY_WORKER_TASK_MAX_THREADS) WORKER_TASK_THREAD_MULTIPLIER(com.adeptj.runtime.server.ServerConstants.WORKER_TASK_THREAD_MULTIPLIER) Times(com.adeptj.runtime.common.Times) DEFAULT_WAIT_TIME(com.adeptj.runtime.server.ServerConstants.DEFAULT_WAIT_TIME) LinkedHashSet(java.util.LinkedHashSet) SYS_TASK_THREAD_MULTIPLIER(com.adeptj.runtime.server.ServerConstants.SYS_TASK_THREAD_MULTIPLIER) SYS_PROP_OVERWRITE_SERVER_CONF(com.adeptj.runtime.common.Constants.SYS_PROP_OVERWRITE_SERVER_CONF) KEY_MULTIPART_FILE_SIZE_THRESHOLD(com.adeptj.runtime.server.ServerConstants.KEY_MULTIPART_FILE_SIZE_THRESHOLD) Config(com.typesafe.config.Config) Files(java.nio.file.Files) SERVER_STRING(io.undertow.util.Headers.SERVER_STRING) SYS_PROP_MAX_CONCUR_REQ(com.adeptj.runtime.server.ServerConstants.SYS_PROP_MAX_CONCUR_REQ) KEY_HOST(com.adeptj.runtime.common.Constants.KEY_HOST) SYS_PROP_WORKER_TASK_THREAD_MULTIPLIER(com.adeptj.runtime.server.ServerConstants.SYS_PROP_WORKER_TASK_THREAD_MULTIPLIER) KEY_REQ_LIMIT_QUEUE_SIZE(com.adeptj.runtime.common.Constants.KEY_REQ_LIMIT_QUEUE_SIZE) IOException(java.io.IOException) KEY_SYSTEM_CONSOLE_PATH(com.adeptj.runtime.common.Constants.KEY_SYSTEM_CONSOLE_PATH) KEY_PROTECTED_PATHS_SECURED_FOR_METHODS(com.adeptj.runtime.server.ServerConstants.KEY_PROTECTED_PATHS_SECURED_FOR_METHODS) H2_MAP_ADMIN_CREDENTIALS(com.adeptj.runtime.common.Constants.H2_MAP_ADMIN_CREDENTIALS) Lifecycle(com.adeptj.runtime.common.Lifecycle) RequestLimitingHandler(io.undertow.server.handlers.RequestLimitingHandler) RequestBufferingHandler(io.undertow.server.handlers.RequestBufferingHandler) KEY_MULTIPART_MAX_FILE_SIZE(com.adeptj.runtime.server.ServerConstants.KEY_MULTIPART_MAX_FILE_SIZE) SYS_PROP_ENABLE_REQ_BUFF(com.adeptj.runtime.server.ServerConstants.SYS_PROP_ENABLE_REQ_BUFF) SYS_PROP_SHUTDOWN_WAIT_TIME(com.adeptj.runtime.server.ServerConstants.SYS_PROP_SHUTDOWN_WAIT_TIME) ServletContext(javax.servlet.ServletContext) GracefulShutdownHandler(io.undertow.server.handlers.GracefulShutdownHandler) KEY_DEFAULT_ENCODING(com.adeptj.runtime.server.ServerConstants.KEY_DEFAULT_ENCODING) HealthCheckHandler(com.adeptj.runtime.handler.HealthCheckHandler) DEPLOYMENT_NAME(com.adeptj.runtime.common.Constants.DEPLOYMENT_NAME) Handlers(io.undertow.Handlers) KEY_USE_CACHED_AUTH_MECHANISM(com.adeptj.runtime.server.ServerConstants.KEY_USE_CACHED_AUTH_MECHANISM) LoggerFactory(org.slf4j.LoggerFactory) SYS_PROP_SYS_TASK_THREAD_MULTIPLIER(com.adeptj.runtime.server.ServerConstants.SYS_PROP_SYS_TASK_THREAD_MULTIPLIER) RuntimeInitializer(com.adeptj.runtime.core.RuntimeInitializer) ServletInfo(io.undertow.servlet.api.ServletInfo) SLF4JBridgeHandler(org.slf4j.bridge.SLF4JBridgeHandler) Undertow(io.undertow.Undertow) KEY_ADMIN_SERVLET_PATH(com.adeptj.runtime.common.Constants.KEY_ADMIN_SERVLET_PATH) KEY_IGNORE_FLUSH(com.adeptj.runtime.server.ServerConstants.KEY_IGNORE_FLUSH) IOUtils(com.adeptj.runtime.common.IOUtils) KEY_HEADER_SERVER(com.adeptj.runtime.common.Constants.KEY_HEADER_SERVER) MVMap(org.h2.mvstore.MVMap) KEY_USER_CREDENTIAL_MAPPING(com.adeptj.runtime.server.ServerConstants.KEY_USER_CREDENTIAL_MAPPING) ErrorPage(io.undertow.servlet.api.ErrorPage) KEY_ALLOWED_METHODS(com.adeptj.runtime.common.Constants.KEY_ALLOWED_METHODS) KEY_MULTIPART_FILE_LOCATION(com.adeptj.runtime.server.ServerConstants.KEY_MULTIPART_FILE_LOCATION) KEY_REQ_BUFF_MAX_BUFFERS(com.adeptj.runtime.common.Constants.KEY_REQ_BUFF_MAX_BUFFERS) ADMIN_LOGIN_URI(com.adeptj.runtime.common.Constants.ADMIN_LOGIN_URI) Configs(com.adeptj.runtime.config.Configs) MultipartConfigElement(javax.servlet.MultipartConfigElement) Collectors(java.util.stream.Collectors) KEY_MAX_CONCURRENT_REQUESTS(com.adeptj.runtime.common.Constants.KEY_MAX_CONCURRENT_REQUESTS) BundleContextHolder(com.adeptj.runtime.common.BundleContextHolder) List(java.util.List) BANNER_TXT(com.adeptj.runtime.common.Constants.BANNER_TXT) KEY_CONTEXT_PATH(com.adeptj.runtime.server.ServerConstants.KEY_CONTEXT_PATH) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) ADMIN_SERVLET_NAME(com.adeptj.runtime.server.ServerConstants.ADMIN_SERVLET_NAME) CrawlerSessionManagerConfig(io.undertow.servlet.api.CrawlerSessionManagerConfig) FORM_AUTH(javax.servlet.http.HttpServletRequest.FORM_AUTH) ERROR_SERVLET_NAME(com.adeptj.runtime.server.ServerConstants.ERROR_SERVLET_NAME) SYS_PROP_REQ_LIMIT_QUEUE_SIZE(com.adeptj.runtime.server.ServerConstants.SYS_PROP_REQ_LIMIT_QUEUE_SIZE) PathHandler(io.undertow.server.handlers.PathHandler) ViewStatusMessagesServlet(ch.qos.logback.classic.ViewStatusMessagesServlet) SecurityConstraint(io.undertow.servlet.api.SecurityConstraint) PredicateHandler(io.undertow.server.handlers.PredicateHandler) KEY_PROTECTED_PATHS(com.adeptj.runtime.server.ServerConstants.KEY_PROTECTED_PATHS) SERVER_CONF_CP_RESOURCE(com.adeptj.runtime.common.Constants.SERVER_CONF_CP_RESOURCE) SYS_PROP_ENABLE_HTTP2(com.adeptj.runtime.server.ServerConstants.SYS_PROP_ENABLE_HTTP2) RuntimeInitializationException(com.adeptj.runtime.exception.RuntimeInitializationException) ServletInitialHandlerWrapper(com.adeptj.runtime.handler.ServletInitialHandlerWrapper) SYS_PROP_SERVER_PORT(com.adeptj.runtime.common.Constants.SYS_PROP_SERVER_PORT) KEY_SESSION_TIMEOUT(com.adeptj.runtime.server.ServerConstants.KEY_SESSION_TIMEOUT) Logger(org.slf4j.Logger) SslContextFactory(com.adeptj.runtime.common.SslContextFactory) AdminServlet(com.adeptj.runtime.servlet.AdminServlet) ErrorServlet(com.adeptj.runtime.servlet.ErrorServlet) DeploymentManager(io.undertow.servlet.api.DeploymentManager) HttpHandler(io.undertow.server.HttpHandler) FrameworkLauncher(com.adeptj.runtime.osgi.FrameworkLauncher) PWD_START_INDEX(com.adeptj.runtime.server.ServerConstants.PWD_START_INDEX) ServletContainerInitializerInfo(io.undertow.servlet.api.ServletContainerInitializerInfo) KEY_CHANGE_SESSION_ID_ON_LOGIN(com.adeptj.runtime.server.ServerConstants.KEY_CHANGE_SESSION_ID_ON_LOGIN) SetHeaderHandler(io.undertow.server.handlers.SetHeaderHandler) InputStream(java.io.InputStream) HttpString(io.undertow.util.HttpString)

Example 42 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project gravitee-management-rest-api by gravitee-io.

the class PageServiceImpl method createPage.

private PageEntity createPage(String apiId, NewPageEntity newPageEntity, String environmentId, String pageId) {
    try {
        logger.debug("Create page {} for API {}", newPageEntity, apiId);
        String id = pageId != null && UUID.fromString(pageId) != null ? pageId : UuidString.generateRandom();
        PageType newPageType = newPageEntity.getType();
        // handle default visibility
        if (newPageEntity.getVisibility() == null) {
            newPageEntity.setVisibility(Visibility.PUBLIC);
        }
        // create page revision only for :
        // - SWAGGER
        // - Markdown
        // - Translation
        boolean createRevision = false;
        // Useful when import api with pages in old version
        if (newPageEntity.getExcludedGroups() != null && !newPageEntity.getExcludedGroups().isEmpty()) {
            newPageEntity.setVisibility(Visibility.PRIVATE);
            newPageEntity.setExcludedAccessControls(true);
            newPageEntity.setAccessControls(newPageEntity.getExcludedGroups().stream().map((groupId -> {
                AccessControlEntity accessControl = new AccessControlEntity();
                accessControl.setReferenceType("GROUP");
                accessControl.setReferenceId(groupId);
                return accessControl;
            })).collect(Collectors.toSet()));
        }
        if (PageType.TRANSLATION.equals(newPageType)) {
            checkTranslationConsistency(newPageEntity.getParentId(), newPageEntity.getConfiguration(), true);
            Optional<Page> optTranslatedPage = this.pageRepository.findById(newPageEntity.getParentId());
            if (optTranslatedPage.isPresent()) {
                newPageEntity.setPublished(optTranslatedPage.get().isPublished());
                // create revision only for Swagger & Markdown page
                createRevision = isSwaggerOrMarkdown(optTranslatedPage.get().getType());
            }
        }
        if (PageType.FOLDER.equals(newPageType)) {
            checkFolderConsistency(newPageEntity);
        }
        if (PageType.LINK.equals(newPageType)) {
            String resourceType = newPageEntity.getConfiguration().get(PageConfigurationKeys.LINK_RESOURCE_TYPE);
            String content = newPageEntity.getContent();
            if (content == null || content.isEmpty()) {
                throw new PageActionException(PageType.LINK, "be created. It must have a URL, a page Id or a category Id");
            }
            if ("root".equals(content) || PageConfigurationKeys.LINK_RESOURCE_TYPE_EXTERNAL.equals(resourceType) || PageConfigurationKeys.LINK_RESOURCE_TYPE_CATEGORY.equals(resourceType)) {
                newPageEntity.setPublished(true);
            } else {
                Optional<Page> optionalRelatedPage = pageRepository.findById(content);
                if (optionalRelatedPage.isPresent()) {
                    Page relatedPage = optionalRelatedPage.get();
                    checkLinkRelatedPageType(relatedPage);
                    newPageEntity.setPublished(relatedPage.isPublished());
                    newPageEntity.setVisibility(Visibility.valueOf(relatedPage.getVisibility()));
                }
            }
        }
        if (PageType.SWAGGER == newPageType || PageType.MARKDOWN == newPageType) {
            checkMarkdownOrSwaggerConsistency(newPageEntity, newPageType);
            createRevision = true;
        }
        Page page = convert(newPageEntity);
        if (page.getSource() != null) {
            fetchPage(page);
        }
        page.setId(id);
        if (StringUtils.isEmpty(apiId)) {
            page.setReferenceId(environmentId);
            page.setReferenceType(PageReferenceType.ENVIRONMENT);
        } else {
            page.setReferenceId(apiId);
            page.setReferenceType(PageReferenceType.API);
        }
        // Set date fields
        page.setCreatedAt(new Date());
        page.setUpdatedAt(page.getCreatedAt());
        List<String> messages = validateSafeContent(page);
        Page createdPage = this.pageRepository.create(page);
        if (createRevision) {
            createPageRevision(createdPage);
        }
        // only one homepage is allowed
        onlyOneHomepage(page);
        createAuditLog(PageReferenceType.API.equals(page.getReferenceType()) ? page.getReferenceId() : null, PAGE_CREATED, page.getCreatedAt(), null, page);
        PageEntity pageEntity = convert(createdPage);
        if (messages != null && messages.size() > 0) {
            pageEntity.setMessages(messages);
        }
        // add document in search engine
        index(pageEntity);
        return pageEntity;
    } catch (TechnicalException | FetcherException ex) {
        logger.error("An error occurs while trying to create {}", newPageEntity, ex);
        throw new TechnicalManagementException("An error occurs while trying create " + newPageEntity, ex);
    }
}
Also used : GraviteeDescriptorEntity(io.gravitee.rest.api.model.descriptor.GraviteeDescriptorEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) PageConfigurationOAITransformer(io.gravitee.rest.api.service.impl.swagger.transformer.page.PageConfigurationOAITransformer) Arrays.asList(java.util.Arrays.asList) Gson(com.google.gson.Gson) FetcherConfigurationFactory(io.gravitee.rest.api.fetcher.FetcherConfigurationFactory) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) PageQuery(io.gravitee.rest.api.model.documentation.PageQuery) CronSequenceGenerator(org.springframework.scheduling.support.CronSequenceGenerator) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeReference(com.fasterxml.jackson.core.type.TypeReference) HtmlSanitizer(io.gravitee.rest.api.service.sanitizer.HtmlSanitizer) PageCriteria(io.gravitee.repository.management.api.search.PageCriteria) GraviteeDescriptorPageEntity(io.gravitee.rest.api.model.descriptor.GraviteeDescriptorPageEntity) PluginManager(io.gravitee.plugin.core.api.PluginManager) OAIParser(io.gravitee.rest.api.service.impl.swagger.parser.OAIParser) Collectors(java.util.stream.Collectors) FetcherPlugin(io.gravitee.plugin.fetcher.FetcherPlugin) MediaType(io.gravitee.common.http.MediaType) Stream(java.util.stream.Stream) Visibility(io.gravitee.rest.api.model.Visibility) CollectionUtils(org.springframework.util.CollectionUtils) UrlSanitizerUtils(io.gravitee.rest.api.service.sanitizer.UrlSanitizerUtils) PAGE(io.gravitee.repository.management.model.Audit.AuditProperties.PAGE) ApplicationContextAware(org.springframework.context.ApplicationContextAware) SearchEngineService(io.gravitee.rest.api.service.search.SearchEngineService) AuditEvent(io.gravitee.repository.management.model.Page.AuditEvent) io.gravitee.rest.api.service(io.gravitee.rest.api.service) java.util(java.util) Pageable(io.gravitee.rest.api.model.common.Pageable) ImportConfiguration(io.gravitee.rest.api.service.spring.ImportConfiguration) EntrypointsOAITransformer(io.gravitee.rest.api.service.impl.swagger.transformer.entrypoints.EntrypointsOAITransformer) Value(org.springframework.beans.factory.annotation.Value) SwaggerTransformer(io.gravitee.rest.api.service.impl.swagger.transformer.SwaggerTransformer) NotificationTemplateService(io.gravitee.rest.api.service.notification.NotificationTemplateService) PageRepository(io.gravitee.repository.management.api.PageRepository) io.gravitee.rest.api.model(io.gravitee.rest.api.model) UuidString(io.gravitee.rest.api.service.common.UuidString) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) Field(java.lang.reflect.Field) InputStreamReader(java.io.InputStreamReader) ApplicationContext(org.springframework.context.ApplicationContext) Collectors.toList(java.util.stream.Collectors.toList) Component(org.springframework.stereotype.Component) io.gravitee.fetcher.api(io.gravitee.fetcher.api) io.gravitee.repository.management.model(io.gravitee.repository.management.model) SwaggerDescriptor(io.gravitee.rest.api.service.swagger.SwaggerDescriptor) io.gravitee.rest.api.service.exceptions(io.gravitee.rest.api.service.exceptions) BufferedReader(java.io.BufferedReader) Collections(java.util.Collections) GraviteeDescriptorPageEntity(io.gravitee.rest.api.model.descriptor.GraviteeDescriptorPageEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UuidString(io.gravitee.rest.api.service.common.UuidString)

Example 43 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project pact-jvm by DiUS.

the class PactBrokerLoader method loadPactsForProvider.

private List<Pact> loadPactsForProvider(final String providerName, final String tag) throws IOException {
    LOGGER.debug("Loading pacts from pact broker for provider " + providerName + " and tag " + tag);
    URIBuilder uriBuilder = new URIBuilder().setScheme(parseExpressions(pactBrokerProtocol)).setHost(parseExpressions(pactBrokerHost)).setPort(Integer.parseInt(parseExpressions(pactBrokerPort)));
    try {
        List<ConsumerInfo> consumers;
        PactBrokerClient pactBrokerClient = newPactBrokerClient(uriBuilder.build());
        if (StringUtils.isEmpty(tag)) {
            consumers = pactBrokerClient.fetchConsumers(providerName);
        } else {
            consumers = pactBrokerClient.fetchConsumersWithTag(providerName, tag);
        }
        if (failIfNoPactsFound && consumers.isEmpty()) {
            throw new NoPactsFoundException("No consumer pacts were found for provider '" + providerName + "' and tag '" + tag + "'. (URL " + pactBrokerClient.getUrlForProvider(providerName, tag) + ")");
        }
        return consumers.stream().map(consumer -> this.loadPact(consumer, pactBrokerClient.getOptions())).collect(toList());
    } catch (URISyntaxException e) {
        throw new IOException("Was not able load pacts from broker as the broker URL was invalid", e);
    }
}
Also used : PactRunnerTagListExpressionParser.parseTagListExpressions(au.com.dius.pact.provider.junit.sysprops.PactRunnerTagListExpressionParser.parseTagListExpressions) java.util(java.util) Logger(org.slf4j.Logger) Pact(au.com.dius.pact.model.Pact) ConsumerInfo(au.com.dius.pact.provider.ConsumerInfo) URIBuilder(org.apache.http.client.utils.URIBuilder) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) PactRunnerExpressionParser.parseExpressions(au.com.dius.pact.provider.junit.sysprops.PactRunnerExpressionParser.parseExpressions) StringUtils(org.apache.commons.lang3.StringUtils) Collectors.toList(java.util.stream.Collectors.toList) PactReader(au.com.dius.pact.model.PactReader) PactBrokerClient(au.com.dius.pact.provider.broker.PactBrokerClient) URI(java.net.URI) ConsumerInfo(au.com.dius.pact.provider.ConsumerInfo) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) PactBrokerClient(au.com.dius.pact.provider.broker.PactBrokerClient) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 44 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project kie-wb-common by kiegroup.

the class BpmnFileIndexer method buildProcessDefinition.

private List<BpmnProcessDataEventListener> buildProcessDefinition(String bpmn2Content, ClassLoader moduleClassLoader) throws IllegalArgumentException {
    if (StringUtils.isEmpty(bpmn2Content)) {
        return Collections.<BpmnProcessDataEventListener>emptyList();
    }
    // Set class loader
    KnowledgeBuilder kbuilder = null;
    if (moduleClassLoader != null) {
        KnowledgeBuilderConfigurationImpl pconf = new KnowledgeBuilderConfigurationImpl(moduleClassLoader);
        kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(pconf);
    } else {
        kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    }
    // Build
    kbuilder.add(new ByteArrayResource(bpmn2Content.getBytes()), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            logger.error("Error: {}", error.getMessage());
        }
        logger.debug("Process Cannot be Parsed! \n {} \n", bpmn2Content);
        return Collections.<BpmnProcessDataEventListener>emptyList();
    }
    // Retrieve ProcessInfoHolder
    List<BpmnProcessDataEventListener> processDataList = new ArrayList<>();
    kbuilder.getKnowledgePackages().forEach(pkg -> pkg.getProcesses().forEach(p -> {
        BpmnProcessDataEventListener processData = (BpmnProcessDataEventListener) p.getMetaData().get(BpmnProcessDataEventListener.NAME);
        processDataList.add(processData);
    }));
    return processDataList;
}
Also used : KnowledgeBuilderFactory(org.kie.internal.builder.KnowledgeBuilderFactory) XmlProcessReader(org.jbpm.compiler.xml.XmlProcessReader) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) ModuleClassLoaderHelper(org.kie.workbench.common.services.backend.project.ModuleClassLoaderHelper) DefaultIndexBuilder(org.kie.workbench.common.services.refactoring.backend.server.indexing.DefaultIndexBuilder) Path(org.uberfire.java.nio.file.Path) ReaderResource(org.drools.core.io.impl.ReaderResource) LoggerFactory(org.slf4j.LoggerFactory) ResourceType(org.kie.api.io.ResourceType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Package(org.guvnor.common.services.project.model.Package) ProcessValidationError(org.jbpm.process.core.validation.ProcessValidationError) Paths(org.uberfire.backend.server.util.Paths) Inject(javax.inject.Inject) ProcessValidatorRegistry(org.jbpm.process.core.validation.ProcessValidatorRegistry) BPMNSemanticModule(org.jbpm.bpmn2.xml.BPMNSemanticModule) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Module(org.guvnor.common.services.project.model.Module) SemanticModules(org.drools.core.xml.SemanticModules) AbstractFileIndexer(org.kie.workbench.common.services.refactoring.backend.server.indexing.AbstractFileIndexer) Logger(org.slf4j.Logger) Process(org.kie.api.definition.process.Process) KieModule(org.kie.workbench.common.services.shared.project.KieModule) BPMNDISemanticModule(org.jbpm.bpmn2.xml.BPMNDISemanticModule) Resource(org.kie.api.io.Resource) BPMNExtensionsSemanticModule(org.jbpm.bpmn2.xml.BPMNExtensionsSemanticModule) List(java.util.List) StringReader(java.io.StringReader) ApplicationScoped(javax.enterprise.context.ApplicationScoped) BPMNDefinitionSetResourceType(org.kie.workbench.common.stunner.bpmn.resource.BPMNDefinitionSetResourceType) Collections(java.util.Collections) ProcessValidator(org.jbpm.process.core.validation.ProcessValidator) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ArrayList(java.util.ArrayList) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource)

Example 45 with StringUtils.isEmpty

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

the class PutElasticsearchHttp method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final int batchSize = context.getProperty(BATCH_SIZE).evaluateAttributeExpressions().asInteger();
    final List<FlowFile> flowFiles = session.get(batchSize);
    if (flowFiles.isEmpty()) {
        return;
    }
    final String id_attribute = context.getProperty(ID_ATTRIBUTE).getValue();
    // Authentication
    final String username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue();
    final String password = context.getProperty(PASSWORD).evaluateAttributeExpressions().getValue();
    OkHttpClient okHttpClient = getClient();
    final ComponentLog logger = getLogger();
    // Keep track of the list of flow files that need to be transferred. As they are transferred, remove them from the list.
    List<FlowFile> flowFilesToTransfer = new LinkedList<>(flowFiles);
    final StringBuilder sb = new StringBuilder();
    final String baseUrl = trimToEmpty(context.getProperty(ES_URL).evaluateAttributeExpressions().getValue());
    HttpUrl.Builder urlBuilder = HttpUrl.parse(baseUrl).newBuilder().addPathSegment("_bulk");
    // Find the user-added properties and set them as query parameters on the URL
    for (Map.Entry<PropertyDescriptor, String> property : context.getProperties().entrySet()) {
        PropertyDescriptor pd = property.getKey();
        if (pd.isDynamic()) {
            if (property.getValue() != null) {
                urlBuilder = urlBuilder.addQueryParameter(pd.getName(), context.getProperty(pd).evaluateAttributeExpressions().getValue());
            }
        }
    }
    final URL url = urlBuilder.build().url();
    for (FlowFile file : flowFiles) {
        final String index = context.getProperty(INDEX).evaluateAttributeExpressions(file).getValue();
        final Charset charset = Charset.forName(context.getProperty(CHARSET).evaluateAttributeExpressions(file).getValue());
        if (StringUtils.isEmpty(index)) {
            logger.error("No value for index in for {}, transferring to failure", new Object[] { id_attribute, file });
            flowFilesToTransfer.remove(file);
            session.transfer(file, REL_FAILURE);
            continue;
        }
        final String docType = context.getProperty(TYPE).evaluateAttributeExpressions(file).getValue();
        String indexOp = context.getProperty(INDEX_OP).evaluateAttributeExpressions(file).getValue();
        if (StringUtils.isEmpty(indexOp)) {
            logger.error("No Index operation specified for {}, transferring to failure.", new Object[] { file });
            flowFilesToTransfer.remove(file);
            session.transfer(file, REL_FAILURE);
            continue;
        }
        switch(indexOp.toLowerCase()) {
            case "index":
            case "update":
            case "upsert":
            case "delete":
                break;
            default:
                logger.error("Index operation {} not supported for {}, transferring to failure.", new Object[] { indexOp, file });
                flowFilesToTransfer.remove(file);
                session.transfer(file, REL_FAILURE);
                continue;
        }
        final String id = (id_attribute != null) ? file.getAttribute(id_attribute) : null;
        // a missing ID indicates one is to be auto-generated by Elasticsearch
        if (id == null && !indexOp.equalsIgnoreCase("index")) {
            logger.error("Index operation {} requires a valid identifier value from a flow file attribute, transferring to failure.", new Object[] { indexOp, file });
            flowFilesToTransfer.remove(file);
            session.transfer(file, REL_FAILURE);
            continue;
        }
        final StringBuilder json = new StringBuilder();
        session.read(file, in -> {
            json.append(IOUtils.toString(in, charset).replace("\r\n", " ").replace('\n', ' ').replace('\r', ' '));
        });
        if (indexOp.equalsIgnoreCase("index")) {
            sb.append("{\"index\": { \"_index\": \"");
            sb.append(index);
            sb.append("\", \"_type\": \"");
            sb.append(docType);
            sb.append("\"");
            if (!StringUtils.isEmpty(id)) {
                sb.append(", \"_id\": \"");
                sb.append(id);
                sb.append("\"");
            }
            sb.append("}}\n");
            sb.append(json);
            sb.append("\n");
        } else if (indexOp.equalsIgnoreCase("upsert") || indexOp.equalsIgnoreCase("update")) {
            sb.append("{\"update\": { \"_index\": \"");
            sb.append(index);
            sb.append("\", \"_type\": \"");
            sb.append(docType);
            sb.append("\", \"_id\": \"");
            sb.append(id);
            sb.append("\" }\n");
            sb.append("{\"doc\": ");
            sb.append(json);
            sb.append(", \"doc_as_upsert\": ");
            sb.append(indexOp.equalsIgnoreCase("upsert"));
            sb.append(" }\n");
        } else if (indexOp.equalsIgnoreCase("delete")) {
            sb.append("{\"delete\": { \"_index\": \"");
            sb.append(index);
            sb.append("\", \"_type\": \"");
            sb.append(docType);
            sb.append("\", \"_id\": \"");
            sb.append(id);
            sb.append("\" }\n");
        }
    }
    if (!flowFilesToTransfer.isEmpty()) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), sb.toString());
        final Response getResponse;
        try {
            getResponse = sendRequestToElasticsearch(okHttpClient, url, username, password, "PUT", requestBody);
        } catch (final Exception e) {
            logger.error("Routing to {} due to exception: {}", new Object[] { REL_FAILURE.getName(), e }, e);
            flowFilesToTransfer.forEach((flowFileToTransfer) -> {
                flowFileToTransfer = session.penalize(flowFileToTransfer);
                session.transfer(flowFileToTransfer, REL_FAILURE);
            });
            flowFilesToTransfer.clear();
            return;
        }
        final int statusCode = getResponse.code();
        if (isSuccess(statusCode)) {
            ResponseBody responseBody = getResponse.body();
            try {
                final byte[] bodyBytes = responseBody.bytes();
                JsonNode responseJson = parseJsonResponse(new ByteArrayInputStream(bodyBytes));
                boolean errors = responseJson.get("errors").asBoolean(false);
                if (errors) {
                    ArrayNode itemNodeArray = (ArrayNode) responseJson.get("items");
                    if (itemNodeArray.size() > 0) {
                        // All items are returned whether they succeeded or failed, so iterate through the item array
                        // at the same time as the flow file list, moving each to success or failure accordingly,
                        // but only keep the first error for logging
                        String errorReason = null;
                        for (int i = itemNodeArray.size() - 1; i >= 0; i--) {
                            JsonNode itemNode = itemNodeArray.get(i);
                            if (flowFilesToTransfer.size() > i) {
                                FlowFile flowFile = flowFilesToTransfer.remove(i);
                                int status = itemNode.findPath("status").asInt();
                                if (!isSuccess(status)) {
                                    if (errorReason == null) {
                                        // Use "result" if it is present; this happens for status codes like 404 Not Found, which may not have an error/reason
                                        String reason = itemNode.findPath("//result").asText();
                                        if (StringUtils.isEmpty(reason)) {
                                            // If there was no result, we expect an error with a string description in the "reason" field
                                            reason = itemNode.findPath("//error/reason").asText();
                                        }
                                        errorReason = reason;
                                        logger.error("Failed to process {} due to {}, transferring to failure", new Object[] { flowFile, errorReason });
                                    }
                                    flowFile = session.penalize(flowFile);
                                    session.transfer(flowFile, REL_FAILURE);
                                } else {
                                    session.transfer(flowFile, REL_SUCCESS);
                                    // Record provenance event
                                    session.getProvenanceReporter().send(flowFile, url.toString());
                                }
                            }
                        }
                    }
                }
                // Transfer any remaining flowfiles to success
                flowFilesToTransfer.forEach(file -> {
                    session.transfer(file, REL_SUCCESS);
                    // Record provenance event
                    session.getProvenanceReporter().send(file, url.toString());
                });
            } catch (IOException ioe) {
                // Something went wrong when parsing the response, log the error and route to failure
                logger.error("Error parsing Bulk API response: {}", new Object[] { ioe.getMessage() }, ioe);
                session.transfer(flowFilesToTransfer, REL_FAILURE);
                context.yield();
            }
        } else if (statusCode / 100 == 5) {
            // 5xx -> RETRY, but a server error might last a while, so yield
            logger.warn("Elasticsearch returned code {} with message {}, transferring flow file to retry. This is likely a server problem, yielding...", new Object[] { statusCode, getResponse.message() });
            session.transfer(flowFilesToTransfer, REL_RETRY);
            context.yield();
        } else {
            // 1xx, 3xx, 4xx, etc. -> NO RETRY
            logger.warn("Elasticsearch returned code {} with message {}, transferring flow file to failure", new Object[] { statusCode, getResponse.message() });
            session.transfer(flowFilesToTransfer, REL_FAILURE);
        }
        getResponse.close();
    }
}
Also used : StandardValidators(org.apache.nifi.processor.util.StandardValidators) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) StringUtils.trimToEmpty(org.apache.commons.lang3.StringUtils.trimToEmpty) ValidationContext(org.apache.nifi.components.ValidationContext) URL(java.net.URL) EventDriven(org.apache.nifi.annotation.behavior.EventDriven) SystemResource(org.apache.nifi.annotation.behavior.SystemResource) ComponentLog(org.apache.nifi.logging.ComponentLog) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) RequestBody(okhttp3.RequestBody) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ByteArrayInputStream(java.io.ByteArrayInputStream) Charset(java.nio.charset.Charset) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) Response(okhttp3.Response) JsonNode(com.fasterxml.jackson.databind.JsonNode) LinkedList(java.util.LinkedList) MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) ValidationResult(org.apache.nifi.components.ValidationResult) FlowFile(org.apache.nifi.flowfile.FlowFile) Collection(java.util.Collection) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) ProcessSession(org.apache.nifi.processor.ProcessSession) IOException(java.io.IOException) StringUtils(org.apache.nifi.util.StringUtils) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IOUtils(org.apache.commons.io.IOUtils) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) DynamicProperty(org.apache.nifi.annotation.behavior.DynamicProperty) SupportsBatching(org.apache.nifi.annotation.behavior.SupportsBatching) AttributeExpression(org.apache.nifi.expression.AttributeExpression) SystemResourceConsideration(org.apache.nifi.annotation.behavior.SystemResourceConsideration) Tags(org.apache.nifi.annotation.documentation.Tags) HttpUrl(okhttp3.HttpUrl) Collections(java.util.Collections) OkHttpClient(okhttp3.OkHttpClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) URL(java.net.URL) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) RequestBody(okhttp3.RequestBody) FlowFile(org.apache.nifi.flowfile.FlowFile) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) Charset(java.nio.charset.Charset) IOException(java.io.IOException) ComponentLog(org.apache.nifi.logging.ComponentLog) LinkedList(java.util.LinkedList) HttpUrl(okhttp3.HttpUrl) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map)

Aggregations

StringUtils (org.apache.commons.lang3.StringUtils)76 List (java.util.List)53 Collectors (java.util.stream.Collectors)40 ArrayList (java.util.ArrayList)39 Map (java.util.Map)39 HashMap (java.util.HashMap)28 IOException (java.io.IOException)26 Collections (java.util.Collections)25 Set (java.util.Set)25 Logger (org.slf4j.Logger)25 Arrays (java.util.Arrays)24 LoggerFactory (org.slf4j.LoggerFactory)24 Optional (java.util.Optional)22 HashSet (java.util.HashSet)18 Collection (java.util.Collection)14 Autowired (org.springframework.beans.factory.annotation.Autowired)13 Objects (java.util.Objects)12 UUID (java.util.UUID)11 java.util (java.util)10 Component (org.springframework.stereotype.Component)10