Search in sources :

Example 31 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration in project camel by apache.

the class RestComponent method findGlobalRestConfiguration.

// ****************************************
// Helpers
// ****************************************
private RestConfiguration findGlobalRestConfiguration() {
    CamelContext context = getCamelContext();
    RestConfiguration conf = CamelContextHelper.lookup(context, RestConstants.DEFAULT_REST_CONFIGURATION_ID, RestConfiguration.class);
    if (conf == null) {
        conf = CamelContextHelper.findByType(getCamelContext(), RestConfiguration.class);
    }
    return conf;
}
Also used : CamelContext(org.apache.camel.CamelContext) RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 32 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration in project camel by apache.

the class DefaultCamelContext method getRestConfiguration.

public RestConfiguration getRestConfiguration() {
    RestConfiguration config = restConfigurations.get("");
    if (config == null) {
        config = new RestConfiguration();
        setRestConfiguration(config);
    }
    return config;
}
Also used : RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 33 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration in project camel by apache.

the class RestBindingDefinition method createRestBindingAdvice.

public RestBindingAdvice createRestBindingAdvice(RouteContext routeContext) throws Exception {
    CamelContext context = routeContext.getCamelContext();
    RestConfiguration config = context.getRestConfiguration(component, true);
    // these options can be overridden per rest verb
    String mode = config.getBindingMode().name();
    if (bindingMode != null) {
        mode = bindingMode.name();
    }
    boolean cors = config.isEnableCORS();
    if (enableCORS != null) {
        cors = enableCORS;
    }
    boolean skip = config.isSkipBindingOnErrorCode();
    if (skipBindingOnErrorCode != null) {
        skip = skipBindingOnErrorCode;
    }
    // cors headers
    Map<String, String> corsHeaders = config.getCorsHeaders();
    if (mode == null || "off".equals(mode)) {
        // binding mode is off, so create a off mode binding processor
        return new RestBindingAdvice(context, null, null, null, null, consumes, produces, mode, skip, cors, corsHeaders, defaultValues);
    }
    // setup json data format
    String name = config.getJsonDataFormat();
    if (name != null) {
        // must only be a name, not refer to an existing instance
        Object instance = context.getRegistry().lookupByName(name);
        if (instance != null) {
            throw new IllegalArgumentException("JsonDataFormat name: " + name + " must not be an existing bean instance from the registry");
        }
    } else {
        name = "json-jackson";
    }
    // this will create a new instance as the name was not already pre-created
    DataFormat json = context.resolveDataFormat(name);
    DataFormat outJson = context.resolveDataFormat(name);
    // is json binding required?
    if (mode.contains("json") && json == null) {
        throw new IllegalArgumentException("JSon DataFormat " + name + " not found.");
    }
    if (json != null) {
        Class<?> clazz = null;
        if (type != null) {
            String typeName = type.endsWith("[]") ? type.substring(0, type.length() - 2) : type;
            clazz = context.getClassResolver().resolveMandatoryClass(typeName);
        }
        if (clazz != null) {
            IntrospectionSupport.setProperty(context.getTypeConverter(), json, "unmarshalType", clazz);
            IntrospectionSupport.setProperty(context.getTypeConverter(), json, "useList", type.endsWith("[]"));
        }
        setAdditionalConfiguration(config, context, json, "json.in.");
        Class<?> outClazz = null;
        if (outType != null) {
            String typeName = outType.endsWith("[]") ? outType.substring(0, outType.length() - 2) : outType;
            outClazz = context.getClassResolver().resolveMandatoryClass(typeName);
        }
        if (outClazz != null) {
            IntrospectionSupport.setProperty(context.getTypeConverter(), outJson, "unmarshalType", outClazz);
            IntrospectionSupport.setProperty(context.getTypeConverter(), outJson, "useList", outType.endsWith("[]"));
        }
        setAdditionalConfiguration(config, context, outJson, "json.out.");
    }
    // setup xml data format
    name = config.getXmlDataFormat();
    if (name != null) {
        // must only be a name, not refer to an existing instance
        Object instance = context.getRegistry().lookupByName(name);
        if (instance != null) {
            throw new IllegalArgumentException("XmlDataFormat name: " + name + " must not be an existing bean instance from the registry");
        }
    } else {
        name = "jaxb";
    }
    // this will create a new instance as the name was not already pre-created
    DataFormat jaxb = context.resolveDataFormat(name);
    DataFormat outJaxb = context.resolveDataFormat(name);
    // is xml binding required?
    if (mode.contains("xml") && jaxb == null) {
        throw new IllegalArgumentException("XML DataFormat " + name + " not found.");
    }
    if (jaxb != null) {
        Class<?> clazz = null;
        if (type != null) {
            String typeName = type.endsWith("[]") ? type.substring(0, type.length() - 2) : type;
            clazz = context.getClassResolver().resolveMandatoryClass(typeName);
        }
        if (clazz != null) {
            JAXBContext jc = JAXBContext.newInstance(clazz);
            IntrospectionSupport.setProperty(context.getTypeConverter(), jaxb, "context", jc);
        }
        setAdditionalConfiguration(config, context, jaxb, "xml.in.");
        Class<?> outClazz = null;
        if (outType != null) {
            String typeName = outType.endsWith("[]") ? outType.substring(0, outType.length() - 2) : outType;
            outClazz = context.getClassResolver().resolveMandatoryClass(typeName);
        }
        if (outClazz != null) {
            JAXBContext jc = JAXBContext.newInstance(outClazz);
            IntrospectionSupport.setProperty(context.getTypeConverter(), outJaxb, "context", jc);
        } else if (clazz != null) {
            // fallback and use the context from the input
            JAXBContext jc = JAXBContext.newInstance(clazz);
            IntrospectionSupport.setProperty(context.getTypeConverter(), outJaxb, "context", jc);
        }
        setAdditionalConfiguration(config, context, outJaxb, "xml.out.");
    }
    return new RestBindingAdvice(context, json, jaxb, outJson, outJaxb, consumes, produces, mode, skip, cors, corsHeaders, defaultValues);
}
Also used : CamelContext(org.apache.camel.CamelContext) DataFormat(org.apache.camel.spi.DataFormat) RestConfiguration(org.apache.camel.spi.RestConfiguration) JAXBContext(javax.xml.bind.JAXBContext) RestBindingAdvice(org.apache.camel.processor.RestBindingAdvice)

Example 34 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration in project camel by apache.

the class RestDefinition method asRouteDefinition.

/**
     * Transforms this REST definition into a list of {@link org.apache.camel.model.RouteDefinition} which
     * Camel routing engine can add and run. This allows us to define REST services using this
     * REST DSL and turn those into regular Camel routes.
     *
     * @param camelContext The Camel context
     */
public List<RouteDefinition> asRouteDefinition(CamelContext camelContext) {
    ObjectHelper.notNull(camelContext, "CamelContext");
    // sanity check this rest definition do not have duplicates
    validateUniquePaths();
    List<RouteDefinition> answer = new ArrayList<RouteDefinition>();
    if (camelContext.getRestConfigurations().isEmpty()) {
        camelContext.getRestConfiguration();
    }
    for (RestConfiguration config : camelContext.getRestConfigurations()) {
        addRouteDefinition(camelContext, answer, config.getComponent());
    }
    return answer;
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) ArrayList(java.util.ArrayList) RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 35 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration in project camel by apache.

the class RouteBuilder method populateRests.

protected void populateRests() throws Exception {
    ModelCamelContext camelContext = getContext();
    if (camelContext == null) {
        throw new IllegalArgumentException("CamelContext has not been injected!");
    }
    getRestCollection().setCamelContext(camelContext);
    // setup rest configuration before adding the rests
    if (getRestConfigurations() != null) {
        for (Map.Entry<String, RestConfigurationDefinition> entry : getRestConfigurations().entrySet()) {
            RestConfiguration config = entry.getValue().asRestConfiguration(getContext());
            if ("".equals(entry.getKey())) {
                camelContext.setRestConfiguration(config);
            } else {
                camelContext.addRestConfiguration(config);
            }
        }
    }
    camelContext.addRestDefinitions(getRestCollection().getRests());
    // convert rests into routes so we they are routes for runtime
    List<RouteDefinition> routes = new ArrayList<RouteDefinition>();
    for (RestDefinition rest : getRestCollection().getRests()) {
        List<RouteDefinition> list = rest.asRouteDefinition(getContext());
        routes.addAll(list);
    }
    // convert rests api-doc into routes so they are routes for runtime
    for (RestConfiguration config : camelContext.getRestConfigurations()) {
        if (config.getApiContextPath() != null) {
            // avoid adding rest-api multiple times, in case multiple RouteBuilder classes is added
            // to the CamelContext, as we only want to setup rest-api once
            // so we check all existing routes if they have rest-api route already added
            boolean hasRestApi = false;
            for (RouteDefinition route : camelContext.getRouteDefinitions()) {
                FromDefinition from = route.getInputs().get(0);
                if (from.getUri() != null && from.getUri().startsWith("rest-api:")) {
                    hasRestApi = true;
                }
            }
            if (!hasRestApi) {
                RouteDefinition route = RestDefinition.asRouteApiDefinition(camelContext, config);
                log.debug("Adding routeId: {} as rest-api route", route.getId());
                routes.add(route);
            }
        }
    }
    // add the rest routes
    for (RouteDefinition route : routes) {
        getRouteCollection().route(route);
    }
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) FromDefinition(org.apache.camel.model.FromDefinition) InterceptFromDefinition(org.apache.camel.model.InterceptFromDefinition) ArrayList(java.util.ArrayList) ModelCamelContext(org.apache.camel.model.ModelCamelContext) RestConfigurationDefinition(org.apache.camel.model.rest.RestConfigurationDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition) RestConfiguration(org.apache.camel.spi.RestConfiguration) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RestConfiguration (org.apache.camel.spi.RestConfiguration)36 HashMap (java.util.HashMap)10 Consumer (org.apache.camel.Consumer)10 Endpoint (org.apache.camel.Endpoint)8 CamelContext (org.apache.camel.CamelContext)6 Component (org.apache.camel.Component)3 NoSuchBeanException (org.apache.camel.NoSuchBeanException)3 RouteDefinition (org.apache.camel.model.RouteDefinition)3 Test (org.junit.Test)3 Swagger (io.swagger.models.Swagger)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)2 HttpConsumer (org.apache.camel.http.common.HttpConsumer)2 DefaultEndpoint (org.apache.camel.impl.DefaultEndpoint)2 FromDefinition (org.apache.camel.model.FromDefinition)2 InterceptFromDefinition (org.apache.camel.model.InterceptFromDefinition)2 RestDefinition (org.apache.camel.model.rest.RestDefinition)2 FactoryFinder (org.apache.camel.spi.FactoryFinder)2