1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.beanstalk.maven.plugin;
31
32 import com.google.common.base.Joiner;
33 import com.google.common.io.CharStreams;
34 import com.google.common.io.Closeables;
35 import com.jcabi.log.Logger;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.InputStreamReader;
39 import java.util.Enumeration;
40 import java.util.zip.ZipEntry;
41 import java.util.zip.ZipFile;
42 import org.apache.commons.lang3.NotImplementedException;
43 import org.apache.maven.plugin.MojoFailureException;
44
45
46
47
48
49
50
51
52 @SuppressWarnings("PMD.UnusedFormalParameter")
53 public final class WarFile {
54
55
56
57 private final transient ZipFile war;
58
59
60
61
62
63 public WarFile(final ZipFile file) {
64 this.war = file;
65 }
66
67
68
69
70
71
72
73 public void checkEbextensionsValidity() throws MojoFailureException {
74 final ZipEntry ebextdir = this.war.getEntry(".ebextensions");
75 if (ebextdir == null) {
76 throw new MojoFailureException(
77 ".ebextensions directory does not exist in the WAR file"
78 );
79 }
80 final Enumeration<? extends ZipEntry> entries = this.war.entries();
81 int files = 0;
82 while (entries.hasMoreElements()) {
83 final ZipEntry entry = entries.nextElement();
84 if (entry.getName().startsWith(".ebextensions/")
85 && !entry.isDirectory()) {
86 files += 1;
87 final String text = this.readFile(this.war, entry);
88 if (!(this.validJson(text) || this.validYaml(text))) {
89 throw new MojoFailureException(
90 Joiner.on("").join(
91 "File '",
92 entry.getName(),
93 "' in .ebextensions is neither valid JSON,",
94 " nor valid YAML"
95 )
96 );
97 }
98 }
99 }
100 if (files < 1) {
101 throw new MojoFailureException(
102 ".ebextensions contains no config files."
103 );
104 }
105 }
106
107
108
109
110
111
112
113 private String readFile(final ZipFile warfile, final ZipEntry entry) {
114 String text = null;
115 InputStream inputStream = null;
116 InputStreamReader reader = null;
117 try {
118 inputStream = warfile.getInputStream(entry);
119 reader = new InputStreamReader(inputStream);
120 text = CharStreams.toString(reader);
121 } catch (final IOException exception) {
122 Logger.error(this, exception.getMessage());
123 } finally {
124 Closeables.closeQuietly(inputStream);
125 Closeables.closeQuietly(reader);
126 }
127 return text;
128 }
129
130
131
132
133
134
135
136
137 private boolean validYaml(final String text) {
138 throw new NotImplementedException(
139 "com.jcabi.beanstalk.maven.plugin.AbstractBeanstalkMojo.validYaml"
140 );
141 }
142
143
144
145
146
147
148
149
150 private boolean validJson(final String text) {
151 throw new NotImplementedException(
152 "com.jcabi.beanstalk.maven.plugin.AbstractBeanstalkMojo.validJson"
153 );
154 }
155
156 }