private void validatePathName(int p, String name) { if (!PARAM_NAME_REGEX.matcher(name).matches()) { throw parameterError(method, p, "@Path parameter name must match %s. Found: %s", PARAM_URL_REGEX.pattern(), name); } // Verify URL replacement name is actually present in the URL path. if (!relativeUrlParamNames.contains(name)) { throw parameterError(method, p, "URL \"%s\" does not contain \"{%s}\".", relativeUrl, name); } }
private void validateResolvableType(int p, Type type) { if (Utils.hasUnresolvableType(type)) { throw parameterError(method, p, "Parameter type must not include a type variable or wildcard: %s", type); } }
@Override void apply(RequestBuilder builder, @Nullable Object value) { if (value == null) { throw Utils.parameterError(method, p, "@Url parameter is null."); } builder.setRelativeUrl(value); } }
private ParameterHandler<?> parseParameter( int p, Type parameterType, @Nullable Annotation[] annotations) { ParameterHandler<?> result = null; if (annotations != null) { for (Annotation annotation : annotations) { ParameterHandler<?> annotationAction = parseParameterAnnotation(p, parameterType, annotations, annotation); if (annotationAction == null) { continue; } if (result != null) { throw parameterError(method, p, "Multiple Retrofit annotations found, only one allowed."); } result = annotationAction; } } if (result == null) { throw parameterError(method, p, "No Retrofit annotation found."); } return result; }
@Override void apply(RequestBuilder builder, @Nullable Map<String, T> value) throws IOException { if (value == null) { throw Utils.parameterError(method, p, "Header map was null."); } for (Map.Entry<String, T> entry : value.entrySet()) { String headerName = entry.getKey(); if (headerName == null) { throw Utils.parameterError(method, p, "Header map contained null key."); } T headerValue = entry.getValue(); if (headerValue == null) { throw Utils.parameterError(method, p, "Header map contained null value for key '" + headerName + "'."); } builder.addHeader(headerName, valueConverter.convert(headerValue)); } } }
@Override void apply(RequestBuilder builder, @Nullable T value) { if (value == null) { throw Utils.parameterError(method, p, "Body parameter value must not be null."); } RequestBody body; try { body = converter.convert(value); } catch (IOException e) { throw Utils.parameterError(method, e, p, "Unable to convert " + value + " to RequestBody"); } builder.setBody(body); } }
@Override void apply(RequestBuilder builder, @Nullable Map<String, T> value) throws IOException { if (value == null) { throw Utils.parameterError(method, p, "Query map was null"); } for (Map.Entry<String, T> entry : value.entrySet()) { String entryKey = entry.getKey(); if (entryKey == null) { throw Utils.parameterError(method, p, "Query map contained null key."); } T entryValue = entry.getValue(); if (entryValue == null) { throw Utils.parameterError(method, p, "Query map contained null value for key '" + entryKey + "'."); } String convertedEntryValue = valueConverter.convert(entryValue); if (convertedEntryValue == null) { throw Utils.parameterError(method, p, "Query map value '" + entryValue + "' converted to null by " + valueConverter.getClass().getName() + " for key '" + entryKey + "'."); } builder.addQueryParam(entryKey, convertedEntryValue, encoded); } } }
private void validatePathName(int p, String name) { if (!PARAM_NAME_REGEX.matcher(name).matches()) { throw parameterError(method, p, "@Path parameter name must match %s. Found: %s", PARAM_URL_REGEX.pattern(), name); } // Verify URL replacement name is actually present in the URL path. if (!relativeUrlParamNames.contains(name)) { throw parameterError(method, p, "URL \"%s\" does not contain \"{%s}\".", relativeUrl, name); } }
@Override void apply(RequestBuilder builder, @Nullable Map<String, T> value) throws IOException { if (value == null) { throw Utils.parameterError(method, p, "Field map was null."); } for (Map.Entry<String, T> entry : value.entrySet()) { String entryKey = entry.getKey(); if (entryKey == null) { throw Utils.parameterError(method, p, "Field map contained null key."); } T entryValue = entry.getValue(); if (entryValue == null) { throw Utils.parameterError(method, p, "Field map contained null value for key '" + entryKey + "'."); } String fieldEntry = valueConverter.convert(entryValue); if (fieldEntry == null) { throw Utils.parameterError(method, p, "Field map value '" + entryValue + "' converted to null by " + valueConverter.getClass().getName() + " for key '" + entryKey + "'."); } builder.addFormField(entryKey, fieldEntry, encoded); } } }
@Override void apply(RequestBuilder builder, @Nullable T value) throws IOException { if (value == null) { throw Utils.parameterError(method, p, "Path parameter \"" + name + "\" value must not be null."); } builder.addPathParam(name, valueConverter.convert(value), encoded); } }
@Override void apply(RequestBuilder builder, @Nullable T value) { if (value == null) return; // Skip null values. RequestBody body; try { body = converter.convert(value); } catch (IOException e) { throw Utils.parameterError(method, p, "Unable to convert " + value + " to RequestBody", e); } builder.addPart(headers, body); } }
@Override void apply(RequestBuilder builder, @Nullable Map<String, T> value) throws IOException { if (value == null) { throw Utils.parameterError(method, p, "Part map was null."); } for (Map.Entry<String, T> entry : value.entrySet()) { String entryKey = entry.getKey(); if (entryKey == null) { throw Utils.parameterError(method, p, "Part map contained null key."); } T entryValue = entry.getValue(); if (entryValue == null) { throw Utils.parameterError(method, p, "Part map contained null value for key '" + entryKey + "'."); } Headers headers = Headers.of( "Content-Disposition", "form-data; name=\"" + entryKey + "\"", "Content-Transfer-Encoding", transferEncoding); builder.addPart(headers, valueConverter.convert(entryValue)); } } }
private void validateResolvableType(int p, Type type) { if (Utils.hasUnresolvableType(type)) { throw parameterError(method, p, "Parameter type must not include a type variable or wildcard: %s", type); } }
private ParameterHandler<?> parseParameter( int p, Type parameterType, @Nullable Annotation[] annotations) { ParameterHandler<?> result = null; if (annotations != null) { for (Annotation annotation : annotations) { ParameterHandler<?> annotationAction = parseParameterAnnotation(p, parameterType, annotations, annotation); if (annotationAction == null) { continue; } if (result != null) { throw parameterError(method, p, "Multiple Retrofit annotations found, only one allowed."); } result = annotationAction; } } if (result == null) { throw parameterError(method, p, "No Retrofit annotation found."); } return result; }
validateResolvableType(p, type); if (gotUrl) { throw parameterError(method, p, "Multiple @Url method annotations found."); throw parameterError(method, p, "@Path parameters may not be used with @Url."); throw parameterError(method, p, "A @Url parameter must not come after a @Query."); throw parameterError(method, p, "A @Url parameter must not come after a @QueryName."); throw parameterError(method, p, "A @Url parameter must not come after a @QueryMap."); throw parameterError(method, p, "@Url cannot be used with @%s URL", httpMethod); return new ParameterHandler.RelativeUrl(method, p); } else { throw parameterError(method, p, "@Url must be okhttp3.HttpUrl, String, java.net.URI, or android.net.Uri type."); validateResolvableType(p, type); if (gotQuery) { throw parameterError(method, p, "A @Path parameter must not come after a @Query."); throw parameterError(method, p, "A @Path parameter must not come after a @QueryName."); throw parameterError(method, p, "A @Path parameter must not come after a @QueryMap."); throw parameterError(method, p, "@Path parameters may not be used with @Url.");
validateResolvableType(p, type); if (gotUrl) { throw parameterError(method, p, "Multiple @Url method annotations found."); throw parameterError(method, p, "@Path parameters may not be used with @Url."); throw parameterError(method, p, "A @Url parameter must not come after a @Query."); throw parameterError(method, p, "A @Url parameter must not come after a @QueryName."); throw parameterError(method, p, "A @Url parameter must not come after a @QueryMap."); throw parameterError(method, p, "@Url cannot be used with @%s URL", httpMethod); return new ParameterHandler.RelativeUrl(); } else { throw parameterError(method, p, "@Url must be okhttp3.HttpUrl, String, java.net.URI, or android.net.Uri type."); validateResolvableType(p, type); if (gotQuery) { throw parameterError(method, p, "A @Path parameter must not come after a @Query."); throw parameterError(method, p, "A @Path parameter must not come after a @QueryName."); throw parameterError(method, p, "A @Path parameter must not come after a @QueryMap."); throw parameterError(method, p, "@Path parameters may not be used with @Url.");