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