Search in sources :

Example 1 with Parameter

use of org.glassfish.hk2.classmodel.reflect.Parameter in project graphhopper by graphhopper.

the class GraphHopperBundle method run.

@Override
public void run(GraphHopperBundleConfiguration configuration, Environment environment) {
    for (Object k : System.getProperties().keySet()) {
        if (k instanceof String && ((String) k).startsWith("graphhopper."))
            throw new IllegalArgumentException("You need to prefix system parameters with '-Ddw.graphhopper.' instead of '-Dgraphhopper.' see #1879 and #1897");
    }
    // When Dropwizard's Hibernate Validation misvalidates a query parameter,
    // a JerseyViolationException is thrown.
    // With this mapper, we use our custom format for that (backwards compatibility),
    // and also coerce the media type of the response to JSON, so we can return JSON error
    // messages from methods that normally have a different return type.
    // That's questionable, but on the other hand, Dropwizard itself does the same thing,
    // not here, but in a different place (the custom parameter parsers).
    // So for the moment we have to assume that both mechanisms
    // a) always return JSON error messages, and
    // b) there's no need to annotate the method with media type JSON for that.
    // 
    // However, for places that throw IllegalArgumentException or MultiException,
    // we DO need to use the media type JSON annotation, because
    // those are agnostic to the media type (could be GPX!), so the server needs to know
    // that a JSON error response is supported. (See below.)
    environment.jersey().register(new GHJerseyViolationExceptionMapper());
    // If the "?type=gpx" parameter is present, sets a corresponding media type header
    environment.jersey().register(new TypeGPXFilter());
    // Together, these two take care that MultiExceptions thrown from RouteResource
    // come out as JSON or GPX, depending on the media type
    environment.jersey().register(new MultiExceptionMapper());
    environment.jersey().register(new MultiExceptionGPXMessageBodyWriter());
    // This makes an IllegalArgumentException come out as a MultiException with
    // a single entry.
    environment.jersey().register(new IllegalArgumentExceptionMapper());
    final GraphHopperManaged graphHopperManaged = new GraphHopperManaged(configuration.getGraphHopperConfiguration());
    environment.lifecycle().manage(graphHopperManaged);
    final GraphHopper graphHopper = graphHopperManaged.getGraphHopper();
    environment.jersey().register(new AbstractBinder() {

        @Override
        protected void configure() {
            bind(configuration.getGraphHopperConfiguration()).to(GraphHopperConfig.class);
            bind(graphHopper).to(GraphHopper.class);
            bind(new JTSTriangulator(graphHopper.getRouterConfig())).to(Triangulator.class);
            bindFactory(PathDetailsBuilderFactoryFactory.class).to(PathDetailsBuilderFactory.class);
            bindFactory(ProfileResolverFactory.class).to(ProfileResolver.class);
            bindFactory(HasElevation.class).to(Boolean.class).named("hasElevation");
            bindFactory(LocationIndexFactory.class).to(LocationIndex.class);
            bindFactory(TranslationMapFactory.class).to(TranslationMap.class);
            bindFactory(EncodingManagerFactory.class).to(EncodingManager.class);
            bindFactory(GraphHopperStorageFactory.class).to(GraphHopperStorage.class);
            bindFactory(GtfsStorageFactory.class).to(GtfsStorage.class);
        }
    });
    environment.jersey().register(MVTResource.class);
    environment.jersey().register(NearestResource.class);
    environment.jersey().register(RouteResource.class);
    environment.jersey().register(IsochroneResource.class);
    environment.jersey().register(MapMatchingResource.class);
    if (configuration.getGraphHopperConfiguration().has("gtfs.file")) {
        // These are pt-specific implementations of /route and /isochrone, but the same API.
        // We serve them under different paths (/route-pt and /isochrone-pt), and forward
        // requests for ?vehicle=pt there.
        environment.jersey().register(new AbstractBinder() {

            @Override
            protected void configure() {
                if (configuration.getGraphHopperConfiguration().getBool("gtfs.free_walk", false)) {
                    bind(PtRouterFreeWalkImpl.class).to(PtRouter.class);
                } else {
                    bind(PtRouterImpl.class).to(PtRouter.class);
                }
            }
        });
        environment.jersey().register(PtRouteResource.class);
        environment.jersey().register(PtIsochroneResource.class);
        environment.jersey().register(PtMVTResource.class);
        environment.jersey().register(PtRedirectFilter.class);
    }
    environment.jersey().register(SPTResource.class);
    environment.jersey().register(I18NResource.class);
    environment.jersey().register(InfoResource.class);
    environment.healthChecks().register("graphhopper", new GraphHopperHealthCheck(graphHopper));
    environment.jersey().register(environment.healthChecks());
    environment.jersey().register(HealthCheckResource.class);
}
Also used : JTSTriangulator(com.graphhopper.isochrone.algorithm.JTSTriangulator) Triangulator(com.graphhopper.isochrone.algorithm.Triangulator) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) GraphHopper(com.graphhopper.GraphHopper) GraphHopperConfig(com.graphhopper.GraphHopperConfig) ProfileResolver(com.graphhopper.routing.ProfileResolver) EncodingManager(com.graphhopper.routing.util.EncodingManager) JTSTriangulator(com.graphhopper.isochrone.algorithm.JTSTriangulator) LocationIndex(com.graphhopper.storage.index.LocationIndex) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) PathDetailsBuilderFactory(com.graphhopper.util.details.PathDetailsBuilderFactory) GraphHopperHealthCheck(com.graphhopper.http.health.GraphHopperHealthCheck) TranslationMap(com.graphhopper.util.TranslationMap)

Example 2 with Parameter

use of org.glassfish.hk2.classmodel.reflect.Parameter in project Payara by payara.

the class ParameterImpl method createInstance.

public static Parameter createInstance(AnnotationModel annotation, ApiContext context) {
    ParameterImpl from = new ParameterImpl();
    from.setName(annotation.getValue("name", String.class));
    EnumModel inEnum = annotation.getValue("in", EnumModel.class);
    if (inEnum != null) {
        from.setIn(In.valueOf(inEnum.getValue()));
    }
    from.setDescription(annotation.getValue("description", String.class));
    from.setRequired(annotation.getValue("required", Boolean.class));
    from.setDeprecated(annotation.getValue("deprecated", Boolean.class));
    from.setAllowEmptyValue(annotation.getValue("allowEmptyValue", Boolean.class));
    String ref = annotation.getValue("ref", String.class);
    if (ref != null && !ref.isEmpty()) {
        from.setRef(ref);
    }
    EnumModel styleEnum = annotation.getValue("style", EnumModel.class);
    if (styleEnum != null) {
        from.setStyle(Style.valueOf(styleEnum.getValue()));
    }
    from.setExplode(annotation.getValue("explode", Boolean.class));
    from.setAllowReserved(annotation.getValue("allowReserved", Boolean.class));
    AnnotationModel schemaAnnotation = annotation.getValue("schema", AnnotationModel.class);
    if (schemaAnnotation != null) {
        Boolean hidden = schemaAnnotation.getValue("hidden", Boolean.class);
        if (hidden == null || !hidden) {
            from.setSchema(SchemaImpl.createInstance(schemaAnnotation, context));
        }
    }
    extractAnnotations(annotation, context, "examples", "name", ExampleImpl::createInstance, from::addExample);
    from.setExample(annotation.getValue("example", Object.class));
    final List<ContentImpl> contents = createList();
    extractAnnotations(annotation, context, "content", ContentImpl::createInstance, contents::add);
    for (ContentImpl content : contents) {
        content.getMediaTypes().forEach(from.content::addMediaType);
    }
    return from;
}
Also used : EnumModel(org.glassfish.hk2.classmodel.reflect.EnumModel) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) ContentImpl(fish.payara.microprofile.openapi.impl.model.media.ContentImpl) ExampleImpl(fish.payara.microprofile.openapi.impl.model.examples.ExampleImpl)

Example 3 with Parameter

use of org.glassfish.hk2.classmodel.reflect.Parameter in project Payara by payara.

the class ApplicationProcessor method getArraySchema.

private static SchemaImpl getArraySchema(AnnotatedElement element, ApiContext context) {
    SchemaImpl arraySchema = new SchemaImpl();
    List<ParameterizedType> parameterizedType;
    if (element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
        org.glassfish.hk2.classmodel.reflect.Parameter parameter = (org.glassfish.hk2.classmodel.reflect.Parameter) element;
        parameterizedType = parameter.getParameterizedTypes();
    } else {
        FieldModel field = (FieldModel) element;
        parameterizedType = field.getParameterizedTypes();
    }
    arraySchema.setType(ModelUtils.getSchemaType(parameterizedType.get(0).getTypeName(), context));
    return arraySchema;
}
Also used : ParameterizedType(org.glassfish.hk2.classmodel.reflect.ParameterizedType) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Example 4 with Parameter

use of org.glassfish.hk2.classmodel.reflect.Parameter in project Payara by payara.

the class ApplicationProcessor method addParameter.

private static void addParameter(AnnotatedElement element, ApiContext context, String name, In in, Boolean required) {
    Boolean hidden = false;
    AnnotationModel paramAnnotation = element.getAnnotation(org.eclipse.microprofile.openapi.annotations.parameters.Parameter.class.getName());
    if (paramAnnotation != null) {
        hidden = paramAnnotation.getValue("hidden", Boolean.class);
    }
    if (hidden != null && hidden) {
        return;
    }
    Parameter newParameter = new ParameterImpl();
    newParameter.setName(name);
    newParameter.setIn(in);
    newParameter.setRequired(required);
    SchemaImpl schema = new SchemaImpl();
    String defaultValue = getDefaultValueIfPresent(element);
    if (element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
        org.glassfish.hk2.classmodel.reflect.Parameter parameter = (org.glassfish.hk2.classmodel.reflect.Parameter) element;
        schema.setType(ModelUtils.getSchemaType(parameter.getTypeName(), context));
    } else {
        FieldModel field = (FieldModel) element;
        schema.setType(ModelUtils.getSchemaType(field.getTypeName(), context));
    }
    if (schema.getType() == SchemaType.ARRAY) {
        schema.setItems(getArraySchema(element, context));
        if (defaultValue != null) {
            schema.getItems().setDefaultValue(defaultValue);
        }
    } else if (defaultValue != null) {
        schema.setDefaultValue(defaultValue);
    }
    newParameter.setSchema(schema);
    final Operation workingOperation = context.getWorkingOperation();
    if (workingOperation != null) {
        for (Parameter parameter : workingOperation.getParameters()) {
            final String parameterName = parameter.getName();
            if (parameterName != null && parameterName.equals(newParameter.getName())) {
                ParameterImpl.merge(newParameter, parameter, false, context);
                return;
            }
        }
        workingOperation.addParameter(newParameter);
    } else {
        LOGGER.log(SEVERE, "Couldn''t add {0} parameter, \"{1}\" to the OpenAPI Document. This is usually caused by declaring parameter under a method with an unsupported annotation.", new Object[] { newParameter.getIn(), newParameter.getName() });
    }
}
Also used : Operation(org.eclipse.microprofile.openapi.models.Operation) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ParameterImpl(fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Example 5 with Parameter

use of org.glassfish.hk2.classmodel.reflect.Parameter in project Payara by payara.

the class ApplicationProcessor method visitParameters.

@Override
public void visitParameters(AnnotationModel annotation, AnnotatedElement element, ApiContext context) {
    List<AnnotationModel> parameters = annotation.getValue("value", List.class);
    if (parameters != null) {
        for (AnnotationModel paramAnnotation : parameters) {
            final Parameter parameter = ParameterImpl.createInstance(paramAnnotation, context);
            final Operation workingOperation = context.getWorkingOperation();
            if (workingOperation != null) {
                workingOperation.addParameter(parameter);
            }
        }
    }
}
Also used : AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) Operation(org.eclipse.microprofile.openapi.models.Operation)

Aggregations

AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)5 FieldModel (org.glassfish.hk2.classmodel.reflect.FieldModel)5 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)4 Parameter (org.eclipse.microprofile.openapi.models.parameters.Parameter)4 MethodModel (org.glassfish.hk2.classmodel.reflect.MethodModel)4 ContentImpl (fish.payara.microprofile.openapi.impl.model.media.ContentImpl)2 ParameterImpl (fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl)2 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 Collection (java.util.Collection)2 Operation (org.eclipse.microprofile.openapi.models.Operation)2 AnnotatedElement (org.glassfish.hk2.classmodel.reflect.AnnotatedElement)2 ClassModel (org.glassfish.hk2.classmodel.reflect.ClassModel)2 ExtensibleType (org.glassfish.hk2.classmodel.reflect.ExtensibleType)2 GraphHopper (com.graphhopper.GraphHopper)1 GraphHopperConfig (com.graphhopper.GraphHopperConfig)1 GraphHopperHealthCheck (com.graphhopper.http.health.GraphHopperHealthCheck)1 JTSTriangulator (com.graphhopper.isochrone.algorithm.JTSTriangulator)1 Triangulator (com.graphhopper.isochrone.algorithm.Triangulator)1 ProfileResolver (com.graphhopper.routing.ProfileResolver)1