Java Code Examples for io.vertx.ext.web.RoutingContext#pathParams()

The following examples show how to use io.vertx.ext.web.RoutingContext#pathParams() . 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: ApiDispatcher.java    From servicecomb-samples with Apache License 2.0 6 votes vote down vote up
protected void onRequest(RoutingContext context) {
  Map<String, String> pathParams = context.pathParams();
  String microserviceName = pathParams.get("param0");
  String path = "/" + pathParams.get("param1");

  EdgeInvocation invoker = new EdgeInvocation() {
    // Authentication. Notice: adding context must after setContext or will override by network
    protected void setContext() throws Exception {
      super.setContext();
      // get session id from header and cookie for debug reasons
      String sessionId = context.request().getHeader("session-id");
      if (sessionId != null) {
        this.invocation.addContext("session-id", sessionId);
      } else {
        Cookie sessionCookie = context.cookieMap().get("session-id");
        if (sessionCookie != null) {
          this.invocation.addContext("session-id", sessionCookie.getValue());
        }
      }
    }
  };
  invoker.init(microserviceName, context, path, httpServerFilters);
  invoker.edgeInvoke();
}
 
Example 2
Source File: ApiDispatcher.java    From servicecomb-samples with Apache License 2.0 6 votes vote down vote up
protected void onRequest(RoutingContext context) {
  Map<String, String> pathParams = context.pathParams();
  String microserviceName = pathParams.get("param0");
  String path = "/" + pathParams.get("param1");

  EdgeInvocation invoker = new EdgeInvocation() {
    // Authentication. Notice: adding context must after setContext or will override by network
    protected void setContext() throws Exception {
      super.setContext();
      // get session id from header and cookie for debug reasons
      String sessionId = context.request().getHeader("session-id");
      if (sessionId != null) {
        this.invocation.addContext("session-id", sessionId);
      } else {
        Cookie sessionCookie = context.cookieMap().get("session-id");
        if (sessionCookie != null) {
          this.invocation.addContext("session-id", sessionCookie.getValue());
        }
      }
    }
  };
  invoker.init(microserviceName, context, path, httpServerFilters);
  invoker.edgeInvoke();
}
 
Example 3
Source File: EdgeDispatcher.java    From scaffold with Apache License 2.0 6 votes vote down vote up
private void onRequest(RoutingContext context) {
  Map<String, String> pathParams = context.pathParams();
  //从匹配的param0拿到{ServiceComb微服务Name}
  final String service = pathParams.get("param0");
  //从匹配的param1拿到{服务路径&参数}
  String operationPath = "/" + pathParams.get("param1");

  //还记得我们之前说的做出一点点改进吗?引入一个自定义配置edge.routing-short-path.{简称},映射微服务名;如果简称没有配置,那么就认为直接是微服务的名
  final String serviceName = DynamicPropertyFactory.getInstance()
      .getStringProperty("edge.routing-short-path." + service, service).get();

  //检查灰度策略是否更新
  checkDarkLaunchRule(serviceName);

  //创建一个Edge转发
  EdgeInvocation edgeInvocation = new EdgeInvocation();
  //设定灰度版本策略
  edgeInvocation.setVersionRule(
      darkLaunchRules.get(serviceName).getRule().matchVersion(context.request().headers().entries()));
  edgeInvocation.init(serviceName, context, operationPath, httpServerFilters);
  edgeInvocation.edgeInvoke();
}
 
Example 4
Source File: BaseValidationHandler.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
private Map<String, RequestParameter> validatePathParams(RoutingContext routingContext) throws ValidationException {
  // Validation process validate only params that are registered in the validation -> extra params are allowed
  Map<String, RequestParameter> parsedParams = new HashMap<>();
  Map<String, String> pathParams = routingContext.pathParams();
  for (ParameterValidationRule rule : pathParamsRules.values()) {
    String name = rule.getName();
    if (pathParams.containsKey(name)) {
      if (pathParams.get(name) != null || !rule.isOptional() ) {
          RequestParameter parsedParam = rule.validateSingleParam(pathParams.get(name));
          if (parsedParams.containsKey(parsedParam.getName()))
            parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
          parsedParams.put(parsedParam.getName(), parsedParam);
      }
    } else // Path params are required!
      throw ValidationException.ValidationExceptionFactory.generateNotFoundValidationException(name,
        ParameterLocation.PATH);
  }
  return parsedParams;
}
 
Example 5
Source File: AbstractParameterResolver.java    From festival with Apache License 2.0 5 votes vote down vote up
protected MultiMap resolveParams(RoutingContext routingContext) {
    Map<String, String> pathParams = routingContext.pathParams();
    if (routingContext.request().method() == HttpMethod.GET) {
        return routingContext.queryParams().addAll(pathParams);
    } else {
        HttpServerRequest httpServerRequest = routingContext.request();
        return httpServerRequest.formAttributes().addAll(pathParams);
    }
}
 
Example 6
Source File: EdgeDispatcher.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
protected void onRequest(RoutingContext context) {
  Map<String, String> pathParams = context.pathParams();
  String microserviceName = pathParams.get("param0");
  String pathVersion = pathParams.get("param1");
  String path = context.request().path().substring(5 + microserviceName.length());

  EdgeInvocation edgeInvocation = new EdgeInvocation();
  edgeInvocation.setVersionRule(versionMapper.getOrCreate(pathVersion).getVersionRule());

  edgeInvocation.init(microserviceName, context, path, httpServerFilters);
  edgeInvocation.edgeInvoke();
}
 
Example 7
Source File: EncryptEdgeDispatcher.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private void routeToBackend(RoutingContext context, Hcr hcr, String userId) {
  Map<String, String> pathParams = context.pathParams();
  String microserviceName = pathParams.get("param0");
  String pathVersion = pathParams.get("param1");
  String path = context.request().path().substring(prefix.length() + 1);

  EncryptEdgeInvocation edgeInvocation = new EncryptEdgeInvocation(new EncryptContext(hcr, userId));
  edgeInvocation.setVersionRule(versionMapper.getOrCreate(pathVersion).getVersionRule());

  edgeInvocation.init(microserviceName, context, path, httpServerFilters);
  edgeInvocation.edgeInvoke();
}
 
Example 8
Source File: EdgeDispatcher.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
protected void onRequest(RoutingContext context) {
  Map<String, String> pathParams = context.pathParams();
  String microserviceName = pathParams.get("param0");
  String pathVersion = pathParams.get("param1");
  String path = context.request().path().substring(4);

  EdgeInvocation edgeInvocation = new EdgeInvocation();
  edgeInvocation.setVersionRule(versionMapper.getOrCreate(pathVersion).getVersionRule());

  edgeInvocation.init(microserviceName, context, path, httpServerFilters);
  edgeInvocation.edgeInvoke();
}
 
Example 9
Source File: EncryptEdgeDispatcher.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private void routeToBackend(RoutingContext context, Hcr hcr, String userId) {
  Map<String, String> pathParams = context.pathParams();
  String microserviceName = pathParams.get("param0");
  String pathVersion = pathParams.get("param1");
  String path = context.request().path().substring(prefix.length() + 1);

  EncryptEdgeInvocation edgeInvocation = new EncryptEdgeInvocation(new EncryptContext(hcr, userId));
  edgeInvocation.setVersionRule(versionMapper.getOrCreate(pathVersion).getVersionRule());

  edgeInvocation.init(microserviceName, context, path, httpServerFilters);
  edgeInvocation.edgeInvoke();
}
 
Example 10
Source File: DefaultEdgeDispatcher.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
protected void onRequest(RoutingContext context) {
  Map<String, String> pathParams = context.pathParams();
  String microserviceName = pathParams.get("param0");
  String path = Utils.findActualPath(context.request().path(), prefixSegmentCount);

  EdgeInvocation edgeInvocation =  createEdgeInvocation();
  if (withVersion) {
    String pathVersion = pathParams.get("param1");
    edgeInvocation.setVersionRule(versionMapper.getOrCreate(pathVersion).getVersionRule());
  }
  edgeInvocation.init(microserviceName, context, path, httpServerFilters);
  edgeInvocation.edgeInvoke();
}
 
Example 11
Source File: TestDefaultEdgeDispatcher.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testOnRequest(@Mocked Router router, @Mocked Route route
    , @Mocked RoutingContext context
    , @Mocked HttpServerRequest requst
    , @Mocked EdgeInvocation invocation) {
  DefaultEdgeDispatcher dispatcher = new DefaultEdgeDispatcher();
  Map<String, String> pathParams = new HashMap<>();
  pathParams.put("param0", "testService");
  pathParams.put("param1", "v1");

  new Expectations() {
    {
      router.routeWithRegex("/api/([^\\\\/]+)/([^\\\\/]+)/(.*)");
      result = route;
      route.handler((Handler<RoutingContext>) any);
      result = route;
      route.failureHandler((Handler<RoutingContext>) any);
      result = route;
      context.pathParams();
      result = pathParams;
      context.request();
      result = requst;
      requst.path();
      result = "/api/testService/v1/hello";
      invocation.setVersionRule("1.0.0.0-2.0.0.0");
      invocation.init("testService", context, "/testService/v1/hello",
          Deencapsulation.getField(dispatcher, "httpServerFilters"));
      invocation.edgeInvoke();
    }
  };
  dispatcher.init(router);
  Assert.assertEquals(dispatcher.enabled(), false);
  Assert.assertEquals(Utils.findActualPath("/api/test", 1), "/test");
  Assert.assertEquals(dispatcher.getOrder(), 20000);

  dispatcher.onRequest(context);
  // assert done in expectations.
}