Skip to content

Commit

Permalink
feat(el): Add API informations into the template variables
Browse files Browse the repository at this point in the history
  • Loading branch information
brasseld authored and NicolasGeraud committed Nov 16, 2018
1 parent 5a5423a commit b71e77b
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 9 deletions.
Expand Up @@ -25,6 +25,7 @@
import io.gravitee.gateway.core.endpoint.resolver.EndpointResolver;
import io.gravitee.gateway.core.endpoint.resolver.impl.TargetEndpointResolver;
import io.gravitee.gateway.core.invoker.InvokerFactory;
import io.gravitee.gateway.handlers.api.context.ApiTemplateVariableProvider;
import io.gravitee.gateway.handlers.api.path.PathResolver;
import io.gravitee.gateway.handlers.api.path.impl.ApiPathResolverImpl;
import io.gravitee.gateway.handlers.api.policy.security.PlanBasedAuthenticationHandlerEnhancer;
Expand Down Expand Up @@ -137,4 +138,9 @@ public EndpointResolver endpointResolver() {
public EndpointFactory endpointFactory() {
return new SpringFactoriesEndpointFactory();
}

@Bean
public ApiTemplateVariableProvider apiTemplateVariableProvider() {
return new ApiTemplateVariableProvider();
}
}
Expand Up @@ -30,8 +30,6 @@
import io.gravitee.gateway.api.Request;
import io.gravitee.gateway.api.Response;
import io.gravitee.gateway.api.buffer.Buffer;
import io.gravitee.gateway.api.expression.TemplateContext;
import io.gravitee.gateway.api.expression.TemplateVariableProvider;
import io.gravitee.gateway.api.handler.Handler;
import io.gravitee.gateway.api.proxy.ProxyResponse;
import io.gravitee.gateway.core.endpoint.lifecycle.GroupLifecyleManager;
Expand Down Expand Up @@ -64,7 +62,7 @@
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class ApiReactorHandler extends AbstractReactorHandler implements TemplateVariableProvider, InitializingBean {
public class ApiReactorHandler extends AbstractReactorHandler implements InitializingBean {

private final Logger logger = LoggerFactory.getLogger(ApiReactorHandler.class);

Expand Down Expand Up @@ -249,11 +247,6 @@ private void handleProcessorFailure(ProcessorFailure failure, Response response)
response.end();
}

@Override
public void provide(TemplateContext templateContext) {
templateContext.setVariable("properties", api.properties());
}

private class ProcessorFailureAsJson {

@JsonProperty
Expand Down
@@ -0,0 +1,47 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.gateway.handlers.api.context;

import io.gravitee.gateway.handlers.api.definition.Api;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
class ApiProperties {

private final Api api;

ApiProperties(final Api api) {
this.api = api;
}

public String getId() {
return this.api.getId();
}

public String getName() {
return this.api.getName();
}

public String getVersion() {
return this.api.getVersion();
}

public io.gravitee.definition.model.Properties getProperties() {
return this.api.getProperties();
}
}
@@ -0,0 +1,47 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.gateway.handlers.api.context;

import io.gravitee.gateway.api.expression.TemplateContext;
import io.gravitee.gateway.api.expression.TemplateVariableProvider;
import io.gravitee.gateway.handlers.api.definition.Api;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class ApiTemplateVariableProvider implements TemplateVariableProvider {

@Autowired
private Api api;

private ApiProperties apiProperties;

@PostConstruct
public void afterPropertiesSet() {
apiProperties = new ApiProperties(api);
}
@Override
public void provide(TemplateContext templateContext) {
// Keep this variable for backward compatibility
templateContext.setVariable("properties", api.properties());

templateContext.setVariable("api", apiProperties);
}
}
Expand Up @@ -2,4 +2,4 @@ io.gravitee.gateway.reactor.handler.ReactorHandlerFactory=\
io.gravitee.gateway.handlers.api.ApiContextHandlerFactory

io.gravitee.gateway.api.expression.TemplateVariableProvider=\
io.gravitee.gateway.reactor.handler.ReactorHandler
io.gravitee.gateway.handlers.api.context.ApiTemplateVariableProvider

0 comments on commit b71e77b

Please sign in to comment.