Java Code Examples for org.apache.servicecomb.swagger.invocation.AsyncResponse#consumerFail()

The following examples show how to use org.apache.servicecomb.swagger.invocation.AsyncResponse#consumerFail() . 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: AuthHandler.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
protected void doHandle(Invocation invocation, AsyncResponse asyncResp, Boolean authSucc, Throwable authException) {
  if (authException != null) {
    asyncResp.consumerFail(new InvocationException(Status.UNAUTHORIZED, (Object) authException.getMessage()));
    return;
  }

  if (!authSucc) {
    asyncResp.consumerFail(new InvocationException(Status.UNAUTHORIZED, (Object) "auth failed"));
  }

  LOGGER.debug("auth success.");
  try {
    invocation.next(asyncResp);
  } catch (Throwable e) {
    asyncResp.consumerFail(e);
  }
}
 
Example 2
Source File: AuthHandler.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
protected void doHandle(Invocation invocation, AsyncResponse asyncResp, Boolean authSucc, Throwable authException) {
  if (authException != null) {
    asyncResp.consumerFail(new InvocationException(Status.UNAUTHORIZED, (Object) authException.getMessage()));
    return;
  }

  if (!authSucc) {
    asyncResp.consumerFail(new InvocationException(Status.UNAUTHORIZED, (Object) "auth failed"));
  }

  LOGGER.debug("auth success.");
  try {
    invocation.next(asyncResp);
  } catch (Throwable e) {
    asyncResp.consumerFail(e);
  }
}
 
Example 3
Source File: ConsumerQpsFlowControlHandler.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
  if (!Config.INSTANCE.isConsumerEnabled()) {
    invocation.next(asyncResp);
    return;
  }

  QpsController qpsController = qpsControllerMgr.getOrCreate(invocation.getMicroserviceName(), invocation);
  if (qpsController.isLimitNewRequest()) {
    // return http status 429
    CommonExceptionData errorData = new CommonExceptionData("rejected by qps flowcontrol");
    asyncResp.consumerFail(
        new InvocationException(QpsConst.TOO_MANY_REQUESTS_STATUS, errorData));
    return;
  }

  invocation.next(asyncResp);
}
 
Example 4
Source File: AbortFault.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Override
public void injectFault(Invocation invocation, FaultParam faultParam, AsyncResponse asyncResponse) {
  if (!shouldAbort(invocation, faultParam)) {
    asyncResponse.success(SUCCESS_RESPONSE);
    return;
  }

  // get the config values related to abort percentage.
  int errorCode = FaultInjectionUtil.getFaultInjectionConfig(invocation, "abort.httpStatus");
  if (errorCode == FaultInjectionConst.FAULT_INJECTION_DEFAULT_VALUE) {
    LOGGER.debug("Fault injection: Abort error code is not configured");
    asyncResponse.success(SUCCESS_RESPONSE);
    return;
  }

  // if request need to be abort then return failure with given error code
  CommonExceptionData errorData = new CommonExceptionData(ABORTED_ERROR_MSG);
  asyncResponse.consumerFail(new InvocationException(errorCode, ABORTED_ERROR_MSG, errorData));
}
 
Example 5
Source File: LoadbalanceHandler.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
private void send(Invocation invocation, AsyncResponse asyncResp, LoadBalancer chosenLB) throws Exception {
  long time = System.currentTimeMillis();
  ServiceCombServer server = chosenLB.chooseServer(invocation);
  if (null == server) {
    asyncResp.consumerFail(new InvocationException(Status.INTERNAL_SERVER_ERROR, "No available address found."));
    return;
  }
  chosenLB.getLoadBalancerStats().incrementNumRequests(server);
  invocation.setEndpoint(server.getEndpoint());
  invocation.next(resp -> {
    // this stats is for WeightedResponseTimeRule
    chosenLB.getLoadBalancerStats().noteResponseTime(server, (System.currentTimeMillis() - time));
    if (isFailedResponse(resp)) {
      chosenLB.getLoadBalancerStats().incrementSuccessiveConnectionFailureCount(server);
      ServiceCombLoadBalancerStats.INSTANCE.markFailure(server);
    } else {
      chosenLB.getLoadBalancerStats().incrementActiveRequestsCount(server);
      ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    }
    asyncResp.handle(resp);
  });
}
 
Example 6
Source File: InternalAccessHandler.java    From servicecomb-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Invocation invocation, AsyncResponse asyncReponse) throws Exception {
  if (invocation.getOperationMeta().getSwaggerOperation().getTags() != null
      && invocation.getOperationMeta().getSwaggerOperation().getTags().contains("INTERNAL")) {
    asyncReponse.consumerFail(new InvocationException(403, "", "not allowed"));
    return;
  }
  invocation.next(asyncReponse);
}
 
Example 7
Source File: InternalAccessHandler.java    From servicecomb-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Invocation invocation, AsyncResponse asyncReponse) throws Exception {
  if (invocation.getOperationMeta().getSwaggerOperation().getTags() != null
      && invocation.getOperationMeta().getSwaggerOperation().getTags().contains("INTERNAL")) {
    asyncReponse.consumerFail(new InvocationException(403, "", "not allowed"));
    return;
  }
  invocation.next(asyncReponse);
}
 
Example 8
Source File: ConsumerAuthHandler.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {

  Optional<String> token = Optional.ofNullable(athenticationTokenManager.getToken());
  if (!token.isPresent()) {
    asyncResp.consumerFail(
        new IllegalStateException("rejected by consumer authentication handler"));
    return;
  }
  invocation.addContext(Const.AUTH_TOKEN, token.get());
  invocation.next(asyncResp);
}
 
Example 9
Source File: SimpleLoadBalanceHandler.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
  if (invocation.getEndpoint() != null) {
    invocation.next(asyncResp);
    return;
  }

  DiscoveryContext context = new DiscoveryContext();
  context.setInputParameters(invocation);
  VersionedCache endpointsVersionedCache = discoveryTree.discovery(context,
      invocation.getAppId(),
      invocation.getMicroserviceName(),
      invocation.getMicroserviceVersionRule());
  if (endpointsVersionedCache.isEmpty()) {
    asyncResp.consumerFail(ExceptionUtils.lbAddressNotFound(invocation.getMicroserviceName(),
        invocation.getMicroserviceVersionRule(),
        endpointsVersionedCache.name()));
    return;
  }

  List<Endpoint> endpoints = endpointsVersionedCache.data();
  AtomicInteger index = indexMap.computeIfAbsent(endpointsVersionedCache.name(), name -> {
    LOGGER.info("Create loadBalancer for {}.", name);
    return new AtomicInteger();
  });
  LOGGER.debug("invocation {} use discoveryGroup {}.",
      invocation.getMicroserviceQualifiedName(),
      endpointsVersionedCache.name());

  int idx = Math.abs(index.getAndIncrement());
  idx = idx % endpoints.size();
  Endpoint endpoint = endpoints.get(idx);

  invocation.setEndpoint(endpoint);

  invocation.next(asyncResp);
}