All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.up.uca.rs.mime.parse.MimeAtomic Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package io.vertx.up.uca.rs.mime.parse;

import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import io.vertx.up.atom.Epsilon;
import io.vertx.up.eon.em.MimeFlow;
import io.vertx.up.exception.WebException;
import io.vertx.up.log.Annal;
import io.vertx.up.uca.rs.mime.Resolver;
import io.vertx.up.uca.rs.mime.resolver.UnsetResolver;
import io.vertx.up.uca.yaml.Node;
import io.vertx.up.util.Ut;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import java.lang.annotation.Annotation;

public class MimeAtomic implements Atomic {

    private static final Node NODE = Node.infix("resolver");
    private static final Annal LOGGER = Annal.get(MimeAtomic.class);

    @Override
    public Epsilon ingest(final RoutingContext context,
                             final Epsilon income)
            throws WebException {
        final Epsilon epsilon;
        if (MimeFlow.TYPED == income.getMime()) {
            /** Resolver **/
            final Atomic atomic = Ut.singleton(TypedAtomic.class);
            epsilon = atomic.ingest(context, income);
        } else if (MimeFlow.STANDARD == income.getMime()) {
            /** System standard filler **/
            final Atomic atomic = Ut.singleton(StandardAtomic.class);
            epsilon = atomic.ingest(context, income);
        } else {
            /** Resolver **/
            final Resolver resolver = getResolver(context, income);
            epsilon = resolver.resolve(context, income);
        }
        return epsilon;
    }

    private Resolver getResolver(final RoutingContext context,
                                    final Epsilon income) {
        /** 1.Read the resolver first **/
        final Annotation annotation = income.getAnnotation();
        final Class resolverCls = Ut.invoke(annotation, "resolver");
        final String header = context.request().getHeader(HttpHeaders.CONTENT_TYPE);
        /** 2.Check configured in default **/
        if (UnsetResolver.class == resolverCls) {
            /** 3. Old path **/
            final JsonObject content = NODE.read();
            // LOGGER.info("[ RESOLVER ] Resolvers = {0}", content.encodePrettily());
            final String resolver;
            if (null == header) {
                resolver = content.getString("default");
            } else {
                final MediaType type = MediaType.valueOf(header);
                final JsonObject resolverMap = content.getJsonObject(type.getType());
                resolver = resolverMap.getString(type.getSubtype());
            }
            LOGGER.info(Info.RESOLVER, resolver, header, context.request().absoluteURI());
            return Ut.singleton(resolver);
        } else {
            LOGGER.info(Info.RESOLVER_CONFIG, resolverCls, header);
            return Ut.singleton(resolverCls);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy