Java Code Examples for org.apache.servicecomb.swagger.invocation.Response#failResp()

The following examples show how to use org.apache.servicecomb.swagger.invocation.Response#failResp() . 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: AuthenticationFilter.java    From scaffold with Apache License 2.0 7 votes vote down vote up
@Override
public Response afterReceiveRequest(Invocation invocation, HttpServletRequestEx httpServletRequestEx) {
  if (isInvocationNeedValidate(invocation.getMicroserviceName(), invocation.getOperationName())) {
    String token = httpServletRequestEx.getHeader(AUTHORIZATION);
    if (StringUtils.isNotEmpty(token)) {
      String userName = template
          .getForObject("cse://" + USER_SERVICE_NAME + "/validate?token={token}", String.class, token);
      if (StringUtils.isNotEmpty(userName)) {
        //Add header
        invocation.getContext().put(EDGE_AUTHENTICATION_NAME, userName);
      } else {
        return Response
            .failResp(new InvocationException(Status.UNAUTHORIZED, "authentication failed, invalid token"));
      }
    } else {
      return Response.failResp(
          new InvocationException(Status.UNAUTHORIZED, "authentication failed, missing AUTHORIZATION header"));
    }
  }
  return null;
}
 
Example 2
Source File: ExceptionToProducerResponseConverters.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public Response convertExceptionToResponse(SwaggerInvocation swaggerInvocation, Throwable e) {
  ExceptionToProducerResponseConverter<Throwable> converter = null;
  Class<?> clazz = e.getClass();
  while (converter == null) {
    converter = exceptionToProducerResponseConverters.get(clazz);
    if (clazz == Throwable.class) {
      break;
    }
    clazz = clazz.getSuperclass();
  }
  if (converter == null) {
    converter = defaultConverter;
  }
  try {
    return converter.convert(swaggerInvocation, e);
  } catch (Throwable throwable) {
    // In case users do not implement correctly and maybe discovered at runtime to cause asycResponse callback hang.
    LOGGER
        .error("ExceptionToProducerResponseConverter {} cannot throw exception, please fix it.", converter.getClass(),
            throwable);
    return Response.failResp(swaggerInvocation.getInvocationType(), e);
  }
}
 
Example 3
Source File: StaticResourceHandler.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public Response handle(String path) {
  path = URI.create(webRoot + path).normalize().getPath();
  if (!path.startsWith(webRoot)) {
    // maybe request of attack, just return 404
    return Response.failResp(new InvocationException(Status.NOT_FOUND, Status.NOT_FOUND.getReasonPhrase()));
  }

  Part part;
  try {
    part = findResource(path);
  } catch (Throwable e) {
    LOGGER.error("failed to process static resource, path={}", path, e);
    return Response
        .failResp(new InvocationException(Status.INTERNAL_SERVER_ERROR, "failed to process static resource."));
  }

  if (part == null) {
    return Response.failResp(new InvocationException(Status.NOT_FOUND, Status.NOT_FOUND.getReasonPhrase()));
  }

  return handler(part);
}
 
Example 4
Source File: CustomExceptionConverter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public Response convert(SwaggerInvocation swaggerInvocation, InvocationException e) {
  if (e.getStatusCode() == 408) {
    CustomException customException = new CustomException("change the response", 777);
    InvocationException stt = new InvocationException(Status.EXPECTATION_FAILED, customException);
    return Response.failResp(stt);
  } else {
    return Response.failResp(e);
  }
}
 
Example 5
Source File: CheckRawFormParamFilter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Response afterReceiveRequest(Invocation invocation, HttpServletRequestEx requestEx) {
  if (!"paramCodec.stringUrlencodedForm".equals(invocation.getOperationMeta().getSchemaQualifiedName())) {
    return null;
  }
  final Object swaggerArgument = invocation.getInvocationArgument("requestMap");

  if (!(swaggerArgument instanceof Map)) {
    return Response.failResp(new InvocationException(Status.BAD_REQUEST, "param is not map"));
  }
  return checkRequestType((Map<Object, Object>) swaggerArgument);
}
 
Example 6
Source File: CheckRawFormParamFilter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private Response checkRequestType(Map<Object, Object> swaggerArgument) {
  final Object valueA = swaggerArgument.get("A");
  if (null != valueA && !(valueA instanceof String)) {
    return Response.failResp(new InvocationException(Status.BAD_REQUEST, "valueA is not String"));
  }
  final Object valueB = swaggerArgument.get("B");
  if (null != valueB && !(valueB instanceof String)) {
    return Response.failResp(new InvocationException(Status.BAD_REQUEST, "valueB is not String"));
  }

  return null;
}
 
Example 7
Source File: CustomExceptionToProducerResponseConverter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public Response convert(SwaggerInvocation swaggerInvocation, IllegalStateException e) {
  IllegalStateErrorData data = new IllegalStateErrorData();
  data.setId(500);
  data.setMessage(e.getMessage());
  data.setState(e.getMessage());
  InvocationException state = new InvocationException(Status.INTERNAL_SERVER_ERROR, data);
  return Response.failResp(state);
}
 
Example 8
Source File: FallbackPolicyManager.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public static Response getFallbackResponse(String type, Throwable error, Invocation invocation) {
  FallbackPolicy policy = getPolicy(type, invocation);
  if (policy != null) {
    return policy.getFallbackResponse(invocation, error);
  } else {
    return Response.failResp(invocation.getInvocationType(),
        BizkeeperExceptionUtils
            .createBizkeeperException(BizkeeperExceptionUtils.SERVICECOMB_BIZKEEPER_FALLBACK,
                error,
                invocation.getOperationMeta().getMicroserviceQualifiedName()));
  }
}
 
Example 9
Source File: ThrowExceptionFallbackPolicy.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public Response getFallbackResponse(Invocation invocation, Throwable error) {
  return Response.failResp(invocation.getInvocationType(),
      BizkeeperExceptionUtils
          .createBizkeeperException(BizkeeperExceptionUtils.SERVICECOMB_BIZKEEPER_FALLBACK,
              error,
              invocation.getOperationMeta().getMicroserviceQualifiedName()));
}
 
Example 10
Source File: InspectorImpl.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Path("/download/schemas")
@GET
@ApiResponse(code = 200, message = "", response = File.class)
public Response downloadSchemas(@QueryParam("format") SchemaFormat format) {
  if (format == null) {
    format = SchemaFormat.SWAGGER;
  }

  // normally, schema will not be too big, just save them in memory temporarily
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  try (ZipOutputStream zos = new ZipOutputStream(os)) {
    for (Entry<String, String> entry : schemas.entrySet()) {
      // begin writing a new ZIP entry, positions the stream to the start of the entry data
      zos.putNextEntry(new ZipEntry(entry.getKey() + format.getSuffix()));

      String content = entry.getValue();
      if (SchemaFormat.HTML.equals(format)) {
        content = swaggerToHtml(content);
      }
      zos.write(content.getBytes(StandardCharsets.UTF_8));
      zos.closeEntry();
    }
  } catch (Throwable e) {
    String msg = "failed to create schemas zip file, format=" + format + ".";
    LOGGER.error(msg, e);
    return Response.failResp(new InvocationException(Status.INTERNAL_SERVER_ERROR, msg));
  }

  Part part = new InputStreamPart(null, new ByteArrayInputStream(os.toByteArray()))
      .setSubmittedFileName(
          RegistrationManager.INSTANCE.getMicroservice().getServiceName() + format.getSuffix() + ".zip");
  return Response.ok(part);
}
 
Example 11
Source File: InvocationExceptionToProducerResponseConverter.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public Response convert(SwaggerInvocation swaggerInvocation, InvocationException e) {
  return Response.failResp(e);
}
 
Example 12
Source File: DefaultExceptionToProducerResponseConverter.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public Response convert(SwaggerInvocation swaggerInvocation, Throwable e) {
  LOGGER.error("invoke failed, invocation={}", swaggerInvocation.getInvocationQualifiedName(), e);
  //not only producer but also consumer
  return Response.failResp(swaggerInvocation.getInvocationType(), e);
}
 
Example 13
Source File: ThrowableExceptionToProducerResponseConverter.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public Response convert(SwaggerInvocation swaggerInvocation, Throwable e) {
  InvocationException invocationException = new InvocationException(Status.INTERNAL_SERVER_ERROR.getStatusCode(), "",
      new CommonExceptionData("Unexpected exception when processing the request."), e);
  return Response.failResp(invocationException);
}
 
Example 14
Source File: IllegalArgumentExceptionToProducerResponseConverter.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public Response convert(SwaggerInvocation swaggerInvocation, IllegalArgumentException e) {
  InvocationException invocationException = new InvocationException(Status.BAD_REQUEST.getStatusCode(), "",
      new CommonExceptionData("Parameters not valid or types not match."), e);
  return Response.failResp(invocationException);
}