protected String fullyQualifiedMethodName() { return methodParser.fullyQualifiedMethodName(); }
/** * Get the method parser that is associated with the provided swaggerMethod. The method parser * can be used to get details about the Swagger REST API call. * @param swaggerMethod The method to generate a parser for. * @return The SwaggerMethodParser associated with the provided swaggerMethod. */ public SwaggerMethodParser methodParser(Method swaggerMethod) { SwaggerMethodParser result = methodParsers.get(swaggerMethod); if (result == null) { result = new SwaggerMethodParser(swaggerMethod, serializer, host()); methodParsers.put(swaggerMethod, result); } return result; }
/** * Get the path that will be used to complete the Swagger method's request. * @param methodArguments The method arguments to use with the path substitutions. * @return The path value with its placeholders replaced by the matching substitutions. */ public String path(Object[] methodArguments) { return applySubstitutions(relativePath, pathSubstitutions, methodArguments, UrlEscapers.PATH_ESCAPER); }
final String path = methodParser.path(args); final UrlBuilder pathUrlBuilder = UrlBuilder.parse(path); if (pathUrlBuilder.scheme() != null) { final String scheme = methodParser.scheme(args); urlBuilder.withScheme(scheme); final String host = methodParser.host(args); urlBuilder.withHost(host); for (final EncodedParameter queryParameter : methodParser.encodedQueryParameters(args)) { urlBuilder.setQueryParameter(queryParameter.name(), queryParameter.encodedValue()); final HttpRequest request = new HttpRequest(methodParser.fullyQualifiedMethodName(), methodParser.httpMethod(), url, new HttpResponseDecoder(methodParser, serializer)); request.withContext(methodParser.context(args)); final Object bodyContentObject = methodParser.body(args); if (bodyContentObject == null) { request.headers().set("Content-Length", "0"); } else { String contentType = methodParser.bodyContentType(); if (contentType == null || contentType.isEmpty()) { if (bodyContentObject instanceof byte[] || bodyContentObject instanceof String) { else if (FlowableUtil.isFlowableByteBuffer(methodParser.bodyJavaType())) { for (final HttpHeader header : methodParser.headers(args)) { request.withHeader(header.name(), header.value());
private HttpRequest createHttpRequest(OperationDescription operationDescription, SwaggerMethodParser methodParser, Object[] args) throws IOException { final HttpRequest request = new HttpRequest( methodParser.fullyQualifiedMethodName(), methodParser.httpMethod(), operationDescription.url(), new HttpResponseDecoder(methodParser, serializer)); final Object bodyContentObject = methodParser.body(args); if (bodyContentObject == null) { request.headers().set("Content-Length", "0"); } else { String contentType = methodParser.bodyContentType(); if (contentType == null || contentType.isEmpty()) { if (bodyContentObject instanceof byte[] || bodyContentObject instanceof String) { request.withBody(bodyContentString); else if (FlowableUtil.isFlowableByteBuffer(methodParser.bodyJavaType())) {
private Exception instantiateUnexpectedException(SwaggerMethodParser methodParser, HttpResponse response, String responseContent) { final int responseStatusCode = response.statusCode(); final Class<? extends RestException> exceptionType = methodParser.exceptionType(); final Class<?> exceptionBodyType = methodParser.exceptionBodyType(); String contentType = response.headerValue("Content-Type"); String bodyRepresentation; if ("application/octet-stream".equalsIgnoreCase(contentType)) { bodyRepresentation = "(" + response.headerValue("Content-Length") + "-byte body)"; } else { bodyRepresentation = responseContent.isEmpty() ? "(empty body)" : "\"" + responseContent + "\""; } Exception result; try { final Constructor<? extends RestException> exceptionConstructor = exceptionType.getConstructor(String.class, HttpResponse.class, exceptionBodyType); result = exceptionConstructor.newInstance("Status code " + responseStatusCode + ", " + bodyRepresentation, response, response.deserializedBody()); } catch (ReflectiveOperationException e) { String message = "Status code " + responseStatusCode + ", but an instance of " + exceptionType.getCanonicalName() + " cannot be created." + " Response body: " + bodyRepresentation; result = new IOException(message, e); } return result; }
/** * Get whether or not the provided response status code is one of the expected status codes for * this Swagger method. * @param responseStatusCode The status code that was returned in the HTTP response. * @param additionalAllowedStatusCodes An additional set of allowed status codes that will be * merged with the existing set of allowed status codes for * this query. * @return whether or not the provided response status code is one of the expected status codes * for this Swagger method. */ public boolean isExpectedResponseStatusCode(int responseStatusCode, int[] additionalAllowedStatusCodes) { boolean result; if (expectedStatusCodes == null) { result = (responseStatusCode < 400); } else { result = contains(expectedStatusCodes, responseStatusCode) || contains(additionalAllowedStatusCodes, responseStatusCode); } return result; }
@Override public HttpResponse apply(String bodyString) throws Exception { bufferedResponse.withDeserializedHeaders(deserializedHeaders); Object body = null; try { body = deserializeBody(bodyString, methodParser.exceptionBodyType(), null, SerializerEncoding.fromHeaders(response.headers())); } catch (IOException ignored) { // This translates in RestProxy as a RestException with no deserialized body. // The response content will still be accessible via the .response() member. } return bufferedResponse.withDeserializedBody(body); } });
@Override protected Object handleResumeOperation(final HttpRequest httpRequest, OperationDescription operationDescription, final SwaggerMethodParser methodParser, Type returnType) throws Exception { final Type operationStatusType = ((ParameterizedType) returnType).getActualTypeArguments()[0]; final TypeToken operationStatusTypeToken = TypeToken.of(operationStatusType); if (!operationStatusTypeToken.isSubtypeOf(OperationStatus.class)) { throw new InvalidReturnTypeException("AzureProxy only supports swagger interface methods that return Observable (such as " + methodParser.fullyQualifiedMethodName() + "()) if the Observable's inner type that is OperationStatus (not " + returnType.toString() + ")."); } PollStrategy.PollStrategyData pollStrategyData = (PollStrategy.PollStrategyData) operationDescription.pollStrategyData(); PollStrategy pollStrategy = pollStrategyData.initializeStrategy(this, methodParser); return pollStrategy.pollUntilDoneWithStatusUpdates(httpRequest, methodParser, operationStatusType); }
/** * Get the scheme to use for HTTP requests for this Swagger method. * @param swaggerMethodArguments The arguments to use for scheme/host substitutions. * @return The final host to use for HTTP requests for this Swagger method. */ public String scheme(Object[] swaggerMethodArguments) { final String substitutedHost = applySubstitutions(rawHost, hostSubstitutions, swaggerMethodArguments, UrlEscapers.PATH_ESCAPER); final String[] substitutedHostParts = substitutedHost.split("://"); return substitutedHostParts.length < 1 ? null : substitutedHostParts[0]; }
/** * Builds an object that can be used to resume the polling of the operation. * @return The OperationDescription. */ public OperationDescription buildDescription() { if (this.isDone()) { return null; } return new OperationDescription( this.pollStrategy.methodParser().fullyQualifiedMethodName(), this.pollStrategy.strategyData(), this.originalHttpRequest); }
/** * Get the host to use for HTTP requests for this Swagger method. * @param swaggerMethodArguments The arguments to use for host substitutions. * @return The final host to use for HTTP requests for this Swagger method. */ public String host(Object[] swaggerMethodArguments) { final String substitutedHost = applySubstitutions(rawHost, hostSubstitutions, swaggerMethodArguments, UrlEscapers.PATH_ESCAPER); final String[] substitutedHostParts = substitutedHost.split("://"); return substitutedHostParts.length < 2 ? substitutedHost : substitutedHost.split("://")[1]; }
final TypeToken operationStatusTypeToken = TypeToken.of(operationStatusType); if (!operationStatusTypeToken.isSubtypeOf(OperationStatus.class)) { throw new InvalidReturnTypeException("AzureProxy only supports swagger interface methods that return Observable (such as " + methodParser.fullyQualifiedMethodName() + "()) if the Observable's inner type that is OperationStatus (not " + returnType.toString() + ").");
throw new InvalidReturnTypeException("RestProxy does not support swagger interface methods (such as " + methodParser.fullyQualifiedMethodName() + "()) with a return type of " + returnType.toString());