Search in sources :

Example 26 with Function

use of java.util.function.Function in project neo4j by neo4j.

the class Settings method advertisedAddress.

public static BaseSetting<AdvertisedSocketAddress> advertisedAddress(String name, Setting<ListenSocketAddress> listenAddressSetting) {
    return new ScopeAwareSetting<AdvertisedSocketAddress>() {

        @Override
        protected String provideName() {
            return name;
        }

        @Override
        public String getDefaultValue() {
            return default_advertised_address.getDefaultValue() + ":" + LISTEN_SOCKET_ADDRESS.apply(listenAddressSetting.getDefaultValue()).socketAddress().getPort();
        }

        @Override
        public AdvertisedSocketAddress from(Configuration config) {
            return config.get(this);
        }

        @Override
        public AdvertisedSocketAddress apply(Function<String, String> config) {
            ListenSocketAddress listenSocketAddress = listenAddressSetting.apply(config);
            String hostname = default_advertised_address.apply(config);
            int port = listenSocketAddress.socketAddress().getPort();
            String name = name();
            String value = config.apply(name);
            return SocketAddressFormat.socketAddress(name, value, hostname, port, AdvertisedSocketAddress::new);
        }

        @Override
        public void withScope(Function<String, String> scopingRule) {
            super.withScope(scopingRule);
            listenAddressSetting.withScope(scopingRule);
        }

        @Override
        public String valueDescription() {
            return ADVERTISED_SOCKET_ADDRESS.toString();
        }
    };
}
Also used : BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) Configuration(org.neo4j.graphdb.config.Configuration) ListenSocketAddress(org.neo4j.helpers.ListenSocketAddress) ScopeAwareSetting(org.neo4j.graphdb.config.ScopeAwareSetting) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress)

Example 27 with Function

use of java.util.function.Function in project jersey by jersey.

the class OutboundMessageContext method getAcceptableMediaTypes.

/**
     * Get a list of media types that are acceptable for the message.
     *
     * @return a read-only list of requested message media types sorted according
     * to their q-value, with highest preference first.
     */
@SuppressWarnings("unchecked")
public List<MediaType> getAcceptableMediaTypes() {
    final List<Object> values = headers.get(HttpHeaders.ACCEPT);
    if (values == null || values.isEmpty()) {
        return WILDCARD_ACCEPTABLE_TYPE_SINGLETON_LIST;
    }
    final List<MediaType> result = new ArrayList<>(values.size());
    final RuntimeDelegate rd = RuntimeDelegate.getInstance();
    boolean conversionApplied = false;
    for (final Object value : values) {
        try {
            if (value instanceof MediaType) {
                final AcceptableMediaType _value = AcceptableMediaType.valueOf((MediaType) value);
                // true if value was not an instance of AcceptableMediaType already
                conversionApplied = _value != value;
                result.add(_value);
            } else {
                conversionApplied = true;
                result.addAll(HttpHeaderReader.readAcceptMediaType(HeaderUtils.asString(value, rd)));
            }
        } catch (java.text.ParseException e) {
            throw exception(HttpHeaders.ACCEPT, value, e);
        }
    }
    if (conversionApplied) {
        // cache converted
        headers.put(HttpHeaders.ACCEPT, result.stream().map((Function<MediaType, Object>) mediaType -> mediaType).collect(Collectors.toList()));
    }
    return Collections.unmodifiableList(result);
}
Also used : CommonProperties(org.glassfish.jersey.CommonProperties) LocalizationMessages(org.glassfish.jersey.internal.LocalizationMessages) Date(java.util.Date) Configuration(javax.ws.rs.core.Configuration) HashMap(java.util.HashMap) NewCookie(javax.ws.rs.core.NewCookie) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) MediaType(javax.ws.rs.core.MediaType) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate) Locale(java.util.Locale) Map(java.util.Map) URI(java.net.URI) ParseException(java.text.ParseException) OutputStream(java.io.OutputStream) GenericEntity(javax.ws.rs.core.GenericEntity) Set(java.util.Set) IOException(java.io.IOException) Logger(java.util.logging.Logger) EntityTag(javax.ws.rs.core.EntityTag) Collectors(java.util.stream.Collectors) ReflectionHelper(org.glassfish.jersey.internal.util.ReflectionHelper) Cookie(javax.ws.rs.core.Cookie) GenericType(javax.ws.rs.core.GenericType) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) ProcessingException(javax.ws.rs.ProcessingException) Collections(java.util.Collections) Link(javax.ws.rs.core.Link) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType) ParseException(java.text.ParseException) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate)

Example 28 with Function

use of java.util.function.Function in project jersey by jersey.

the class OutboundMessageContext method getAcceptableLanguages.

/**
     * Get a list of languages that are acceptable for the message.
     *
     * @return a read-only list of acceptable languages sorted according
     * to their q-value, with highest preference first.
     */
public List<Locale> getAcceptableLanguages() {
    final List<Object> values = headers.get(HttpHeaders.ACCEPT_LANGUAGE);
    if (values == null || values.isEmpty()) {
        return Collections.singletonList(new AcceptableLanguageTag("*", null).getAsLocale());
    }
    final List<Locale> result = new ArrayList<Locale>(values.size());
    final RuntimeDelegate rd = RuntimeDelegate.getInstance();
    boolean conversionApplied = false;
    for (final Object value : values) {
        if (value instanceof Locale) {
            result.add((Locale) value);
        } else {
            conversionApplied = true;
            try {
                result.addAll(HttpHeaderReader.readAcceptLanguage(HeaderUtils.asString(value, rd)).stream().map(LanguageTag::getAsLocale).collect(Collectors.toList()));
            } catch (java.text.ParseException e) {
                throw exception(HttpHeaders.ACCEPT_LANGUAGE, value, e);
            }
        }
    }
    if (conversionApplied) {
        // cache converted
        headers.put(HttpHeaders.ACCEPT_LANGUAGE, result.stream().map((Function<Locale, Object>) locale -> locale).collect(Collectors.toList()));
    }
    return Collections.unmodifiableList(result);
}
Also used : Locale(java.util.Locale) CommonProperties(org.glassfish.jersey.CommonProperties) LocalizationMessages(org.glassfish.jersey.internal.LocalizationMessages) Date(java.util.Date) Configuration(javax.ws.rs.core.Configuration) HashMap(java.util.HashMap) NewCookie(javax.ws.rs.core.NewCookie) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) MediaType(javax.ws.rs.core.MediaType) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate) Locale(java.util.Locale) Map(java.util.Map) URI(java.net.URI) ParseException(java.text.ParseException) OutputStream(java.io.OutputStream) GenericEntity(javax.ws.rs.core.GenericEntity) Set(java.util.Set) IOException(java.io.IOException) Logger(java.util.logging.Logger) EntityTag(javax.ws.rs.core.EntityTag) Collectors(java.util.stream.Collectors) ReflectionHelper(org.glassfish.jersey.internal.util.ReflectionHelper) Cookie(javax.ws.rs.core.Cookie) GenericType(javax.ws.rs.core.GenericType) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) ProcessingException(javax.ws.rs.ProcessingException) Collections(java.util.Collections) Link(javax.ws.rs.core.Link) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate)

Example 29 with Function

use of java.util.function.Function in project jersey by jersey.

the class ParameterInjectionBinder method configure.

@Override
public void configure() {
    // Param converter providers
    // TODO: Replace by non-di version
    bind(new ParamConverters.AggregatedProvider()).to(ParamConverterProvider.class);
    Provider<ContainerRequest> requestProvider = Injections.getProvider(injectionManager, ContainerRequest.class);
    Provider<AsyncContext> asyncContextProvider = Injections.getProvider(injectionManager, AsyncContext.class);
    Function<Class<? extends Configuration>, Configuration> clientConfigProvider = clientConfigClass -> Injections.getOrCreate(injectionManager, clientConfigClass);
    // Param Converters must be initialized Lazy and created at the time of the call on extractor
    LazyValue<ParamConverterFactory> lazyParamConverterFactory = Values.lazy((Value<ParamConverterFactory>) () -> new ParamConverterFactory(Providers.getProviders(injectionManager, ParamConverterProvider.class), Providers.getCustomProviders(injectionManager, ParamConverterProvider.class)));
    LazyValue<Configuration> lazyConfiguration = Values.lazy((Value<Configuration>) () -> injectionManager.getInstance(Configuration.class));
    MultivaluedParameterExtractorFactory paramExtractor = new MultivaluedParameterExtractorFactory(lazyParamConverterFactory);
    bind(paramExtractor).to(MultivaluedParameterExtractorProvider.class);
    // Parameter injection value providers
    AsyncResponseValueSupplierProvider asyncSupplier = new AsyncResponseValueSupplierProvider(asyncContextProvider);
    bindValueSupplier(asyncSupplier);
    CookieParamValueSupplierProvider cookieSupplier = new CookieParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(cookieSupplier);
    EntityParamValueSupplierProvider entitySupplier = new EntityParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(entitySupplier);
    FormParamValueSupplierProvider formSupplier = new FormParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(formSupplier);
    HeaderParamValueSupplierProvider headerSupplier = new HeaderParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(headerSupplier);
    MatrixParamValueSupplierProvider matrixSupplier = new MatrixParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(matrixSupplier);
    PathParamValueSupplierProvider pathSupplier = new PathParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(pathSupplier);
    QueryParamValueSupplierProvider querySupplier = new QueryParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(querySupplier);
    WebTargetValueSupplierProvider webTargetSupplier = new WebTargetValueSupplierProvider(requestProvider, lazyConfiguration, clientConfigProvider);
    bindValueSupplier(webTargetSupplier);
    BeanParamValueSupplierProvider beanSupplier = new BeanParamValueSupplierProvider(paramExtractor, requestProvider, injectionManager);
    bindValueSupplier(beanSupplier);
    // Register InjectionResolvers with param providers
    // TODO: RENAME INJECTION RESOLVER
    bind(new ParamInjectionResolver<>(cookieSupplier, CookieParam.class));
    bind(new ParamInjectionResolver<>(formSupplier, FormParam.class));
    bind(new ParamInjectionResolver<>(headerSupplier, HeaderParam.class));
    bind(new ParamInjectionResolver<>(matrixSupplier, MatrixParam.class));
    bind(new ParamInjectionResolver<>(querySupplier, QueryParam.class));
    bind(new ParamInjectionResolver<>(pathSupplier, PathParam.class));
    bind(new ParamInjectionResolver<>(asyncSupplier, Suspended.class));
    bind(new ParamInjectionResolver<>(webTargetSupplier, Uri.class));
    bind(new ParamInjectionResolver<>(beanSupplier, BeanParam.class));
    // Delegated value supplier for Context InjectionResolver which is implemented directly in DI provider
    ContextInjectionResolver contextInjectionResolver = injectionManager.getInstance(ContextInjectionResolver.class);
    bind(new DelegatedInjectionValueSupplierProvider(contextInjectionResolver, injectionManager::createForeignDescriptor)).to(ValueSupplierProvider.class);
}
Also used : FormParam(javax.ws.rs.FormParam) ContextInjectionResolver(org.glassfish.jersey.internal.inject.ContextInjectionResolver) PathParam(javax.ws.rs.PathParam) Provider(javax.inject.Provider) Configuration(javax.ws.rs.core.Configuration) AsyncContext(org.glassfish.jersey.server.internal.process.AsyncContext) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) Providers(org.glassfish.jersey.internal.inject.Providers) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) Function(java.util.function.Function) Suspended(javax.ws.rs.container.Suspended) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Uri(org.glassfish.jersey.server.Uri) BeanParam(javax.ws.rs.BeanParam) QueryParam(javax.ws.rs.QueryParam) Value(org.glassfish.jersey.internal.util.collection.Value) MatrixParam(javax.ws.rs.MatrixParam) LazyValue(org.glassfish.jersey.internal.util.collection.LazyValue) ValueSupplierProvider(org.glassfish.jersey.server.spi.internal.ValueSupplierProvider) HeaderParam(javax.ws.rs.HeaderParam) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) CookieParam(javax.ws.rs.CookieParam) Injections(org.glassfish.jersey.internal.inject.Injections) Values(org.glassfish.jersey.internal.util.collection.Values) HeaderParam(javax.ws.rs.HeaderParam) MatrixParam(javax.ws.rs.MatrixParam) Configuration(javax.ws.rs.core.Configuration) AsyncContext(org.glassfish.jersey.server.internal.process.AsyncContext) Uri(org.glassfish.jersey.server.Uri) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) PathParam(javax.ws.rs.PathParam) Suspended(javax.ws.rs.container.Suspended) ContextInjectionResolver(org.glassfish.jersey.internal.inject.ContextInjectionResolver) BeanParam(javax.ws.rs.BeanParam) CookieParam(javax.ws.rs.CookieParam) QueryParam(javax.ws.rs.QueryParam) FormParam(javax.ws.rs.FormParam)

Example 30 with Function

use of java.util.function.Function in project POL-POM-5 by PlayOnLinux.

the class NashornEngineFactory method createEngine.

NashornEngine createEngine() {
    final Set<List<String>> includedScripts = new HashSet<>();
    final NashornEngine nashornEngine = new NashornEngine(new ScriptEngineManager().getEngineByName("nashorn"));
    nashornEngine.eval(new InputStreamReader(getClass().getResourceAsStream("utils.js")), this::throwException);
    nashornEngine.put("Bean", (Function<String, Object>) title -> applicationContext.getBean(title), this::throwException);
    nashornEngine.put("SetupWizard", (Function<String, UiSetupWizardImplementation>) (name) -> {
        final UiSetupWizardImplementation uiSetupWizardImplementation = uiSetupWizardFactory.create(name);
        nashornEngine.addErrorHandler(e -> uiSetupWizardImplementation.close());
        return uiSetupWizardImplementation;
    }, this::throwException);
    nashornEngine.put("EngineProgressUi", (Function<String, UiProgressWizardImplementation>) (name) -> {
        final UiProgressWizardImplementation uiProgressWizardImplementation = uiProgressWizardFactory.create(name);
        nashornEngine.addErrorHandler(e -> uiProgressWizardImplementation.close());
        return uiProgressWizardImplementation;
    }, this::throwException);
    nashornEngine.put("include", (Consumer<ScriptObjectMirror>) args -> {
        final String[] arguments = args.to(String[].class);
        final String script = scriptFetcher.getScript(arguments);
        if (script == null) {
            throwException(new ScriptException(Arrays.asList(arguments).toString() + " is not found"));
        }
        if (includedScripts.add(Arrays.asList(arguments))) {
            nashornEngine.eval("//# sourceURL=" + Arrays.asList(arguments).toString() + "\n" + script, this::throwException);
        }
    }, this::throwException);
    return nashornEngine;
}
Also used : Arrays(java.util.Arrays) UiProgressWizardFactory(org.phoenicis.scripts.wizard.UiProgressWizardFactory) ScriptException(org.phoenicis.scripts.interpreter.ScriptException) UiSetupWizardFactory(org.phoenicis.scripts.wizard.UiSetupWizardFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ScriptEngineManager(javax.script.ScriptEngineManager) Set(java.util.Set) ScriptFetcher(org.phoenicis.scripts.interpreter.ScriptFetcher) ApplicationContext(org.springframework.context.ApplicationContext) InputStreamReader(java.io.InputStreamReader) Function(java.util.function.Function) UiSetupWizardImplementation(org.phoenicis.scripts.wizard.UiSetupWizardImplementation) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) List(java.util.List) UiProgressWizardImplementation(org.phoenicis.scripts.wizard.UiProgressWizardImplementation) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InputStreamReader(java.io.InputStreamReader) UiSetupWizardImplementation(org.phoenicis.scripts.wizard.UiSetupWizardImplementation) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptException(org.phoenicis.scripts.interpreter.ScriptException) UiProgressWizardImplementation(org.phoenicis.scripts.wizard.UiProgressWizardImplementation) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

Function (java.util.function.Function)1261 List (java.util.List)606 Map (java.util.Map)447 ArrayList (java.util.ArrayList)416 Test (org.junit.Test)358 Collectors (java.util.stream.Collectors)324 HashMap (java.util.HashMap)287 Collections (java.util.Collections)284 Arrays (java.util.Arrays)271 Set (java.util.Set)255 IOException (java.io.IOException)252 Collection (java.util.Collection)192 HashSet (java.util.HashSet)191 TimeUnit (java.util.concurrent.TimeUnit)174 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)158 Optional (java.util.Optional)157 Assert (org.junit.Assert)137 Consumer (java.util.function.Consumer)134 Supplier (java.util.function.Supplier)126 CompletableFuture (java.util.concurrent.CompletableFuture)121