|
| 1 | +package graphql.scalars.uri; |
| 2 | + |
| 3 | +import graphql.GraphQLContext; |
| 4 | +import graphql.Internal; |
| 5 | +import graphql.execution.CoercedVariables; |
| 6 | +import graphql.language.StringValue; |
| 7 | +import graphql.language.Value; |
| 8 | +import graphql.schema.Coercing; |
| 9 | +import graphql.schema.CoercingParseLiteralException; |
| 10 | +import graphql.schema.CoercingParseValueException; |
| 11 | +import graphql.schema.CoercingSerializeException; |
| 12 | +import graphql.schema.GraphQLScalarType; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.net.MalformedURLException; |
| 16 | +import java.net.URI; |
| 17 | +import java.net.URISyntaxException; |
| 18 | +import java.net.URL; |
| 19 | +import java.util.Locale; |
| 20 | +import java.util.Optional; |
| 21 | +import java.util.function.Function; |
| 22 | + |
| 23 | +import static graphql.scalars.util.Kit.typeName; |
| 24 | + |
| 25 | +@Internal |
| 26 | +public final class UriScalar { |
| 27 | + |
| 28 | + private UriScalar() { |
| 29 | + } |
| 30 | + |
| 31 | + public static final GraphQLScalarType INSTANCE; |
| 32 | + |
| 33 | + static { |
| 34 | + Coercing<URL, URL> coercing = new Coercing<>() { |
| 35 | + @Override |
| 36 | + public URL serialize(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException { |
| 37 | + Optional<URL> url; |
| 38 | + if (input instanceof String) { |
| 39 | + url = Optional.of(parseURL(input.toString(), CoercingSerializeException::new)); |
| 40 | + } else { |
| 41 | + url = toURL(input); |
| 42 | + } |
| 43 | + if (url.isPresent()) { |
| 44 | + return url.get(); |
| 45 | + } |
| 46 | + throw new CoercingSerializeException( |
| 47 | + "Expected a 'URL' like object but was '" + typeName(input) + "'." |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public URL parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException { |
| 53 | + String urlStr; |
| 54 | + if (input instanceof String) { |
| 55 | + urlStr = String.valueOf(input); |
| 56 | + } else { |
| 57 | + Optional<URL> url = toURL(input); |
| 58 | + if (url.isEmpty()) { |
| 59 | + throw new CoercingParseValueException( |
| 60 | + "Expected a 'URL' like object but was '" + typeName(input) + "'." |
| 61 | + ); |
| 62 | + } |
| 63 | + return url.get(); |
| 64 | + } |
| 65 | + return parseURL(urlStr, CoercingParseValueException::new); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public URL parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException { |
| 70 | + if (!(input instanceof StringValue)) { |
| 71 | + throw new CoercingParseLiteralException( |
| 72 | + "Expected AST type 'StringValue' but was '" + typeName(input) + "'." |
| 73 | + ); |
| 74 | + } |
| 75 | + return parseURL(((StringValue) input).getValue(), CoercingParseLiteralException::new); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public Value<?> valueToLiteral(Object input, GraphQLContext graphQLContext, Locale locale) { |
| 80 | + URL url = serialize(input, graphQLContext, locale); |
| 81 | + return StringValue.newStringValue(url.toExternalForm()).build(); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + private URL parseURL(String input, Function<String, RuntimeException> exceptionMaker) { |
| 86 | + try { |
| 87 | + return new URI(input).toURL(); |
| 88 | + } catch (URISyntaxException | IllegalArgumentException | MalformedURLException e) { |
| 89 | + throw exceptionMaker.apply("Invalid URL value : '" + input + "'."); |
| 90 | + } |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + INSTANCE = GraphQLScalarType.newScalar() |
| 95 | + .name("Url") |
| 96 | + .description("A Url scalar") |
| 97 | + .coercing(coercing) |
| 98 | + .build(); |
| 99 | + } |
| 100 | + |
| 101 | + private static Optional<URL> toURL(Object input) { |
| 102 | + if (input instanceof URL) { |
| 103 | + return Optional.of((URL) input); |
| 104 | + } else if (input instanceof URI) { |
| 105 | + try { |
| 106 | + return Optional.of(((URI) input).toURL()); |
| 107 | + } catch (MalformedURLException ignored) { |
| 108 | + } |
| 109 | + } else if (input instanceof File) { |
| 110 | + try { |
| 111 | + return Optional.of(((File) input).toURI().toURL()); |
| 112 | + } catch (MalformedURLException ignored) { |
| 113 | + } |
| 114 | + } |
| 115 | + return Optional.empty(); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments