|
1 | 1 | package graphql.kickstart.tools.boot; |
2 | 2 |
|
3 | | -import org.apache.commons.io.IOUtils; |
4 | | -import org.springframework.beans.factory.annotation.Autowired; |
5 | | -import org.springframework.context.ApplicationContext; |
6 | | -import org.springframework.core.io.Resource; |
| 3 | +import static java.util.stream.Collectors.joining; |
7 | 4 |
|
| 5 | +import java.io.BufferedReader; |
8 | 6 | import java.io.IOException; |
9 | 7 | import java.io.InputStream; |
10 | | -import java.io.StringWriter; |
| 8 | +import java.io.InputStreamReader; |
11 | 9 | import java.nio.charset.StandardCharsets; |
12 | 10 | import java.util.Arrays; |
13 | 11 | import java.util.List; |
14 | 12 | import java.util.stream.Collectors; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.context.ApplicationContext; |
| 15 | +import org.springframework.core.io.Resource; |
15 | 16 |
|
16 | 17 | public class ClasspathResourceSchemaStringProvider implements SchemaStringProvider { |
17 | 18 |
|
18 | | - @Autowired |
19 | | - private ApplicationContext applicationContext; |
20 | | - private String schemaLocationPattern; |
21 | | - |
22 | | - public ClasspathResourceSchemaStringProvider(String schemaLocationPattern) { |
23 | | - this.schemaLocationPattern = schemaLocationPattern; |
24 | | - } |
25 | | - |
26 | | - @Override |
27 | | - public List<String> schemaStrings() throws IOException { |
28 | | - Resource[] resources = applicationContext.getResources("classpath*:" + schemaLocationPattern); |
29 | | - if (resources.length <= 0) { |
30 | | - throw new IllegalStateException( |
31 | | - "No graphql schema files found on classpath with location pattern '" |
32 | | - + schemaLocationPattern |
33 | | - + "'. Please add a graphql schema to the classpath or add a SchemaParser bean to your application context."); |
34 | | - } |
35 | | - |
36 | | - return Arrays.stream(resources) |
37 | | - .map(this::readSchema) |
38 | | - .collect(Collectors.toList()); |
| 19 | + @Autowired |
| 20 | + private ApplicationContext applicationContext; |
| 21 | + private String schemaLocationPattern; |
| 22 | + |
| 23 | + public ClasspathResourceSchemaStringProvider(String schemaLocationPattern) { |
| 24 | + this.schemaLocationPattern = schemaLocationPattern; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public List<String> schemaStrings() throws IOException { |
| 29 | + Resource[] resources = applicationContext.getResources("classpath*:" + schemaLocationPattern); |
| 30 | + if (resources.length <= 0) { |
| 31 | + throw new IllegalStateException( |
| 32 | + "No graphql schema files found on classpath with location pattern '" |
| 33 | + + schemaLocationPattern |
| 34 | + + "'. Please add a graphql schema to the classpath or add a SchemaParser bean to your application context."); |
39 | 35 | } |
40 | 36 |
|
41 | | - private String readSchema(Resource resource) { |
42 | | - StringWriter writer = new StringWriter(); |
43 | | - try (InputStream inputStream = resource.getInputStream()) { |
44 | | - IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8); |
45 | | - } catch (IOException e) { |
46 | | - throw new IllegalStateException("Cannot read graphql schema from resource " + resource, e); |
47 | | - } |
48 | | - return writer.toString(); |
| 37 | + return Arrays.stream(resources) |
| 38 | + .map(this::readSchema) |
| 39 | + .collect(Collectors.toList()); |
| 40 | + } |
| 41 | + |
| 42 | + private String readSchema(Resource resource) { |
| 43 | + try ( |
| 44 | + InputStream inputStream = resource.getInputStream(); |
| 45 | + InputStreamReader bufferedInputStream = new InputStreamReader(inputStream, StandardCharsets.UTF_8.name()); |
| 46 | + BufferedReader reader = new BufferedReader(bufferedInputStream) |
| 47 | + ) { |
| 48 | + return reader.lines().collect(joining()); |
| 49 | + } catch (IOException e) { |
| 50 | + throw new IllegalStateException("Cannot read graphql schema from resource " + resource, e); |
49 | 51 | } |
| 52 | + } |
50 | 53 |
|
51 | 54 | } |
0 commit comments