Java Code Examples for io.micronaut.core.convert.ConversionService#SHARED

The following examples show how to use io.micronaut.core.convert.ConversionService#SHARED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MicronautAwsProxyRequest.java    From micronaut-aws with Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
public Cookies getCookies() {
    if (cookies == null) {
        SimpleCookies simpleCookies = new SimpleCookies(ConversionService.SHARED);
        getHeaders().getAll(HttpHeaders.COOKIE).forEach(cookieValue -> {
            List<HeaderValue> parsedHeaders = parseHeaderValue(cookieValue, ";", ",");


            parsedHeaders.stream()
                    .filter(e -> e.getKey() != null)
                    .map(e -> new SimpleCookie(SecurityUtils.crlf(e.getKey()), SecurityUtils.crlf(e.getValue())))
                    .forEach(simpleCookie ->
                            simpleCookies.put(simpleCookie.getName(), simpleCookie));
        });

        cookies = simpleCookies;
    }
    return cookies;
}
 
Example 2
Source File: GoogleFunctionHttpRequest.java    From micronaut-gcp with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public Cookies getCookies() {
    ServletCookies cookies = this.cookies;
    if (cookies == null) {
        synchronized (this) { // double check
            cookies = this.cookies;
            if (cookies == null) {
                cookies = new ServletCookies(getPath(), getHeaders(), ConversionService.SHARED);
                this.cookies = cookies;
            }
        }
    }
    return cookies;
}
 
Example 3
Source File: MicronautAwsProxyRequest.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 *
 * @param path            The path
 * @param awsProxyRequest The underlying request
 * @param securityContext The {@link SecurityContext}
 * @param lambdaContext   The lambda context
 * @param config          The container configuration
 */
MicronautAwsProxyRequest(
        String path,
        AwsProxyRequest awsProxyRequest,
        SecurityContext securityContext,
        Context lambdaContext,
        ContainerConfig config) {
    this.config = config;
    this.awsProxyRequest = awsProxyRequest;
    this.path = path;
    final String httpMethod = awsProxyRequest.getHttpMethod();
    this.httpMethod = StringUtils.isNotEmpty(httpMethod) ? HttpMethod.valueOf(httpMethod) : HttpMethod.GET;
    final Headers multiValueHeaders = awsProxyRequest.getMultiValueHeaders();
    this.headers = multiValueHeaders != null ? new AwsHeaders() : new SimpleHttpHeaders(ConversionService.SHARED);
    final MultiValuedTreeMap<String, String> params = awsProxyRequest.getMultiValueQueryStringParameters();
    this.parameters = params != null ? new AwsParameters() : new SimpleHttpParameters(ConversionService.SHARED);

    final AwsProxyRequestContext requestContext = awsProxyRequest.getRequestContext();
    setAttribute(API_GATEWAY_CONTEXT_PROPERTY, requestContext);
    setAttribute(API_GATEWAY_STAGE_VARS_PROPERTY, awsProxyRequest.getStageVariables());
    setAttribute(API_GATEWAY_EVENT_PROPERTY, awsProxyRequest);
    if (requestContext != null) {
        setAttribute(ALB_CONTEXT_PROPERTY, requestContext.getElb());
    }
    setAttribute(LAMBDA_CONTEXT_PROPERTY, lambdaContext);
    setAttribute(JAX_SECURITY_CONTEXT_PROPERTY, config);
    if (securityContext != null && requestContext != null) {
        setAttribute("micronaut.AUTHENTICATION", securityContext.getUserPrincipal());
    }
}
 
Example 4
Source File: DataInitializer.java    From micronaut-data with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 */
DataInitializer() {
    ConversionService<?> conversionService = ConversionService.SHARED;
    conversionService.addConverter(OffsetDateTime.class, java.sql.Date.class, offsetDateTime ->
            new java.sql.Date(offsetDateTime.toInstant().toEpochMilli())
    );
    conversionService.addConverter(byte[].class, UUID.class, UUID::nameUUIDFromBytes);
    conversionService.addConverter(Timestamp.class, ZonedDateTime.class, timestamp ->
            timestamp.toLocalDateTime().atZone(ZoneId.systemDefault())
    );
    conversionService.addConverter(ZonedDateTime.class, Timestamp.class, zonedDateTime ->
            new Timestamp(zonedDateTime.toInstant().toEpochMilli())
    );
    conversionService.addConverter(Timestamp.class, LocalDateTime.class, Timestamp::toLocalDateTime);
    conversionService.addConverter(Instant.class, Date.class, instant ->
            new Date(instant.toEpochMilli())
    );
    conversionService.addConverter(LocalDateTime.class, Date.class, localDateTime ->
            new Date(localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli())
    );
    conversionService.addConverter(Date.class, LocalDateTime.class, date ->
            Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime()
    );
    conversionService.addConverter(Date.class, OffsetDateTime.class, date ->
            Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toOffsetDateTime()
    );
    conversionService.addConverter(Date.class, LocalDate.class, date ->
            Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate()
    );
    conversionService.addConverter(Date.class, Instant.class, date ->
            Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toInstant()
    );
    conversionService.addConverter(ChronoLocalDate.class, Date.class, localDate ->
            new Date(localDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli())
    );
    conversionService.addConverter(OffsetDateTime.class, Date.class, offsetDateTime ->
            new Date(offsetDateTime.toInstant().toEpochMilli())
    );
    conversionService.addConverter(OffsetDateTime.class, Instant.class, OffsetDateTime::toInstant);
    conversionService.addConverter(OffsetDateTime.class, Long.class, offsetDateTime ->
            offsetDateTime.toInstant().toEpochMilli()
    );
    conversionService.addConverter(OffsetDateTime.class, Timestamp.class, offsetDateTime ->
            new Timestamp(offsetDateTime.toInstant().toEpochMilli())
    );
    conversionService.addConverter(OffsetDateTime.class, LocalDateTime.class, OffsetDateTime::toLocalDateTime);
    conversionService.addConverter(OffsetDateTime.class, LocalDate.class, OffsetDateTime::toLocalDate);
}
 
Example 5
Source File: TypeMapper.java    From micronaut-data with Apache License 2.0 4 votes vote down vote up
/**
 * @return The conversion service to use.
 */
default @NonNull ConversionService<?> getConversionService() {
    return ConversionService.SHARED;
}
 
Example 6
Source File: EventIntegrator.java    From micronaut-data with Apache License 2.0 4 votes vote down vote up
@Override
public void integrate(
        Metadata metadata,
        SessionFactoryImplementor sessionFactory,
        SessionFactoryServiceRegistry serviceRegistry) {
    EventListenerRegistry eventListenerRegistry =
            serviceRegistry.getService(EventListenerRegistry.class);

    Collection<PersistentClass> entityBindings = metadata.getEntityBindings();
    Map<Class, BeanProperty> lastUpdates = new HashMap<>(entityBindings.size());
    Map<Class, BeanProperty> dateCreated = new HashMap<>(entityBindings.size());

    entityBindings.forEach(e -> {
                Class<?> mappedClass = e.getMappedClass();
                if (mappedClass != null) {
                    BeanIntrospection<?> introspection = BeanIntrospector.SHARED.findIntrospection(mappedClass).orElse(null);
                    if (introspection != null) {
                        introspection.getIndexedProperty(DateCreated.class).ifPresent(bp ->
                                dateCreated.put(mappedClass, bp)
                        );
                        introspection.getIndexedProperty(DateUpdated.class).ifPresent(bp ->
                                lastUpdates.put(mappedClass, bp)
                        );
                    }
                }
            }
    );

    ConversionService<?> conversionService = ConversionService.SHARED;

    if (CollectionUtils.isNotEmpty(dateCreated)) {

        eventListenerRegistry.getEventListenerGroup(EventType.PRE_INSERT)
                .appendListener((PreInsertEventListener) event -> {
                    Object[] state = event.getState();
                    timestampIfNecessary(
                            dateCreated,
                            lastUpdates,
                            conversionService,
                            event,
                            state,
                            true
                    );
                    return false;
                });
    }

    if (CollectionUtils.isNotEmpty(lastUpdates)) {

        eventListenerRegistry.getEventListenerGroup(EventType.PRE_UPDATE)
                .appendListener((PreUpdateEventListener) event -> {
                    timestampIfNecessary(
                            dateCreated, lastUpdates,
                            conversionService,
                            event,
                            event.getState(),
                            false
                    );
                    return false;
                });
    }
}