Search in sources :

Example 16 with NonNull

use of io.micronaut.core.annotation.NonNull in project micronaut-security by micronaut-projects.

the class X509AuthenticationFetcher method fetchAuthentication.

@Override
public Publisher<Authentication> fetchAuthentication(HttpRequest<?> request) {
    return Mono.<Authentication>create(emitter -> {
        Optional<Certificate> optionalCertificate = request.getCertificate();
        if (optionalCertificate.isPresent()) {
            Certificate certificate = optionalCertificate.get();
            if (certificate instanceof X509Certificate) {
                emitter.success(new Authentication() {

                    X509Certificate x509Certificate = ((X509Certificate) certificate);

                    @Override
                    public String getName() {
                        return x509Certificate.getIssuerX500Principal().getName();
                    }

                    @NonNull
                    @Override
                    public Map<String, Object> getAttributes() {
                        return Collections.emptyMap();
                    }
                });
                return;
            }
        }
        emitter.success();
    });
}
Also used : Authentication(io.micronaut.security.authentication.Authentication) NonNull(io.micronaut.core.annotation.NonNull) Map(java.util.Map) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 17 with NonNull

use of io.micronaut.core.annotation.NonNull in project micronaut-views by micronaut-projects.

the class SoySauceViewsRenderer method render.

/**
 * @param viewName view name to be rendered
 * @param data     response body to render it with a view
 * @param request  HTTP request
 * @return A writable where the view will be written to.
 */
@NonNull
@Override
public Writable render(@NonNull String viewName, @Nullable T data, @Nullable HttpRequest<?> request) {
    ArgumentUtils.requireNonNull("viewName", viewName);
    Map<String, Object> ijOverlay = new HashMap<>(1);
    Map<String, Object> context = ViewUtils.modelOf(data);
    final SoySauce.Renderer renderer = soySauce.newRenderer(new SoyTemplate() {

        @Override
        public String getTemplateName() {
            return viewName;
        }

        @Override
        public Map<String, SoyValueProvider> getParamsAsMap() {
            return null;
        }
    });
    renderer.setData(context);
    if (injectNonce) {
        Optional<Object> nonceObj = request != null ? request.getAttribute(CspFilter.NONCE_PROPERTY) : Optional.empty();
        if (nonceObj.isPresent()) {
            String nonceValue = ((String) nonceObj.get());
            ijOverlay.put(INJECTED_NONCE_PROPERTY, nonceValue);
        }
    }
    renderer.setIj(ijOverlay);
    if (this.soyMicronautConfiguration.isRenamingEnabled() && this.namingMapProvider != null) {
        SoyCssRenamingMap cssMap = this.namingMapProvider.cssRenamingMap();
        SoyIdRenamingMap idMap = this.namingMapProvider.idRenamingMap();
        if (cssMap != null) {
            renderer.setCssRenamingMap(cssMap);
        }
        if (idMap != null) {
            renderer.setXidRenamingMap(idMap);
        }
    }
    try {
        final AppendableToWritable target = new AppendableToWritable();
        SoySauce.WriteContinuation state;
        state = renderer.renderHtml(target);
        while (state.result().type() != RenderResult.Type.DONE) {
            switch(state.result().type()) {
                // If it's done, do nothing.
                case DONE:
                    break;
                // Render engine is signalling that we are waiting on an async task.
                case DETACH:
                    state.result().future().get();
                    state = state.continueRender();
                    break;
                // Output buffer is full.
                case LIMITED:
                    break;
                default:
                    break;
            }
        }
        return target;
    } catch (IOException e) {
        throw new ViewRenderingException("Error rendering Soy Sauce view [" + viewName + "]: " + e.getMessage(), e);
    } catch (InterruptedException ixe) {
        throw new ViewRenderingException("Interrupted while rendering Soy Sauce view [" + viewName + "]: " + ixe.getMessage(), ixe);
    } catch (ExecutionException exe) {
        throw new ViewRenderingException("Execution error while rendering Soy Sauce view [" + viewName + "]: " + exe.getMessage(), exe);
    }
}
Also used : SoySauce(com.google.template.soy.jbcsrc.api.SoySauce) SoyIdRenamingMap(com.google.template.soy.shared.SoyIdRenamingMap) HashMap(java.util.HashMap) ViewRenderingException(io.micronaut.views.exceptions.ViewRenderingException) IOException(java.io.IOException) SoyCssRenamingMap(com.google.template.soy.shared.SoyCssRenamingMap) SoyTemplate(com.google.template.soy.data.SoyTemplate) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) SoyIdRenamingMap(com.google.template.soy.shared.SoyIdRenamingMap) Map(java.util.Map) SoyCssRenamingMap(com.google.template.soy.shared.SoyCssRenamingMap) NonNull(io.micronaut.core.annotation.NonNull)

Example 18 with NonNull

use of io.micronaut.core.annotation.NonNull in project micronaut-multitenancy by micronaut-projects.

the class SessionTenantResolver method resolveTenantIdentifier.

@Override
@NonNull
public Serializable resolveTenantIdentifier(@NonNull @NotNull HttpRequest<?> request) throws TenantNotFoundException {
    if (this.attribute == null) {
        throw new TenantNotFoundException("Tenant could not be resolved from HTTP Session, because session attribute name is not set");
    }
    Optional<Session> opt = request.getAttributes().get(HttpSessionFilter.SESSION_ATTRIBUTE, Session.class);
    if (!opt.isPresent()) {
        throw new TenantNotFoundException("Tenant could not be resolved from HTTP Session, if session not present");
    }
    Session session = opt.get();
    Optional<Object> tenantId = session.get(attribute);
    if (!tenantId.isPresent()) {
        throw new TenantNotFoundException("Tenant could not be resolved from HTTP Session, if session attribute (" + attribute + ") not present");
    }
    if (!(tenantId.get() instanceof Serializable)) {
        throw new TenantNotFoundException("Tenant was resolved from HTTP Session, but it is not serializable");
    }
    return (Serializable) tenantId.get();
}
Also used : Serializable(java.io.Serializable) TenantNotFoundException(io.micronaut.multitenancy.exceptions.TenantNotFoundException) Session(io.micronaut.session.Session) NonNull(io.micronaut.core.annotation.NonNull)

Example 19 with NonNull

use of io.micronaut.core.annotation.NonNull in project micronaut-views by micronaut-projects.

the class RockerViewsRenderer method render.

@NonNull
@Override
public Writable render(@NonNull String view, @Nullable T data, @Nullable HttpRequest<?> request) {
    ArgumentUtils.requireNonNull("view", view);
    Map<String, Object> context = ViewUtils.modelOf(data);
    BindableRockerModel model = rockerConfiguration.isRelaxed() ? rockerEngine.template(view).relaxedBind(context) : rockerEngine.template(view).bind(context);
    return new RockerWritable(model);
}
Also used : BindableRockerModel(com.fizzed.rocker.BindableRockerModel) NonNull(io.micronaut.core.annotation.NonNull)

Aggregations

NonNull (io.micronaut.core.annotation.NonNull)19 Singleton (jakarta.inject.Singleton)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 ApplicationContextBuilder (io.micronaut.context.ApplicationContextBuilder)2 BeanContext (io.micronaut.context.BeanContext)2 StringUtils (io.micronaut.core.util.StringUtils)2 MediaType (io.micronaut.http.MediaType)2 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)2 OauthClientConfiguration (io.micronaut.security.oauth2.configuration.OauthClientConfiguration)2 AccessTokenGeneratedEvent (io.micronaut.security.token.event.AccessTokenGeneratedEvent)2 IOException (java.io.IOException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BindableRockerModel (com.fizzed.rocker.BindableRockerModel)1 BatchingSettings (com.google.api.gax.batching.BatchingSettings)1 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 FixedExecutorProvider (com.google.api.gax.core.FixedExecutorProvider)1 TransportChannelProvider (com.google.api.gax.rpc.TransportChannelProvider)1