Debian Codegraph @

src/com/google/javascript/jscomp/webservice/common/Protocol.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp.webservice.common;
18
19 import com.google.common.collect.Sets;
20
21 import java.util.Set;
22
23 /**
24 * All the strings used by the webservice protocol.
25 *
26 */
27 public class Protocol {
28
29 private Protocol() {}
30
31 /**
32 * All enums that need to be shared between the Java and JS code should
33 * implement this interface.
34 */
35 public static interface ProtocolEnum {
36 /**
37 * @return A string representing the key or value specified by the
38 * protocol.
39 */
40 public String getValue();
41 }
42
43 /**
44 * All the keys that can be part of the http request.
45 */
46 public static enum RequestKey implements ProtocolEnum {
47 CODE_URL("code_url"),
48 JS_CODE("js_code"),
49 EXCLUDE_DEFAULT_EXTERNS("exclude_default_externs"),
50 EXTERNS_URL("externs_url"),
51 EXTERNS_CODE("js_externs"),
52 COMPILATION_LEVEL("compilation_level"),
53 OUTPUT_FORMAT("output_format"),
54 OUTPUT_INFO("output_info"),
55 OUTPUT_FILE_NAME("output_file_name"),
56 OUTPUT_WRAPPER("output_wrapper"),
57 API_KEY("api_key"),
58 FORMATTING("formatting"),
59 WARNING_LEVEL("warning_level"),
60 USER_ID("uid"),
61 USE_CLOSURE("use_closure_library"),
62 BUILD_DEBUG("debug"),
63 CHARSET("charset"),
64 LANGUAGE("language"),
65 USE_TYPES_FOR_OPTIMIZATIONS("use_types_for_optimization"),
66
67 // Old ROBOCOMP urls.
68 RAWJS("rawjs"),
69 BASE("base"),
70 MODE("mode"),
71 SCRIPT("script"),
72 NOCACHE("nocache") // Ignored.
73 ;
74
75 private static final Set<String> permittedKeys = getPermittedKeys();
76
77 private static Set<String> getPermittedKeys() {
78 Set<String> keys = Sets.newHashSet();
79
80 for (RequestKey key : RequestKey.values()) {
81 keys.add(key.asGetParameter());
82 }
83 return keys;
84 }
85
86 private final String asGetParameter;
87
88 private RequestKey(String asGetParameter) {
89 this.asGetParameter = asGetParameter;
90 }
91
92 public String asGetParameter() {
93 return asGetParameter;
94 }
95
96 @Override
97 public String toString() {
98 return asGetParameter;
99 }
100
101 public static boolean isKeyValid(String key) {
102 return permittedKeys.contains(key.toLowerCase());
103 }
104
105 @Override
106 public String getValue() {
107 return asGetParameter;
108 }
109 }
110
111 /**
112 * All the possible values for the OUTPUT_INFO key.
113 */
114 public static enum OutputInfoKey implements ProtocolEnum {
115 VARIABLE_MAP("variable_map"),
116 COMPILED_CODE("compiled_code"),
117 WARNINGS("warnings"),
118 ERRORS("errors"),
119 STATISTICS("statistics"),
120 ;
121
122 private final String value;
123
124 private OutputInfoKey(String value) {
125 this.value = value;
126 }
127
128 @Override
129 public String getValue() {
130 return value;
131 }
132 }
133
134 /**
135 * All the possible values for the FORMATTING key.
136 */
137 public static enum FormattingKey implements ProtocolEnum {
138 PRETTY_PRINT("pretty_print"),
139 PRINT_INPUT_DELIMITER("print_input_delimiter"),
140 ;
141
142 private final String value;
143
144 private FormattingKey(String value) {
145 this.value = value;
146 }
147
148 @Override
149 public String getValue() {
150 return value;
151 }
152 }
153
154 public static enum CompilationLevelKey implements ProtocolEnum {
155 WHITESPACE_ONLY("whitespace_only"),
156 SIMPLE_OPTIMIZATIONS("simple_optimizations"),
157 ADVANCED_OPTIMIZATIONS("advanced_optimizations"),
158 ;
159
160 private final String value;
161
162 CompilationLevelKey(String value) {
163 this.value = value;
164 }
165
166 @Override
167 public String getValue() {
168 return value;
169 }
170 }
171
172 public static enum WarningLevelKey implements ProtocolEnum {
173 QUIET("quiet"),
174 DEFAULT("default"),
175 VERBOSE("verbose"),
176 ;
177
178 private final String value;
179
180 private WarningLevelKey(String value) {
181 this.value = value;
182 }
183
184 @Override
185 public String getValue() {
186 return value;
187 }
188 }
189
190 public static enum OutputFormatKey implements ProtocolEnum {
191 TEXT("text"),
192 XML("xml"),
193 JSON("json"),
194 ;
195
196 private final String value;
197
198 private OutputFormatKey(String value) {
199 this.value = value;
200 }
201
202 @Override
203 public String getValue() {
204 return value;
205 }
206
207 @Override
208 public String toString() {
209 return getValue();
210 }
211 }
212
213 /**
214 * Fields in the JSON response from the ApiKeyGenerationServlet.
215 */
216 public static enum ApiKeyResponse implements ProtocolEnum {
217 API_KEY("api_key"),
218 ;
219
220 private final String responseParam;
221
222 ApiKeyResponse(String responseParam) {
223 this.responseParam = responseParam;
224 }
225
226 /**
227 * Name of the key as it appears in the JSON.
228 */
229 public String getResponseParam() {
230 return responseParam;
231 }
232
233 @Override
234 public String toString() {
235 return getResponseParam();
236 }
237
238 @Override
239 public String getValue() {
240 return getResponseParam();
241 }
242 }
243
244 /**
245 * All the xml/json tags that can be returned by the backend if xml or json is
246 * selected as the output mode.
247 */
248 public static enum ResponseTag implements ProtocolEnum {
249 ROOT_TAG("compilationResult"),
250 COMPILED_CODE_TAG("compiledCode"),
251 WARNINGS_TAG("warnings"),
252 WARNING_TAG("warning"),
253 ERRORS_TAG("errors"),
254 ERROR_TAG("error"),
255 ERROR_LINE_NO_ATTR("lineno"),
256 ERROR_LINE_ATTR("line"),
257 // Charno is negative if error occurred outside range of columns that
258 // JSCompiler records. Change the sign of the value to find the
259 // maximum column represented.
260 // Note that JSCompiler uses -1 as an "I don't know" state, and it can
261 // also turn up occasionally.
262 ERROR_CHAR_ATTR("charno"),
263 ERROR_FILE_ATTR("file"),
264 ERROR_TYPE_ATTR("type"),
265 STATS_TAG("statistics"),
266 ORIGINAL_SIZE_TAG("originalSize"),
267 ORIGINAL_GZIP_SIZE_TAG("originalGzipSize"),
268 COMPRESSED_SIZE_TAG("compressedSize"),
269 COMPRESSED_GZIP_SIZE_TAG("compressedGzipSize"),
270 COMPILE_TIME_TAG("compileTime"),
271 SERVER_ERRORS_TAG("serverErrors"),
272 SERVER_ERROR_TAG("error"),
273 SERVER_ERROR_CODE_ATTR("code"),
274 VARIABLE_MAP("variableMap"),
275 VARIABLE_MAP_ENTRY("variableMapEntry"),
276 ORIGINAL_NAME_ATTR("originalName"),
277 NEW_NAME_ATTR("newName"),
278 OUTPUT_FILE_PATH("outputFilePath"),
279 ;
280
281 private final String responseTag;
282
283 private ResponseTag(String responseTag) {
284 this.responseTag = responseTag;
285 }
286
287 public String getResponseTag() {
288 return responseTag;
289 }
290
291 @Override
292 public String toString() {
293 return getResponseTag();
294 }
295
296 @Override
297 public String getValue() {
298 return getResponseTag();
299 }
300 }
301
302 /**
303 * Properties key for getting the maximum input file size that may be
304 * compiled by the service. This is parameterized so we can have different
305 * values for inside and outside Google.
306 * The value should be a string representation of an integer representing
307 * the maximum input size in bytes.
308 */
309 public static final String MAX_INPUT_SIZE_KEY =
310 "com.google.javascript.jscomp.webservice.maximumInputSize";
311
312 /**
313 * Fallback value in case no setting is provided.
314 */
315 public static final int FALLBACK_MAX_INPUT_SIZE =
316 500 * 1024;
317
318 /**
319 * Hard limit on input size set at execution time from the MAX_INPUT_SIZE_KEY
320 * property.
321 */
322 private static int maxInputSize;
323
324 /**
325 * Initialize maxInputSize to the value from the MAX_INPUT_SIZE_KEY property
326 * at startup.
327 */
328 static {
329 resetMaximumInputSize();
330 }
331
332 /**
333 * Find the maximum input size that this configuration of the web service
334 * allows.
335 * @return maximum input size permitted (in bytes)
336 */
337 public static final int maximumInputSize() {
338 // Limit the number of files downloaded if they are too big to compile.
339 return maxInputSize;
340 }
341
342 /**
343 * Reset the maximum input size so that the property key is rechecked.
344 * This is needed for testing code because we are caching the maximum
345 * input size value.
346 */
347 public static final void resetMaximumInputSize() {
348 String maxInputSizeStr = System.getProperty(Protocol.MAX_INPUT_SIZE_KEY);
349
350 if (maxInputSizeStr == null) {
351 maxInputSize = Protocol.FALLBACK_MAX_INPUT_SIZE;
352 } else {
353 maxInputSize = Integer.parseInt(maxInputSizeStr);
354 }
355 }
356 }